@xmtp/browser-sdk 1.0.0-rc1 → 1.0.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,75 @@
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 getLatestInboxState(inboxId: string) {
21
+ return this.#client.sendMessage("getLatestInboxState", { inboxId });
22
+ }
23
+
24
+ async setConsentStates(records: SafeConsent[]) {
25
+ return this.#client.sendMessage("setConsentStates", { records });
26
+ }
27
+
28
+ async getConsentState(entityType: ConsentEntityType, entity: string) {
29
+ return this.#client.sendMessage("getConsentState", { entityType, entity });
30
+ }
31
+
32
+ async streamConsent(callback?: StreamCallback<SafeConsent[]>) {
33
+ const streamId = v4();
34
+ const asyncStream = new AsyncStream<SafeConsent[]>();
35
+ const endStream = this.#client.handleStreamMessage<SafeConsent[]>(
36
+ streamId,
37
+ (error, value) => {
38
+ void asyncStream.callback(error, value ?? undefined);
39
+ void callback?.(error, value ?? undefined);
40
+ },
41
+ );
42
+ await this.#client.sendMessage("streamConsent", {
43
+ streamId,
44
+ });
45
+ asyncStream.onReturn = () => {
46
+ void this.#client.sendMessage("endStream", {
47
+ streamId,
48
+ });
49
+ endStream();
50
+ };
51
+ return asyncStream;
52
+ }
53
+
54
+ async streamPreferences(callback?: StreamCallback<UserPreference[]>) {
55
+ const streamId = v4();
56
+ const asyncStream = new AsyncStream<UserPreference[]>();
57
+ const endStream = this.#client.handleStreamMessage<UserPreference[]>(
58
+ streamId,
59
+ (error, value) => {
60
+ void asyncStream.callback(error, value ?? undefined);
61
+ void callback?.(error, value ?? undefined);
62
+ },
63
+ );
64
+ await this.#client.sendMessage("streamPreferences", {
65
+ streamId,
66
+ });
67
+ asyncStream.onReturn = () => {
68
+ void this.#client.sendMessage("endStream", {
69
+ streamId,
70
+ });
71
+ endStream();
72
+ };
73
+ return asyncStream;
74
+ }
75
+ }
@@ -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,58 @@
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 getLatestInboxState(inboxId: string) {
25
+ return this.#client.getLatestInboxState(inboxId);
26
+ }
27
+
28
+ async setConsentStates(records: SafeConsent[]) {
29
+ return this.#client.setConsentStates(records.map(fromSafeConsent));
30
+ }
31
+
32
+ async getConsentState(entityType: ConsentEntityType, entity: string) {
33
+ return this.#client.getConsentState(entityType, entity);
34
+ }
35
+
36
+ streamConsent(callback?: StreamCallback<Consent[]>) {
37
+ const on_consent_update = (consent: Consent[]) => {
38
+ void callback?.(null, consent);
39
+ };
40
+ const on_error = (error: Error | null) => {
41
+ void callback?.(error, undefined);
42
+ };
43
+ return this.#conversations.streamConsent({ on_consent_update, on_error });
44
+ }
45
+
46
+ streamPreferences(callback?: StreamCallback<UserPreference[]>) {
47
+ const on_user_preference_update = (preferences: UserPreference[]) => {
48
+ void callback?.(null, preferences);
49
+ };
50
+ const on_error = (error: Error | null) => {
51
+ void callback?.(error, undefined);
52
+ };
53
+ return this.#conversations.streamPreferences({
54
+ on_user_preference_update,
55
+ on_error,
56
+ });
57
+ }
58
+ }
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,
@@ -194,24 +194,28 @@ 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
  }
202
204
  case "getLatestInboxState": {
203
- const inboxState = await client.getLatestInboxState(data.inboxId);
205
+ const inboxState = await client.preferences.getLatestInboxState(
206
+ data.inboxId,
207
+ );
204
208
  const result = toSafeInboxState(inboxState);
205
209
  postMessage({ id, action, result });
206
210
  break;
207
211
  }
208
212
  case "setConsentStates": {
209
- await client.setConsentStates(data.records);
213
+ await client.preferences.setConsentStates(data.records);
210
214
  postMessage({ id, action, result: undefined });
211
215
  break;
212
216
  }
213
217
  case "getConsentState": {
214
- const result = await client.getConsentState(
218
+ const result = await client.preferences.getConsentState(
215
219
  data.entityType,
216
220
  data.entity,
217
221
  );
@@ -325,7 +329,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
325
329
  });
326
330
  }
327
331
  };
328
- const streamCloser = client.conversations.streamConsent(streamCallback);
332
+ const streamCloser = client.preferences.streamConsent(streamCallback);
329
333
  streamClosers.set(data.streamId, streamCloser);
330
334
  postMessage({
331
335
  id,
@@ -354,7 +358,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
354
358
  }
355
359
  };
356
360
  const streamCloser =
357
- client.conversations.streamPreferences(streamCallback);
361
+ client.preferences.streamPreferences(streamCallback);
358
362
  streamClosers.set(data.streamId, streamCloser);
359
363
  postMessage({
360
364
  id,