Skip to content

Commit a1df5ca

Browse files
committed
fix(compiler): detect stale updates in deferred contexts inside Effect Events
effectEventDepth stayed elevated for the entire Effect Event visit, so checkStaleUpdate was skipped after a nested await or in a timer/promise callback scheduled from that event. Those closures still capture the render snapshot from invocation time, making setN(n + 1) after await save() or inside setTimeout a stale write. Reset effectEventDepth to 0 in two places where deferred sub-contexts are entered within an Effect Event body: - visitStatements: when transitioning to deferred phase after an await - Function visit: when visiting a function classified as an async callback (e.g. setTimeout/promise callback) Both save and restore the depth so outer Effect Event scopes remain correctly suppressed.
1 parent 7593104 commit a1df5ca

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎packages/octane/src/compiler/strong-mode.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
22222222

22232223
function visitStatements(statements, scope, phase) {
22242224
let executionPhase = phase;
2225+
let savedEffectEventDepth = -1;
22252226
for (const statement of statements ?? []) {
22262227
visit(statement, scope, executionPhase);
22272228
if (
@@ -2239,8 +2240,13 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
22392240
statementAlwaysAwaits(statement)
22402241
) {
22412242
executionPhase = 'deferred';
2243+
if (effectEventDepth > 0) {
2244+
savedEffectEventDepth = effectEventDepth;
2245+
effectEventDepth = 0;
2246+
}
22422247
}
22432248
}
2249+
if (savedEffectEventDepth !== -1) effectEventDepth = savedEffectEventDepth;
22442250
return executionPhase;
22452251
}
22462252

@@ -4118,21 +4124,21 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
41184124
// A declaration does not execute a callback. Calls and callbacks
41194125
// passed to external work are visited separately with the effect.
41204126
const enclosingEffect = currentEffect;
4127+
const enclosingEffectEventDepth = effectEventDepth;
41214128
currentEffect = null;
41224129
try {
41234130
// A nested callback runs at an unknown later time. Only timer and
41244131
// promise callbacks, like code after an await, are provably deferred.
4132+
const deferredCallback = phase !== 'module' && statePolicy.isAsynchronousCallback(node);
4133+
if (deferredCallback) effectEventDepth = 0;
41254134
visitFunction(
41264135
node,
41274136
scope,
4128-
phase === 'module'
4129-
? 'render'
4130-
: statePolicy.isAsynchronousCallback(node)
4131-
? 'deferred'
4132-
: 'event',
4137+
phase === 'module' ? 'render' : deferredCallback ? 'deferred' : 'event',
41334138
);
41344139
} finally {
41354140
currentEffect = enclosingEffect;
4141+
effectEventDepth = enclosingEffectEventDepth;
41364142
}
41374143
return;
41384144
}

‎packages/octane/tests/compiler/strong-state.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,19 @@ describe('Strong stale deferred state updates', () => {
698698
).toBeNull();
699699
});
700700

701+
it.each([
702+
[
703+
'an update after await inside an Effect Event',
704+
`export function A({ save }) { const [n, setN] = useState(0); const run = useEffectEvent(async () => { await save(); setN(n + 1); }); useEffect(() => { run(); }); return <b>{n}</b>; }`,
705+
],
706+
[
707+
'a timer callback inside an Effect Event',
708+
`export function A() { const [n, setN] = useState(0); const run = useEffectEvent(() => { setTimeout(() => setN(n + 1), 100); }); useEffect(() => { run(); }); return <b>{n}</b>; }`,
709+
],
710+
])('rejects %s', (_label, body) => {
711+
expect(rejected(body)).toBe(STALE_STATE_UPDATE);
712+
});
713+
701714
it('checks .tsrx components and plain .ts custom hooks', () => {
702715
const tsrx = `"use strong";
703716
import { useState } from 'octane';

0 commit comments

Comments
 (0)