Skip to content

Commit

Permalink
Merge pull request #56 from xmtp/rygine/performance-update
Browse files Browse the repository at this point in the history
Greatly improve performance by removing mutexes
  • Loading branch information
rygine authored Aug 18, 2023
2 parents dd41eab + 83735a8 commit b509a6b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 176 deletions.
1 change: 0 additions & 1 deletion packages/react-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"@xmtp/content-type-remote-attachment": "^1.0.7",
"@xmtp/content-type-reply": "^1.0.0",
"@xmtp/xmtp-js": "^10.2.0",
"async-mutex": "^0.4.0",
"date-fns": "^2.30.0",
"dexie": "^3.2.4",
"dexie-react-hooks": "^1.1.6",
Expand Down
57 changes: 25 additions & 32 deletions packages/react-sdk/src/helpers/caching/conversations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Conversation, Client, InvitationContext } from "@xmtp/xmtp-js";
import type { Table } from "dexie";
import type Dexie from "dexie";
import { Mutex } from "async-mutex";
import type { CachedMetadata, CachedMetadataValues } from "./db";

export type CachedConversation<M = CachedMetadata> = {
Expand Down Expand Up @@ -93,8 +92,6 @@ export const getConversationByTopic = async (
return conversation;
};

const updateConversationMutex = new Mutex();

/**
* Update properties of a cached conversation
*/
Expand All @@ -104,19 +101,18 @@ export const updateConversation = async (
Pick<CachedConversation, "updatedAt" | "isReady" | "metadata">
>,
db: Dexie,
) =>
updateConversationMutex.runExclusive(async () => {
const conversationsTable = db.table(
"conversations",
) as CachedConversationsTable;
const existing = await conversationsTable
.where("topic")
.equals(topic)
.first();
if (existing) {
await conversationsTable.update(existing, update);
}
});
) => {
const conversationsTable = db.table(
"conversations",
) as CachedConversationsTable;
const existing = await conversationsTable
.where("topic")
.equals(topic)
.first();
if (existing) {
await conversationsTable.update(existing, update);
}
};

/**
* Update metadata of a cached conversation using the specified namespace
Expand Down Expand Up @@ -174,8 +170,6 @@ export const toCachedConversation = (
walletAddress,
});

const saveConversationMutex = new Mutex();

/**
* Save a conversation to the cache
*
Expand All @@ -184,21 +178,20 @@ const saveConversationMutex = new Mutex();
export const saveConversation = async (
conversation: CachedConversation,
db: Dexie,
) =>
saveConversationMutex.runExclusive(async () => {
const conversations = db.table("conversations") as CachedConversationsTable;
) => {
const conversations = db.table("conversations") as CachedConversationsTable;

const existing = await conversations
.where("topic")
.equals(conversation.topic)
.first();
const existing = await conversations
.where("topic")
.equals(conversation.topic)
.first();

if (existing) {
return existing as CachedConversationWithId;
}
if (existing) {
return existing as CachedConversationWithId;
}

// eslint-disable-next-line no-param-reassign
conversation.id = await conversations.add(conversation);
// eslint-disable-next-line no-param-reassign
conversation.id = await conversations.add(conversation);

return conversation as CachedConversationWithId;
});
return conversation as CachedConversationWithId;
};
Loading

0 comments on commit b509a6b

Please sign in to comment.