@@ -11,6 +11,22 @@ let io
1111let syncingId = null
1212const wait = ( time = 1000 ) => new Promise ( ( resolve , reject ) => setTimeout ( resolve , time ) )
1313
14+ const patchListData = listData => {
15+ return Object . assign ( { } , {
16+ defaultList : {
17+ id : 'default' ,
18+ name : '试听列表' ,
19+ list : [ ] ,
20+ } ,
21+ loveList : {
22+ id : 'love' ,
23+ name : '我的收藏' ,
24+ list : [ ] ,
25+ } ,
26+ userList : [ ] ,
27+ } , listData )
28+ }
29+
1430const getRemoteListData = socket => new Promise ( ( resolve , reject ) => {
1531 console . log ( 'getRemoteListData' )
1632 const handleError = reason => {
@@ -23,7 +39,7 @@ const getRemoteListData = socket => new Promise((resolve, reject) => {
2339 const data = JSON . parse ( decryptMsg ( socket . data . keyInfo , enData ) )
2440 if ( ! data ) return reject ( new Error ( 'Get remote list data failed' ) )
2541 if ( data . action != 'getData' ) return
26- resolve ( data . data )
42+ resolve ( patchListData ( data . data ) )
2743 }
2844
2945 socket . on ( 'disconnect' , handleError )
@@ -35,7 +51,7 @@ const getLocalListData = () => new Promise((resolve, reject) => {
3551 const handleSuccess = ( { action, data } ) => {
3652 if ( action !== 'getData' ) return
3753 global . lx_event . sync . off ( SYNC_EVENT_NAMES . sync_handle_list , handleSuccess )
38- resolve ( data )
54+ resolve ( patchListData ( data ) )
3955 }
4056 global . lx_event . sync . on ( SYNC_EVENT_NAMES . sync_handle_list , handleSuccess )
4157 global . lx_event . sync . sync_list ( {
@@ -87,10 +103,10 @@ const updateSnapshot = (path, data) => {
87103}
88104
89105
90- const createListDataObj = listData => {
91- const listDataObj = { }
92- for ( const list of listData . userList ) listDataObj [ list . id ] = list
93- return listDataObj
106+ const createUserListDataObj = listData => {
107+ const userListDataObj = { }
108+ for ( const list of listData . userList ) userListDataObj [ list . id ] = list
109+ return userListDataObj
94110}
95111
96112const handleMergeList = ( sourceList , targetList , addMusicLocationType ) => {
@@ -137,11 +153,11 @@ const mergeList = (sourceListData, targetListData) => {
137153 newListData . defaultList = handleMergeList ( sourceListData . defaultList , targetListData . defaultList , addMusicLocationType )
138154 newListData . loveList = handleMergeList ( sourceListData . loveList , targetListData . loveList , addMusicLocationType )
139155
140- const listDataObj = createListDataObj ( sourceListData )
156+ const userListDataObj = createUserListDataObj ( sourceListData )
141157 newListData . userList = [ ...sourceListData . userList ]
142158
143159 for ( const list of targetListData . userList ) {
144- const targetList = listDataObj [ list . id ]
160+ const targetList = userListDataObj [ list . id ]
145161 if ( targetList ) {
146162 targetList . list = handleMergeList ( targetList , list , addMusicLocationType ) . list
147163 } else {
@@ -156,11 +172,11 @@ const overwriteList = (sourceListData, targetListData) => {
156172 newListData . defaultList = sourceListData . defaultList
157173 newListData . loveList = sourceListData . loveList
158174
159- const listDataObj = createListDataObj ( sourceListData )
175+ const userListDataObj = createUserListDataObj ( sourceListData )
160176 newListData . userList = [ ...sourceListData . userList ]
161177
162178 for ( const list of targetListData . userList ) {
163- const targetList = listDataObj [ list . id ]
179+ const targetList = userListDataObj [ list . id ]
164180 if ( targetList ) continue
165181 newListData . userList . push ( list )
166182 }
@@ -259,8 +275,10 @@ const mergeListDataFromSnapshot = (sourceList, targetList, snapshotList, addMusi
259275 const targetListItemIds = new Set ( )
260276 for ( const m of sourceList . list ) sourceListItemIds . add ( m . songmid )
261277 for ( const m of targetList . list ) targetListItemIds . add ( m . songmid )
262- for ( const m of snapshotList . list ) {
263- if ( ! sourceListItemIds . has ( m . songmid ) || ! targetListItemIds . has ( m . songmid ) ) removedListIds . add ( m . songmid )
278+ if ( snapshotList ) {
279+ for ( const m of snapshotList . list ) {
280+ if ( ! sourceListItemIds . has ( m . songmid ) || ! targetListItemIds . has ( m . songmid ) ) removedListIds . add ( m . songmid )
281+ }
264282 }
265283
266284 let newList
@@ -294,13 +312,12 @@ const mergeListDataFromSnapshot = (sourceList, targetList, snapshotList, addMusi
294312const handleMergeListDataFromSnapshot = async ( socket , snapshot ) => {
295313 const addMusicLocationType = global . appSetting . list . addMusicLocationType
296314 const [ remoteListData , localListData ] = await Promise . all ( [ getRemoteListData ( socket ) , getLocalListData ( ) ] )
297- console . log ( 'handleMergeListDataFromSnapshot' , 'remoteListData, localListData' )
298315 const newListData = { }
299316 newListData . defaultList = mergeListDataFromSnapshot ( localListData . defaultList , remoteListData . defaultList , snapshot . defaultList , addMusicLocationType )
300317 newListData . loveList = mergeListDataFromSnapshot ( localListData . loveList , remoteListData . loveList , snapshot . loveList , addMusicLocationType )
301- const localUserListData = createListDataObj ( localListData )
302- const remoteUserListData = createListDataObj ( remoteListData )
303- const snapshotUserListData = createListDataObj ( snapshot )
318+ const localUserListData = createUserListDataObj ( localListData )
319+ const remoteUserListData = createUserListDataObj ( remoteListData )
320+ const snapshotUserListData = createUserListDataObj ( snapshot )
304321 const removedListIds = new Set ( )
305322 const localUserListIds = new Set ( )
306323 const remoteUserListIds = new Set ( )
@@ -367,7 +384,7 @@ const syncList = async socket => {
367384 }
368385 console . log ( 'isSyncRequired' , isSyncRequired )
369386 if ( isSyncRequired ) return handleSyncList ( socket )
370- return handleMergeListDataFromSnapshot ( socket , fileData )
387+ return handleMergeListDataFromSnapshot ( socket , patchListData ( fileData ) )
371388}
372389
373390const checkSyncQueue = async ( ) => {
0 commit comments