Files
hyperframes/packages/core/src/runtime/wallClock.ts
T
Miguel Ángel 59c2065d63 fix(producer): page timers during a render run on film time (#7362)
* fix(producer): page timers during a render run on film time

* test(producer): a regression film whose timer bars must follow film time

* test(producer): schedule the timers regression film in CI shards

* fix(producer): keep setup timers on the wall clock until the page reports ready

* fix(producer): keep the renderer's own waits and deadlines on the wall clock

* test(engine): fail CI on a bare timer in code the engine runs in the page

* fix(core): type the wall-clock timers like the global timers they replace

* chore(engine): drop the comment on the shim's window type
2026-11-07 18:06:55 +00:00

45 lines
1.4 KiB
TypeScript

// During a render the page's setTimeout and setInterval run on film time and fire only at the next seek.
// The runtime's own waits and deadlines must not depend on that seek, so they use the browser's timers.
type TimeoutId = ReturnType<typeof setTimeout>;
type IntervalId = ReturnType<typeof setInterval>;
interface VirtualTimeTimers {
originalSetTimeout?: typeof setTimeout;
originalClearTimeout?: typeof clearTimeout;
originalSetInterval?: typeof setInterval;
originalClearInterval?: typeof clearInterval;
}
const shim = (): VirtualTimeTimers | undefined =>
(globalThis as { __HF_VIRTUAL_TIME__?: VirtualTimeTimers }).__HF_VIRTUAL_TIME__;
type Handler = (...args: never[]) => void;
export function wallSetTimeout(handler: Handler, timeout?: number, ...args: unknown[]): TimeoutId {
return (shim()?.originalSetTimeout ?? setTimeout)(
handler as (...a: unknown[]) => void,
timeout,
...args,
);
}
export function wallClearTimeout(id: TimeoutId | null | undefined): void {
(shim()?.originalClearTimeout ?? clearTimeout)(id ?? undefined);
}
export function wallSetInterval(
handler: Handler,
timeout?: number,
...args: unknown[]
): IntervalId {
return (shim()?.originalSetInterval ?? setInterval)(
handler as (...a: unknown[]) => void,
timeout,
...args,
);
}
export function wallClearInterval(id: IntervalId | null | undefined): void {
(shim()?.originalClearInterval ?? clearInterval)(id ?? undefined);
}