From 5158183260e4585d8dcc89445f1fbf5d1a75c4df Mon Sep 17 00:00:00 2001 From: Kirat Date: Sat, 8 Apr 2023 00:39:05 +0530 Subject: [PATCH] Fixes #3633 (#3643) --- packages/db/src/api/friendships.ts | 22 +++++++++++++--------- packages/db/src/db/chats.ts | 4 ++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/db/src/api/friendships.ts b/packages/db/src/api/friendships.ts index a80254f5b0..944893d753 100644 --- a/packages/db/src/api/friendships.ts +++ b/packages/db/src/api/friendships.ts @@ -23,15 +23,17 @@ export const refreshFriendships = async (uuid: string, jwt?: string) => { return; } const existingChats = await db.inbox.toArray(); - existingChats.forEach((existingChat) => { + for (const existingChat of existingChats) { if (!chats.find((x) => x.remoteUserId === existingChat.remoteUserId)) { - db.inbox.delete(existingChat.remoteUserId); + await db.inbox.delete(existingChat.remoteUserId); } - }); + } if (chats) { - chats?.forEach((chat) => { - db.inbox.put(chat); - }); + await Promise.all( + chats?.map(async (chat) => { + await db.inbox.put(chat); + }) || [] + ); } } catch (e) { console.error(e); @@ -49,9 +51,11 @@ export const refreshGroups = async (uuid: string, jwt?: string) => { const res = await response.json(); const collections: CollectionChatData[] = res.collections; - collections?.forEach((collection) => { - createOrUpdateCollection(uuid, collection); - }); + await Promise.all( + collections?.map(async (collection) => { + await createOrUpdateCollection(uuid, collection); + }) || [] + ); } catch (e) { console.error(e); } diff --git a/packages/db/src/db/chats.ts b/packages/db/src/db/chats.ts index 381f1f7607..f662456e19 100644 --- a/packages/db/src/db/chats.ts +++ b/packages/db/src/db/chats.ts @@ -108,8 +108,8 @@ export const createOrUpdateCollection = async ( ) => { const db = getDb(uuid); if (await db.collections.get(data.collectionId)) { - db.collections.update(data.collectionId, data); + return db.collections.update(data.collectionId, data); } else { - db.collections.put(data); + return db.collections.put(data); } };