okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Route change: the view transition's update callback no longer waits for requestAnimationFrame (#3673)642f5120a

main
2.8 KiB50 linesraw
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
2    transition'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) => {
13  t.mock.timers.enable({ apis: ['setTimeout'] });
14  globalThis.requestAnimationFrame = () => 1;                 // the browser never calls back while the transition is pending
15  globalThis.document = { body: {} };
16  globalThis.MutationObserver = undefined;                    // nothing draws: only the deadline is left
17  let done = 0;
18  afterRouteUpdate(() => { done++; }, 150);
19  t.mock.timers.tick(149);
20  assert.equal(done, 0);
21  t.mock.timers.tick(1);
22  assert.equal(done, 1, 'resolved at the deadline, with no frame ever delivered');
23  t.mock.timers.tick(1000);
24  assert.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) => {
28  t.mock.timers.enable({ apis: ['setTimeout'] });
29  globalThis.requestAnimationFrame = () => 1;
30  let notify; let disconnected = false;
31  globalThis.document = { body: {} };
32  globalThis.MutationObserver = class { constructor(fn) { notify = fn; } observe() {} disconnect() { disconnected = true; } };
33  let done = 0;
34  afterRouteUpdate(() => { done++; }, 150);
35  notify();                                                   // React committed the new screen
36  t.mock.timers.tick(0);
37  assert.equal(done, 1, 'well before the 150 ms deadline');
38  assert.equal(disconnected, true);
39  t.mock.timers.tick(500);
40  assert.equal(done, 1);
41});
42
43test('the transition callback no longer waits on a frame', async () => {
44  const { readFileSync } = await import('node:fs');
45  const text = readFileSync(new URL('../src/lib/router.ts', import.meta.url), 'utf8');
46  const callback = text.slice(text.indexOf('const transition = document.startViewTransition'), text.indexOf('transition.ready.catch'));
47  assert.ok(callback.length > 40);
48  assert.match(callback, /afterRouteUpdate\(resolve\)/);
49  assert.doesNotMatch(callback, /requestAnimationFrame/);
50});