kubemq-io / kubemq-js

Apache License 2.0
9 stars 7 forks source link

Fixed RPC subscribe reconnection and incorrect closing handling. #7

Open usein-abilev opened 4 months ago

usein-abilev commented 4 months ago

This PR fixes #1.

Previously, the subscribe method of the QueriesClient class had incorrect handling of close and unsubscribe events. The event received from stream.on("close") did not lead to any consequences. If the connection to KubeMQ was lost, then reconnection could not occur, because isClosed was always false.

I also moved the subscription from cycle-dependent to event mode, so as not to once again load the event loop with unnecessary functions deferred every second.

Here is a bug, onClose was not called on the other side: https://github.com/kubemq-io/kubemq-js/blob/main/src/queries.ts#L296C1-L304C12

let onClose = new TypedEvent<void>();
stream.on('close', () => {
  onClose.emit();
});
resolve({
  onClose: onClose,
  stream: stream,
});