From 902afc142d5f16491f56267f36b3ca4e42dc6fc3 Mon Sep 17 00:00:00 2001 From: Ronak Parmar <84374383+ronak-create@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:20:15 +0530 Subject: [PATCH 1/4] Seek without waiting for frame callbacks in a hidden tab A hidden tab runs no requestVideoFrameCallback, so every hard seek waited out all three retries (about 3 s each) and a slow seek was restarted before it landed. In a hidden tab a finished seek now resolves at once, and the seek gets longer before it is retried. --- app.js | 38 +++++++++++++++++++++++--------------- test/export-seek.test.js | 15 +++++++++++++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 82e35d8ea..f796be5a1 100644 --- a/app.js +++ b/app.js @@ -17210,25 +17210,31 @@ function sameSourceFrame(el, mt, shown) { return el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002 && mt >= shown - 1e-4 && mt < shown + dur - 1e-4; } +/** A hidden tab renders nothing, so it presents no frames and runs no frame callbacks. */ +function tabHidden() { return typeof document !== "undefined" && document.hidden === true; } function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer - const after = () => waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) - .then(res) - .catch((err) => { - // Slow motion lands several output frames on one source frame: seeking within the - // frame already on screen presents nothing new, so no frame callback ever comes. - // The seek finished on mt and the frame shown just before is within one source frame - // of it (≥ 20 fps footage): that frame is the right picture. - if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } - if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } - // Last try: the seek finished on mt with the frame's data loaded, so the element holds - // that frame even though no callback reported it. Better than failing the whole export. - if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } - rej(err); - }); + const after = () => { + // a hidden tab renders nothing, so no frame callback comes: a finished seek is the signal + if (tabHidden() && el.readyState >= 2 && !el.seeking) { notePresented(el, el.currentTime); res(); return; } + waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) + .then(res) + .catch((err) => { + // Slow motion lands several output frames on one source frame: seeking within the + // frame already on screen presents nothing new, so no frame callback ever comes. + // The seek finished on mt and the frame shown just before is within one source frame + // of it (≥ 20 fps footage): that frame is the right picture. + if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } + if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } + // Last try: the seek finished on mt with the frame's data loaded, so the element holds + // that frame even though no callback reported it. Better than failing the whole export. + if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } + rej(err); + }); + }; if (presentedClose(el, mt, 0.002) && el.readyState >= 2) { res(); return; @@ -17246,7 +17252,9 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { el.removeEventListener("seeked", done); after(); }; - const tm = setTimeout(done, 1500); + // Chrome slows a hidden tab's decoding: re-seeking on a short timeout would restart a seek + // that is still on its way, so wait longer for it there + const tm = setTimeout(done, tabHidden() ? 10000 : 1500); el.addEventListener("seeked", done); try { if (!el.paused) el.pause(); diff --git a/test/export-seek.test.js b/test/export-seek.test.js index 73289473e..b2ddd6847 100644 --- a/test/export-seek.test.js +++ b/test/export-seek.test.js @@ -192,3 +192,18 @@ test("hardSeekVideo: a seek that never lands still fails", async () => { const el = seekingVideo({ currentTime: 0, presented: 0, completes: false }); await assert.rejects(hardSeekVideo(el, 2.0), /presented frame timeout/); }); + +/* A hidden tab (the user switched away while an agent works) runs no frame + callbacks: a finished seek must resolve at once, not wait out every retry. */ +test("hardSeekVideo: in a hidden tab a finished seek resolves without a frame callback", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 0, frameDur: 0 }); + globalThis.document = { hidden: true }; + try { + const t0 = Date.now(); + await hardSeekVideo(el, 2.0); + assert.ok(Date.now() - t0 < 500, "resolved without the frame-callback retries"); + assert.equal(el._fcPresentedTime, 2.0); + assert.equal(el.rvfcCalls, 0); + } finally { delete globalThis.document; } +}); From 0f6fde36d4a3e3fd1fee38700ef3d2d604cc1035 Mon Sep 17 00:00:00 2001 From: Ronak Parmar <84374383+ronak-create@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:27:28 +0530 Subject: [PATCH 2/4] Changelog and rebuilt playground for the background-tab seek fix --- CHANGELOG.md | 7 +++++++ docs/demo/app.js | 38 +++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c15b275c7..22761d07a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- **Frames and exports from a background tab.** When the editor tab was in the + background, every seek waited about 3 s for a frame the browser never shows + there, and a slow seek could be restarted until it failed, so + `fablecut_frame {time}`, scopes and exports from that tab were slow or + failed. A finished seek now counts at once in a background tab. + ## [1.20.0] - 2026-11-20 ### Added diff --git a/docs/demo/app.js b/docs/demo/app.js index 82e35d8ea..f796be5a1 100644 --- a/docs/demo/app.js +++ b/docs/demo/app.js @@ -17210,25 +17210,31 @@ function sameSourceFrame(el, mt, shown) { return el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002 && mt >= shown - 1e-4 && mt < shown + dur - 1e-4; } +/** A hidden tab renders nothing, so it presents no frames and runs no frame callbacks. */ +function tabHidden() { return typeof document !== "undefined" && document.hidden === true; } function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer - const after = () => waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) - .then(res) - .catch((err) => { - // Slow motion lands several output frames on one source frame: seeking within the - // frame already on screen presents nothing new, so no frame callback ever comes. - // The seek finished on mt and the frame shown just before is within one source frame - // of it (≥ 20 fps footage): that frame is the right picture. - if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } - if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } - // Last try: the seek finished on mt with the frame's data loaded, so the element holds - // that frame even though no callback reported it. Better than failing the whole export. - if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } - rej(err); - }); + const after = () => { + // a hidden tab renders nothing, so no frame callback comes: a finished seek is the signal + if (tabHidden() && el.readyState >= 2 && !el.seeking) { notePresented(el, el.currentTime); res(); return; } + waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) + .then(res) + .catch((err) => { + // Slow motion lands several output frames on one source frame: seeking within the + // frame already on screen presents nothing new, so no frame callback ever comes. + // The seek finished on mt and the frame shown just before is within one source frame + // of it (≥ 20 fps footage): that frame is the right picture. + if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } + if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } + // Last try: the seek finished on mt with the frame's data loaded, so the element holds + // that frame even though no callback reported it. Better than failing the whole export. + if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } + rej(err); + }); + }; if (presentedClose(el, mt, 0.002) && el.readyState >= 2) { res(); return; @@ -17246,7 +17252,9 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { el.removeEventListener("seeked", done); after(); }; - const tm = setTimeout(done, 1500); + // Chrome slows a hidden tab's decoding: re-seeking on a short timeout would restart a seek + // that is still on its way, so wait longer for it there + const tm = setTimeout(done, tabHidden() ? 10000 : 1500); el.addEventListener("seeked", done); try { if (!el.paused) el.pause(); From 1090c3d3bb059d553fd01854e669003f8239e2c0 Mon Sep 17 00:00:00 2001 From: Ronak Parmar <84374383+ronak-create@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:36:23 +0530 Subject: [PATCH 3/4] Hidden-tab seeks: check the landing time, learn no frame duration, stop on Cancel --- app.js | 16 ++++++++++++++-- docs/demo/app.js | 16 ++++++++++++++-- test/export-seek.test.js | 25 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index f796be5a1..4419bb55f 100644 --- a/app.js +++ b/app.js @@ -17215,11 +17215,21 @@ function tabHidden() { return typeof document !== "undefined" && document.hidden function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; + // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s) + const signal = typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; + if (signal && signal.aborted) return Promise.reject(new Error("cancelled")); return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer const after = () => { - // a hidden tab renders nothing, so no frame callback comes: a finished seek is the signal - if (tabHidden() && el.readyState >= 2 && !el.seeking) { notePresented(el, el.currentTime); res(); return; } + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + // a hidden tab renders nothing, so no frame callback comes: a seek that finished on mt + // is the signal. It is no presented frame, so it teaches nothing about frame durations. + if (tabHidden() && el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { + el._fcPresentedTime = el.currentTime; + el._fcLastShown = null; + res(); + return; + } waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) .then(res) .catch((err) => { @@ -17250,12 +17260,14 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const done = () => { clearTimeout(tm); el.removeEventListener("seeked", done); + if (signal) signal.removeEventListener("abort", done); after(); }; // Chrome slows a hidden tab's decoding: re-seeking on a short timeout would restart a seek // that is still on its way, so wait longer for it there const tm = setTimeout(done, tabHidden() ? 10000 : 1500); el.addEventListener("seeked", done); + if (signal) signal.addEventListener("abort", done); try { if (!el.paused) el.pause(); // Same currentTime does not fire seeked — nudge so the decoder must present mt. diff --git a/docs/demo/app.js b/docs/demo/app.js index f796be5a1..4419bb55f 100644 --- a/docs/demo/app.js +++ b/docs/demo/app.js @@ -17215,11 +17215,21 @@ function tabHidden() { return typeof document !== "undefined" && document.hidden function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; + // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s) + const signal = typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; + if (signal && signal.aborted) return Promise.reject(new Error("cancelled")); return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer const after = () => { - // a hidden tab renders nothing, so no frame callback comes: a finished seek is the signal - if (tabHidden() && el.readyState >= 2 && !el.seeking) { notePresented(el, el.currentTime); res(); return; } + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + // a hidden tab renders nothing, so no frame callback comes: a seek that finished on mt + // is the signal. It is no presented frame, so it teaches nothing about frame durations. + if (tabHidden() && el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { + el._fcPresentedTime = el.currentTime; + el._fcLastShown = null; + res(); + return; + } waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) .then(res) .catch((err) => { @@ -17250,12 +17260,14 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const done = () => { clearTimeout(tm); el.removeEventListener("seeked", done); + if (signal) signal.removeEventListener("abort", done); after(); }; // Chrome slows a hidden tab's decoding: re-seeking on a short timeout would restart a seek // that is still on its way, so wait longer for it there const tm = setTimeout(done, tabHidden() ? 10000 : 1500); el.addEventListener("seeked", done); + if (signal) signal.addEventListener("abort", done); try { if (!el.paused) el.pause(); // Same currentTime does not fire seeked — nudge so the decoder must present mt. diff --git a/test/export-seek.test.js b/test/export-seek.test.js index b2ddd6847..e26ebbf2b 100644 --- a/test/export-seek.test.js +++ b/test/export-seek.test.js @@ -207,3 +207,28 @@ test("hardSeekVideo: in a hidden tab a finished seek resolves without a frame ca assert.equal(el.rvfcCalls, 0); } finally { delete globalThis.document; } }); + +test("hardSeekVideo: a hidden-tab seek teaches nothing about frame durations", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 1.0, frameDur: 1 / 30 }); + globalThis.document = { hidden: true }; + try { + await hardSeekVideo(el, 1.01); + assert.equal(el._fcFrameDur, 1 / 30); + assert.equal(el._fcLastShown, null); + } finally { delete globalThis.document; } +}); + +test("hardSeekVideo: an export's Cancel stops a seek still waiting in a hidden tab", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 0, completes: false }); + globalThis.document = { hidden: true }; + globalThis.exportAbort = new AbortController(); + try { + const t0 = Date.now(); + const p = hardSeekVideo(el, 2.0); + setTimeout(() => globalThis.exportAbort.abort(), 20); + await assert.rejects(p, /cancelled/); + assert.ok(Date.now() - t0 < 1000, "rejected on Cancel, not after the 10 s wait"); + } finally { delete globalThis.document; delete globalThis.exportAbort; } +}); From b08e61efdec8bdda53c3db5737240e95fb88171f Mon Sep 17 00:00:00 2001 From: Ronak Parmar <84374383+ronak-create@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:44:13 +0530 Subject: [PATCH 4/4] Hidden-tab seeks: retries keep the export's cancel signal --- app.js | 11 +++++++---- docs/demo/app.js | 11 +++++++---- test/export-seek.test.js | 13 +++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index 4419bb55f..62d11f632 100644 --- a/app.js +++ b/app.js @@ -17212,11 +17212,13 @@ function sameSourceFrame(el, mt, shown) { } /** A hidden tab renders nothing, so it presents no frames and runs no frame callbacks. */ function tabHidden() { return typeof document !== "undefined" && document.hidden === true; } -function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { +/** The running export's cancel signal, if any. */ +function exportSignal() { return typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; } +function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime, signal = exportSignal()) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; - // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s) - const signal = typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; + // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s); + // retries keep the signal of the export that started them, even once it is cleared if (signal && signal.aborted) return Promise.reject(new Error("cancelled")); return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer @@ -17238,7 +17240,8 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { // The seek finished on mt and the frame shown just before is within one source frame // of it (≥ 20 fps footage): that frame is the right picture. if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } - if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown, signal).then(res, rej); return; } // Last try: the seek finished on mt with the frame's data loaded, so the element holds // that frame even though no callback reported it. Better than failing the whole export. if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } diff --git a/docs/demo/app.js b/docs/demo/app.js index 4419bb55f..62d11f632 100644 --- a/docs/demo/app.js +++ b/docs/demo/app.js @@ -17212,11 +17212,13 @@ function sameSourceFrame(el, mt, shown) { } /** A hidden tab renders nothing, so it presents no frames and runs no frame callbacks. */ function tabHidden() { return typeof document !== "undefined" && document.hidden === true; } -function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { +/** The running export's cancel signal, if any. */ +function exportSignal() { return typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; } +function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime, signal = exportSignal()) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; - // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s) - const signal = typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; + // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s); + // retries keep the signal of the export that started them, even once it is cleared if (signal && signal.aborted) return Promise.reject(new Error("cancelled")); return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer @@ -17238,7 +17240,8 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { // The seek finished on mt and the frame shown just before is within one source frame // of it (≥ 20 fps footage): that frame is the right picture. if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } - if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown, signal).then(res, rej); return; } // Last try: the seek finished on mt with the frame's data loaded, so the element holds // that frame even though no callback reported it. Better than failing the whole export. if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } diff --git a/test/export-seek.test.js b/test/export-seek.test.js index e26ebbf2b..a3b894763 100644 --- a/test/export-seek.test.js +++ b/test/export-seek.test.js @@ -232,3 +232,16 @@ test("hardSeekVideo: an export's Cancel stops a seek still waiting in a hidden t assert.ok(Date.now() - t0 < 1000, "rejected on Cancel, not after the 10 s wait"); } finally { delete globalThis.document; delete globalThis.exportAbort; } }); + +test("hardSeekVideo: retries keep the export's cancel signal after it is cleared", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 0, frameDur: 0 }); // seeks land, no frame callback + const ctl = new AbortController(); + globalThis.exportAbort = ctl; + try { + const p = hardSeekVideo(el, 2.0); + globalThis.exportAbort = null; // export cleanup after a sibling seek was cancelled + ctl.abort(); + await assert.rejects(p, /cancelled/); + } finally { delete globalThis.exportAbort; } +});