代码健壮性

This commit is contained in:
bob
2024-12-31 00:01:29 +08:00
parent 8b6a2cb895
commit d57432e2d2
2 changed files with 7 additions and 7 deletions

View File

@@ -133,10 +133,10 @@ export const messageStore = defineStore('anyim-message', () => {
msgRecordsList.value[sessionId] = {}
}
// seq为undefined或者seq没有被缓存才能add这条消息
if (item.seq === undefined || !(item.seq in msgUniqueSeq.value)) {
// seq没有或者seq没有被缓存才能add这条消息
if (!item.seq || !(item.seq in msgUniqueSeq.value)) {
msgRecordsList.value[sessionId][item.msgId] = item
if (item.seq !== undefined) {
if (item.seq) {
// seq不为空则添加至缓存
msgUniqueSeq.value[item.seq] = item.msgId
}
@@ -155,7 +155,7 @@ export const messageStore = defineStore('anyim-message', () => {
*/
const removeMsgRecord = (sessionId, msgId) => {
const msg = msgRecordsList.value[sessionId][msgId]
if (msg === undefined) return
if (!msg) return
if (msg.seq in msgUniqueSeq.value) delete msgUniqueSeq.value[msg.seq]
if (msgId in msgRecordsList.value[sessionId]) delete msgRecordsList.value[sessionId][msgId]
}

View File

@@ -98,7 +98,7 @@ const msgIdSortArray = computed(() => {
// 缓存的消息列表是否为空注意和hasNoMoreMsg的区别前者为空可以再拉取
const noMsgRecords = computed(() => {
return msgIdSortArray.value.length === 0
return msgIdSortArray.value?.length === 0
})
// 当前session的第一条消息ID
const firstMsgId = computed(() => {
@@ -111,8 +111,8 @@ const firstMsgId = computed(() => {
// 当前session的最后一条消息ID
const lastMsgId = computed(() => {
if (!noMsgRecords.value) {
const len = msgIdSortArray.value.length
return msgIdSortArray.value[len - 1]
const len = msgIdSortArray.value?.length
return len ? msgIdSortArray.value[len - 1] : 0
} else {
return 0
}