Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): notify subscribers when late fallbackDuration updates playback store

GhaZy2ec202cd698c
diff --git a/apps/web/__tests__/unit/playback-store.test.ts b/apps/web/__tests__/unit/playback-store.test.ts
index 5022e68b5..6b8abec03 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 0bc286e13..d15ab69e7 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) => {