Skip to content

Commit ad613bd

Browse files
committed
fix(runtime): keep plan misses off the host and nest journal suppress
Store sources-plan miss counts on the plan record instead of a live DOM expando, and scope journal attr suppress to the dispatching host with save/restore so nested writers still snapshot.
1 parent 6d86991 commit ad613bd

3 files changed

Lines changed: 196 additions & 24 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,16 +4550,19 @@ function journalText(node: Text, previous: string | null): void {
45504550
}
45514551

45524552
/**
4553-
* True while a spread commit's write loop runs under a journaled transaction:
4554-
* the element's own replay entry supersedes per-attribute snapshots, so the
4555-
* writers below skip their getAttribute reads. Replaying writes (a rollback
4556-
* re-running a writer) never journal either — they only append dead entries
4557-
* to a log the rollback discards after replay.
4553+
* Host whose in-flight spread/planned write owns the journal window: that
4554+
* element's replay entry supersedes per-attribute snapshots, so its writers
4555+
* skip getAttribute reads. Other hosts keep snapshotting — a nested
4556+
* custom-element callback or flushSync can write them, and those writes are
4557+
* not covered by the outer undo. Nested dispatches save/restore this slot so
4558+
* an inner finally cannot unmute the outer write. Replaying writes never
4559+
* journal either — they only append dead entries to a log the rollback
4560+
* discards after replay.
45584561
*/
4559-
let JOURNAL_ATTR_SUPPRESS = false;
4562+
let JOURNAL_ATTR_SUPPRESS_EL: Element | null = null;
45604563

45614564
function journalAttr(el: Element, name: string): void {
4562-
if (JOURNAL_ATTR_SUPPRESS || TRANSITION_JOURNAL_REPLAYING) return;
4565+
if (el === JOURNAL_ATTR_SUPPRESS_EL || TRANSITION_JOURNAL_REPLAYING) return;
45634566
TRANSITION_JOURNAL!.push(JOURNAL_ATTR, el, name, (STAGED_DOM?.view(el) ?? el).getAttribute(name));
45644567
journalBag();
45654568
}
@@ -23807,9 +23810,9 @@ const SOURCES_PLAN = /* @__PURE__ */ Symbol('octane.host-prop-sources-plan');
2380723810
* attachSourcesPlan's per-commit allocations — a source shape that never
2380823811
* stabilizes reverts to the full path for good. ponytail: poisoned is
2380923812
* permanent; a shape that stabilizes later keeps the full path (reviving it
23810-
* would take a decay or per-shape plans).
23813+
* would take a decay or per-shape plans). Count lives on the plan (`misses`),
23814+
* not on the host.
2381123815
*/
23812-
const SOURCES_PLAN_MISSES = /* @__PURE__ */ Symbol('octane.host-prop-sources-plan-misses');
2381323816
const SOURCES_PLAN_MISS_LIMIT = 3;
2381423817

2381523818
/**
@@ -23840,6 +23843,12 @@ interface ResolvedSourcesPlan {
2384023843
readonly diffIdx: number[];
2384123844
readonly diffVal: unknown[];
2384223845
diffLen: number;
23846+
/**
23847+
* Consecutive failed verifications. Lives on the plan, not the host: a DOM
23848+
* expando on the committed write path is banned, and the plan is already
23849+
* the per-element planning record.
23850+
*/
23851+
misses: number;
2384323852
}
2384423853

