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.
- package/.idea/workspace.xml +21 -13
- package/build/index.cjs.js +14 -10
- package/build/index.cjs.js.map +1 -1
- package/build/index.umd.js +1 -1
- package/build/index.umd.js.map +1 -1
- package/build/types/WebSocketChatClient.d.ts +2 -0
- package/package.json +1 -1
- package/src/AbstractChatClient.ts +1 -1
- package/src/WebSocketChatClient.ts +19 -11
package/package.json
CHANGED
|
@@ -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)[
|
|
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.
|
|
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.
|
|
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
|
}
|