Skip to content

Commit 7476232

Browse files
committed
fix: reject scoped updates that cross try boundaries
resolveScopedTarget could lift anonymous for-item owners past a try/Suspense boundary to a parent component, bypassing prepareOwnedUpdate's ancestor guard. Fall back to full-root replay when the path from scheduled owners to the scoped target crosses isBoundary.
1 parent bf44293 commit 7476232

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

‎packages/octane/src/universal-core.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6717,19 +6717,35 @@ class UniversalRootImpl<Container, PublicInstance> implements UniversalRoot<any>
67176717
// or several owners raised by one event scope) still has one smallest
67186718
// ancestor whose retained props and range can replay every queued update:
67196719
// the nearest enclosing component owner of their common ancestor.
6720+
// The walk must not cross a try/Suspense boundary: context and for-item
6721+
// owners have component === null, so lifting past isBoundary would select a
6722+
// parent above boundary coordination that prepareOwnedUpdate only guards on
6723+
// the target's ancestors.
67206724
private resolveScopedTarget(): UniversalOwnerRecord | undefined {
67216725
let scope: UniversalOwnerRecord | null = null;
67226726
for (const owner of this.scheduledOwners) {
67236727
if (owner.disposed) continue;
67246728
scope = scope === null ? owner : commonOwnerAncestor(scope, owner);
67256729
if (scope === null) return undefined;
67266730
}
6731+
let target: UniversalOwnerRecord | undefined;
67276732
for (let current = scope; current !== null; current = current.parent) {
6733+
if (current.isBoundary) return undefined;
67286734
if (current.component !== null && current.componentProps !== null && current.range !== null) {
6729-
return current;
6735+
target = current;
6736+
break;
6737+
}
6738+
}
6739+
if (target === undefined) return undefined;
6740+
// When the common ancestor already sits above a boundary, the upward
6741+
// walk never visits it. Reject any target that would replay across one.
6742+
for (const owner of this.scheduledOwners) {
6743+
if (owner.disposed) continue;
6744+
for (let current = owner; current !== target; current = current.parent!) {
6745+
if (current === null || current.isBoundary) return undefined;
67306746
}
67316747
}
6732-
return undefined;
6748+
return target;
67336749
}
67346750

67356751
prepare(component: UniversalComponent<any>, props: any): UniversalPreparedAttempt {

‎packages/octane/tests/universal-event-scope.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
universalFor,
1212
universalPlan,
1313
universalProps,
14+
universalTry,
1415
universalValue,
1516
useContext,
1617
useLayoutEffect,
@@ -288,6 +289,64 @@ describe('universal event scopes', () => {
288289
root.unmount();
289290
});
290291

292+
it('replays through the root when a keyed list item under a try boundary updates', () => {
293+
const container = createObjectContainer();
294+
const root = createUniversalRoot(container, createObjectDriver());
295+
const rowPlan = universalPlan('object', { kind: 'host', type: 'row', propsSlot: 0 });
296+
const shellPlan = universalPlan('object', {
297+
kind: 'host',
298+
type: 'shell',
299+
bindings: [['stamp', 0]],
300+
});
301+
const log: string[] = [];
302+
const Panel = defineUniversalComponent('object', () =>
303+
universalTry(
304+
() =>
305+
universalFor(
306+
[1, 2],
307+
(id) => id,
308+
(id) => {
309+
const [count, setCount] = useState(0, 'count');
310+
return universalValue(rowPlan, [
311+
universalProps([
312+
['set', 'value', `${id}:${count}`],
313+
['set', 'onPress', () => setCount((value) => value + 1)],
314+
]),
315+
]);
316+
},
317+
),
318+
() => universalValue(rowPlan, [universalProps([['set', 'value', 'loading']])]),
319+
),
320+
);
321+
const Shell = defineUniversalComponent('object', () => {
322+
// deps null: run on every Shell render. A scoped lift above the try
323+
// would replay Panel alone and skip this effect; full-root fallback
324+
// required for boundary coordination must re-run it.
325+
useLayoutEffect(
326+
() => {
327+
log.push('shell');
328+
return () => log.push('shell-clean');
329+
},
330+
null,
331+
'shell-every',
332+
);
333+
return [universalValue(shellPlan, ['root']), universalComponent('object', Panel)];
334+
});
335+
336+
root.render(Shell, undefined);
337+
const shell = container.children[0];
338+
const rows = container.children.filter((child) => child.type === 'row');
339+
expect(rows.map((row) => row.props.value)).toEqual(['1:0', '2:0']);
340+
expect(log).toEqual(['shell']);
341+
342+
flushUniversalSync(() => container.dispatchEvent(rows[1], 'press', {}));
343+
expect(container.children[0]).toBe(shell);
344+
expect(container.children.filter((child) => child.type === 'row')).toEqual(rows);
345+
expect(rows.map((row) => row.props.value)).toEqual(['1:0', '2:1']);
346+
expect(log).toEqual(['shell', 'shell-clean', 'shell']);
347+
root.unmount();
348+
});
349+
291350
it('applies parent and child state raised by one event in a single commit', () => {
292351
const container = createObjectContainer();
293352
const root = createUniversalRoot(container, createObjectDriver());

0 commit comments

Comments
 (0)