修复潜在同步问题

pull/733/head
lyswhut 2021-10-30 10:06:49 +08:00
parent faff0edb4a
commit 58f016ca00
3 changed files with 35 additions and 27 deletions

View File

@ -153,8 +153,6 @@ exports.initSetting = isShowErrorAlert => {
// 迁移列表滚动位置设置 ~0.18.3
if (setting.list.scroll) {
let scroll = setting.list.scroll
// electronStore_list.set('defaultList.location', scroll.locations.default || 0)
// electronStore_list.set('loveList.location', scroll.locations.love || 0)
electronStore_config.delete('setting.list.scroll')
electronStore_config.set('setting.list.isSaveScrollLocation', scroll.enable)
delete setting.list.scroll

View File

@ -11,6 +11,22 @@ let io
let syncingId = null
const wait = (time = 1000) => new Promise((resolve, reject) => setTimeout(resolve, time))
const patchListData = listData => {
return Object.assign({}, {
defaultList: {
id: 'default',
name: '试听列表',
list: [],
},
loveList: {
id: 'love',
name: '我的收藏',
list: [],
},
userList: [],
}, listData)
}
const getRemoteListData = socket => new Promise((resolve, reject) => {
console.log('getRemoteListData')
const handleError = reason => {
@ -23,7 +39,7 @@ const getRemoteListData = socket => new Promise((resolve, reject) => {
const data = JSON.parse(decryptMsg(socket.data.keyInfo, enData))
if (!data) return reject(new Error('Get remote list data failed'))
if (data.action != 'getData') return
resolve(data.data)
resolve(patchListData(data.data))
}
socket.on('disconnect', handleError)
@ -35,7 +51,7 @@ const getLocalListData = () => new Promise((resolve, reject) => {
const handleSuccess = ({ action, data }) => {
if (action !== 'getData') return
global.lx_event.sync.off(SYNC_EVENT_NAMES.sync_handle_list, handleSuccess)
resolve(data)
resolve(patchListData(data))
}
global.lx_event.sync.on(SYNC_EVENT_NAMES.sync_handle_list, handleSuccess)
global.lx_event.sync.sync_list({
@ -87,10 +103,10 @@ const updateSnapshot = (path, data) => {
}
const createListDataObj = listData => {
const listDataObj = {}
for (const list of listData.userList) listDataObj[list.id] = list
return listDataObj
const createUserListDataObj = listData => {
const userListDataObj = {}
for (const list of listData.userList) userListDataObj[list.id] = list
return userListDataObj
}
const handleMergeList = (sourceList, targetList, addMusicLocationType) => {
@ -137,11 +153,11 @@ const mergeList = (sourceListData, targetListData) => {
newListData.defaultList = handleMergeList(sourceListData.defaultList, targetListData.defaultList, addMusicLocationType)
newListData.loveList = handleMergeList(sourceListData.loveList, targetListData.loveList, addMusicLocationType)
const listDataObj = createListDataObj(sourceListData)
const userListDataObj = createUserListDataObj(sourceListData)
newListData.userList = [...sourceListData.userList]
for (const list of targetListData.userList) {
const targetList = listDataObj[list.id]
const targetList = userListDataObj[list.id]
if (targetList) {
targetList.list = handleMergeList(targetList, list, addMusicLocationType).list
} else {
@ -156,11 +172,11 @@ const overwriteList = (sourceListData, targetListData) => {
newListData.defaultList = sourceListData.defaultList
newListData.loveList = sourceListData.loveList
const listDataObj = createListDataObj(sourceListData)
const userListDataObj = createUserListDataObj(sourceListData)
newListData.userList = [...sourceListData.userList]
for (const list of targetListData.userList) {
const targetList = listDataObj[list.id]
const targetList = userListDataObj[list.id]
if (targetList) continue
newListData.userList.push(list)
}
@ -299,9 +315,9 @@ const handleMergeListDataFromSnapshot = async(socket, snapshot) => {
const newListData = {}
newListData.defaultList = mergeListDataFromSnapshot(localListData.defaultList, remoteListData.defaultList, snapshot.defaultList, addMusicLocationType)
newListData.loveList = mergeListDataFromSnapshot(localListData.loveList, remoteListData.loveList, snapshot.loveList, addMusicLocationType)
const localUserListData = createListDataObj(localListData)
const remoteUserListData = createListDataObj(remoteListData)
const snapshotUserListData = createListDataObj(snapshot)
const localUserListData = createUserListDataObj(localListData)
const remoteUserListData = createUserListDataObj(remoteListData)
const snapshotUserListData = createUserListDataObj(snapshot)
const removedListIds = new Set()
const localUserListIds = new Set()
const remoteUserListIds = new Set()
@ -368,7 +384,7 @@ const syncList = async socket => {
}
console.log('isSyncRequired', isSyncRequired)
if (isSyncRequired) return handleSyncList(socket)
return handleMergeListDataFromSnapshot(socket, fileData)
return handleMergeListDataFromSnapshot(socket, patchListData(fileData))
}
const checkSyncQueue = async() => {

View File

@ -28,19 +28,16 @@ const state = {
id: 'default',
name: '试听列表',
list: [],
location: 0,
},
loveList: {
id: 'love',
name: '我的收藏',
list: [],
location: 0,
},
tempList: {
id: 'temp',
name: '临时列表',
list: [],
location: 0,
},
userList: [],
}
@ -75,8 +72,8 @@ const actions = {
// mitations
const mutations = {
initList(state, { defaultList, loveList, userList }) {
if (defaultList != null) Object.assign(state.defaultList, { list: defaultList.list, location: defaultList.location })
if (loveList != null) Object.assign(state.loveList, { list: loveList.list, location: loveList.location })
if (defaultList != null) Object.assign(state.defaultList, { list: defaultList.list })
if (loveList != null) Object.assign(state.loveList, { list: loveList.list })
if (userList != null) state.userList = userList
allListInit(state.defaultList, state.loveList, state.userList)
state.isInitedList = true
@ -104,18 +101,17 @@ const mutations = {
state.userList = userList
allListInit(state.defaultList, state.loveList, state.userList)
},
setList(state, { id, list, name, location, source, sourceListId, isSync }) {
setList(state, { id, list, name, source, sourceListId, isSync }) {
const targetList = allList[id]
if (targetList) {
if (name && targetList.name === name) {
if (!isSync) {
window.eventHub.$emit(eventSyncName.send_action_list, {
action: 'set_list',
data: { id, list, name, location, source, sourceListId },
data: { id, list, name, source, sourceListId },
})
}
targetList.list.splice(0, targetList.list.length, ...list)
targetList.location = location
return
}
@ -124,14 +120,13 @@ const mutations = {
if (!isSync) {
window.eventHub.$emit(eventSyncName.send_action_list, {
action: 'set_list',
data: { id, list, name, location, source, sourceListId },
data: { id, list, name, source, sourceListId },
})
}
let newList = {
name,
id,
list,
location,
source,
sourceListId,
}
@ -316,7 +311,6 @@ const mutations = {
name,
id,
list: [],
location: 0,
source,
sourceListId,
}