Skip to content

Commit 4abccc7

Browse files
committed
fix(core): one waiter per playing clip when its light copy is put back
1 parent 385b071 commit 4abccc7

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎packages/core/src/runtime/mediaProxy.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,37 @@ describe("preview quality: lighter copies of heavy files", () => {
12621262
expect(isProxied(el)).toBe(false);
12631263
});
12641264

1265+
// fails without the fix: each ask while the clip played added a waiter with its own listeners and timer.
1266+
it("waits once for a playing clip's light copy to be put back, however often its size is asked", async () => {
1267+
vi.doMock("./media", async (original) => ({
1268+
...(await original<typeof import("./media")>()),
1269+
isActiveAtPlayhead: () => true,
1270+
}));
1271+
const { video, setPreviewQuality, setProxyDisplayScale } = await stage({ "/video.mp4": HEAVY });
1272+
const el = video();
1273+
setPreviewQuality(0.5);
1274+
await settle();
1275+
expect(isProxied(el)).toBe(true);
1276+
let playing = true;
1277+
Object.defineProperty(el, "paused", { configurable: true, get: () => !playing });
1278+
const listen = vi.spyOn(el, "addEventListener");
1279+
1280+
// The file now counts as light at this box, so its light copy is no longer needed.
1281+
window.__HF_MEDIA_SOURCE_FACTS__ = { "/video.mp4": LIGHT };
1282+
for (const scale of [0.99, 0.98, 0.97]) {
1283+
setProxyDisplayScale(scale);
1284+
await settle();
1285+
}
1286+
expect(isProxied(el)).toBe(true);
1287+
expect(listen.mock.calls.filter(([type]) => type === "pause")).toHaveLength(1);
1288+
1289+
playing = false;
1290+
el.dispatchEvent(new Event("pause"));
1291+
await settle();
1292+
expect(el.getAttribute("src")).toBe("/video.mp4");
1293+
vi.doUnmock("./media");
1294+
});
1295+
12651296
it("puts the original back at full quality", async () => {
12661297
const { video, setPreviewQuality } = await stage({ "/video.mp4": HEAVY });
12671298
const el = video();

‎packages/core/src/runtime/mediaProxy.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,14 @@ function keepSized(el: HTMLMediaElement, scale: number): void {
160160
const needed = shown && lowered ? scalePreviewProxyBox(shown, previewQuality) : shown;
161161
if (lowered && !needed) return;
162162
if (needed && lowered && copy.light && lightCopyNoLongerNeeded(el, needed)) {
163+
// One waiter per clip: a drag of the panel asks this again and again while the clip plays.
164+
if (restoreWaiting.has(el)) return;
165+
restoreWaiting.add(el);
163166
return whenNotPlayingHere(
164167
el,
165168
() => boxedElements.get(el) === copy,
166169
() => restoreOriginal(el),
170+
() => restoreWaiting.delete(el),
167171
);
168172
}
169173
if (boxCovers(copy.box, needed, lowered)) return dropUnneededCopy(el);
@@ -176,11 +180,18 @@ function playingHere(el: HTMLMediaElement): boolean {
176180
}
177181

178182
const SETTLE_POLL_MS = 250;
183+
const restoreWaiting = new WeakSet<HTMLMediaElement>();
179184

180185
/** Runs `apply` once `el` is not a clip playing at the playhead: now when it is not, else when it pauses, seeks, ends
181-
* or leaves its window. Nothing runs when `wanted` has turned false by then. */
182-
function whenNotPlayingHere(el: HTMLMediaElement, wanted: () => boolean, apply: () => void): void {
186+
* or leaves its window. Nothing runs when `wanted` has turned false by then. `settled` runs first either way. */
187+
function whenNotPlayingHere(
188+
el: HTMLMediaElement,
189+
wanted: () => boolean,
190+
apply: () => void,
191+
settled?: () => void,
192+
): void {
183193
if (!playingHere(el)) {
194+
settled?.();
184195
if (wanted()) apply();
185196
return;
186197
}
@@ -190,6 +201,7 @@ function whenNotPlayingHere(el: HTMLMediaElement, wanted: () => boolean, apply:
190201
if (wanted() && playingHere(el)) return;
191202
events.forEach((type) => el.removeEventListener(type, check));
192203
clearInterval(poll);
204+
settled?.();
193205
if (wanted()) apply();
194206
};
195207
events.forEach((type) => el.addEventListener(type, check));

0 commit comments

Comments
 (0)