Skip to content

Commit c96eff1

Browse files
committed
In song player, keep checking if another player started even if the current one is not currently visible.
1 parent 03ff1e4 commit c96eff1

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

player/main.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,26 @@ function onWindowResize(): void {
281281
renderTimeline();
282282
}
283283

284+
let pauseIfAnotherPlayerStartsHandle: ReturnType<typeof setInterval> | null = null;
285+
function pauseIfAnotherPlayerStarts(): void {
286+
if (!synth.playing) {
287+
clearInterval(pauseIfAnotherPlayerStartsHandle!);
288+
return;
289+
}
290+
291+
const storedPlayerId: string | null = getLocalStorage("playerId");
292+
if (storedPlayerId != null && storedPlayerId != id) {
293+
onTogglePlay();
294+
renderPlayhead();
295+
clearInterval(pauseIfAnotherPlayerStartsHandle!);
296+
}
297+
}
298+
284299
function animate(): void {
285300
if (synth.playing) {
286301
animationRequest = requestAnimationFrame(animate);
287-
const storedPlayerId: string | null = getLocalStorage("playerId");
288-
if (storedPlayerId != null && storedPlayerId != id) {
289-
onTogglePlay();
290-
}
291302
renderPlayhead();
292303
}
293-
294304
if (pauseButtonDisplayed != synth.playing) {
295305
renderPlayButton();
296306
}
@@ -306,6 +316,8 @@ function onTogglePlay(): void {
306316
synth.play();
307317
setLocalStorage("playerId", id);
308318
animate();
319+
clearInterval(pauseIfAnotherPlayerStartsHandle!);
320+
pauseIfAnotherPlayerStartsHandle = setInterval(pauseIfAnotherPlayerStarts, 100);
309321
}
310322
}
311323
renderPlayButton();
@@ -337,6 +349,7 @@ function onTimelineMouseDown(event: MouseEvent): void {
337349
}
338350

339351
function onTimelineMouseMove(event: MouseEvent): void {
352+
if (!draggingPlayhead) return;
340353
event.preventDefault();
341354
onTimelineCursorMove(event.clientX || event.pageX);
342355
}

0 commit comments

Comments
 (0)