polfan-server-js-client 0.2.51 → 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 +31 -25
- package/build/index.cjs.js +29 -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/build/types/types/src/schemes/events/Session.d.ts +1 -0
- package/package.json +1 -1
- package/src/AbstractChatClient.ts +1 -1
- package/src/WebSocketChatClient.ts +19 -11
- package/src/state-tracker/RoomsManager.ts +12 -0
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
|
}
|
|
@@ -174,6 +174,7 @@ export class RoomsManager {
|
|
|
174
174
|
const newMember = ev.member;
|
|
175
175
|
const user = member.spaceMember?.user ?? member.user;
|
|
176
176
|
|
|
177
|
+
// Preserving user object, because it's not included in event
|
|
177
178
|
if (newMember.spaceMember) {
|
|
178
179
|
newMember.spaceMember.user = user;
|
|
179
180
|
} else {
|
|
@@ -297,6 +298,7 @@ export class RoomsManager {
|
|
|
297
298
|
}
|
|
298
299
|
|
|
299
300
|
private handleUserUpdated(ev: UserUpdated): void {
|
|
301
|
+
// Update room members users
|
|
300
302
|
this.members.items.forEach((members) => {
|
|
301
303
|
const member = members.get(ev.user.id);
|
|
302
304
|
|
|
@@ -315,6 +317,16 @@ export class RoomsManager {
|
|
|
315
317
|
|
|
316
318
|
members.set(newMember);
|
|
317
319
|
});
|
|
320
|
+
|
|
321
|
+
// Update recipients users
|
|
322
|
+
const newRooms: Room[] = [];
|
|
323
|
+
this.list.items.forEach(room => {
|
|
324
|
+
if (room.recipients?.some(user => user.id === ev.user.id)) {
|
|
325
|
+
room.recipients = room.recipients.map(user => user.id === ev.user.id ? ev.user : user);
|
|
326
|
+
newRooms.push({...room});
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
this.list.set(...newRooms);
|
|
318
330
|
}
|
|
319
331
|
|
|
320
332
|
private handleNewMessage(ev: NewMessage): void {
|