@@ -679,6 +679,8 @@ export const LOCKED_WARMUP_TICKS = 60;
679679export interface WarmupTickState {
680680 running : boolean ;
681681 ticks : number ;
682+ /** Set when session init fails: stops the loop in either mode, since a closed page fails every tick. */
683+ cancelled ?: boolean ;
682684}
683685
684686export interface WarmupTickOptions {
@@ -794,12 +796,25 @@ export function prepareBeginFrameTimeline(
794796 } ;
795797}
796798
799+ /** Runs BeginFrame session setup; a failure stops the warm-up loop, which would otherwise tick forever. */
800+ export async function cancelWarmupOnError < T > (
801+ state : WarmupTickState ,
802+ setup : ( ) => Promise < T > ,
803+ ) : Promise < T > {
804+ try {
805+ return await setup ( ) ;
806+ } catch ( error ) {
807+ state . cancelled = true ;
808+ throw error ;
809+ }
810+ }
811+
797812export async function driveWarmupTicks (
798813 options : WarmupTickOptions ,
799814 state : WarmupTickState ,
800815) : Promise < void > {
801816 const sleep = options . sleep ?? realSleep ;
802- while ( true ) {
817+ while ( ! state . cancelled ) {
803818 if ( options . lockWarmupTicks ) {
804819 // Locked mode exits on the iteration count, ignoring `state.running` —
805820 // the caller flips `running=false` after page-readiness but we keep
@@ -2462,95 +2477,93 @@ export async function initializeSession(session: CaptureSession): Promise<void>
24622477 warmupLoopPromise . catch ( ( ) => { } ) ;
24632478 logInitPhase ( "warmup loop started" ) ;
24642479
2465- await gotoEntryPage ( ) ;
2466- logInitPhase ( "page.goto complete" ) ;
2480+ await cancelWarmupOnError ( warmupState , async ( ) => {
2481+ await gotoEntryPage ( ) ;
2482+ logInitPhase ( "page.goto complete" ) ;
24672483
2468- // Flush the GSAP proxy queue synchronously. In BeginFrame mode the rAF-based
2469- // batch drain runs on the warmup loop's 33ms ticks — for tween-heavy
2470- // compositions this is the dominant init cost. Flushing synchronously
2471- // eliminates the wait entirely.
2472- await page . evaluate ( `window.__hfFlushSync?.()` ) ;
2473- logInitPhase ( "GSAP proxy flush complete" ) ;
2484+ // Flush the GSAP proxy queue synchronously. In BeginFrame mode the rAF-based
2485+ // batch drain runs on the warmup loop's 33ms ticks — for tween-heavy
2486+ // compositions this is the dominant init cost. Flushing synchronously
2487+ // eliminates the wait entirely.
2488+ await page . evaluate ( `window.__hfFlushSync?.()` ) ;
2489+ logInitPhase ( "GSAP proxy flush complete" ) ;
24742490
2475- // Poll for window.__hf readiness using manual evaluate loop (waitForFunction
2476- // uses rAF polling internally, which won't fire in beginFrame mode).
2477- const pageReadyTimeout = session . config ?. playerReadyTimeout ?? DEFAULT_CONFIG . playerReadyTimeout ;
2478- try {
2491+ // Poll for window.__hf readiness using manual evaluate loop (waitForFunction
2492+ // uses rAF polling internally, which won't fire in beginFrame mode).
2493+ const pageReadyTimeout =
2494+ session . config ?. playerReadyTimeout ?? DEFAULT_CONFIG . playerReadyTimeout ;
24792495 await pollHfReady ( page , pageReadyTimeout ) ;
24802496 logInitPhase ( "pollHfReady complete" ) ;
2481- } catch ( err ) {
2482- warmupState . running = false ;
2483- throw err ;
2484- }
24852497
2486- session . subTimelineWaitOutcome = await pollSubCompositionTimelines (
2487- page ,
2488- pageReadyTimeout ,
2489- undefined ,
2490- ( ) => session . scriptLoadFailures ,
2491- undefined ,
2492- ( ids ) => {
2493- session . pendingTimelineIds = [ ...ids ] ;
2494- } ,
2495- ) ;
2496- logInitPhase ( `pollSubCompositionTimelines complete (${ session . subTimelineWaitOutcome } )` ) ;
2497- recordSubTimelineWarning ( session , pageReadyTimeout ) ;
2498-
2499- await applyVideoMetadataHints ( page , session . options . videoMetadataHints ) ;
2500- logInitPhase ( "applyVideoMetadataHints complete" ) ;
2501-
2502- // Run independent readiness checks in parallel — videos, images, fonts,
2503- // and Tailwind don't depend on each other's completion.
2504- const bfSkipVideoIds = session . options . skipReadinessVideoIds ?? [ ] ;
2505- const [ bfVideosReady ] = await Promise . all ( [
2506- pollVideosReady ( page , bfSkipVideoIds , pageReadyTimeout ) ,
2507- pollImagesReady ( page , pageReadyTimeout ) . then ( async ( ready ) => {
2508- if ( ! ready ) {
2509- const failedImages = await page . evaluate ( ( ) => {
2510- return Array . from ( document . querySelectorAll ( "img" ) )
2511- . filter ( ( img ) => {
2512- const ie = img as HTMLImageElement ;
2513- const src = ie . getAttribute ( "src" ) || "" ;
2514- if ( ! src || src . startsWith ( "data:" ) ) return false ;
2515- return ! ( ie . complete && ie . naturalWidth > 0 ) ;
2516- } )
2517- . map ( ( img ) => ( img as HTMLImageElement ) . src || img . getAttribute ( "src" ) || "(no src)" )
2518- . join ( ", " ) ;
2519- } ) ;
2520- console . warn (
2521- `[FrameCapture] Some image elements did not load within ${ pageReadyTimeout } ms: ${ failedImages } . ` +
2522- `Continuing render — affected images may appear blank/missing in early frames.` ,
2523- ) ;
2524- }
2525- await decodeAllImages ( page ) ;
2526- return ready ;
2527- } ) ,
2528- page . evaluate ( `document.fonts?.ready` ) ,
2529- waitForOptionalTailwindReady ( page , pageReadyTimeout ) ,
2530- ] ) ;
2531- logInitPhase ( "media + fonts + tailwind ready" ) ;
2532-
2533- if ( ! bfVideosReady ) {
2534- const failedVideos = await page . evaluate ( ( skipIdList : readonly string [ ] ) => {
2535- const skip = new Set ( skipIdList ) ;
2536- return Array . from ( document . querySelectorAll ( "video" ) )
2537- . filter ( ( v ) => ! skip . has ( v . id ) )
2538- . filter ( ( v ) => ( v as HTMLVideoElement ) . readyState < 2 && ! ( v as HTMLVideoElement ) . error )
2539- . map ( ( v ) => ( v as HTMLVideoElement ) . src || v . getAttribute ( "src" ) || "(no src)" )
2540- . join ( ", " ) ;
2541- } , bfSkipVideoIds ) ;
2542- console . warn (
2543- `[FrameCapture] Some video elements did not decode within ${ pageReadyTimeout } ms: ${ failedVideos } . ` +
2544- `Continuing render — affected videos will appear as blank/black frames.` ,
2498+ session . subTimelineWaitOutcome = await pollSubCompositionTimelines (
2499+ page ,
2500+ pageReadyTimeout ,
2501+ undefined ,
2502+ ( ) => session . scriptLoadFailures ,
2503+ undefined ,
2504+ ( ids ) => {
2505+ session . pendingTimelineIds = [ ...ids ] ;
2506+ } ,
25452507 ) ;
2546- }
2547- recordCaptureWarnings (
2548- session ,
2549- await collectMediaReadinessWarnings ( page , bfSkipVideoIds , pageReadyTimeout ) ,
2550- ) ;
2551- await recordLiveMapWarning ( session , page ) ;
2508+ logInitPhase ( `pollSubCompositionTimelines complete (${ session . subTimelineWaitOutcome } )` ) ;
2509+ recordSubTimelineWarning ( session , pageReadyTimeout ) ;
25522510
2553- await recordSessionInitTelemetry ( session , initStart ) ;
2511+ await applyVideoMetadataHints ( page , session . options . videoMetadataHints ) ;
2512+ logInitPhase ( "applyVideoMetadataHints complete" ) ;
2513+
2514+ // Run independent readiness checks in parallel — videos, images, fonts,
2515+ // and Tailwind don't depend on each other's completion.
2516+ const bfSkipVideoIds = session . options . skipReadinessVideoIds ?? [ ] ;
2517+ const [ bfVideosReady ] = await Promise . all ( [
2518+ pollVideosReady ( page , bfSkipVideoIds , pageReadyTimeout ) ,
2519+ pollImagesReady ( page , pageReadyTimeout ) . then ( async ( ready ) => {
2520+ if ( ! ready ) {
2521+ const failedImages = await page . evaluate ( ( ) => {
2522+ return Array . from ( document . querySelectorAll ( "img" ) )
2523+ . filter ( ( img ) => {
2524+ const ie = img as HTMLImageElement ;
2525+ const src = ie . getAttribute ( "src" ) || "" ;
2526+ if ( ! src || src . startsWith ( "data:" ) ) return false ;
2527+ return ! ( ie . complete && ie . naturalWidth > 0 ) ;
2528+ } )
2529+ . map ( ( img ) => ( img as HTMLImageElement ) . src || img . getAttribute ( "src" ) || "(no src)" )
2530+ . join ( ", " ) ;
2531+ } ) ;
2532+ console . warn (
2533+ `[FrameCapture] Some image elements did not load within ${ pageReadyTimeout } ms: ${ failedImages } . ` +
2534+ `Continuing render — affected images may appear blank/missing in early frames.` ,
2535+ ) ;
2536+ }
2537+ await decodeAllImages ( page ) ;
2538+ return ready ;
2539+ } ) ,
2540+ page . evaluate ( `document.fonts?.ready` ) ,
2541+ waitForOptionalTailwindReady ( page , pageReadyTimeout ) ,
2542+ ] ) ;
2543+ logInitPhase ( "media + fonts + tailwind ready" ) ;
2544+
2545+ if ( ! bfVideosReady ) {
2546+ const failedVideos = await page . evaluate ( ( skipIdList : readonly string [ ] ) => {
2547+ const skip = new Set ( skipIdList ) ;
2548+ return Array . from ( document . querySelectorAll ( "video" ) )
2549+ . filter ( ( v ) => ! skip . has ( v . id ) )
2550+ . filter ( ( v ) => ( v as HTMLVideoElement ) . readyState < 2 && ! ( v as HTMLVideoElement ) . error )
2551+ . map ( ( v ) => ( v as HTMLVideoElement ) . src || v . getAttribute ( "src" ) || "(no src)" )
2552+ . join ( ", " ) ;
2553+ } , bfSkipVideoIds ) ;
2554+ console . warn (
2555+ `[FrameCapture] Some video elements did not decode within ${ pageReadyTimeout } ms: ${ failedVideos } . ` +
2556+ `Continuing render — affected videos will appear as blank/black frames.` ,
2557+ ) ;
2558+ }
2559+ recordCaptureWarnings (
2560+ session ,
2561+ await collectMediaReadinessWarnings ( page , bfSkipVideoIds , pageReadyTimeout ) ,
2562+ ) ;
2563+ await recordLiveMapWarning ( session , page ) ;
2564+
2565+ await recordSessionInitTelemetry ( session , initStart ) ;
2566+ } ) ;
25542567
25552568 // Stop warmup, then drain the loop in BOTH modes before any further
25562569 // BeginFrame on this session (drawElement init, the render-frame commit tick
0 commit comments