fix: 解决消息记录出现警告 Duplicate keys found during update 的问题
This commit is contained in:
@@ -10,11 +10,17 @@ class MqttManager {
|
|||||||
this.messages = new Map() // serverId -> message[]
|
this.messages = new Map() // serverId -> message[]
|
||||||
this.publishHistory = new Map() // serverId -> publishHistory[]
|
this.publishHistory = new Map() // serverId -> publishHistory[]
|
||||||
this.listeners = new Set() // IPC callback functions
|
this.listeners = new Set() // IPC callback functions
|
||||||
|
this.msgIdCounter = 0 // 用于生成唯一消息 id
|
||||||
|
|
||||||
this.dataPath = join(app.getPath('userData'), 'frost-mqtt-data.json')
|
this.dataPath = join(app.getPath('userData'), 'frost-mqtt-data.json')
|
||||||
this.loadData()
|
this.loadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nextMsgId() {
|
||||||
|
this.msgIdCounter = (this.msgIdCounter + 1) % Number.MAX_SAFE_INTEGER
|
||||||
|
return 'msg_' + Date.now() + '_' + this.msgIdCounter
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== 数据持久化 ====================
|
// ==================== 数据持久化 ====================
|
||||||
getDataDir() {
|
getDataDir() {
|
||||||
const dir = app.getPath('userData')
|
const dir = app.getPath('userData')
|
||||||
@@ -269,9 +275,9 @@ class MqttManager {
|
|||||||
qos: packet.qos,
|
qos: packet.qos,
|
||||||
retain: packet.retain,
|
retain: packet.retain,
|
||||||
direction: 'sub',
|
direction: 'sub',
|
||||||
id: 'msg_' + Date.now()
|
id: this.nextMsgId()
|
||||||
}
|
}
|
||||||
const msgs = this.messages.get(id) || []
|
const msgs = this.messages.get(id) || []
|
||||||
msgs.unshift(msg)
|
msgs.unshift(msg)
|
||||||
if (msgs.length > 500) msgs.pop()
|
if (msgs.length > 500) msgs.pop()
|
||||||
this.messages.set(id, msgs)
|
this.messages.set(id, msgs)
|
||||||
@@ -479,10 +485,11 @@ class MqttManager {
|
|||||||
this.publishHistory.set(serverId, history)
|
this.publishHistory.set(serverId, history)
|
||||||
|
|
||||||
// 同时添加到消息日志
|
// 同时添加到消息日志
|
||||||
|
const pubMsgId = this.nextMsgId()
|
||||||
const msgs = this.messages.get(serverId) || []
|
const msgs = this.messages.get(serverId) || []
|
||||||
msgs.unshift({
|
msgs.unshift({
|
||||||
...pubEntry,
|
...pubEntry,
|
||||||
id: 'msg_' + Date.now()
|
id: pubMsgId
|
||||||
})
|
})
|
||||||
if (msgs.length > 500) msgs.pop()
|
if (msgs.length > 500) msgs.pop()
|
||||||
this.messages.set(serverId, msgs)
|
this.messages.set(serverId, msgs)
|
||||||
@@ -491,7 +498,7 @@ class MqttManager {
|
|||||||
serverId,
|
serverId,
|
||||||
message: {
|
message: {
|
||||||
...pubEntry,
|
...pubEntry,
|
||||||
id: 'msg_' + Date.now()
|
id: pubMsgId
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user