优化大列表增删操作性能
This commit is contained in:
@@ -13,7 +13,7 @@ import { getMusicUrl, getPicUrl, getLyricInfo } from '@renderer/core/music/onlin
|
||||
import { appSetting } from '../setting'
|
||||
import { qualityList } from '..'
|
||||
import { proxyCallback } from '@renderer/worker/utils'
|
||||
import { joinPath } from '@renderer/utils'
|
||||
import { arrPush, arrUnshift, joinPath } from '@renderer/utils'
|
||||
import { DOWNLOAD_STATUS } from '@common/constants'
|
||||
|
||||
const waitingUpdateTasks = new Map<string, LX.Download.ListItem>()
|
||||
@@ -48,7 +48,7 @@ export const getDownloadList = async(): Promise<LX.Download.ListItem[]> => {
|
||||
break
|
||||
}
|
||||
}
|
||||
downloadList.push(...list)
|
||||
arrPush(downloadList, list)
|
||||
}
|
||||
return downloadList
|
||||
}
|
||||
@@ -59,9 +59,9 @@ const addTasks = async(list: LX.Download.ListItem[]) => {
|
||||
await downloadTasksCreate(list.map(i => toRaw(i)), addMusicLocationType)
|
||||
|
||||
if (addMusicLocationType === 'top') {
|
||||
downloadList.unshift(...list)
|
||||
arrUnshift(downloadList, list)
|
||||
} else {
|
||||
downloadList.push(...list)
|
||||
arrPush(downloadList, list)
|
||||
}
|
||||
window.app_event.downloadListUpdate()
|
||||
}
|
||||
@@ -367,18 +367,21 @@ export const pauseDownloadTasks = async(list: LX.Download.ListItem[]) => {
|
||||
*/
|
||||
export const removeDownloadTasks = async(ids: string[]) => {
|
||||
await downloadTasksRemove(ids)
|
||||
ids = [...ids]
|
||||
for (let i = downloadList.length; i--;) {
|
||||
const item = downloadList[i]
|
||||
const index = ids.indexOf(item.id)
|
||||
if (index < 0) continue
|
||||
ids.splice(index, 1)
|
||||
downloadList.splice(i, 1)
|
||||
if (runingTask.has(item.id)) {
|
||||
void window.lx.worker.download.removeTask(item.id)
|
||||
runingTask.delete(item.id)
|
||||
|
||||
const listSet = new Set<string>()
|
||||
for (const item of downloadList) listSet.add(item.id)
|
||||
for (const id of ids) listSet.delete(id)
|
||||
const newList = downloadList.filter(task => {
|
||||
if (runingTask.has(task.id)) {
|
||||
void window.lx.worker.download.removeTask(task.id)
|
||||
runingTask.delete(task.id)
|
||||
}
|
||||
}
|
||||
return listSet.has(task.id)
|
||||
})
|
||||
downloadList.splice(0, downloadList.length)
|
||||
arrPush(downloadList, newList)
|
||||
|
||||
|
||||
void checkStartTask()
|
||||
window.app_event.downloadListUpdate()
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from './state'
|
||||
import { overwriteListPosition, overwriteListUpdateInfo, removeListPosition, removeListUpdateInfo } from '@renderer/utils/data'
|
||||
import { LIST_IDS } from '@common/constants'
|
||||
import { arrPush, arrUnshift } from '@common/utils/common'
|
||||
|
||||
export const setUserLists = (lists: LX.List.UserListInfo[]) => {
|
||||
userLists.splice(0, userLists.length, ...lists)
|
||||
@@ -25,7 +26,8 @@ const overwriteMusicList = (id: string, list: LX.Music.MusicInfo[]) => {
|
||||
markRawList(list)
|
||||
let targetList = allMusicList.get(id)
|
||||
if (targetList) {
|
||||
targetList.splice(0, targetList.length, ...list)
|
||||
targetList.splice(0, targetList.length)
|
||||
arrPush(targetList, list)
|
||||
} else {
|
||||
allMusicList.set(id, shallowReactive(list))
|
||||
}
|
||||
@@ -179,16 +181,18 @@ export const userListsUpdatePosition = (position: number, ids: string[]) => {
|
||||
|
||||
const updateLists: LX.List.UserListInfo[] = []
|
||||
|
||||
for (let i = newUserLists.length; i--;) {
|
||||
if (ids.includes(newUserLists[i].id)) {
|
||||
const list = newUserLists.splice(i, 1)[0]
|
||||
list.locationUpdateTime = Date.now()
|
||||
updateLists.push(list)
|
||||
}
|
||||
// const targetItem = list[position]
|
||||
const map = new Map<string, LX.List.UserListInfo>()
|
||||
for (const item of newUserLists) map.set(item.id, item)
|
||||
for (const id of ids) {
|
||||
const listInfo = map.get(id) as LX.List.UserListInfo
|
||||
listInfo.locationUpdateTime = Date.now()
|
||||
updateLists.push(listInfo)
|
||||
map.delete(id)
|
||||
}
|
||||
position = Math.min(newUserLists.length, position)
|
||||
newUserLists.splice(0, newUserLists.length, ...newUserLists.filter(mInfo => map.has(mInfo.id)))
|
||||
newUserLists.splice(Math.min(position, newUserLists.length), 0, ...updateLists)
|
||||
|
||||
newUserLists.splice(position, 0, ...updateLists)
|
||||
setUserLists(newUserLists)
|
||||
}
|
||||
|
||||
@@ -212,11 +216,11 @@ export const listMusicAdd = (id: string, musicInfos: LX.Music.MusicInfo[], addMu
|
||||
})
|
||||
switch (addMusicLocationType) {
|
||||
case 'top':
|
||||
targetList.unshift(...musicInfos)
|
||||
arrUnshift(targetList, musicInfos)
|
||||
break
|
||||
case 'bottom':
|
||||
default:
|
||||
targetList.push(...musicInfos)
|
||||
arrPush(targetList, musicInfos)
|
||||
break
|
||||
}
|
||||
|
||||
@@ -234,14 +238,13 @@ export const listMusicRemove = (listId: string, ids: string[]): string[] => {
|
||||
let targetList = allMusicList.get(listId)
|
||||
if (!targetList) return listId == loveList.id ? [listId] : []
|
||||
|
||||
ids = [...ids]
|
||||
for (let i = targetList.length - 1; i > -1; i--) {
|
||||
const item = targetList[i]
|
||||
const index = ids.indexOf(item.id)
|
||||
if (index < 0) continue
|
||||
ids.splice(index, 1)
|
||||
targetList.splice(i, 1)
|
||||
}
|
||||
const listSet = new Set<string>()
|
||||
for (const item of targetList) listSet.add(item.id)
|
||||
for (const id of ids) listSet.delete(id)
|
||||
const newList = targetList.filter(mInfo => listSet.has(mInfo.id))
|
||||
targetList.splice(0, targetList.length)
|
||||
arrPush(targetList, newList)
|
||||
|
||||
return [listId]
|
||||
}
|
||||
|
||||
@@ -284,7 +287,9 @@ export const listMusicUpdatePosition = async(listId: string, position: number, i
|
||||
|
||||
const list = await window.lx.worker.main.createSortedList(toRaw(targetList), position, ids)
|
||||
markRawList(list)
|
||||
targetList.splice(0, targetList.length, ...list)
|
||||
targetList.splice(0, targetList.length)
|
||||
arrPush(targetList, list)
|
||||
|
||||
// console.timeEnd('ts')
|
||||
return [listId]
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import { setProgress } from './playProgress'
|
||||
import { playNext } from '@renderer/core/player'
|
||||
import { LIST_IDS } from '@common/constants'
|
||||
import { toRaw } from '@common/utils/vueTools'
|
||||
import { arrPush, arrUnshift } from '@common/utils/common'
|
||||
|
||||
|
||||
type PlayerMusicInfoKeys = keyof typeof musicInfo
|
||||
@@ -231,8 +232,8 @@ export const addTempPlayList = (list: LX.Player.TempPlayListItem[]) => {
|
||||
}
|
||||
return true
|
||||
})
|
||||
if (topList.length) tempPlayList.unshift(...topList.map(({ musicInfo, listId }) => ({ musicInfo, listId, isTempPlay: true })))
|
||||
if (bottomList.length) tempPlayList.push(...bottomList.map(({ musicInfo, listId }) => ({ musicInfo, listId, isTempPlay: true })))
|
||||
if (topList.length) arrUnshift(tempPlayList, topList.map(({ musicInfo, listId }) => ({ musicInfo, listId, isTempPlay: true })))
|
||||
if (bottomList.length) arrPush(tempPlayList, bottomList.map(({ musicInfo, listId }) => ({ musicInfo, listId, isTempPlay: true })))
|
||||
|
||||
if (!playMusicInfo.musicInfo) void playNext()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user