mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 23:10:20 +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[]
|
updatedBlocks: MessageBlock[]
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
// Always update blocks if provided
|
await db.transaction('rw', db.topics, db.message_blocks, async () => {
|
||||||
if (updatedBlocks.length > 0) {
|
// Always update blocks if provided
|
||||||
await db.message_blocks.bulkPut(updatedBlocks)
|
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.`)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// 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) {
|
} catch (error) {
|
||||||
console.error(`[updateExistingMsg] Failed to update message ${updatedMessage.id}:`, error)
|
console.error(`[updateExistingMsg] Failed to update message ${updatedMessage.id}:`, error)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user