Skip to content

Commit

Permalink
Refactor some methods to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Dec 18, 2024
1 parent d4dfeac commit ede9473
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
6 changes: 3 additions & 3 deletions sdks/browser-sdk/src/WorkerConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class WorkerConversation {
return this.#group.createdAtNs();
}

get metadata() {
const metadata = this.#group.groupMetadata();
async metadata() {
const metadata = await this.#group.groupMetadata();
return {
creatorInboxId: metadata.creatorInboxId(),
conversationType: metadata.conversationType(),
Expand Down Expand Up @@ -155,7 +155,7 @@ export class WorkerConversation {
return this.#group.send(encodedContent);
}

messages(options?: SafeListMessagesOptions) {
async messages(options?: SafeListMessagesOptions) {
return this.#group.findMessages(
options ? fromSafeListMessagesOptions(options) : undefined,
);
Expand Down
6 changes: 3 additions & 3 deletions sdks/browser-sdk/src/utils/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ export type SafeConversation = {
createdAtNs: bigint;
};

export const toSafeConversation = (
export const toSafeConversation = async (
conversation: WorkerConversation,
): SafeConversation => ({
): Promise<SafeConversation> => ({
id: conversation.id,
name: conversation.name,
imageUrl: conversation.imageUrl,
Expand All @@ -332,7 +332,7 @@ export const toSafeConversation = (
},
isActive: conversation.isActive,
addedByInboxId: conversation.addedByInboxId,
metadata: conversation.metadata,
metadata: await conversation.metadata(),
admins: conversation.admins,
superAdmins: conversation.superAdmins,
createdAtNs: conversation.createdAtNs,
Expand Down
34 changes: 22 additions & 12 deletions sdks/browser-sdk/src/workers/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: conversations.map((conversation) =>
toSafeConversation(conversation),
result: await Promise.all(
conversations.map((conversation) =>
toSafeConversation(conversation),
),
),
});
break;
Expand All @@ -272,8 +274,10 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: conversations.map((conversation) =>
toSafeConversation(conversation),
result: await Promise.all(
conversations.map((conversation) =>
toSafeConversation(conversation),
),
),
});
break;
Expand All @@ -283,8 +287,10 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: conversations.map((conversation) =>
toSafeConversation(conversation),
result: await Promise.all(
conversations.map((conversation) =>
toSafeConversation(conversation),
),
),
});
break;
Expand All @@ -302,7 +308,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: toSafeConversation(conversation),
result: await toSafeConversation(conversation),
});
break;
}
Expand All @@ -313,7 +319,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: toSafeConversation(conversation),
result: await toSafeConversation(conversation),
});
break;
}
Expand All @@ -340,7 +346,9 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: conversation ? toSafeConversation(conversation) : undefined,
result: conversation
? await toSafeConversation(conversation)
: undefined,
});
break;
}
Expand All @@ -358,7 +366,9 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: conversation ? toSafeConversation(conversation) : undefined,
result: conversation
? await toSafeConversation(conversation)
: undefined,
});
break;
}
Expand All @@ -372,7 +382,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
postMessage({
id,
action,
result: toSafeConversation(group),
result: await toSafeConversation(group),
});
} else {
postMessageError({
Expand Down Expand Up @@ -516,7 +526,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
case "getGroupMessages": {
const group = client.conversations.getConversationById(data.id);
if (group) {
const messages = group.messages(data.options);
const messages = await group.messages(data.options);
postMessage({
id,
action,
Expand Down
10 changes: 5 additions & 5 deletions sdks/node-sdk/src/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export class Conversation {
return nsToDate(this.createdAtNs);
}

get metadata() {
const metadata = this.#group.groupMetadata();
async metadata() {
const metadata = await this.#group.groupMetadata();
return {
creatorInboxId: metadata.creatorInboxId(),
conversationType: metadata.conversationType(),
Expand Down Expand Up @@ -194,10 +194,10 @@ export class Conversation {
return this.#group.send(encodedContent);
}

messages(options?: ListMessagesOptions): DecodedMessage[] {
async messages(options?: ListMessagesOptions): Promise<DecodedMessage[]> {
const messages = await this.#group.findMessages(options);
return (
this.#group
.findMessages(options)
messages
.map((message) => new DecodedMessage(this.#client, message))
// filter out messages without content
.filter((message) => message.content !== undefined)
Expand Down
30 changes: 17 additions & 13 deletions sdks/node-sdk/test/Conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Conversation", () => {
const newName = "foo";
await conversation.updateName(newName);
expect(conversation.name).toBe(newName);
const messages = conversation.messages();
const messages = await conversation.messages();
expect(messages.length).toBe(2);

await client2.conversations.sync();
Expand All @@ -31,7 +31,8 @@ describe("Conversation", () => {
await conversation2.sync();
expect(conversation2.id).toBe(conversation.id);
expect(conversation2.name).toBe(newName);
expect(conversation2.messages().length).toBe(1);
const messages2 = await conversation2.messages();
expect(messages2.length).toBe(1);
});

it("should update conversation image URL", async () => {
Expand All @@ -45,7 +46,7 @@ describe("Conversation", () => {
const imageUrl = "https://foo/bar.jpg";
await conversation.updateImageUrl(imageUrl);
expect(conversation.imageUrl).toBe(imageUrl);
const messages = conversation.messages();
const messages = await conversation.messages();
expect(messages.length).toBe(2);

await client2.conversations.sync();
Expand All @@ -57,7 +58,8 @@ describe("Conversation", () => {
await conversation2.sync();
expect(conversation2.id).toBe(conversation.id);
expect(conversation2.imageUrl).toBe(imageUrl);
expect(conversation2.messages().length).toBe(1);
const messages2 = await conversation2.messages();
expect(messages2.length).toBe(1);
});

it("should update conversation description", async () => {
Expand All @@ -71,7 +73,7 @@ describe("Conversation", () => {
const newDescription = "foo";
await conversation.updateDescription(newDescription);
expect(conversation.description).toBe(newDescription);
const messages = conversation.messages();
const messages = await conversation.messages();
expect(messages.length).toBe(2);

await client2.conversations.sync();
Expand All @@ -83,7 +85,8 @@ describe("Conversation", () => {
await conversation2.sync();
expect(conversation2.id).toBe(conversation.id);
expect(conversation2.description).toBe(newDescription);
expect(conversation2.messages().length).toBe(1);
const messages2 = await conversation2.messages();
expect(messages2.length).toBe(1);
});

it("should update conversation pinned frame URL", async () => {
Expand All @@ -97,7 +100,7 @@ describe("Conversation", () => {
const pinnedFrameUrl = "https://foo/bar";
await conversation.updatePinnedFrameUrl(pinnedFrameUrl);
expect(conversation.pinnedFrameUrl).toBe(pinnedFrameUrl);
const messages = conversation.messages();
const messages = await conversation.messages();
expect(messages.length).toBe(2);

await client2.conversations.sync();
Expand All @@ -109,7 +112,8 @@ describe("Conversation", () => {
await conversation2.sync();
expect(conversation2.id).toBe(conversation.id);
expect(conversation2.pinnedFrameUrl).toBe(pinnedFrameUrl);
expect(conversation2.messages().length).toBe(1);
const messages2 = await conversation2.messages();
expect(messages2.length).toBe(1);
});

it("should add and remove members", async () => {
Expand Down Expand Up @@ -201,7 +205,7 @@ describe("Conversation", () => {
const text = "gm";
await conversation.send(text);

const messages = conversation.messages();
const messages = await conversation.messages();
expect(messages.length).toBe(2);
expect(messages[1].content).toBe(text);

Expand All @@ -214,7 +218,7 @@ describe("Conversation", () => {
await conversation2.sync();
expect(conversation2.id).toBe(conversation.id);

const messages2 = conversation2.messages();
const messages2 = await conversation2.messages();
expect(messages2.length).toBe(1);
expect(messages2[0].content).toBe(text);
});
Expand Down Expand Up @@ -249,7 +253,7 @@ describe("Conversation", () => {
const text = "gm";
conversation.sendOptimistic(text);

const messages = conversation.messages();
const messages = await conversation.messages();
expect(messages.length).toBe(2);
expect(messages[1].content).toBe(text);

Expand All @@ -263,13 +267,13 @@ describe("Conversation", () => {
await conversation2.sync();
expect(conversation2.id).toBe(conversation.id);

const messages2 = conversation2.messages();
const messages2 = await conversation2.messages();
expect(messages2.length).toBe(0);

await conversation.publishMessages();
await conversation2.sync();

const messages4 = conversation2.messages();
const messages4 = await conversation2.messages();
expect(messages4.length).toBe(1);
expect(messages4[0].content).toBe(text);
});
Expand Down
10 changes: 5 additions & 5 deletions sdks/node-sdk/test/Conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ describe("Conversations", () => {
updateGroupPinnedFrameUrlPolicy: 0,
});
expect(conversation.addedByInboxId).toBe(client1.inboxId);
expect(conversation.messages().length).toBe(1);
expect((await conversation.messages()).length).toBe(1);

const members = await conversation.members();
expect(members.length).toBe(2);
const memberInboxIds = members.map((member) => member.inboxId);
expect(memberInboxIds).toContain(client1.inboxId);
expect(memberInboxIds).toContain(client2.inboxId);
expect(conversation.metadata).toEqual({
expect(await conversation.metadata()).toEqual({
conversationType: "group",
creatorInboxId: client1.inboxId,
});
Expand Down Expand Up @@ -97,14 +97,14 @@ describe("Conversations", () => {
updateGroupPinnedFrameUrlPolicy: 0,
});
expect(group.addedByInboxId).toBe(client1.inboxId);
expect(group.messages().length).toBe(0);
expect((await group.messages()).length).toBe(0);
const members = await group.members();
expect(members.length).toBe(2);
const memberInboxIds = members.map((member) => member.inboxId);
expect(memberInboxIds).toContain(client1.inboxId);
expect(memberInboxIds).toContain(client2.inboxId);
expect(group.metadata.conversationType).toBe("dm");
expect(group.metadata.creatorInboxId).toBe(client1.inboxId);
expect((await group.metadata()).conversationType).toBe("dm");
expect((await group.metadata()).creatorInboxId).toBe(client1.inboxId);

expect(group.consentState).toBe(ConsentState.Allowed);

Expand Down

0 comments on commit ede9473

Please sign in to comment.