okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
642f5120a
main
1/** #3673: the route change froze every click for 4-5 s (Chromium and the WebView2 window about 4 s, Firefox about 5 s) because the view 2transition's update callback waited for requestAnimationFrame, which the browser holds while that callback is pending. */ 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { build } from 'esbuild' ; 6import { fileURLToPath } from 'node:url' ; 7 8const result = await build ({ entryPoints :[ fileURLToPath ( new URL ( '../src/lib/router.ts' , import . meta. url ))], bundle :true , format :'esm' , platform :'node' , write :false , logLevel :'silent' , alias :{ react :fileURLToPath ( new URL ( './helpers/react-stub.mjs' , import . meta. url )) } }); 9globalThis . window ??= globalThis ; 10const { afterRouteUpdate} = await import ( `data:text/javascript;base64, ${ Buffer . from ( result . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 11 12test ( 'a held requestAnimationFrame cannot hold the route update: it resolves on the deadline' , ( t ) => { 13t . mock . timers . enable ({ apis :[ 'setTimeout' ] }); 14globalThis . requestAnimationFrame = () => 1 ; // the browser never calls back while the transition is pending 15globalThis . document = { body :{} }; 16globalThis . MutationObserver = undefined ; // nothing draws: only the deadline is left 17let done = 0 ; 18afterRouteUpdate (() => { done ++ ; }, 150 ); 19t . mock . timers . tick ( 149 ); 20assert . equal ( done , 0 ); 21t . mock . timers . tick ( 1 ); 22assert . equal ( done , 1 , 'resolved at the deadline, with no frame ever delivered' ); 23t . mock . timers . tick ( 1000 ); 24assert . equal ( done , 1 , 'resolves once' ); 25}); 26 27test ( 'the first change to the page after the hash flipped resolves it at once, and the observer is released' , ( t ) => { 28t . mock . timers . enable ({ apis :[ 'setTimeout' ] }); 29globalThis . requestAnimationFrame = () => 1 ; 30let notify ; let disconnected = false ; 31globalThis . document = { body :{} }; 32globalThis . MutationObserver = class { constructor ( fn ) { notify = fn ; } observe () {} disconnect () { disconnected = true ; } }; 33let done = 0 ; 34afterRouteUpdate (() => { done ++ ; }, 150 ); 35notify (); // React committed the new screen 36t . mock . timers . tick ( 0 ); 37assert . equal ( done , 1 , 'well before the 150 ms deadline' ); 38assert . equal ( disconnected , true ); 39t . mock . timers . tick ( 500 ); 40assert . equal ( done , 1 ); 41}); 42 43test ( 'the transition callback no longer waits on a frame' , async () => { 44const { readFileSync} = await import ( 'node:fs' ); 45const text = readFileSync ( new URL ( '../src/lib/router.ts' , import . meta. url ), 'utf8' ); 46const callback = text . slice ( text . indexOf ( 'const transition = document.startViewTransition' ), text . indexOf ( 'transition.ready.catch' )); 47assert . ok ( callback . length > 40 ); 48assert . match ( callback , / afterRouteUpdate\(resolve\) / ); 49assert . doesNotMatch ( callback , / requestAnimationFrame / ); 50});