@xmtp/browser-sdk 1.0.0-rc1 → 1.1.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.
@@ -0,0 +1,85 @@
1
+ import type { ConsentEntityType, UserPreference } from "@xmtp/wasm-bindings";
2
+ import { v4 } from "uuid";
3
+ import { AsyncStream, type StreamCallback } from "@/AsyncStream";
4
+ import type { SafeConsent } from "@/utils/conversions";
5
+ import type { Client } from "./Client";
6
+
7
+ export class Preferences {
8
+ #client: Client;
9
+
10
+ constructor(client: Client) {
11
+ this.#client = client;
12
+ }
13
+
14
+ async inboxState(refreshFromNetwork?: boolean) {
15
+ return this.#client.sendMessage("inboxState", {
16
+ refreshFromNetwork: refreshFromNetwork ?? false,
17
+ });
18
+ }
19
+
20
+ async inboxStateFromInboxIds(
21
+ inboxIds: string[],
22
+ refreshFromNetwork?: boolean,
23
+ ) {
24
+ return this.#client.sendMessage("inboxStateFromInboxIds", {
25
+ inboxIds,
26
+ refreshFromNetwork: refreshFromNetwork ?? false,
27
+ });
28
+ }
29
+
30
+ async getLatestInboxState(inboxId: string) {
31
+ return this.#client.sendMessage("getLatestInboxState", { inboxId });
32
+ }
33
+
34
+ async setConsentStates(records: SafeConsent[]) {
35
+ return this.#client.sendMessage("setConsentStates", { records });
36
+ }
37
+
38
+ async getConsentState(entityType: ConsentEntityType, entity: string) {
39
+ return this.#client.sendMessage("getConsentState", { entityType, entity });
40
+ }
41
+
42
+ async streamConsent(callback?: StreamCallback<SafeConsent[]>) {
43
+ const streamId = v4();
44
+ const asyncStream = new AsyncStream<SafeConsent[]>();
45
+ const endStream = this.#client.handleStreamMessage<SafeConsent[]>(
46
+ streamId,
47
+ (error, value) => {
48
+ void asyncStream.callback(error, value ?? undefined);
49
+ void callback?.(error, value ?? undefined);
50
+ },
51
+ );
52
+ await this.#client.sendMessage("streamConsent", {
53
+ streamId,
54
+ });
55
+ asyncStream.onReturn = () => {
56
+ void this.#client.sendMessage("endStream", {
57
+ streamId,
58
+ });
59
+ endStream();
60
+ };
61
+ return asyncStream;
62
+ }
63
+
64
+ async streamPreferences(callback?: StreamCallback<UserPreference[]>) {
65
+ const streamId = v4();
66
+ const asyncStream = new AsyncStream<UserPreference[]>();
67
+ const endStream = this.#client.handleStreamMessage<UserPreference[]>(
68
+ streamId,
69
+ (error, value) => {
70
+ void asyncStream.callback(error, value ?? undefined);
71
+ void callback?.(error, value ?? undefined);
72
+ },
73
+ );
74
+ await this.#client.sendMessage("streamPreferences", {
75
+ streamId,
76
+ });
77
+ asyncStream.onReturn = () => {
78
+ void this.#client.sendMessage("endStream", {
79
+ streamId,
80
+ });
81
+ endStream();
82
+ };
83
+ return asyncStream;
84
+ }
85
+ }
@@ -1,23 +1,24 @@
1
1
  import {
2
2
  verifySignedWithPublicKey,
3
3
  type Client,
4
- type ConsentEntityType,
5
4
  type Identifier,
6
5
  type SignatureRequestType,
7
6
  } from "@xmtp/wasm-bindings";
8
7
  import type { ClientOptions } from "@/types";
9
- import { fromSafeConsent, type SafeConsent } from "@/utils/conversions";
10
8
  import { createClient } from "@/utils/createClient";
11
9
  import { WorkerConversations } from "@/WorkerConversations";
10
+ import { WorkerPreferences } from "@/WorkerPreferences";
12
11
 
