perf: 优化缓存

This commit is contained in:
kuaifan 2022-12-10 21:21:42 +08:00
parent 0077925fc0
commit 3dc8269406
3 changed files with 29 additions and 22 deletions

View File

@ -1222,16 +1222,14 @@ const localforage = require("localforage");
$.extend({ $.extend({
__IDBTimer: {}, __IDBTimer: {},
IDBSave(key, value, delay = 0) { IDBSave(key, value, delay = 100) {
if (typeof this.__IDBTimer[key] !== "undefined") { if (typeof this.__IDBTimer[key] !== "undefined") {
clearTimeout(this.__IDBTimer[key]) clearTimeout(this.__IDBTimer[key])
delete this.__IDBTimer[key] delete this.__IDBTimer[key]
} }
if (delay > 0) { this.__IDBTimer[key] = setTimeout(async _ => {
this.__IDBTimer[key] = setTimeout(_ => this.IDBSave(key, value, 0), delay) await localforage.setItem(key, value)
} else { }, delay)
localforage.setItem(key, value).then(_ => {})
}
}, },
IDBDel(key) { IDBDel(key) {

View File

@ -387,6 +387,10 @@ export default {
return this.dialogMsgs.find(item => item.id === replyId) return this.dialogMsgs.find(item => item.id === replyId)
} }
return null; return null;
},
cacheKey() {
return this.dialogId || `t_${this.taskId}`
} }
}, },
watch: { watch: {
@ -400,7 +404,10 @@ export default {
this.quill.setText('') this.quill.setText('')
} }
} }
this.setInputCache(val) this.$store.dispatch("saveDialogInputCache", {
key: this.cacheKey,
cache: val
})
}, },
// Watch disabled change // Watch disabled change
@ -777,24 +784,10 @@ export default {
}, },
getInputCache() { getInputCache() {
const key = this.dialogId || `t_${this.taskId}`; const item = this.dialogInputCache.find(item => item.key == this.cacheKey);
const item = this.dialogInputCache.find(item => item.key == key);
return item ? item.cache : ''; return item ? item.cache : '';
}, },
setInputCache(cache) {
const key = this.dialogId || `t_${this.taskId}`;
const index = this.dialogInputCache.findIndex(item => item.key == key);
const data = {key, cache}
if (index > -1) {
this.$store.state.dialogInputCache.splice(index, 1, data)
} else {
this.$store.state.dialogInputCache.push(data)
}
//
$A.IDBSave("dialogInputCache", this.$store.state.dialogInputCache, 600);
},
onClickEditor() { onClickEditor() {
this.updateEmojiQuick(this.value) this.updateEmojiQuick(this.value)
}, },

View File

@ -2346,6 +2346,22 @@ export default {
} }
}, },
/**
* 保存聊天草稿
* @param state
* @param data {key, cache}
*/
saveDialogInputCache({state}, data) {
const index = state.dialogInputCache.findIndex(item => item.key == data.key);
if (index > -1) {
state.dialogInputCache.splice(index, 1, data)
} else {
state.dialogInputCache.push(data)
}
//
$A.IDBSave("dialogInputCache", state.dialogInputCache, 600);
},
/** *****************************************************************************************/ /** *****************************************************************************************/
/** ************************************** 消息 **********************************************/ /** ************************************** 消息 **********************************************/
/** *****************************************************************************************/ /** *****************************************************************************************/