From 3b68a311cc8d320417415449afebf8549e80a9db Mon Sep 17 00:00:00 2001 From: MyPrototypeWhat Date: Mon, 19 May 2025 12:28:16 +0800 Subject: [PATCH] refactor(messageThunk): optimize message update logic with atomic modifications * Wrapped message and block updates in a database transaction for improved consistency. * Replaced direct updates with atomic modifications using where().modify() for better performance and clarity. * Enhanced error handling for message updates to ensure robustness. --- src/renderer/src/store/thunk/messageThunk.ts | 53 ++++++++++---------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 78f2876212..0b65edd138 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -74,33 +74,34 @@ const updateExistingMessageAndBlocksInDB = async ( updatedBlocks: MessageBlock[] ) => { try { - // Always update blocks if provided - if (updatedBlocks.length > 0) { - await db.message_blocks.bulkPut(updatedBlocks) - } - - // Check if there are message properties to update beyond id and topicId - const messageKeysToUpdate = Object.keys(updatedMessage).filter((key) => key !== 'id' && key !== 'topicId') - - // Only proceed with topic update if there are actual message changes - if (messageKeysToUpdate.length > 0) { - const topic = await db.topics.get(updatedMessage.topicId) - if (topic) { - const messageIndex = topic.messages.findIndex((m) => m.id === updatedMessage.id) - if (messageIndex !== -1) { - const newMessages = [...topic.messages] - // Apply the updates passed in updatedMessage - Object.assign(newMessages[messageIndex], updatedMessage) - // Logger.log('updateExistingMessageAndBlocksInDB', updatedMessage) - await db.topics.update(updatedMessage.topicId, { messages: newMessages }) - } else { - console.error(`[updateExistingMsg] Message ${updatedMessage.id} not found in topic ${updatedMessage.topicId}`) - } - } else { - console.error(`[updateExistingMsg] Topic ${updatedMessage.topicId} not found.`) + await db.transaction('rw', db.topics, db.message_blocks, async () => { + // Always update blocks if provided + if (updatedBlocks.length > 0) { + await db.message_blocks.bulkPut(updatedBlocks) } - } - // If messageKeysToUpdate.length === 0, we skip topic fetch/update entirely + + // Check if there are message properties to update beyond id and topicId + const messageKeysToUpdate = Object.keys(updatedMessage).filter((key) => key !== 'id' && key !== 'topicId') + + // Only proceed with topic update if there are actual message changes + if (messageKeysToUpdate.length > 0) { + // 使用 where().modify() 进行原子更新 + await db.topics + .where('id') + .equals(updatedMessage.topicId) + .modify((topic) => { + if (!topic) return + + const messageIndex = topic.messages.findIndex((m) => m.id === updatedMessage.id) + if (messageIndex !== -1) { + // 直接在原对象上更新需要修改的属性 + messageKeysToUpdate.forEach((key) => { + topic.messages[messageIndex][key] = updatedMessage[key] + }) + } + }) + } + }) } catch (error) { console.error(`[updateExistingMsg] Failed to update message ${updatedMessage.id}:`, error) }