13
12
  export class WorkerClient {
14
13
  #client: Client;
15
-
16
14
  #conversations: WorkerConversations;
15
+ #preferences: WorkerPreferences;
17
16
 
18
17
  constructor(client: Client) {
19
18
  this.#client = client;
20
- this.#conversations = new WorkerConversations(this, client.conversations());
19
+ const conversations = client.conversations();
20
+ this.#conversations = new WorkerConversations(this, conversations);
21
+ this.#preferences = new WorkerPreferences(client, conversations);
21
22
  }
22
23
 
23
24
  static async create(
@@ -49,6 +50,14 @@ export class WorkerClient {
49
50
  return this.#client.isRegistered;
50
51
  }
51
52
 
53
+ get conversations() {
54
+ return this.#conversations;
55
+ }
56
+
57
+ get preferences() {
58
+ return this.#preferences;
59
+ }
60
+
52
61
  createInboxSignatureText() {
53
62
  try {
54
63
  return this.#client.createInboxSignatureText();
@@ -122,26 +131,6 @@ export class WorkerClient {
122
131
  return this.#client.findInboxIdByIdentifier(identifier);
123
132
  }
124
133
 
125
- async inboxState(refreshFromNetwork: boolean) {
126
- return this.#client.inboxState(refreshFromNetwork);
127
- }
128
-
129
- async getLatestInboxState(inboxId: string) {
130
- return this.#client.getLatestInboxState(inboxId);
131
- }
132
-
133
- async setConsentStates(records: SafeConsent[]) {
134
- return this.#client.setConsentStates(records.map(fromSafeConsent));
135
- }
136
-
137
- async getConsentState(entityType: ConsentEntityType, entity: string) {
138
- return this.#client.getConsentState(entityType, entity);
139
- }
140
-
141
- get conversations() {
142
- return this.#conversations;
143
- }
144
-
145
134
  signWithInstallationKey(signatureText: string) {
146
135
  return this.#client.signWithInstallationKey(signatureText);
147
136
  }
@@ -1,13 +1,11 @@
1
1
  import {
2
2
  ConversationType,
3
- type Consent,
4
3
  type ConsentState,
5
4
  type Conversation,
6
5
  type ConversationListItem,
7
6
  type Conversations,
8
7
  type Identifier,
9
8
  type Message,
10
- type UserPreference,
11
9
  } from "@xmtp/wasm-bindings";
12
10
  import type { StreamCallback } from "@/AsyncStream";
13
11
  import {
@@ -178,27 +176,4 @@ export class WorkerConversations {
178
176
  conversationType,
179
177
  );
180
178
  }
181
-
182
- streamConsent(callback?: StreamCallback<Consent[]>) {
183
- const on_consent_update = (consent: Consent[]) => {
184
- void callback?.(null, consent);
185
- };
186
- const on_error = (error: Error | null) => {
187
- void callback?.(error, undefined);
188
- };
189
- return this.#conversations.streamConsent({ on_consent_update, on_error });
190
- }
191
-
192
- streamPreferences(callback?: StreamCallback<UserPreference[]>) {
193
- const on_user_preference_update = (preferences: UserPreference[]) => {
194
- void callback?.(null, preferences);
195
- };
196
- const on_error = (error: Error | null) => {
197
- void callback?.(error, undefined);
198
- };
199
- return this.#conversations.streamPreferences({
200
- on_user_preference_update,
201
- on_error,
202
- });
203
- }
204
179
  }
@@ -0,0 +1,68 @@
1
+ import {
2
+ type Client,
3
+ type Consent,
4
+ type ConsentEntityType,
5
+ type Conversations,
6
+ type UserPreference,
7
+ } from "@xmtp/wasm-bindings";
8
+ import type { StreamCallback } from "@/AsyncStream";
9
+ import { fromSafeConsent, type SafeConsent } from "@/utils/conversions";
10
+
11
+ export class WorkerPreferences {
12
+ #client: Client;
13
+ #conversations: Conversations;
14
+
15
+ constructor(client: Client, conversations: Conversations) {
16
+ this.#client = client;
17
+ this.#conversations = conversations;
18
+ }
19
+
20
+ async inboxState(refreshFromNetwork: boolean) {
21
+ return this.#client.inboxState(refreshFromNetwork);
22
+ }
23
+
24
+ async inboxStateFromInboxIds(
25
+ inboxIds: string[],
26
+ refreshFromNetwork?: boolean,
27
+ ) {
28
+ return this.#client.inboxStateFromInboxIds(
29
+ inboxIds,
30
+ refreshFromNetwork ?? false,
31
+ );
32
+ }
33
+
34
+ async getLatestInboxState(inboxId: string) {
35
+ return this.#client.getLatestInboxState(inboxId);
36
+ }
37
+
38
+ async setConsentStates(records: SafeConsent[]) {
39
+ return this.#client.setConsentStates(records.map(fromSafeConsent));
40
+ }
41
+
42
+ async getConsentState(entityType: ConsentEntityType, entity: string) {
43
+ return this.#client.getConsentState(entityType, entity);
44
+ }
45
+
46
+ streamConsent(callback?: StreamCallback<Consent[]>) {
47
+ const on_consent_update = (consent: Consent[]) => {
48
+ void callback?.(null, consent);
49
+ };
50
+ const on_error = (error: Error | null) => {
51
+ void callback?.(error, undefined);
52
+ };
53
+ return this.#conversations.streamConsent({ on_consent_update, on_error });
54
+ }
55
+
56
+ streamPreferences(callback?: StreamCallback<UserPreference[]>) {
57
+ const on_user_preference_update = (preferences: UserPreference[]) => {
58
+ void callback?.(null, preferences);
59
+ };
60
+ const on_error = (error: Error | null) => {
61
+ void callback?.(error, undefined);
62
+ };
63
+ return this.#conversations.streamPreferences({
64
+ on_user_preference_update,
65
+ on_error,
66
+ });
67
+ }
68
+ }
package/src/index.ts CHANGED
@@ -41,6 +41,7 @@ export {
41
41
  Message,
42
42
  MessageDisappearingSettings,
43
43
  MetadataField,
44
+ Opfs,
44
45
  PermissionLevel,
45
46
  PermissionPolicy,
46
47
  PermissionPolicySet,
@@ -152,6 +152,15 @@ export type ClientEvents =
152
152
  refreshFromNetwork: boolean;
153
153
  };
