@xmtp/browser-sdk 0.0.16 → 0.0.18
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/dist/index.d.ts +108 -27
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/workers/client.js +1 -1
- package/dist/workers/client.js.map +1 -1
- package/package.json +2 -2
- package/src/AsyncStream.ts +80 -0
- package/src/ClientWorkerClass.ts +28 -0
- package/src/Conversation.ts +29 -15
- package/src/Conversations.ts +79 -0
- package/src/WorkerClient.ts +2 -2
- package/src/WorkerConversation.ts +12 -8
- package/src/WorkerConversations.ts +47 -1
- package/src/types/clientEvents.ts +39 -9
- package/src/types/clientStreamEvents.ts +30 -0
- package/src/types/utils.ts +26 -0
- package/src/utils/conversions.ts +45 -43
- package/src/utils/createClient.ts +3 -3
- package/src/workers/client.ts +151 -19
package/src/workers/client.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
import type { Conversation, Message, StreamCloser } from "@xmtp/wasm-bindings";
|
|
1
2
|
import type {
|
|
2
3
|
ClientEventsActions,
|
|
3
4
|
ClientEventsClientMessageData,
|
|
4
5
|
ClientEventsErrorData,
|
|
5
6
|
ClientEventsWorkerPostMessageData,
|
|
6
7
|
} from "@/types";
|
|
8
|
+
import type {
|
|
9
|
+
ClientStreamEventsErrorData,
|
|
10
|
+
ClientStreamEventsTypes,
|
|
11
|
+
ClientStreamEventsWorkerPostMessageData,
|
|
12
|
+
} from "@/types/clientStreamEvents";
|
|
7
13
|
import {
|
|
8
14
|
fromEncodedContent,
|
|
9
15
|
fromSafeEncodedContent,
|
|
@@ -13,10 +19,13 @@ import {
|
|
|
13
19
|
toSafeMessage,
|
|
14
20
|
} from "@/utils/conversions";
|
|
15
21
|
import { WorkerClient } from "@/WorkerClient";
|
|
22
|
+
import { WorkerConversation } from "@/WorkerConversation";
|
|
16
23
|
|
|
17
24
|
let client: WorkerClient;
|
|
18
25
|
let enableLogging = false;
|
|
19
26
|
|
|
27
|
+
const streamClosers = new Map<string, StreamCloser>();
|
|
28
|
+
|
|
20
29
|
/**
|
|
21
30
|
* Type-safe postMessage
|
|
22
31
|
*/
|
|
@@ -33,6 +42,22 @@ const postMessageError = (data: ClientEventsErrorData) => {
|
|
|
33
42
|
self.postMessage(data);
|
|
34
43
|
};
|
|
35
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Type-safe postMessage for streams
|
|
47
|
+
*/
|
|
48
|
+
const postStreamMessage = <A extends ClientStreamEventsTypes>(
|
|
49
|
+
data: ClientStreamEventsWorkerPostMessageData<A>,
|
|
50
|
+
) => {
|
|
51
|
+
self.postMessage(data);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Type-safe postMessage for stream errors
|
|
56
|
+
*/
|
|
57
|
+
const postStreamMessageError = (data: ClientStreamEventsErrorData) => {
|
|
58
|
+
self.postMessage(data);
|
|
59
|
+
};
|
|
60
|
+
|
|
36
61
|
self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
|
|
37
62
|
const { action, id, data } = event.data;
|
|
38
63
|
|
|
@@ -53,6 +78,28 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
|
|
|
53
78
|
|
|
54
79
|
try {
|
|
55
80
|
switch (action) {
|
|
81
|
+
/**
|
|
82
|
+
* Stream actions
|
|
83
|
+
*/
|
|
84
|
+
case "endStream": {
|
|
85
|
+
const streamCloser = streamClosers.get(data.streamId);
|
|
86
|
+
if (streamCloser) {
|
|
87
|
+
streamCloser.end();
|
|
88
|
+
streamClosers.delete(data.streamId);
|
|
89
|
+
postMessage({
|
|
90
|
+
id,
|
|
91
|
+
action,
|
|
92
|
+
result: undefined,
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
postMessageError({
|
|
96
|
+
id,
|
|
97
|
+
action,
|
|
98
|
+
error: `Stream "${data.streamId}" not found`,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
56
103
|
/**
|
|
57
104
|
* Client actions
|
|
58
105
|
*/
|
|
@@ -76,7 +123,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
|
|
|
76
123
|
});
|
|
77
124
|
break;
|
|
78
125
|
case "createInboxSignatureText": {
|
|
79
|
-
const result =
|
|
126
|
+
const result = client.createInboxSignatureText();
|
|
80
127
|
postMessage({
|
|
81
128
|
id,
|
|
82
129
|
action,
|
|
@@ -266,6 +313,72 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
|
|
|
266
313
|
/**
|
|
267
314
|
* Conversations actions
|
|
268
315
|
*/
|
|
316
|
+
case "streamAllGroups": {
|
|
317
|
+
const streamCallback = async (
|
|
318
|
+
error: Error | null,
|
|
319
|
+
value: Conversation | undefined,
|
|
320
|
+
) => {
|
|
321
|
+
if (error) {
|
|
322
|
+
postStreamMessageError({
|
|
323
|
+
type: "group",
|
|
324
|
+
streamId: data.streamId,
|
|
325
|
+
error: error.message,
|
|
326
|
+
});
|
|
327
|
+
} else {
|
|
328
|
+
postStreamMessage({
|
|
329
|
+
type: "group",
|
|
330
|
+
streamId: data.streamId,
|
|
331
|
+
result: value
|
|
332
|
+
? await toSafeConversation(
|
|
333
|
+
new WorkerConversation(client, value),
|
|
334
|
+
)
|
|
335
|
+
: undefined,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
const streamCloser = client.conversations.stream(
|
|
340
|
+
streamCallback,
|
|
341
|
+
data.conversationType,
|
|
342
|
+
);
|
|
343
|
+
streamClosers.set(data.streamId, streamCloser);
|
|
344
|
+
postMessage({
|
|
345
|
+
id,
|
|
346
|
+
action,
|
|
347
|
+
result: undefined,
|
|
348
|
+
});
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
case "streamAllMessages": {
|
|
352
|
+
const streamCallback = (
|
|
353
|
+
error: Error | null,
|
|
354
|
+
value: Message | undefined,
|
|
355
|
+
) => {
|
|
356
|
+
if (error) {
|
|
357
|
+
postStreamMessageError({
|
|
358
|
+
type: "message",
|
|
359
|
+
streamId: data.streamId,
|
|
360
|
+
error: error.message,
|
|
361
|
+
});
|
|
362
|
+
} else {
|
|
363
|
+
postStreamMessage({
|
|
364
|
+
type: "message",
|
|
365
|
+
streamId: data.streamId,
|
|
366
|
+
result: value ? toSafeMessage(value) : undefined,
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
const streamCloser = client.conversations.streamAllMessages(
|
|
371
|
+
streamCallback,
|
|
372
|
+
data.conversationType,
|
|
373
|
+
);
|
|
374
|
+
streamClosers.set(data.streamId, streamCloser);
|
|
375
|
+
postMessage({
|
|
376
|
+
id,
|
|
377
|
+
action,
|
|
378
|
+
result: undefined,
|
|
379
|
+
});
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
269
382
|
case "getConversations": {
|
|
270
383
|
const conversations = client.conversations.list(data.options);
|
|
271
384
|
postMessage({
|
|
@@ -466,24 +579,6 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
|
|
|
466
579
|
}
|
|
467
580
|
break;
|
|
468
581
|
}
|
|
469
|
-
case "updateGroupPinnedFrameUrl": {
|
|
470
|
-
const group = client.conversations.getConversationById(data.id);
|
|
471
|
-
if (group) {
|
|
472
|
-
await group.updatePinnedFrameUrl(data.pinnedFrameUrl);
|
|
473
|
-
postMessage({
|
|
474
|
-
id,
|
|
475
|
-
action,
|
|
476
|
-
result: undefined,
|
|
477
|
-
});
|
|
478
|
-
} else {
|
|
479
|
-
postMessageError({
|
|
480
|
-
id,
|
|
481
|
-
action,
|
|
482
|
-
error: "Group not found",
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
break;
|
|
486
|
-
}
|
|
487
582
|
case "sendGroupMessage": {
|
|
488
583
|
const group = client.conversations.getConversationById(data.id);
|
|
489
584
|
if (group) {
|
|
@@ -885,6 +980,43 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
|
|
|
885
980
|
}
|
|
886
981
|
break;
|
|
887
982
|
}
|
|
983
|
+
case "streamGroupMessages": {
|
|
984
|
+
const group = client.conversations.getConversationById(data.groupId);
|
|
985
|
+
if (group) {
|
|
986
|
+
const streamCallback = (
|
|
987
|
+
error: Error | null,
|
|
988
|
+
value: Message | undefined,
|
|
989
|
+
) => {
|
|
990
|
+
if (error) {
|
|
991
|
+
postStreamMessageError({
|
|
992
|
+
type: "message",
|
|
993
|
+
streamId: data.streamId,
|
|
994
|
+
error: error.message,
|
|
995
|
+
});
|
|
996
|
+
} else {
|
|
997
|
+
postStreamMessage({
|
|
998
|
+
type: "message",
|
|
999
|
+
streamId: data.streamId,
|
|
1000
|
+
result: value ? toSafeMessage(value) : undefined,
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
const streamCloser = group.stream(streamCallback);
|
|
1005
|
+
streamClosers.set(data.streamId, streamCloser);
|
|
1006
|
+
postMessage({
|
|
1007
|
+
id,
|
|
1008
|
+
action,
|
|
1009
|
+
result: undefined,
|
|
1010
|
+
});
|
|
1011
|
+
} else {
|
|
1012
|
+
postMessageError({
|
|
1013
|
+
id,
|
|
1014
|
+
action,
|
|
1015
|
+
error: "Group not found",
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
break;
|
|
1019
|
+
}
|
|
888
1020
|
}
|
|
889
1021
|
} catch (e) {
|
|
890
1022
|
postMessageError({
|