|
7 | 7 | createMemo, |
8 | 8 | createEffect, |
9 | 9 | getOwner, |
| 10 | + onCleanup, |
10 | 11 | flush, |
11 | 12 | DEV, |
12 | 13 | $DEVCOMP, |
@@ -114,10 +115,16 @@ describe("effect cleanup ordering through the dev component wrapper", () => { |
114 | 115 | // (unwind order). The transparent observedComponent root adds a nesting level at |
115 | 116 | // the same position, so DFS unwind order relative to siblings is identical |
116 | 117 | // with and without the wrapper — dev matches prod for the idiomatic 2.0 |
117 | | - // cleanup form. (Raw onCleanup in a component body still lands on the |
118 | | - // wrapper and is a known dev divergence — see #1561/#2710 assessment.) |
119 | | - // Disposal order is not a documented guarantee; this pins implementation |
120 | | - // behavior so changes to it are deliberate. |
| 118 | + // cleanup form. |
| 119 | + // |
| 120 | + // Disposal order IS documented (#3572): children before the owner's own |
| 121 | + // cleanups, and within one owner later registrations before earlier ones. |
| 122 | + // Raw `onCleanup` in a component body lands on the wrapper in dev and on |
| 123 | + // the enclosing owner in prod; the unwind rule makes the two agree when |
| 124 | + // the parent registers BEFORE creating its children (the tests below). |
| 125 | + // Two shapes remain documented divergences: a parent registering |
| 126 | + // `onCleanup` AFTER creating its children, and a parent effect-returned |
| 127 | + // cleanup vs a child body's `onCleanup` — see #1561/#2710. |
121 | 128 | test("effect-returned cleanups order the same with and without the wrapper", () => { |
122 | 129 | const run = (wrap: boolean) => { |
123 | 130 | const order: string[] = []; |
@@ -156,6 +163,58 @@ describe("effect cleanup ordering through the dev component wrapper", () => { |
156 | 163 | // Children unwind newest-first at their structural positions. |
157 | 164 | expect(dev).toEqual(["sibling:after", "child:B", "child:A", "sibling:before"]); |
158 | 165 | }); |
| 166 | + |
| 167 | + // The #3572 shapes: raw `onCleanup` in component bodies where the parent |
| 168 | + // registers before rendering its children. `wrap=true` is the dev tier |
| 169 | + // (each component gets its own transparent owner); `wrap=false` is the |
| 170 | + // flattened prod shape (every body shares the enclosing owner). Both must |
| 171 | + // unwind children-first. |
| 172 | + const unwind = (build: (call: (C: () => any) => any, order: string[]) => void) => { |
| 173 | + const run = (wrap: boolean) => { |
| 174 | + const order: string[] = []; |
| 175 | + const call = (C: () => any) => (wrap ? createComponent(C, {}) : C()); |
| 176 | + createRoot(dispose => { |
| 177 | + build(call, order); |
| 178 | + flush(); |
| 179 | + dispose(); |
| 180 | + }); |
| 181 | + flush(); |
| 182 | + return order; |
| 183 | + }; |
| 184 | + const wrapped = run(true); |
| 185 | + const flattened = run(false); |
| 186 | + expect(flattened).toEqual(wrapped); |
| 187 | + return wrapped; |
| 188 | + }; |
| 189 | + |
| 190 | + test("parent registers onCleanup then renders a child: child tears down first (wrapped and flattened)", () => { |
| 191 | + const order = unwind((call, order) => { |
| 192 | + const Child = () => (onCleanup(() => order.push("child")), null); |
| 193 | + const Parent = () => (onCleanup(() => order.push("parent")), call(Child)); |
| 194 | + call(Parent); |
| 195 | + }); |
| 196 | + expect(order).toEqual(["child", "parent"]); |
| 197 | + }); |
| 198 | + |
| 199 | + test("parent registers onCleanup then renders siblings: siblings unwind, then parent", () => { |
| 200 | + const order = unwind((call, order) => { |
| 201 | + const A = () => (onCleanup(() => order.push("A")), null); |
| 202 | + const B = () => (onCleanup(() => order.push("B")), null); |
| 203 | + const Parent = () => (onCleanup(() => order.push("parent")), [call(A), call(B)]); |
| 204 | + call(Parent); |
| 205 | + }); |
| 206 | + expect(order).toEqual(["B", "A", "parent"]); |
| 207 | + }); |
| 208 | + |
| 209 | + test("3-deep chain, each level registering before its child: innermost first", () => { |
| 210 | + const order = unwind((call, order) => { |
| 211 | + const Leaf = () => (onCleanup(() => order.push("leaf")), null); |
| 212 | + const Mid = () => (onCleanup(() => order.push("mid")), call(Leaf)); |
| 213 | + const Top = () => (onCleanup(() => order.push("top")), call(Mid)); |
| 214 | + call(Top); |
| 215 | + }); |
| 216 | + expect(order).toEqual(["leaf", "mid", "top"]); |
| 217 | + }); |
159 | 218 | }); |
160 | 219 |
|
161 | 220 | describe("$DEVCOMP marking", () => { |
|
0 commit comments