mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-25 03:10:08 +08:00
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.
This commit is contained in:
parent
ca1a4e06a1
commit
3b68a311cc
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user