@meshagent/meshagent 0.37.2 → 0.38.0
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/CHANGELOG.md +3 -0
- package/dist/browser/agent.js +74 -10
- package/dist/browser/developer-client.js +3 -0
- package/dist/browser/helpers.d.ts +2 -2
- package/dist/browser/helpers.js +1 -1
- package/dist/browser/meshagent-client.d.ts +25 -0
- package/dist/browser/meshagent-client.js +65 -0
- package/dist/browser/messaging-client.d.ts +29 -16
- package/dist/browser/messaging-client.js +256 -154
- package/dist/browser/participant.d.ts +7 -2
- package/dist/browser/participant.js +9 -9
- package/dist/browser/protocol.d.ts +85 -28
- package/dist/browser/protocol.js +356 -119
- package/dist/browser/room-client.d.ts +165 -29
- package/dist/browser/room-client.js +1114 -74
- package/dist/browser/room-event.d.ts +11 -0
- package/dist/browser/room-event.js +21 -1
- package/dist/browser/room-server-client.d.ts +2 -0
- package/dist/browser/room-server-client.js +6 -0
- package/dist/browser/runtime.d.ts +1 -1
- package/dist/browser/runtime.js +3 -1
- package/dist/browser/secrets-client.js +6 -2
- package/dist/browser/storage-client.d.ts +1 -0
- package/dist/browser/storage-client.js +9 -0
- package/dist/browser/sync-client.d.ts +16 -14
- package/dist/browser/sync-client.js +195 -116
- package/dist/esm/agent.js +74 -10
- package/dist/esm/developer-client.js +3 -0
- package/dist/esm/helpers.d.ts +2 -2
- package/dist/esm/helpers.js +1 -1
- package/dist/esm/meshagent-client.d.ts +25 -0
- package/dist/esm/meshagent-client.js +65 -0
- package/dist/esm/messaging-client.d.ts +29 -16
- package/dist/esm/messaging-client.js +256 -154
- package/dist/esm/participant.d.ts +7 -2
- package/dist/esm/participant.js +9 -9
- package/dist/esm/protocol.d.ts +85 -28
- package/dist/esm/protocol.js +352 -118
- package/dist/esm/room-client.d.ts +165 -29
- package/dist/esm/room-client.js +1112 -73
- package/dist/esm/room-event.d.ts +11 -0
- package/dist/esm/room-event.js +19 -0
- package/dist/esm/room-server-client.d.ts +2 -0
- package/dist/esm/room-server-client.js +7 -1
- package/dist/esm/runtime.d.ts +1 -1
- package/dist/esm/runtime.js +1 -1
- package/dist/esm/secrets-client.js +6 -2
- package/dist/esm/storage-client.d.ts +1 -0
- package/dist/esm/storage-client.js +9 -0
- package/dist/esm/sync-client.d.ts +16 -14
- package/dist/esm/sync-client.js +196 -117
- package/dist/node/agent.js +74 -10
- package/dist/node/developer-client.js +3 -0
- package/dist/node/helpers.d.ts +2 -2
- package/dist/node/helpers.js +1 -1
- package/dist/node/meshagent-client.d.ts +25 -0
- package/dist/node/meshagent-client.js +65 -0
- package/dist/node/messaging-client.d.ts +29 -16
- package/dist/node/messaging-client.js +256 -154
- package/dist/node/participant.d.ts +7 -2
- package/dist/node/participant.js +9 -9
- package/dist/node/protocol.d.ts +85 -28
- package/dist/node/protocol.js +356 -119
- package/dist/node/room-client.d.ts +165 -29
- package/dist/node/room-client.js +1114 -74
- package/dist/node/room-event.d.ts +11 -0
- package/dist/node/room-event.js +21 -1
- package/dist/node/room-server-client.d.ts +2 -0
- package/dist/node/room-server-client.js +6 -0
- package/dist/node/runtime.d.ts +1 -1
- package/dist/node/runtime.js +3 -1
- package/dist/node/secrets-client.js +6 -2
- package/dist/node/storage-client.d.ts +1 -0
- package/dist/node/storage-client.js +9 -0
- package/dist/node/sync-client.d.ts +16 -14
- package/dist/node/sync-client.js +195 -116
- package/package.json +6 -3
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MessagingClient = void 0;
|
|
4
|
-
const event_emitter_1 = require("./event-emitter");
|
|
5
4
|
const completer_1 = require("./completer");
|
|
5
|
+
const event_emitter_1 = require("./event-emitter");
|
|
6
6
|
const participant_1 = require("./participant");
|
|
7
|
+
const response_1 = require("./response");
|
|
7
8
|
const room_event_1 = require("./room-event");
|
|
8
9
|
const room_server_client_1 = require("./room-server-client");
|
|
9
10
|
const utils_1 = require("./utils");
|
|
10
11
|
const globalScope = globalThis;
|
|
11
12
|
function bytesToBase64(bytes) {
|
|
12
|
-
if (globalScope.Buffer) {
|
|
13
|
+
if (globalScope.Buffer != null) {
|
|
13
14
|
return globalScope.Buffer.from(bytes).toString("base64");
|
|
14
15
|
}
|
|
15
|
-
if (
|
|
16
|
+
if (globalScope.btoa == null) {
|
|
16
17
|
throw new Error("base64 encoding is not available in this runtime");
|
|
17
18
|
}
|
|
18
19
|
let binary = "";
|
|
@@ -30,10 +31,21 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
30
31
|
this._messageQueued = null;
|
|
31
32
|
this._sendTask = null;
|
|
32
33
|
this._messageQueueClosed = false;
|
|
33
|
-
this.
|
|
34
|
+
this._desiredEnabled = false;
|
|
35
|
+
this._online = false;
|
|
36
|
+
this._enableInFlight = false;
|
|
34
37
|
this.client = room;
|
|
35
38
|
this.client.protocol.addHandler("messaging.send", this._messageHandler);
|
|
36
39
|
}
|
|
40
|
+
get isEnabled() {
|
|
41
|
+
return this._desiredEnabled;
|
|
42
|
+
}
|
|
43
|
+
get online() {
|
|
44
|
+
return this._online;
|
|
45
|
+
}
|
|
46
|
+
get remoteParticipants() {
|
|
47
|
+
return Object.values(this._participants);
|
|
48
|
+
}
|
|
37
49
|
_messageInput(params) {
|
|
38
50
|
const input = {
|
|
39
51
|
type: params.type,
|
|
@@ -47,16 +59,110 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
47
59
|
}
|
|
48
60
|
return input;
|
|
49
61
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
async _invoke({ operation, input, }) {
|
|
63
|
+
await this.client.invoke({
|
|
64
|
+
toolkit: "messaging",
|
|
65
|
+
tool: operation,
|
|
66
|
+
input: new response_1.JsonContent({ json: input }),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
_invokeNowait({ operation, input, }) {
|
|
70
|
+
this.client.invokeNowait({
|
|
71
|
+
toolkit: "messaging",
|
|
72
|
+
tool: operation,
|
|
73
|
+
input: new response_1.JsonContent({ json: input }),
|
|
58
74
|
});
|
|
59
75
|
}
|
|
76
|
+
start() {
|
|
77
|
+
if (this._sendTask != null) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this._messageQueueClosed = false;
|
|
81
|
+
this._sendTask = this._sendMessages();
|
|
82
|
+
if (this._desiredEnabled && this.client.isConnected) {
|
|
83
|
+
this._enableCurrentConnectionNowait();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async stop() {
|
|
87
|
+
const stoppedError = this.client._messageStopError();
|
|
88
|
+
this._messageQueueClosed = true;
|
|
89
|
+
this._wakeMessageQueue();
|
|
90
|
+
this._drainQueuedMessages({ error: stoppedError });
|
|
91
|
+
const sendTask = this._sendTask;
|
|
92
|
+
this._sendTask = null;
|
|
93
|
+
if (sendTask != null) {
|
|
94
|
+
await sendTask;
|
|
95
|
+
}
|
|
96
|
+
this._desiredEnabled = false;
|
|
97
|
+
this._clearCurrentConnectionState();
|
|
98
|
+
}
|
|
99
|
+
async _nextQueuedMessage() {
|
|
100
|
+
while (true) {
|
|
101
|
+
if (this._messageQueue.length > 0) {
|
|
102
|
+
return this._messageQueue.shift() ?? null;
|
|
103
|
+
}
|
|
104
|
+
if (this._messageQueueClosed) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
this._messageQueued ?? (this._messageQueued = new completer_1.Completer());
|
|
108
|
+
await this._messageQueued.fut;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
_wakeMessageQueue() {
|
|
112
|
+
const signal = this._messageQueued;
|
|
113
|
+
this._messageQueued = null;
|
|
114
|
+
if (signal != null && !signal.completed) {
|
|
115
|
+
signal.complete();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
_queueMessage(message) {
|
|
119
|
+
if (this._messageQueueClosed) {
|
|
120
|
+
throw new room_server_client_1.RoomServerException("Cannot send messages because messaging has been stopped");
|
|
121
|
+
}
|
|
122
|
+
this._messageQueue.push(message);
|
|
123
|
+
this._wakeMessageQueue();
|
|
124
|
+
}
|
|
125
|
+
_setOnline(online) {
|
|
126
|
+
if (this._online === online) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this._online = online;
|
|
130
|
+
}
|
|
131
|
+
async _waitUntilOnline() {
|
|
132
|
+
while (!this._online) {
|
|
133
|
+
if (!this.client.isConnected && !this.client._allowDisconnectedRequests) {
|
|
134
|
+
await this.client._waitUntilConnectedForMessages();
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
this.client._raiseIfTerminalForMessages();
|
|
138
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
_enableCurrentConnectionNowait() {
|
|
142
|
+
if (this._online || this._enableInFlight) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
this._enableInFlight = true;
|
|
146
|
+
this._invokeNowait({ operation: "enable", input: {} });
|
|
147
|
+
}
|
|
148
|
+
_clearCurrentConnectionState() {
|
|
149
|
+
this._enableInFlight = false;
|
|
150
|
+
this._setOnline(false);
|
|
151
|
+
if (Object.keys(this._participants).length === 0) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
for (const participantId of Object.keys(this._participants)) {
|
|
155
|
+
this._removeParticipant(participantId);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
_onRoomDisconnect({ reason: _reason }) {
|
|
159
|
+
this._clearCurrentConnectionState();
|
|
160
|
+
}
|
|
161
|
+
_onRoomReconnect() {
|
|
162
|
+
if (this._desiredEnabled) {
|
|
163
|
+
this._enableCurrentConnectionNowait();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
60
166
|
_removeParticipant(participantId) {
|
|
61
167
|
const participant = this._participants[participantId];
|
|
62
168
|
if (participant === undefined) {
|
|
@@ -71,18 +177,14 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
71
177
|
return;
|
|
72
178
|
}
|
|
73
179
|
participant._setOnline(false);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return;
|
|
180
|
+
if (this._participants[participant.id] !== undefined) {
|
|
181
|
+
this._removeParticipant(participant.id);
|
|
77
182
|
}
|
|
78
|
-
this._removeParticipant(participant.id);
|
|
79
|
-
this.emit("participant_removed", this._syntheticMessageEvent({
|
|
80
|
-
fromParticipantId: participant.id,
|
|
81
|
-
type: "participant.disabled",
|
|
82
|
-
message: { id: participant.id },
|
|
83
|
-
}));
|
|
84
183
|
}
|
|
85
184
|
_resolveMessageRecipient(to) {
|
|
185
|
+
if (to == null) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
86
188
|
if (!(to instanceof participant_1.RemoteParticipant)) {
|
|
87
189
|
return to;
|
|
88
190
|
}
|
|
@@ -91,116 +193,100 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
91
193
|
}
|
|
92
194
|
return this._participants[to.id] ?? null;
|
|
93
195
|
}
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
this._messageQueue.push(message);
|
|
99
|
-
const waiter = this._messageQueued;
|
|
100
|
-
if (waiter !== null) {
|
|
101
|
-
this._messageQueued = null;
|
|
102
|
-
waiter.complete();
|
|
196
|
+
_dropQueuedMessage({ message, error, }) {
|
|
197
|
+
if (message.completer != null && !message.completer.completed) {
|
|
198
|
+
message.completer.completeError(error);
|
|
103
199
|
}
|
|
104
200
|
}
|
|
105
|
-
|
|
201
|
+
_drainQueuedMessages({ error }) {
|
|
106
202
|
while (this._messageQueue.length > 0) {
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
_isParticipantNotFound(error) {
|
|
112
|
-
return error instanceof room_server_client_1.RoomServerException && error.message === "the participant was not found";
|
|
113
|
-
}
|
|
114
|
-
async _nextQueuedMessage() {
|
|
115
|
-
while (this._messageQueue.length === 0) {
|
|
116
|
-
if (this._messageQueueClosed) {
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
if (this._messageQueued === null) {
|
|
120
|
-
this._messageQueued = new completer_1.Completer();
|
|
203
|
+
const queued = this._messageQueue.shift();
|
|
204
|
+
if (queued != null) {
|
|
205
|
+
this._dropQueuedMessage({ message: queued, error });
|
|
121
206
|
}
|
|
122
|
-
await this._messageQueued.fut;
|
|
123
207
|
}
|
|
124
|
-
return this._messageQueue.shift() ?? null;
|
|
125
208
|
}
|
|
126
209
|
async _sendMessages() {
|
|
127
210
|
while (true) {
|
|
128
|
-
const
|
|
129
|
-
if (
|
|
211
|
+
const message = await this._nextQueuedMessage();
|
|
212
|
+
if (message == null) {
|
|
130
213
|
return;
|
|
131
214
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
215
|
+
try {
|
|
216
|
+
await this.client._waitUntilConnectedForMessages();
|
|
217
|
+
if (this._desiredEnabled) {
|
|
218
|
+
await this._waitUntilOnline();
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
if (error instanceof room_server_client_1.RoomServerException) {
|
|
223
|
+
this._dropQueuedMessage({ message, error });
|
|
224
|
+
this._drainQueuedMessages({ error });
|
|
137
225
|
}
|
|
138
226
|
else {
|
|
139
|
-
|
|
227
|
+
const wrapped = new room_server_client_1.RoomServerException(String(error));
|
|
228
|
+
this._dropQueuedMessage({ message, error: wrapped });
|
|
229
|
+
this._drainQueuedMessages({ error: wrapped });
|
|
140
230
|
}
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const resolvedTo = this._resolveMessageRecipient(message.to);
|
|
234
|
+
if (resolvedTo == null) {
|
|
235
|
+
this._dropQueuedMessage({
|
|
236
|
+
message,
|
|
237
|
+
error: new room_server_client_1.RoomServerException("the participant was not found"),
|
|
238
|
+
});
|
|
141
239
|
continue;
|
|
142
240
|
}
|
|
143
241
|
try {
|
|
144
|
-
await this.
|
|
145
|
-
|
|
146
|
-
tool: "send",
|
|
242
|
+
await this._invoke({
|
|
243
|
+
operation: "send",
|
|
147
244
|
input: this._messageInput({
|
|
148
245
|
toParticipantId: resolvedTo.id,
|
|
149
|
-
type:
|
|
150
|
-
message:
|
|
151
|
-
attachment:
|
|
246
|
+
type: message.type,
|
|
247
|
+
message: message.message,
|
|
248
|
+
attachment: message.attachment,
|
|
152
249
|
}),
|
|
153
250
|
});
|
|
154
|
-
|
|
251
|
+
if (message.completer != null && !message.completer.completed) {
|
|
252
|
+
message.completer.complete();
|
|
253
|
+
}
|
|
155
254
|
}
|
|
156
255
|
catch (error) {
|
|
157
|
-
if (
|
|
158
|
-
this.
|
|
159
|
-
if (
|
|
160
|
-
|
|
256
|
+
if (error instanceof room_server_client_1.RoomServerException) {
|
|
257
|
+
const wrapped = this.client._coerceMessageSendError(error);
|
|
258
|
+
if (wrapped.message === "the participant was not found") {
|
|
259
|
+
this._markParticipantOffline(message.to);
|
|
260
|
+
this._dropQueuedMessage({ message, error: wrapped });
|
|
161
261
|
continue;
|
|
162
262
|
}
|
|
263
|
+
this._dropQueuedMessage({ message, error: wrapped });
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (message.completer != null && !message.completer.completed) {
|
|
267
|
+
message.completer.completeError(error);
|
|
163
268
|
}
|
|
164
|
-
queued.completer?.completeError(error);
|
|
165
269
|
}
|
|
166
270
|
}
|
|
167
271
|
}
|
|
168
|
-
async
|
|
169
|
-
if (this._sendTask
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
this._messageQueueClosed = false;
|
|
173
|
-
this._sendTask = this._sendMessages();
|
|
174
|
-
}
|
|
175
|
-
async stop() {
|
|
176
|
-
if (this._sendTask === null) {
|
|
177
|
-
this._enabled = false;
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
this._messageQueueClosed = true;
|
|
181
|
-
const waiter = this._messageQueued;
|
|
182
|
-
if (waiter !== null) {
|
|
183
|
-
this._messageQueued = null;
|
|
184
|
-
waiter.complete();
|
|
272
|
+
async sendMessage({ to, type, message, attachment, ignoreOffline = false, }) {
|
|
273
|
+
if (this._sendTask == null) {
|
|
274
|
+
throw new room_server_client_1.RoomServerException("Cannot send messages because messaging has not been started");
|
|
185
275
|
}
|
|
186
|
-
const
|
|
187
|
-
this._sendTask = null;
|
|
188
|
-
await sendTask;
|
|
189
|
-
this._enabled = false;
|
|
190
|
-
}
|
|
191
|
-
async sendMessage({ to, type, message, attachment, ignoreOffline = false }) {
|
|
192
|
-
const completer = new completer_1.Completer();
|
|
193
|
-
this._queueMessage({
|
|
276
|
+
const queued = {
|
|
194
277
|
to,
|
|
195
278
|
type,
|
|
196
279
|
message,
|
|
197
280
|
attachment,
|
|
198
281
|
dropIfOffline: ignoreOffline,
|
|
199
|
-
completer,
|
|
200
|
-
}
|
|
201
|
-
|
|
282
|
+
completer: ignoreOffline ? undefined : new completer_1.Completer(),
|
|
283
|
+
};
|
|
284
|
+
this._queueMessage(queued);
|
|
285
|
+
if (queued.completer != null) {
|
|
286
|
+
await queued.completer.fut;
|
|
287
|
+
}
|
|
202
288
|
}
|
|
203
|
-
sendMessageNowait({ to, type, message, attachment }) {
|
|
289
|
+
sendMessageNowait({ to, type, message, attachment, }) {
|
|
204
290
|
this._queueMessage({
|
|
205
291
|
to,
|
|
206
292
|
type,
|
|
@@ -209,34 +295,40 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
209
295
|
dropIfOffline: true,
|
|
210
296
|
});
|
|
211
297
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
});
|
|
218
|
-
this._enabled = true;
|
|
219
|
-
}
|
|
220
|
-
async disable() {
|
|
221
|
-
await this.client.invoke({
|
|
222
|
-
toolkit: "messaging",
|
|
223
|
-
tool: "disable",
|
|
224
|
-
input: {},
|
|
225
|
-
});
|
|
226
|
-
this._enabled = false;
|
|
227
|
-
}
|
|
228
|
-
async broadcastMessage({ type, message, attachment }) {
|
|
229
|
-
await this.client.invoke({
|
|
230
|
-
toolkit: "messaging",
|
|
231
|
-
tool: "broadcast",
|
|
232
|
-
input: this._messageInput({ type, message, attachment }),
|
|
233
|
-
});
|
|
298
|
+
enable() {
|
|
299
|
+
this._desiredEnabled = true;
|
|
300
|
+
if (this.client.isConnected) {
|
|
301
|
+
this._enableCurrentConnectionNowait();
|
|
302
|
+
}
|
|
234
303
|
}
|
|
235
|
-
|
|
236
|
-
|
|
304
|
+
disable() {
|
|
305
|
+
const wasOnline = this._online;
|
|
306
|
+
this._desiredEnabled = false;
|
|
307
|
+
this._clearCurrentConnectionState();
|
|
308
|
+
if (this.client.isConnected && wasOnline) {
|
|
309
|
+
this._invokeNowait({ operation: "disable", input: {} });
|
|
310
|
+
}
|
|
237
311
|
}
|
|
238
|
-
|
|
239
|
-
|
|
312
|
+
async broadcastMessage({ type, message, attachment, }) {
|
|
313
|
+
if (this._sendTask == null) {
|
|
314
|
+
throw new room_server_client_1.RoomServerException("Cannot send messages because messaging has not been started");
|
|
315
|
+
}
|
|
316
|
+
await this.client._waitUntilConnectedForMessages();
|
|
317
|
+
if (this._desiredEnabled) {
|
|
318
|
+
await this._waitUntilOnline();
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
await this._invoke({
|
|
322
|
+
operation: "broadcast",
|
|
323
|
+
input: this._messageInput({ type, message, attachment }),
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
if (error instanceof room_server_client_1.RoomServerException) {
|
|
328
|
+
throw this.client._coerceMessageSendError(error);
|
|
329
|
+
}
|
|
330
|
+
throw error;
|
|
331
|
+
}
|
|
240
332
|
}
|
|
241
333
|
getParticipants() {
|
|
242
334
|
return this.remoteParticipants;
|
|
@@ -252,15 +344,18 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
252
344
|
}
|
|
253
345
|
return null;
|
|
254
346
|
}
|
|
255
|
-
async _handleMessageSend(protocol,
|
|
256
|
-
|
|
257
|
-
|
|
347
|
+
async _handleMessageSend(protocol, _messageId, _type, bytes) {
|
|
348
|
+
if (!this.client.isActiveProtocol(protocol)) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const headerStr = (0, utils_1.splitMessageHeader)(bytes);
|
|
352
|
+
const payload = (0, utils_1.splitMessagePayload)(bytes);
|
|
258
353
|
const header = JSON.parse(headerStr);
|
|
259
354
|
const message = new room_event_1.RoomMessage({
|
|
260
355
|
fromParticipantId: header["from_participant_id"],
|
|
261
356
|
type: header["type"],
|
|
262
357
|
message: header["message"],
|
|
263
|
-
attachment: payload,
|
|
358
|
+
attachment: payload.length > 0 ? payload : undefined,
|
|
264
359
|
});
|
|
265
360
|
switch (message.type) {
|
|
266
361
|
case "messaging.enabled":
|
|
@@ -275,55 +370,62 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
275
370
|
case "participant.disabled":
|
|
276
371
|
this._onParticipantDisabled(message);
|
|
277
372
|
break;
|
|
373
|
+
default:
|
|
374
|
+
break;
|
|
278
375
|
}
|
|
279
|
-
const
|
|
280
|
-
this.client.emit(
|
|
281
|
-
this.emit("message",
|
|
376
|
+
const event = new room_event_1.RoomMessageEvent({ message });
|
|
377
|
+
this.client.emit(event);
|
|
378
|
+
this.emit("message", event);
|
|
282
379
|
}
|
|
283
380
|
_onParticipantEnabled(message) {
|
|
284
381
|
const data = message.message;
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
this._participants[data
|
|
288
|
-
this.emit("participant_added", { message });
|
|
382
|
+
const participant = new participant_1.RemoteParticipant(this.client, data.id, data.role, true);
|
|
383
|
+
participant._setAttributes(data.attributes ?? {});
|
|
384
|
+
this._participants[data.id] = participant;
|
|
385
|
+
this.emit("participant_added", new room_event_1.RoomMessageEvent({ message }));
|
|
289
386
|
}
|
|
290
387
|
_onParticipantAttributes(message) {
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
388
|
+
const participant = this._participants[message.fromParticipantId];
|
|
389
|
+
if (participant == null) {
|
|
293
390
|
return;
|
|
294
391
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
this.emit("participant_attributes_updated", { message });
|
|
392
|
+
participant._setAttributes(message.message["attributes"]);
|
|
393
|
+
this.emit("participant_attributes_updated", new room_event_1.RoomMessageEvent({ message }));
|
|
298
394
|
}
|
|
299
395
|
_onParticipantDisabled(message) {
|
|
300
|
-
const
|
|
301
|
-
if (
|
|
302
|
-
this.emit("participant_removed", { message });
|
|
396
|
+
const removed = this._removeParticipant(String(message.message["id"]));
|
|
397
|
+
if (removed != null) {
|
|
398
|
+
this.emit("participant_removed", new room_event_1.RoomMessageEvent({ message }));
|
|
303
399
|
}
|
|
304
400
|
}
|
|
305
401
|
_onMessagingEnabled(message) {
|
|
402
|
+
this._enableInFlight = false;
|
|
403
|
+
for (const participantId of Object.keys(this._participants)) {
|
|
404
|
+
delete this._participants[participantId];
|
|
405
|
+
}
|
|
306
406
|
const participants = message.message["participants"];
|
|
307
407
|
for (const data of participants) {
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
this._participants[data
|
|
408
|
+
const participant = new participant_1.RemoteParticipant(this.client, data.id, data.role, true);
|
|
409
|
+
participant._setAttributes(data.attributes ?? {});
|
|
410
|
+
this._participants[data.id] = participant;
|
|
411
|
+
}
|
|
412
|
+
this._setOnline(true);
|
|
413
|
+
if (!this._desiredEnabled) {
|
|
414
|
+
this._invokeNowait({ operation: "disable", input: {} });
|
|
415
|
+
this._clearCurrentConnectionState();
|
|
416
|
+
return;
|
|
311
417
|
}
|
|
312
|
-
this.
|
|
313
|
-
this.emit("messaging_enabled", { message });
|
|
418
|
+
this.emit("messaging_enabled", new room_event_1.RoomMessageEvent({ message }));
|
|
314
419
|
}
|
|
315
420
|
dispose() {
|
|
316
421
|
const error = new room_server_client_1.RoomServerException("messaging client disposed");
|
|
317
422
|
this._messageQueueClosed = true;
|
|
318
|
-
this.
|
|
319
|
-
this.
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
waiter.complete();
|
|
324
|
-
}
|
|
423
|
+
this._wakeMessageQueue();
|
|
424
|
+
this._drainQueuedMessages({ error });
|
|
425
|
+
this._desiredEnabled = false;
|
|
426
|
+
this._clearCurrentConnectionState();
|
|
427
|
+
this.client.protocol.removeHandler("messaging.send", this._messageHandler);
|
|
325
428
|
super.dispose();
|
|
326
|
-
this.client.protocol.removeHandler("messaging.send");
|
|
327
429
|
}
|
|
328
430
|
}
|
|
329
431
|
exports.MessagingClient = MessagingClient;
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { RoomClient } from "./room-client";
|
|
2
2
|
export declare abstract class Participant {
|
|
3
|
-
|
|
3
|
+
id: string;
|
|
4
4
|
protected readonly client: RoomClient;
|
|
5
5
|
protected _attributes: Record<string, unknown>;
|
|
6
6
|
protected _connections: string[];
|
|
7
7
|
constructor(client: RoomClient, id: string);
|
|
8
8
|
get connections(): ReadonlyArray<string>;
|
|
9
9
|
getAttribute(name: string): unknown;
|
|
10
|
+
_replaceIdentity({ participantId, attributes, }: {
|
|
11
|
+
participantId: string;
|
|
12
|
+
attributes: Record<string, unknown>;
|
|
13
|
+
}): void;
|
|
10
14
|
_setAttribute(name: string, value: unknown): void;
|
|
11
15
|
_setAttributes(attributes: Record<string, unknown>): void;
|
|
16
|
+
_attributesSnapshot(): Record<string, unknown>;
|
|
12
17
|
}
|
|
13
18
|
export declare class RemoteParticipant extends Participant {
|
|
14
19
|
readonly role: string;
|
|
@@ -18,5 +23,5 @@ export declare class RemoteParticipant extends Participant {
|
|
|
18
23
|
}
|
|
19
24
|
export declare class LocalParticipant extends Participant {
|
|
20
25
|
constructor(client: RoomClient, id: string);
|
|
21
|
-
setAttribute(name: string, value: unknown):
|
|
26
|
+
setAttribute(name: string, value: unknown): void;
|
|
22
27
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LocalParticipant = exports.RemoteParticipant = exports.Participant = void 0;
|
|
4
|
-
const utils_1 = require("./utils");
|
|
5
4
|
class Participant {
|
|
6
5
|
constructor(client, id) {
|
|
7
6
|
this._attributes = {};
|
|
@@ -15,6 +14,10 @@ class Participant {
|
|
|
15
14
|
getAttribute(name) {
|
|
16
15
|
return this._attributes[name];
|
|
17
16
|
}
|
|
17
|
+
_replaceIdentity({ participantId, attributes, }) {
|
|
18
|
+
this.id = participantId;
|
|
19
|
+
this._attributes = { ...attributes };
|
|
20
|
+
}
|
|
18
21
|
_setAttribute(name, value) {
|
|
19
22
|
this._attributes[name] = value;
|
|
20
23
|
}
|
|
@@ -23,6 +26,9 @@ class Participant {
|
|
|
23
26
|
this._setAttribute(name, value);
|
|
24
27
|
}
|
|
25
28
|
}
|
|
29
|
+
_attributesSnapshot() {
|
|
30
|
+
return { ...this._attributes };
|
|
31
|
+
}
|
|
26
32
|
}
|
|
27
33
|
exports.Participant = Participant;
|
|
28
34
|
class RemoteParticipant extends Participant {
|
|
@@ -40,15 +46,9 @@ class LocalParticipant extends Participant {
|
|
|
40
46
|
constructor(client, id) {
|
|
41
47
|
super(client, id);
|
|
42
48
|
}
|
|
43
|
-
|
|
49
|
+
setAttribute(name, value) {
|
|
44
50
|
this._setAttribute(name, value);
|
|
45
|
-
|
|
46
|
-
const payload = (0, utils_1.packMessage)({ [name]: value });
|
|
47
|
-
await this.client.protocol.send("set_attributes", payload);
|
|
48
|
-
}
|
|
49
|
-
catch (err) {
|
|
50
|
-
console.warn("Unable to send attribute changes", err);
|
|
51
|
-
}
|
|
51
|
+
this.client._sendLocalAttributesNowait({ [name]: value });
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
exports.LocalParticipant = LocalParticipant;
|