perf: 优化数据流消息

This commit is contained in:
kuaifan 2024-12-12 13:41:56 +08:00
parent a032c6114f
commit 5b02d8008f
4 changed files with 36 additions and 36 deletions

View File

@ -873,7 +873,6 @@ export default {
mounted() { mounted() {
this.subMsgListener() this.subMsgListener()
this.msgSubscribe = Store.subscribe('dialogMsgChange', this.onMsgChange);
}, },
beforeDestroy() { beforeDestroy() {
@ -885,10 +884,6 @@ export default {
this.$store.dispatch('closeDialog', this.dialogId) this.$store.dispatch('closeDialog', this.dialogId)
} }
// //
if (this.msgSubscribe) {
this.msgSubscribe.unsubscribe();
this.msgSubscribe = null;
}
this.observers.forEach(({observer}) => observer.disconnect()) this.observers.forEach(({observer}) => observer.disconnect())
this.observers = [] this.observers = []
// //
@ -910,6 +905,7 @@ export default {
'dialogMsgTransfer', 'dialogMsgTransfer',
'dialogMsgKeep', 'dialogMsgKeep',
'dialogIns', 'dialogIns',
'dialogMsgStream',
'cacheDialogs', 'cacheDialogs',
'wsOpenNum', 'wsOpenNum',
'touchBackInProgress', 'touchBackInProgress',
@ -1489,6 +1485,34 @@ export default {
} }
document.getSelection().removeAllRanges(); document.getSelection().removeAllRanges();
}, },
dialogMsgStream(data) {
const item = this.allMsgs.find(({type, id}) => type == "text" && id == data.id)
if (!item) {
return
}
if (typeof this.msgChangeCache[data.id] === "undefined") {
this.msgChangeCache[data.id] = []
this.msgChangeCache[`${data.id}_load`] = false
}
switch (data.type) {
case 'append':
data.text && this.msgChangeCache[data.id].push(...`${data.text}`.split("").map(text => {
return {
type: 'append',
text
}
}))
break;
case 'replace':
this.msgChangeCache[data.id] = [{
type: 'replace',
text: data.text
}]
break;
}
this.onMsgOutput(data.id, item.msg)
}
}, },
methods: { methods: {
@ -1863,34 +1887,6 @@ export default {
} }
}, },
/**
* 消息变化处理
* @param data
*/
onMsgChange(data) {
const item = this.allMsgs.find(({type, id}) => type == "text" && id == data.id)
if (item) {
if (typeof this.msgChangeCache[data.id] === "undefined") {
this.msgChangeCache[data.id] = []
this.msgChangeCache[`${data.id}_load`] = false
}
if (data.type === 'append') {
this.msgChangeCache[data.id].push(...`${data.text}`.split("").map(text => {
return {
type: 'append',
text
}
}))
} else if (data.type === 'replace') {
this.msgChangeCache[data.id] = [{
type: 'replace',
text: data.text
}]
}
this.onMsgOutput(data.id, item.msg)
}
},
/** /**
* 追加或替换消息 * 追加或替换消息
* @param id * @param id

View File

@ -3436,9 +3436,10 @@ export default {
* 消息流 * 消息流
* @param state * @param state
* @param dispatch * @param dispatch
* @param commit
* @param streamUrl * @param streamUrl
*/ */
streamDialogMsg({state, dispatch}, streamUrl) { streamDialogMsg({state, dispatch, commit}, streamUrl) {
if (!/^https?:\/\//i.test(streamUrl)) { if (!/^https?:\/\//i.test(streamUrl)) {
streamUrl = $A.mainUrl(streamUrl.substring(1)) streamUrl = $A.mainUrl(streamUrl.substring(1))
} }
@ -3451,7 +3452,7 @@ export default {
case 'append': case 'append':
case 'replace': case 'replace':
const data = $A.jsonParse(e.data); const data = $A.jsonParse(e.data);
Store.set('dialogMsgChange', { commit('setDialogMsgStream', {
type, type,
id: e.lastEventId, id: e.lastEventId,
text: data.content text: data.content

View File

@ -1,3 +1,5 @@
export default { export default {
setDialogMsgStream(state, data) {
state.dialogMsgStream = data;
}
} }

View File

@ -121,6 +121,7 @@ export default {
dialogHistory: [], dialogHistory: [],
dialogDraftTimer: {}, dialogDraftTimer: {},
dialogMsgTransfer: {time: 0}, dialogMsgTransfer: {time: 0},
dialogMsgStream: {},
dialogSseList: [], dialogSseList: [],
dialogDroupWordChain: {}, dialogDroupWordChain: {},
dialogGroupVote: {}, dialogGroupVote: {},