优化列表存储逻辑

pull/459/head
lyswhut 2021-02-21 16:19:08 +08:00
parent 6ffe61562b
commit b6107b7281
2 changed files with 31 additions and 23 deletions

View File

@ -26,12 +26,18 @@ mainHandle(ipcMainWindowNames.get_playlist, async(event, isIgnoredError = false)
} }
}) })
const handleSaveList = ({ defaultList, loveList, userList }) => {
if (!electronStore_list) return
let data = {}
if (defaultList != null) data.defaultList = defaultList
if (loveList != null) data.loveList = loveList
if (userList != null) data.userList = userList
electronStore_list.set(data)
}
mainOn(ipcMainWindowNames.save_playlist, (event, { type, data }) => { mainOn(ipcMainWindowNames.save_playlist, (event, { type, data }) => {
switch (type) { switch (type) {
case 'defaultList': case 'myList':
case 'loveList': handleSaveList(data)
case 'userList':
electronStore_list && electronStore_list.set(type, data)
break break
case 'downloadList': case 'downloadList':
electronStore_downloadList && electronStore_downloadList.set('list', data) electronStore_downloadList && electronStore_downloadList.set('list', data)

View File

@ -37,6 +37,20 @@ dnscache({
cachesize: 1000, cachesize: 1000,
}) })
const listThrottle = (fn, delay = 100) => {
let timer = null
let _data = {}
return function(data) {
Object.assign(_data, data)
if (timer) return
timer = setTimeout(() => {
timer = null
fn.call(this, _data)
_data = {}
}, delay)
}
}
export default { export default {
data() { data() {
return { return {
@ -70,24 +84,12 @@ export default {
}), }),
}, },
created() { created() {
this.saveDefaultList = throttle(n => { this.saveMyList = listThrottle(data => {
rendererSend(NAMES.mainWindow.save_playlist, { rendererSend(NAMES.mainWindow.save_playlist, {
type: 'defaultList', type: 'myList',
data: n, data,
}) })
}, 500) }, 300)
this.saveLoveList = throttle(n => {
rendererSend(NAMES.mainWindow.save_playlist, {
type: 'loveList',
data: n,
})
}, 500)
this.saveUserList = throttle(n => {
rendererSend(NAMES.mainWindow.save_playlist, {
type: 'userList',
data: n,
})
}, 500)
this.saveDownloadList = throttle(n => { this.saveDownloadList = throttle(n => {
rendererSend(NAMES.mainWindow.save_playlist, { rendererSend(NAMES.mainWindow.save_playlist, {
type: 'downloadList', type: 'downloadList',
@ -115,19 +117,19 @@ export default {
}, },
defaultList: { defaultList: {
handler(n) { handler(n) {
this.saveDefaultList(n) this.saveMyList({ defaultList: n })
}, },
deep: true, deep: true,
}, },
loveList: { loveList: {
handler(n) { handler(n) {
this.saveLoveList(n) this.saveMyList({ loveList: n })
}, },
deep: true, deep: true,
}, },
userList: { userList: {
handler(n) { handler(n) {
this.saveUserList(n) this.saveMyList({ userList: n })
}, },
deep: true, deep: true,
}, },