From 128bf57171404ba4e75f587bc4cfdeab5275528a Mon Sep 17 00:00:00 2001 From: vcapp Date: Tue, 14 May 2024 23:55:21 +0200 Subject: [PATCH] Fix Incorrect Method Call and Potential Race Condition in src/db/index.ts - Corrected deleteChatHistory method call to include id parameter. - Addressed potential race condition in addMessage method to handle simultaneous message additions. --- src/db/index.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/db/index.ts b/src/db/index.ts index f709905..088038a 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -89,7 +89,7 @@ export class PageAssitDatabase { const history_id = message.history_id const chatHistory = await this.getChatHistory(history_id) const newChatHistory = [message, ...chatHistory] - this.db.set({ [history_id]: newChatHistory }) + await this.db.set({ [history_id]: newChatHistory }) } async removeChatHistory(id: string) { @@ -112,12 +112,13 @@ export class PageAssitDatabase { this.db.clear() } - async deleteChatHistory() { + async deleteChatHistory(id: string) { const chatHistories = await this.getChatHistories() - for (const history of chatHistories) { - this.db.remove(history.id) - } - this.db.remove("chatHistories") + const newChatHistories = chatHistories.filter( + (history) => history.id !== id + ) + this.db.set({ chatHistories: newChatHistories }) + this.db.remove(id) } async deleteMessage(history_id: string) {