polfan-server-js-client 0.2.52 → 0.2.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,5 +34,7 @@ export declare class WebSocketChatClient extends AbstractChatClient implements O
34
34
  private onClose;
35
35
  private sendFromQueue;
36
36
  private triggerConnectionTimeout;
37
+ private isPendingReadyWsState;
38
+ private isReadyToSendWsState;
37
39
  }
38
40
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polfan-server-js-client",
3
- "version": "0.2.52",
3
+ "version": "0.2.53",
4
4
  "description": "JavaScript client library for handling communication with Polfan chat server.",
5
5
  "author": "Jarosław Żak",
6
6
  "license": "MIT",
@@ -154,7 +154,7 @@ export abstract class AbstractChatClient extends EventTarget {
154
154
  if (!this.awaitingResponse.has(envelope.ref)) {
155
155
  return;
156
156
  }
157
- this.awaitingResponse.get(envelope.ref)[0](error);
157
+ this.awaitingResponse.get(envelope.ref)[1](error);
158
158
  this.awaitingResponse.delete(envelope.ref);
159
159
  }
160
160
  }
@@ -60,28 +60,28 @@ export class WebSocketChatClient extends AbstractChatClient implements Observabl
60
60
 
61
61
  public async send<CommandType extends keyof CommandsMap>(commandType: CommandType, commandData: CommandsMap[CommandType][0]):
62
62
  Promise<CommandResult<CommandsMap[CommandType][1]>> {
63
- if (!this.ws || [this.ws.CLOSED, this.ws.CLOSING].includes(this.ws.readyState)) {
64
- throw new Error('Cannot send; close or closing connection state');
65
- }
66
-
67
63
  const envelope = this.createEnvelope<CommandsMap[CommandType][0]>(commandType, commandData);
68
64
  const promise = this.createPromiseFromCommandEnvelope<CommandType>(envelope);
69
65
 
70
- if (this.ws.readyState === this.ws.CONNECTING || !this.authenticated) {
66
+ if (this.isPendingReadyWsState()) {
71
67
  this.sendQueue.push(envelope);
72
68
  return promise;
73
69
  }
74
70
 
75
- if (this.ws.readyState !== this.ws.OPEN) {
76
- throw new Error(`Invalid websocket state=${this.ws.readyState}`);
77
- }
78
-
79
71
  this.sendEnvelope(envelope);
80
72
  return promise;
81
73
  }
82
74
 
83
75
  private sendEnvelope(envelope: Envelope): void {
84
- this.ws.send(JSON.stringify(envelope));
76
+ if (this.isReadyToSendWsState()) {
77
+ this.ws.send(JSON.stringify(envelope));
78
+ return;
79
+ }
80
+
81
+ this.handleEnvelopeSendError(
82
+ envelope,
83
+ new Error(`Cannot send; invalid websocket state=${this.ws?.readyState}`)
84
+ );
85
85
  }
86
86
 
87
87
  private onMessage(event: MessageEvent): void {
@@ -108,7 +108,7 @@ export class WebSocketChatClient extends AbstractChatClient implements Observabl
108
108
  clearTimeout(this.connectingTimeoutId);
109
109
  const reconnect = event.code !== 1000; // Connection was closed because of error
110
110
  if (reconnect) {
111
- this.connect();
111
+ void this.connect();
112
112
  }
113
113
  this.emit(this.Event.disconnect, reconnect);
114
114
  }
@@ -129,4 +129,12 @@ export class WebSocketChatClient extends AbstractChatClient implements Observabl
129
129
  this.disconnect();
130
130
  this.emit(this.Event.error, new Error('Connection timeout'));
131
131
  }
132
+
133
+ private isPendingReadyWsState(): boolean {
134
+ return this.ws.readyState === this.ws.CONNECTING || !this.authenticated;
135
+ }
136
+
137
+ private isReadyToSendWsState(): boolean {
138
+ return this.ws?.readyState === this.ws.OPEN && this.authenticated;
139
+ }
132
140
  }