Skip to content

Commit df5bd81

Browse files
committed
fix(octane): defer transition followup dispatch in wrapResume until flush
adoptTransitionQueue dispatches TRANSITION_FOLLOWUPS as microtasks under the contract that the caller flushes synchronously in the same turn. wrapResume breaks that contract on two paths: 1. When waiting on in-flight view transition handles (promise-deferred), followups run as microtasks before the promise resolves. 2. When vtNativeAvailable returns false, followups run before the scheduled flush microtask. Save and clear TRANSITION_FOLLOWUPS before adoptTransitionQueue, then dispatch them at each exit path's correct flush point: - Urgent/cascade path: queued before synchronous work()+flushWork(). - Promise-deferred path: re-injected into TRANSITION_FOLLOWUPS for the recursive wrapResume's adopt to dispatch at the right time. - !vtNativeAvailable path: queued after vtScheduleQueuedWork() so they run after the scheduled flush. - vtFlush path: queued before the synchronous vtFlush call.
1 parent 2d993e4 commit df5bd81

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7051,6 +7051,13 @@ function ensureViewTransitionDriver(): ViewTransitionDriver {
70517051
// then include every sibling update those passives scheduled. Transitions
70527052
// waiting for their task join too, so one view transition captures them.
70537053
const passivesSettled = vtDrainPassivesBeforeCapture();
7054+
// adoptTransitionQueue dispatches TRANSITION_FOLLOWUPS as microtasks
7055+
// under the contract that the caller flushes synchronously in the same
7056+
// turn. wrapResume may wait on in-flight handles or return without
7057+
// flushing, so save and clear followups here, then dispatch them at
7058+
// each exit path's flush point.
7059+
const savedFollowups = TRANSITION_FOLLOWUPS;
7060+
TRANSITION_FOLLOWUPS = null;
70547061
adoptTransitionQueue();
70557062
const capture = VT_CAPTURE;
70567063
const resumed = getBlocks?.() ?? [];
@@ -7061,14 +7068,20 @@ function ensureViewTransitionDriver(): ViewTransitionDriver {
70617068
// can touch further owners, so only that fallback interrupts all scopes.
70627069
if (passivesSettled) vtInterruptOwners(owners);
70637070
else vtInterrupt();
7071+
if (savedFollowups !== null)
7072+
for (let i = 0; i < savedFollowups.length; i++) queueMicrotask(savedFollowups[i]);
70647073
work();
70657074
flushWork();
70667075
return true;
70677076
}
70687077
const handles = vtActiveHandles(owners);
70697078
if (capture !== null || handles.size > 0) {
70707079
vtWaitForHandles(handles, owners);
7080+
const deferred = savedFollowups;
70717081
const resume = (): void => {
7082+
// Re-inject so the recursive wrapResume's adopt dispatches
7083+
// them at the right point before its own flush.
7084+
if (deferred !== null) (TRANSITION_FOLLOWUPS ??= []).push(...deferred);
70727085
if (!driver.wrapResume(work, getBlocks)) work();
70737086
};
70747087
const waits: Promise<void>[] = [];
@@ -7079,9 +7092,14 @@ function ensureViewTransitionDriver(): ViewTransitionDriver {
70797092
}
70807093
if (!vtNativeAvailable(owners)) {
70817094
// The caller commits only the resume; adopted transitions still need a flush.
7095+
// Queue followups after the scheduled flush microtask so they run after it.
70827096
vtScheduleQueuedWork();
7097+
if (savedFollowups !== null)
7098+
for (let i = 0; i < savedFollowups.length; i++) queueMicrotask(savedFollowups[i]);
70837099
return false;
70847100
}
7101+
if (savedFollowups !== null)
7102+
for (let i = 0; i < savedFollowups.length; i++) queueMicrotask(savedFollowups[i]);
70857103
vtFlush(
70867104
() => {
70877105
work();

0 commit comments

Comments
 (0)