optimize sync

master
adb-sh 2 years ago
parent cc4d92f0ef
commit 41691a452b

@ -9,17 +9,20 @@ export const syncSession = async sessionStore => {
const hostStore = await UserStore.findById(sessionStore.host._id); const hostStore = await UserStore.findById(sessionStore.host._id);
const host = await hostStore.spotify.local; const host = await hostStore.spotify.local;
const hostCP = await host.player.getCurrentlyPlaying("track"); const hostCP = await host.player.getCurrentlyPlaying("track");
const hostCPDate = new Date();
await Promise.all([...sessionStore.clients].map(async client => { await Promise.all([...sessionStore.clients].map(async client => {
const clientStore = await UserStore.findById(client._id); const clientStore = await UserStore.findById(client._id);
const clientLocal = await clientStore.spotify.local; const clientLocal = await clientStore.spotify.local;
const clientCP = await clientLocal.player.getCurrentlyPlaying("track"); const clientCP = await clientLocal.player.getCurrentlyPlaying("track");
if (clientCP?.item?.id !== hostCP?.item?.id) { if (clientCP.isPlaying && !hostCP?.item?.uri || !hostCP.isPlaying) {
await clientLocal.player.pause();
} else if (hostCP.isPlaying && clientCP?.item?.id !== hostCP?.item?.id || !clientCP.isPlaying) {
await clientLocal.player.play({ await clientLocal.player.play({
uris: [hostCP.item?.uri], uris: [hostCP?.item?.uri],
position: hostCP.progress, position: hostCP.progress + (new Date - hostCPDate),
}); });
} else if (Math.abs(clientCP.progress - hostCP.progress) > 1000) { } else if (Math.abs(clientCP.progress - hostCP.progress + (new Date - hostCPDate)) > 500) {
await clientLocal.player.seek(hostCP.progress); await clientLocal.player.seek(hostCP.progress + (new Date - hostCPDate));
} }
})); }));
}; };

Loading…
Cancel
Save