2384523854
/**
@@ -23898,6 +23907,7 @@ function attachSourcesPlan(
2389823907
resolved: Record<string, unknown>,
2389923908
sources: readonly HostPropSource[],
2390023909
winners: HostPropWriter[],
23910+
misses = 0,
2390123911
): void {
2390223912
let spreadIndex = -1;
2390323913
let spreadKeys: readonly string[] | null = null;
@@ -23953,6 +23963,7 @@ function attachSourcesPlan(
2395323963
diffIdx: new Array<number>(winners.length),
2395423964
diffVal: new Array<unknown>(winners.length),
2395523965
diffLen: 0,
23966+
misses,
2395623967
} satisfies ResolvedSourcesPlan,
2395723968
});
2395823969
}
@@ -24101,10 +24112,11 @@ function writePlannedDiff(
2410124112
TRANSITION_JOURNAL !== null &&
2410224113
!TRANSITION_JOURNAL_REPLAYING &&
2410324114
activeHydration() === null;
24115+
const prevSuppress = JOURNAL_ATTR_SUPPRESS_EL;
2410424116
if (journalReplay) {
2410524117
journalUndo(() => writePlannedDiff(el, prev, next, plan, scope, true));
2410624118
journalBag();
24107-
JOURNAL_ATTR_SUPPRESS = true;
24119+
JOURNAL_ATTR_SUPPRESS_EL = el;
2410824120
}
2410924121
try {
2411024122
if (!replay) stampSpreadPropFlags(el, next);
@@ -24165,7 +24177,7 @@ function writePlannedDiff(
2416524177
}
2416624178
if (process.env.NODE_ENV !== 'production' && !replay) queueDevFormDiagnostic(el, scope);
2416724179
} finally {
24168-
if (journalReplay) JOURNAL_ATTR_SUPPRESS = false;
24180+
if (journalReplay) JOURNAL_ATTR_SUPPRESS_EL = prevSuppress;
2416924181
}
2417024182
}
2417124183

@@ -24190,11 +24202,10 @@ function replaySourcesPlan(
2419024202
const plan = (prev as Record<symbol, unknown>)[SOURCES_PLAN] as ResolvedSourcesPlan | undefined;
2419124203
if (plan === undefined) return null;
2419224204
if (!scanSourcesForPlan(sources, plan, prev, readStyle)) {
24193-
const tagged = el as any;
24194-
tagged[SOURCES_PLAN_MISSES] = ((tagged[SOURCES_PLAN_MISSES] as number | undefined) ?? 0) + 1;
24205+
plan.misses++;
2419524206
return null;
2419624207
}
24197-
(el as any)[SOURCES_PLAN_MISSES] = 0;
24208+
if (plan.misses !== 0) plan.misses = 0;
2419824209
const next = buildFromSourcesPlan(plan, prev);
2419924210
if (
2420024211
next === prev &&
@@ -24424,14 +24435,15 @@ export function setHostPropSources(
2442424435
// allocations. The plan stamps the record actually retained — on an
2442524436
// unchanged commit resolvedHostPropsEqual just proved `prev` carries the
2442624437
// identical keys, so the plan replays correctly against it — rather than
24427-
// dying with a discarded `resolved`. Elements whose scans keep failing stop
24428-
// paying the attach once SOURCES_PLAN_MISS_LIMIT trips.
24429-
if (
24430-
!formHost &&
24431-
prev !== undefined &&
24432-
(((el as any)[SOURCES_PLAN_MISSES] as number | undefined) ?? 0) < SOURCES_PLAN_MISS_LIMIT
24433-
)
24434-
attachSourcesPlan(el, out!, sources, winners);
24438+
// dying with a discarded `resolved`. Consecutive scan misses live on the
24439+
// previous plan; once SOURCES_PLAN_MISS_LIMIT trips, attach stops.
24440+
const prevPlan =
24441+
prev === undefined
24442+
? undefined
24443+
: ((prev as Record<symbol, unknown>)[SOURCES_PLAN] as ResolvedSourcesPlan | undefined);
24444+
const planMisses = prevPlan?.misses ?? 0;
24445+
if (!formHost && prev !== undefined && planMisses < SOURCES_PLAN_MISS_LIMIT)
24446+
attachSourcesPlan(el, out!, sources, winners, planMisses);
2443524447
return out;
2443624448
}
2443724449

@@ -25099,7 +25111,8 @@ export function setSpread(
2509925111
setSpread(el, prev, value, mountScope, skipDangerouslySetInnerHTML, skipFormControls, true),
2510025112
);
2510125113
journalBag();
25102-
JOURNAL_ATTR_SUPPRESS = true;
25114+
const prevSuppress = JOURNAL_ATTR_SUPPRESS_EL;
25115+
JOURNAL_ATTR_SUPPRESS_EL = el;
2510325116
try {
2510425117
setSpreadBody(
2510525118
el,
@@ -25111,7 +25124,7 @@ export function setSpread(
2511125124
false,
2511225125
);
2511325126
} finally {
25114-
JOURNAL_ATTR_SUPPRESS = false;
25127+
JOURNAL_ATTR_SUPPRESS_EL = prevSuppress;
2511525128
}
2511625129
}
2511725130

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { expect, it } from 'vitest';
2+
import { setClassAttr, use, useState, useTransition } from 'octane';
3+
import { act, mount } from './_helpers';
4+
5+
declare module 'octane/jsx-runtime' {
6+
namespace Octane {
7+
namespace JSX {
8+
interface IntrinsicElements {
9+
'octane-spread-journal-host': IntrinsicElements['div'];
10+
}
11+
}
12+
}
13+
}
14+
15+
// A spread commit journals one undo for that host. A custom-element callback
16+
// can still write a *different* host through the public attribute writers;
17+
// those writes are not covered by the outer undo and must snapshot themselves.
18+
if (customElements.get('octane-spread-journal-host') === undefined) {
19+
customElements.define(
20+
'octane-spread-journal-host',
21+
class extends HTMLElement {
22+
static observedAttributes = ['data-mark'];
23+
attributeChangedCallback(_name: string, previous: string | null, value: string | null) {
24+
// Only the forward transition write. Rollback replays data-mark
25+
// back to 'old' and must not re-mutate the sibling.
26+
if (!this.isConnected || previous !== 'old' || value !== 'new') return;
27+
const sibling = document.getElementById('probe-sibling');
28+
if (sibling) setClassAttr(sibling, 'mutated');
29+
}
30+
},
31+
);
32+
}
33+
34+
interface Deferred<T> {
35+
promise: Promise<T>;
36+
resolve(value: T): void;
37+
}
38+
39+
function deferred<T>(): Deferred<T> {
40+
let resolve!: (value: T) => void;
41+
const promise = new Promise<T>((res) => {
42+
resolve = res;
43+
});
44+
return { promise, resolve };
45+
}
46+
47+
function SuspendedValue(props: { load: (step: number) => Promise<string>; step: number }) @{
48+
const value = use(props.load(props.step));
49+
<span id="value">{value as string}</span>
50+
}
51+
52+
function SpreadJournalNestedHost(props: { load: (step: number) => Promise<string> }) @{
53+
const [step, setStep] = useState(0);
54+
const [isPending, start] = useTransition();
55+
<div>
56+
<span id="pending">
57+
{isPending ? 'pending' : 'idle'}
58+
</span>
59+
<button id="bump" onClick={() => start(() => setStep(1))}>{'bump'}</button>
60+
<span id="probe-sibling" />
61+
@try {
62+
<>
63+
<octane-spread-journal-host
64+
id="probe-host"
65+
{...(step === 0
66+
? { 'data-mark': 'old', 'data-extra': 'a' }
67+
: { 'data-mark': 'new', 'data-extra': 'b' })}
68+
/>
69+
<SuspendedValue load={props.load} step={step} />
70+
</>
71+
} @pending {
72+
<span id="fallback">{'fallback'}</span>
73+
}
74+
</div>
75+
}
76+
77+
it(
78+
'restores a sibling written from a spread host callback while a transition is held',
79+
async () => {
80+
const entries = new Map<number, Deferred<string>>();
81+
const load = (step: number) => {
82+
let entry = entries.get(step);
83+
if (entry === undefined) {
84+
entry = deferred<string>();
85+
entries.set(step, entry);
86+
}
87+
return entry.promise;
88+
};
89+
load(0);
90+
entries.get(0)!.resolve('zero');
91+
92+
const r = mount(SpreadJournalNestedHost, { load });
93+
await act(() => {});
94+
try {
95+
expect(r.find('#probe-host').getAttribute('data-mark')).toBe('old');
96+
expect(r.find('#probe-sibling').getAttribute('class')).toBe(null);
97+
98+
r.click('#bump');
99+
expect(r.findAll('#fallback')).toHaveLength(0);
100+
expect(r.find('#pending').textContent).toBe('pending');
101+
expect(r.find('#probe-host').getAttribute('data-mark')).toBe('old');
102+
expect(r.find('#probe-host').getAttribute('data-extra')).toBe('a');
103+
// The callback wrote class="mutated" during the discarded transition.
104+
// Rollback must restore the sibling; the outer spread undo only covers
105+
// the host that dispatched it.
106+
expect(r.find('#probe-sibling').getAttribute('class')).toBe(null);
107+
expect(r.find('#value').textContent).toBe('zero');
108+
109+
await act(() => {
110+
entries.get(1)!.resolve('one');
111+
});
112+
expect(r.find('#probe-host').getAttribute('data-mark')).toBe('new');
113+
expect(r.find('#probe-sibling').getAttribute('class')).toBe('mutated');
114+
expect(r.find('#value').textContent).toBe('one');
115+
expect(r.find('#pending').textContent).toBe('idle');
116+
} finally {
117+
r.unmount();
118+
}
119+
},
120+
);

‎packages/octane/tests/write-path-slimming.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,45 @@ describe('compiled spread/style commits', () => {
246246
}
247247
});
248248

249+
it('planned host-prop commits do not assign own properties onto the live host', () => {
250+
const r = mount(WritePathSpread, {
251+
tick: 'a',
252+
spread: { 'data-x': '1' },
253+
});
254+
try {
255+
const el = r.find('#wp-spread');
256+
// Second commit attaches the sources plan on the resolved record.
257+
r.update(WritePathSpread, {
258+
tick: 'b',
259+
spread: { 'data-x': '2' },
260+
});
261+
expect(el.getAttribute('data-x')).toBe('2');
262+
const names = Object.getOwnPropertyNames(el);
263+
const symbols = Object.getOwnPropertySymbols(el);
264+
// Planned hit: same source shape, changed value. Planning state must
265+
// stay on the resolved record, not as a DOM expando on the element.
266+
r.update(WritePathSpread, {
267+
tick: 'c',
268+
spread: { 'data-x': '3' },
269+
});
270+
expect(el.getAttribute('data-x')).toBe('3');
271+
expect(Object.getOwnPropertyNames(el)).toEqual(names);
272+
expect(Object.getOwnPropertySymbols(el)).toEqual(symbols);
273+
// Shape miss: a new key set takes the full path. Same rule — the miss
274+
// count rides the plan, so the host's own-property set stays put.
275+
r.update(WritePathSpread, {
276+
tick: 'd',
277+
spread: { 'data-y': '4' },
278+
});
279+
expect(el.getAttribute('data-y')).toBe('4');
280+
expect(el.hasAttribute('data-x')).toBe(false);
281+
expect(Object.getOwnPropertyNames(el)).toEqual(names);
282+
expect(Object.getOwnPropertySymbols(el)).toEqual(symbols);
283+
} finally {
284+
r.unmount();
285+
}
286+
});
287+
249288
it('an unchanged spread record commits no writes; a changed data-* key commits', () => {
250289
const r = mount(WritePathSpread, {
251290
tick: 'a',

0 commit comments

Comments
 (0)