Skip to content

Commit 7c69a60

Browse files
committed
fix(octane): wrap batchClearItems unmount+DOM in a teardown bracket
The PR moved the unmount loop before the DOM manipulation so that cleanups observe attached hosts and focusout finds them retired. Each unmountBlock call installs its own teardown bracket at TEARDOWN_DEPTH 0, so dispatchTeardownErrors fires between items—letting an ancestor @catch boundary dispose the list while batchClearItems still accesses state.start for the subsequent DOM clear. Install an outer bracket around both the unmount walk and the DOM removal, matching the pattern in resetScopeChildren and unmountBlock. The individual unmountBlock calls now find TEARDOWN_DEPTH > 0 and skip their own dispatch; errors accumulate and dispatch only after the markers are consistent.
1 parent 46a8577 commit 7c69a60

1 file changed

Lines changed: 49 additions & 33 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44806,40 +44806,56 @@ function batchClearItems(
4480644806
// nextSibling) rather than the Map's iterator: zero allocation and a
4480744807
// monomorphic pointer chase. Callers reset head/tail only AFTER this returns,
4480844808
// so the chain still covers exactly the old items here.
44809-
for (let b: Block | null = state.head; b !== null; b = b.nextSibling) {
44810-
if (b.cleanups !== null || b.children !== null || b._slots !== null) {
44811-
unmountBlock(b, false);
44809+
//
44810+
// The bracket spans the unmount walk AND the DOM removal so that a cleanup
44811+
// error is deferred until the markers are consistent: without it each
44812+
// unmountBlock installs its own bracket at TEARDOWN_DEPTH 0, and the dispatch
44813+
// between items can activate a boundary that disposes the list while the DOM
44814+
// clear still needs state.start.
44815+
const firstItem = state.head;
44816+
if (firstItem !== null && TEARDOWN_DEPTH === 0) {
44817+
TEARDOWN_HANDLER = findTryHandler(firstItem.parentBlock) ?? rendererRegionTryHandler(firstItem);
44818+
TEARDOWN_BLOCK = firstItem;
44819+
}
44820+
if (firstItem !== null) TEARDOWN_DEPTH++;
44821+
try {
44822+
for (let b: Block | null = firstItem; b !== null; b = b.nextSibling) {
44823+
if (b.cleanups !== null || b.children !== null || b._slots !== null) {
44824+
unmountBlock(b, false);
44825+
} else {
44826+
// Pure-host de-opt item (deoptItemBody with no component descendants):
44827+
// nothing to unmount scope-wise, but its subtree may carry stamped refs
44828+
// that must not keep pointing at the batch-removed DOM. Guarded so the
44829+
// common template-row clear stays a single null check; `deoptRefs`
44830+
// additionally skips the subtree scan for ref-free items.
44831+
if (b.deoptNode !== null && b.deoptRefs) detachDeoptTreeRefs(b.deoptNode, null);
44832+
b.disposed = true;
44833+
if (STAGED_COMMIT_CAPTURE) DEFERRED_LAYOUT_DRIVER!.retireHosts(b);
44834+
else b.retired = ++eventRootEpoch;
44835+
}
44836+
}
44837+
const p = domNode(state.start).parentNode!;
44838+
if (domNode(state.start).previousSibling === null && getNextSibling(state.end) === null) {
44839+
// forBlock owns the parent — nuke everything in one DOM op, then re-add markers.
44840+
(STAGED_DOM?.view(p as Element) ?? (p as Element)).textContent = '';
44841+
(STAGED_DOM?.view(p) ?? p).appendChild(state.start);
44842+
(STAGED_DOM?.view(p) ?? p).appendChild(state.end);
44843+
} else if (oldItems.size < RANGE_CLEAR_MIN_ITEMS) {
44844+
// Shared parent (other JSX interleaved) — detach the marker span directly.
44845+
// Each removal takes a whole item subtree, so this is one call per ITEM,
44846+
// not per node.
44847+
removeRange(getNextSibling(state.start), state.end);
44848+
} else if (STAGED_DOM !== null) {
44849+
STAGED_DOM.clearBetween(state.start, state.end);
4481244850
} else {
44813-
// Pure-host de-opt item (deoptItemBody with no component descendants):
44814-
// nothing to unmount scope-wise, but its subtree may carry stamped refs
44815-
// that must not keep pointing at the batch-removed DOM. Guarded so the
44816-
// common template-row clear stays a single null check; `deoptRefs`
44817-
// additionally skips the subtree scan for ref-free items.
44818-
if (b.deoptNode !== null && b.deoptRefs) detachDeoptTreeRefs(b.deoptNode, null);
44819-
b.disposed = true;
44820-
if (STAGED_COMMIT_CAPTURE) DEFERRED_LAYOUT_DRIVER!.retireHosts(b);
44821-
else b.retired = ++eventRootEpoch;
44822-
}
44823-
}
44824-
const p = domNode(state.start).parentNode!;
44825-
if (domNode(state.start).previousSibling === null && getNextSibling(state.end) === null) {
44826-
// forBlock owns the parent — nuke everything in one DOM op, then re-add markers.
44827-
(STAGED_DOM?.view(p as Element) ?? (p as Element)).textContent = '';
44828-
(STAGED_DOM?.view(p) ?? p).appendChild(state.start);
44829-
(STAGED_DOM?.view(p) ?? p).appendChild(state.end);
44830-
} else if (oldItems.size < RANGE_CLEAR_MIN_ITEMS) {
44831-
// Shared parent (other JSX interleaved) — detach the marker span directly.
44832-
// Each removal takes a whole item subtree, so this is one call per ITEM,
44833-
// not per node.
44834-
removeRange(getNextSibling(state.start), state.end);
44835-
} else if (STAGED_DOM !== null) {
44836-
STAGED_DOM.clearBetween(state.start, state.end);
44837-
} else {
44838-
// Large shared-parent clear — one bulk DOM call amortizes the Range setup.
44839-
const range = document.createRange();
44840-
range.setStartAfter(state.start);
44841-
range.setEndBefore(state.end);
44842-
range.deleteContents();
44851+
// Large shared-parent clear — one bulk DOM call amortizes the Range setup.
44852+
const range = document.createRange();
44853+
range.setStartAfter(state.start);
44854+
range.setEndBefore(state.end);
44855+
range.deleteContents();
44856+
}
44857+
} finally {
44858+
if (firstItem !== null && --TEARDOWN_DEPTH === 0) dispatchTeardownErrors();
4484344859
}
4484444860
oldItems.clear();
4484544861
}

0 commit comments

Comments
 (0)