fix: 解决消息记录出现警告 Duplicate keys found during update 的问题

This commit is contained in:
2026-07-18 21:57:56 +08:00
parent 7ebb384f01
commit 9c2a469e4d

View File

@@ -10,11 +10,17 @@ class MqttManager {
this.messages = new Map() // serverId -> message[]
this.publishHistory = new Map() // serverId -> publishHistory[]
this.listeners = new Set() // IPC callback functions
this.msgIdCounter = 0 // 用于生成唯一消息 id
this.dataPath = join(app.getPath('userData'), 'frost-mqtt-data.json')
this.loadData()
}
nextMsgId() {
this.msgIdCounter = (this.msgIdCounter + 1) % Number.MAX_SAFE_INTEGER
return 'msg_' + Date.now() + '_' + this.msgIdCounter
}
// ==================== 数据持久化 ====================
getDataDir() {
const dir = app.getPath('userData')
@@ -269,9 +275,9 @@ class MqttManager {
qos: packet.qos,
retain: packet.retain,
direction: 'sub',
id: 'msg_' + Date.now()
}
const msgs = this.messages.get(id) || []
id: this.nextMsgId()
}
const msgs = this.messages.get(id) || []
msgs.unshift(msg)
if (msgs.length > 500) msgs.pop()
this.messages.set(id, msgs)
@@ -479,10 +485,11 @@ class MqttManager {
this.publishHistory.set(serverId, history)
// 同时添加到消息日志
const pubMsgId = this.nextMsgId()
const msgs = this.messages.get(serverId) || []
msgs.unshift({
...pubEntry,
id: 'msg_' + Date.now()
id: pubMsgId
})
if (msgs.length > 500) msgs.pop()
this.messages.set(serverId, msgs)
@@ -491,7 +498,7 @@ class MqttManager {
serverId,
message: {
...pubEntry,
id: 'msg_' + Date.now()
id: pubMsgId
}
})