From 2ec202cd698cc0caff159a35908692201838e8b8 Mon Sep 17 00:00:00 2001 From: GhaZy Date: Fri, 23 Oct 2026 17:34:38 +0300 Subject: [PATCH] fix(web): notify subscribers when late fallbackDuration updates playback store --- .../web/__tests__/unit/playback-store.test.ts | 36 +++++++++++++++++++ .../_components/playback/playback-store.ts | 4 +++ 2 files changed, 40 insertions(+) diff --git a/apps/web/__tests__/unit/playback-store.test.ts b/apps/web/__tests__/unit/playback-store.test.ts index 5022e68b5a0..6b8abec03d4 100644 --- a/apps/web/__tests__/unit/playback-store.test.ts +++ b/apps/web/__tests__/unit/playback-store.test.ts @@ -80,4 +80,40 @@ describe("playback store", () => { video.dispatchEvent(new Event("timeupdate")); expect(subscriber).toHaveBeenCalledTimes(1); }); + + it("notifies subscribers when late fallbackDuration changes the store duration", () => { + const store = createPlaybackStore(0); + let observedDuration = store.getDuration(); + store.subscribe(() => { + observedDuration = store.getDuration(); + }); + + expect(observedDuration).toBe(0); + + store.setFallbackDuration(60); + + expect(observedDuration).toBe(60); + expect(store.getDuration()).toBe(60); + }); + + it("does not notify subscribers when fallbackDuration does not change total duration", () => { + const video = Object.assign(new EventTarget(), { + currentTime: 0, + duration: 120, + paused: true, + ended: false, + }); + const store = createPlaybackStore(60); + store.attach(video as unknown as HTMLVideoElement); + + const subscriber = vi.fn(() => store.getDuration()); + store.subscribe(subscriber); + subscriber.mockClear(); + + store.setFallbackDuration(90); + + expect(subscriber).not.toHaveBeenCalled(); + expect(store.getDuration()).toBe(120); + store.detach(); + }); }); diff --git a/apps/web/app/s/[videoId]/_components/playback/playback-store.ts b/apps/web/app/s/[videoId]/_components/playback/playback-store.ts index 0bc286e1382..d15ab69e704 100644 --- a/apps/web/app/s/[videoId]/_components/playback/playback-store.ts +++ b/apps/web/app/s/[videoId]/_components/playback/playback-store.ts @@ -175,8 +175,12 @@ export function createPlaybackStore(fallbackDuration: number | null = 0) { const getPlaying = () => playing; const setFallbackDuration = (next: number | null) => { + const previousDuration = getDuration(); fallback = normalizeDuration(next); if (!isFiniteDuration(duration)) duration = fallback; + if (getDuration() !== previousDuration) { + emitTime(); + } }; const seek = (target: number) => {