Skip to content

Commit d293b32

Browse files
committed
fix(compiler): tag state-tuple args as stale in Effect Events
staleValue only handled 'snapshot' and 'derived-state' kinds, so a state-tuple passed into an Effect Event from deferred code stayed untagged. bindTuplePattern then bound the shared tuple.snapshot without stale, and with freshCaptures set, readsBinding treated the read as current, skipping OCTANE_STRONG_STALE_STATE_UPDATE. Extend staleValue to propagate stale into the tuple's inner snapshot.
1 parent f81ccf2 commit d293b32

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,6 +3795,10 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
37953795
}
37963796

37973797
function staleValue(value) {
3798+
if (value?.kind === 'state-tuple') {
3799+
const snapshot = staleValue(value.snapshot);
3800+
return snapshot !== value.snapshot ? { ...value, snapshot } : value;
3801+
}
37983802
return (value?.kind === 'snapshot' || value?.kind === 'derived-state') && value.stale !== true
37993803
? { ...value, stale: true }
38003804
: value;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@ describe('Strong stale deferred state updates', () => {
641641
'a tuple index',
642642
`export function A({ save }) { const tuple = useState(0); return <b onClick={async () => { await save(); tuple[1](tuple[0] + 1); }}>{tuple[0]}</b>; }`,
643643
],
644+
[
645+
'a tuple passed to an Effect Event after await',
646+
`export function A({ save }) { const tuple = useState(0); const apply = useEffectEvent((t) => t[1](t[0] + 1)); return <b onClick={async () => { await save(); apply(tuple); }}>{tuple[0]}</b>; }`,
647+
],
648+
[
649+
'a destructured tuple passed to an Effect Event after await',
650+
`export function A({ save }) { const tuple = useState(0); const apply = useEffectEvent(([n, setN]) => setN(n + 1)); return <b onClick={async () => { await save(); apply(tuple); }}>{tuple[0]}</b>; }`,
651+
],
644652
])('rejects %s', (_label, body) => {
645653
expect(rejected(body)).toBe(STALE_STATE_UPDATE);
646654
});

0 commit comments

Comments
 (0)