diff --git a/src/main/modules/sync/modules/list.ts b/src/main/modules/sync/modules/list.ts index f88859b6..ca4f3c1f 100644 --- a/src/main/modules/sync/modules/list.ts +++ b/src/main/modules/sync/modules/list.ts @@ -138,7 +138,7 @@ const broadcast = async(action: listAction, data: any, excludeIds: string[] = [] if (!io) return const sockets: LX.Sync.RemoteSocket[] = await io.fetchSockets() for (const socket of sockets) { - if (excludeIds.includes(socket.data.keyInfo.clientId)) continue + if (excludeIds.includes(socket.data.keyInfo.clientId) || !socket.data.isReady) continue socket.emit(action, encryptMsg(socket.data.keyInfo, data)) } } @@ -150,10 +150,13 @@ export const sendListAction = async(action: LX.Sync.ActionList) => { } export const registerListHandler = (_io: Server, socket: LX.Sync.Socket) => { - unregisterListHandler() + if (!io) { + io = _io + removeListener = registerListActionEvent() + } - io = _io socket.on('list:action', msg => { + if (!socket.data.isReady) return // console.log(msg) msg = decryptMsg(socket.data.keyInfo, msg) if (!msg) return @@ -162,7 +165,6 @@ export const registerListHandler = (_io: Server, socket: LX.Sync.Socket) => { // socket.broadcast.emit('list:action', { action: 'list_remove', data: { id: 'default', index: 0 } }) }) - removeListener = registerListActionEvent() // socket.on('list:add', addMusic) } export const unregisterListHandler = () => { diff --git a/src/main/modules/sync/server/server.ts b/src/main/modules/sync/server/server.ts index eb5e20e8..f6f4a4d7 100644 --- a/src/main/modules/sync/server/server.ts +++ b/src/main/modules/sync/server/server.ts @@ -121,6 +121,7 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje setClientKeyInfo(keyInfo) // socket.lx_keyInfo = keyInfo socket.data.keyInfo = keyInfo + socket.data.isReady = false try { await syncList(io, socket) } catch (err) { @@ -130,6 +131,7 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje } status.devices.push(keyInfo) handleConnection(io, socket) + socket.data.isReady = true sendStatus(status) }) diff --git a/src/main/modules/sync/server/syncList.ts b/src/main/modules/sync/server/syncList.ts index 6757b932..8d5042b4 100644 --- a/src/main/modules/sync/server/syncList.ts +++ b/src/main/modules/sync/server/syncList.ts @@ -22,6 +22,8 @@ const patchListData = (listData: Partial): LX.Sync.ListData => const getRemoteListData = async(socket: LX.Sync.Socket): Promise => await new Promise((resolve, reject) => { console.log('getRemoteListData') const handleError = (reason: string) => { + socket.removeListener('list:sync', handleSuccess) + socket.removeListener('disconnect', handleError) reject(new Error(reason)) } const handleSuccess = (enData: string) => { @@ -449,6 +451,7 @@ const syncList = async(socket: LX.Sync.Socket): Promise const checkSyncQueue = async(): Promise => { if (!syncingId) return + console.log('sync queue...') await wait() return await checkSyncQueue() } diff --git a/src/main/types/sync.d.ts b/src/main/types/sync.d.ts index 7976b48a..f12f075b 100644 --- a/src/main/types/sync.d.ts +++ b/src/main/types/sync.d.ts @@ -20,6 +20,7 @@ declare global { snapshotFilePath: string isCreatedSnapshot: boolean keyInfo: KeyInfo + isReady: boolean } type Action = 'list:sync' type ListAction = 'getData' | 'finished'