154
154
  }
155
+ | {
156
+ action: "inboxStateFromInboxIds";
157
+ id: string;
158
+ result: SafeInboxState[];
159
+ data: {
160
+ inboxIds: string[];
161
+ refreshFromNetwork: boolean;
162
+ };
163
+ }
155
164
  | {
156
165
  action: "getLatestInboxState";
157
166
  id: string;
@@ -194,24 +194,37 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
194
194
  break;
195
195
  }
196
196
  case "inboxState": {
197
- const inboxState = await client.inboxState(data.refreshFromNetwork);
197
+ const inboxState = await client.preferences.inboxState(
198
+ data.refreshFromNetwork,
199
+ );
198
200
  const result = toSafeInboxState(inboxState);
199
201
  postMessage({ id, action, result });
200
202
  break;
201
203
  }
204
+ case "inboxStateFromInboxIds": {
205
+ const inboxStates = await client.preferences.inboxStateFromInboxIds(
206
+ data.inboxIds,
207
+ data.refreshFromNetwork,
208
+ );
209
+ const result = inboxStates.map(toSafeInboxState);
210
+ postMessage({ id, action, result });
211
+ break;
212
+ }
202
213
  case "getLatestInboxState": {
203
- const inboxState = await client.getLatestInboxState(data.inboxId);
214
+ const inboxState = await client.preferences.getLatestInboxState(
215
+ data.inboxId,
216
+ );
204
217
  const result = toSafeInboxState(inboxState);
205
218
  postMessage({ id, action, result });
206
219
  break;
207
220
  }
208
221
  case "setConsentStates": {
209
- await client.setConsentStates(data.records);
222
+ await client.preferences.setConsentStates(data.records);
210
223
  postMessage({ id, action, result: undefined });
211
224
  break;
212
225
  }
213
226
  case "getConsentState": {
214
- const result = await client.getConsentState(
227
+ const result = await client.preferences.getConsentState(
215
228
  data.entityType,
216
229
  data.entity,
217
230
  );
@@ -325,7 +338,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
325
338
  });
326
339
  }
327
340
  };
328
- const streamCloser = client.conversations.streamConsent(streamCallback);
341
+ const streamCloser = client.preferences.streamConsent(streamCallback);
329
342
  streamClosers.set(data.streamId, streamCloser);
330
343
  postMessage({
331
344
  id,
@@ -354,7 +367,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
354
367
  }
355
368
  };
356
369
  const streamCloser =
357
- client.conversations.streamPreferences(streamCallback);
370
+ client.preferences.streamPreferences(streamCallback);
358
371
  streamClosers.set(data.streamId, streamCloser);
359
372
  postMessage({
360
373
  id,