Skip to content

Commit af64484

Browse files
committed
fix(data): force urgent priority for store notifications inside transitions
Data store subscription notifications now clear the ambient transition context before scheduling, matching the urgency useSyncExternalStore provided. Without this, a write inside startTransition queued readers on the transition task instead of the microtask flush, delaying the optimistic snapshot the store had already applied.
1 parent 25ca8ed commit af64484

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎packages/octane/src/data/octane.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Relative, like the package's other entries, so the built entry resolves the
33
// same runtime module rather than the package's own source export.
44
import { createContext, createElement, use, useCallback, useSyncExternalStore } from '../index.js';
5-
import { enableNativeReadCollection } from '../runtime.js';
5+
import { enableNativeReadCollection, withUrgentPriority } from '../runtime.js';
66
import { reportNativeRead, type NativeReadSource } from '../signals/read-protocol.js';
77
import { createOctaneDataBinding } from './octane-binding.js';
88
import type { ReadHandle } from './store.js';
@@ -32,7 +32,7 @@ function observe(handle: ReadHandle<unknown>): void {
3232
}
3333
return created.version;
3434
},
35-
subscribe: (notify) => handle.subscribe(notify),
35+
subscribe: (notify) => handle.subscribe(() => withUrgentPriority(notify)),
3636
},
3737
last: handle.getSnapshot(),
3838
version: 0,

‎packages/octane/src/runtime.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export {
138138
act,
139139
useSyncExternalStore,
140140
useTransition,
141+
withUrgentPriority,
141142
} from './runtime/engine.js';
142143
export { renderWritesForbidden, drainPassiveEffects, hasPendingWork } from './runtime/effects.js';
143144
export {

‎packages/octane/src/runtime/engine.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,20 @@ export function scheduleUrgentRender(block: Block): void {
21982198
}
21992199
}
22002200

2201+
/** Run a callback outside any ambient transition context. */
2202+
export function withUrgentPriority(callback: () => void): void {
2203+
const depth = TRANSITION_DEPTH;
2204+
const asyncCount = ASYNC_TRANSITION_COUNT;
2205+
TRANSITION_DEPTH = 0;
2206+
ASYNC_TRANSITION_COUNT = 0;
2207+
try {
2208+
callback();
2209+
} finally {
2210+
TRANSITION_DEPTH = depth;
2211+
ASYNC_TRANSITION_COUNT = asyncCount;
2212+
}
2213+
}
2214+
22012215
/**
22022216
* An urgent update moves a waiting block onto the microtask flush, transition
22032217
* work included. A pending cue does not: outside a flush, the block renders its

0 commit comments

Comments
 (0)