@adobe/data-lit 0.10.13 → 0.10.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,244 @@
1
+ // © 2026 Adobe. MIT License. See /LICENSE for details.
2
+ // @vitest-environment happy-dom
3
+ import { describe, it, expect } from "vitest";
4
+ import { LitElement, html } from "lit";
5
+ import { attachDecorator } from "../attach-decorator.js";
6
+ import { withHooks } from "../with-hooks.js";
7
+ import { useObservable } from "../use-observable.js";
8
+ import { useState } from "../use-state.js";
9
+ import { useConnected } from "../use-connected.js";
10
+ import { useEffect } from "../use-effect.js";
11
+ /**
12
+ * Real-DOM regression tests for the two-slot hook lifecycle:
13
+ *
14
+ * - Value/subscription slots (useState, useObservable) SURVIVE a DOM move and
15
+ * are finalized only on a genuine unmount (deferred `isConnected` re-check).
16
+ * - Connectedness edges (useConnected) fire SYNCHRONOUSLY on every raw
17
+ * connect/disconnect edge — including a move — so listener/portal/drag setup
18
+ * tears down and re-attaches deterministically, with no deferral.
19
+ *
20
+ * These pin the contract that neither the 0.10.11 nuke-on-disconnect nor the
21
+ * 0.10.14 defer-everything controller could satisfy simultaneously.
22
+ */
23
+ const flushMicrotasks = () => new Promise(resolve => queueMicrotask(resolve));
24
+ /** An Observe whose subscribe/unsubscribe churn the test can inspect. */
25
+ function createTrackedObservable(initial = 0) {
26
+ const subscribers = new Set();
27
+ let subscribes = 0;
28
+ let unsubscribes = 0;
29
+ const observable = observer => {
30
+ subscribes++;
31
+ subscribers.add(observer);
32
+ observer(initial);
33
+ return () => {
34
+ unsubscribes++;
35
+ subscribers.delete(observer);
36
+ };
37
+ };
38
+ return {
39
+ observable,
40
+ subscriberCount: () => subscribers.size,
41
+ totalSubscribes: () => subscribes,
42
+ totalUnsubscribes: () => unsubscribes,
43
+ };
44
+ }
45
+ // Module-scoped probes the element writes into, so a test can read edge/render
46
+ // history without reaching into the element instance.
47
+ let connectEdges = [];
48
+ let disconnectEdges = [];
49
+ let openResources = 0;
50
+ let effectSetups = 0;
51
+ let effectCleanups = 0;
52
+ let renderCount = 0;
53
+ let observableUnderTest;
54
+ class LifecycleProbeElement extends LitElement {
55
+ constructor() {
56
+ super();
57
+ attachDecorator(this, "render", withHooks);
58
+ }
59
+ render() {
60
+ renderCount++;
61
+ // Edge primitive: must fire synchronously on every connect/disconnect edge,
62
+ // and open exactly one resource per connect (balanced by its teardown).
63
+ useConnected(() => {
64
+ connectEdges.push("connect");
65
+ openResources++;
66
+ return () => {
67
+ disconnectEdges.push("disconnect");
68
+ openResources--;
69
+ };
70
+ });
71
+ // Plain effect slot: React-parity — cleanup on finalization/dep-change,
72
+ // never on a bare disconnect (so it survives a move too).
73
+ useEffect(() => {
74
+ effectSetups++;
75
+ return () => {
76
+ effectCleanups++;
77
+ };
78
+ }, []);
79
+ // Value slot: must survive a move (retain its number across re-parenting).
80
+ const [n] = useState(() => 41);
81
+ // Subscription slot: must survive a move (no unsubscribe/re-subscribe).
82
+ const value = useObservable(observableUnderTest);
83
+ return html `<span>${n}:${value ?? ""}</span>`;
84
+ }
85
+ }
86
+ customElements.define("lifecycle-probe-element", LifecycleProbeElement);
87
+ function resetProbes(obs) {
88
+ connectEdges = [];
89
+ disconnectEdges = [];
90
+ openResources = 0;
91
+ effectSetups = 0;
92
+ effectCleanups = 0;
93
+ renderCount = 0;
94
+ observableUnderTest = obs;
95
+ }
96
+ describe("two-slot hook lifecycle — real DOM", () => {
97
+ it("useConnected fires its disconnect edge SYNCHRONOUSLY on a genuine unmount", async () => {
98
+ const { observable } = createTrackedObservable();
99
+ resetProbes(observable);
100
+ const el = document.createElement("lifecycle-probe-element");
101
+ document.body.appendChild(el);
102
+ await el.updateComplete;
103
+ expect(connectEdges).toEqual(["connect"]);
104
+ expect(disconnectEdges).toEqual([]);
105
+ el.remove();
106
+ // The edge is synchronous: no microtask flush before this assertion.
107
+ expect(disconnectEdges).toEqual(["disconnect"]);
108
+ });
109
+ it("useConnected runs setup ONCE across ordinary re-renders — no per-render leak", async () => {
110
+ const { observable } = createTrackedObservable();
111
+ resetProbes(observable);
112
+ const el = document.createElement("lifecycle-probe-element");
113
+ document.body.appendChild(el);
114
+ await el.updateComplete;
115
+ expect(connectEdges).toEqual(["connect"]);
116
+ expect(openResources).toBe(1);
117
+ // Ordinary re-renders (no connect/disconnect edge) re-run the hook body but
118
+ // must NOT re-invoke setup — the teardown handle persists across renders.
119
+ el.requestUpdate();
120
+ await el.updateComplete;
121
+ el.requestUpdate();
122
+ await el.updateComplete;
123
+ expect(connectEdges).toEqual(["connect"]);
124
+ expect(openResources).toBe(1);
125
+ // Unmount tears down exactly the one open resource — nothing leaked.
126
+ el.remove();
127
+ expect(disconnectEdges).toEqual(["disconnect"]);
128
+ expect(openResources).toBe(0);
129
+ });
130
+ it("useConnected churns disconnect→connect SYNCHRONOUSLY on a MOVE (re-parent)", async () => {
131
+ const { observable } = createTrackedObservable();
132
+ resetProbes(observable);
133
+ const parentA = document.createElement("div");
134
+ const parentB = document.createElement("div");
135
+ document.body.append(parentA, parentB);
136
+ const el = document.createElement("lifecycle-probe-element");
137
+ parentA.appendChild(el);
138
+ await el.updateComplete;
139
+ expect(connectEdges).toEqual(["connect"]);
140
+ // MOVE: disconnectedCallback then connectedCallback fire synchronously.
141
+ parentB.appendChild(el);
142
+ expect(el.isConnected).toBe(true);
143
+ // The edge primitive tore down and re-attached, synchronously and in order.
144
+ expect(disconnectEdges).toEqual(["disconnect"]);
145
+ expect(connectEdges).toEqual(["connect", "connect"]);
146
+ });
147
+ it("value + subscription slots SURVIVE a MOVE (state kept, no re-subscribe, no forced render)", async () => {
148
+ const { observable, subscriberCount, totalSubscribes, totalUnsubscribes } = createTrackedObservable();
149
+ resetProbes(observable);
150
+ const parentA = document.createElement("div");
151
+ const parentB = document.createElement("div");
152
+ document.body.append(parentA, parentB);
153
+ const el = document.createElement("lifecycle-probe-element");
154
+ parentA.appendChild(el);
155
+ await el.updateComplete;
156
+ expect(totalSubscribes()).toBe(1);
157
+ const rendersBeforeMove = renderCount;
158
+ parentB.appendChild(el);
159
+ await flushMicrotasks();
160
+ await el.updateComplete;
161
+ // Subscription untouched; useState value preserved; no re-render forced.
162
+ expect(subscriberCount()).toBe(1);
163
+ expect(totalSubscribes()).toBe(1);
164
+ expect(totalUnsubscribes()).toBe(0);
165
+ expect(renderCount).toBe(rendersBeforeMove);
166
+ });
167
+ it("finalizes on a genuine unmount — subscription disposed, no leak", async () => {
168
+ const { observable, subscriberCount, totalUnsubscribes } = createTrackedObservable();
169
+ resetProbes(observable);
170
+ const el = document.createElement("lifecycle-probe-element");
171
+ document.body.appendChild(el);
172
+ await el.updateComplete;
173
+ expect(subscriberCount()).toBe(1);
174
+ el.remove();
175
+ await flushMicrotasks();
176
+ expect(subscriberCount()).toBe(0);
177
+ expect(totalUnsubscribes()).toBe(1);
178
+ });
179
+ it("a genuine unmount→remount (across a task) re-initializes and re-subscribes the slots", async () => {
180
+ const { observable, subscriberCount, totalSubscribes } = createTrackedObservable();
181
+ resetProbes(observable);
182
+ const el = document.createElement("lifecycle-probe-element");
183
+ document.body.appendChild(el);
184
+ await el.updateComplete;
185
+ expect(totalSubscribes()).toBe(1);
186
+ // Unmount, let finalization run (task boundary), then remount.
187
+ el.remove();
188
+ await flushMicrotasks();
189
+ expect(subscriberCount()).toBe(0);
190
+ document.body.appendChild(el);
191
+ await el.updateComplete;
192
+ // Slots were finalized while detached, so the remount re-subscribed.
193
+ expect(subscriberCount()).toBe(1);
194
+ expect(totalSubscribes()).toBe(2);
195
+ // And the edge primitive reconnected.
196
+ expect(connectEdges.filter(e => e === "connect").length).toBe(2);
197
+ });
198
+ it("a genuine unmount→remount to a DIFFERENT parent (across a task) re-initializes and re-subscribes", async () => {
199
+ const { observable, subscriberCount, totalSubscribes } = createTrackedObservable();
200
+ resetProbes(observable);
201
+ const parentA = document.createElement("div");
202
+ const parentB = document.createElement("div");
203
+ document.body.append(parentA, parentB);
204
+ const el = document.createElement("lifecycle-probe-element");
205
+ parentA.appendChild(el);
206
+ await el.updateComplete;
207
+ expect(totalSubscribes()).toBe(1);
208
+ // Unmount from A, let finalization run, then remount under a DIFFERENT parent.
209
+ // The finalization gate reads `isConnected` (not "did it reconnect to the same
210
+ // node"), so a remount anywhere is handled uniformly.
211
+ el.remove();
212
+ await flushMicrotasks();
213
+ expect(subscriberCount()).toBe(0);
214
+ parentB.appendChild(el);
215
+ await el.updateComplete;
216
+ expect(subscriberCount()).toBe(1);
217
+ expect(totalSubscribes()).toBe(2);
218
+ expect(connectEdges.filter(e => e === "connect").length).toBe(2);
219
+ });
220
+ it("plain useEffect cleanup does NOT run on a MOVE, and runs (deferred) on a genuine unmount", async () => {
221
+ const { observable } = createTrackedObservable();
222
+ resetProbes(observable);
223
+ const parentA = document.createElement("div");
224
+ const parentB = document.createElement("div");
225
+ document.body.append(parentA, parentB);
226
+ const el = document.createElement("lifecycle-probe-element");
227
+ parentA.appendChild(el);
228
+ await el.updateComplete;
229
+ expect(effectSetups).toBe(1);
230
+ expect(effectCleanups).toBe(0);
231
+ // MOVE: the effect slot survives untouched — no cleanup, no re-setup.
232
+ parentB.appendChild(el);
233
+ await flushMicrotasks();
234
+ await el.updateComplete;
235
+ expect(effectSetups).toBe(1);
236
+ expect(effectCleanups).toBe(0);
237
+ // UNMOUNT: cleanup runs on the finalization microtask, not synchronously.
238
+ el.remove();
239
+ expect(effectCleanups).toBe(0);
240
+ await flushMicrotasks();
241
+ expect(effectCleanups).toBe(1);
242
+ });
243
+ });
244
+ //# sourceMappingURL=hooks-lifecycle.integration.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks-lifecycle.integration.test.js","sourceRoot":"","sources":["../../../src/hooks/component/hooks-lifecycle.integration.test.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,gCAAgC;AAChC,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,IAAI,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xB,MAAM,eAAe,EAAE,CAAC;QACxB,MAAM,EAAE,CAAC,cAAc,CAAC;QACxB,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAC7B,MAAM,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAE/B,0EAA0E;QAC1E,EAAE,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAC/B,MAAM,eAAe,EAAE,CAAC;QACxB,MAAM,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACnC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -1,2 +1,19 @@
1
1
  import type { EffectCallback } from "./use-effect.js";
2
+ /**
3
+ * Run `callback` on every DOM connect edge and its returned teardown on every
4
+ * disconnect edge — the connectedness primitive for `window` / `document`
5
+ * listeners, portals, and drag sessions that must attach and detach
6
+ * *synchronously* as the host enters and leaves the DOM.
7
+ *
8
+ * The hooks controller (see `installHooksController`) dispatches the
9
+ * `"connected"` / `"disconnected"` events synchronously on every raw edge,
10
+ * including a DOM move, so a move tears the setup down and re-attaches it
11
+ * deterministically.
12
+ *
13
+ * Setup runs exactly ONCE per connect. The teardown handle lives in a ref that
14
+ * persists across renders, so ordinary re-renders (which re-run this hook body)
15
+ * neither re-invoke setup nor leak — the setup/teardown pair is driven only from
16
+ * the edge, never from the render pass. The callback is read live through a ref,
17
+ * so callers may pass a fresh closure on every render.
18
+ */
2
19
  export declare function useConnected(callback: EffectCallback, dependencies?: unknown[]): void;
@@ -1,25 +1,53 @@
1
1
  // © 2026 Adobe. MIT License. See /LICENSE for details.
2
2
  import { useEffect } from "./use-effect.js";
3
+ import { useRef } from "./use-ref.js";
3
4
  import { Component_stack } from "./component/stack.js";
5
+ /**
6
+ * Run `callback` on every DOM connect edge and its returned teardown on every
7
+ * disconnect edge — the connectedness primitive for `window` / `document`
8
+ * listeners, portals, and drag sessions that must attach and detach
9
+ * *synchronously* as the host enters and leaves the DOM.
10
+ *
11
+ * The hooks controller (see `installHooksController`) dispatches the
12
+ * `"connected"` / `"disconnected"` events synchronously on every raw edge,
13
+ * including a DOM move, so a move tears the setup down and re-attaches it
14
+ * deterministically.
15
+ *
16
+ * Setup runs exactly ONCE per connect. The teardown handle lives in a ref that
17
+ * persists across renders, so ordinary re-renders (which re-run this hook body)
18
+ * neither re-invoke setup nor leak — the setup/teardown pair is driven only from
19
+ * the edge, never from the render pass. The callback is read live through a ref,
20
+ * so callers may pass a fresh closure on every render.
21
+ */
4
22
  export function useConnected(callback, dependencies) {
5
23
  const component = Component_stack.active();
6
- let disconnect;
7
- function onConnect() {
8
- if (!disconnect) {
9
- disconnect = callback();
10
- }
11
- }
12
- function onDisconnect() {
13
- if (disconnect) {
14
- disconnect?.();
15
- disconnect = undefined;
16
- }
17
- }
18
- // TODO
19
- if (component.isConnected) {
20
- onConnect();
21
- }
24
+ // Live-read the latest callback so a fresh closure each render is honored
25
+ // without re-registering the edge listeners.
26
+ const callbackRef = useRef(callback);
27
+ callbackRef.current = callback;
28
+ // Persistent teardown handle: the cleanup returned by `callback` while
29
+ // connected, or `undefined` while disconnected. A ref (not a per-render
30
+ // local) so the "already connected" guard holds across renders — this is
31
+ // what makes setup idempotent per connect instead of per render.
32
+ const state = useRef({ disconnect: undefined });
22
33
  useEffect(() => {
34
+ const onConnect = () => {
35
+ if (!state.current.disconnect) {
36
+ state.current.disconnect = callbackRef.current();
37
+ }
38
+ };
39
+ const onDisconnect = () => {
40
+ if (state.current.disconnect) {
41
+ state.current.disconnect();
42
+ state.current.disconnect = undefined;
43
+ }
44
+ };
45
+ // On first mount the controller dispatches "connected" during
46
+ // `addController`, before this effect attaches its listener, so run setup
47
+ // now if the host is already connected.
48
+ if (component.isConnected) {
49
+ onConnect();
50
+ }
23
51
  component.addEventListener("connected", onConnect);
24
52
  component.addEventListener("disconnected", onDisconnect);
25
53
  return () => {
@@ -1 +1 @@
1
- {"version":3,"file":"use-connected.js","sourceRoot":"","sources":["../../src/hooks/use-connected.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAGvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,MAAM,UAAU,YAAY,CAAC,QAAwB,EAAE,YAAwB;IAC3E,MAAM,SAAS,GAAG,eAAe,CAAC,MAAM,EAAE,CAAC;IAE3C,IAAI,UAA+B,CAAC;IACpC,SAAS,SAAS;QACd,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,UAAU,GAAG,QAAQ,EAAE,CAAC;QAC5B,CAAC;IACL,CAAC;IAED,SAAS,YAAY;QACjB,IAAI,UAAU,EAAE,CAAC;YACb,UAAU,EAAE,EAAE,CAAC;YACf,UAAU,GAAG,SAAS,CAAC;QAC3B,CAAC;IACL,CAAC;IAED,QAAQ;IACR,IAAI,SAAS,CAAC,WAAW,EAAE,CAAC;QACxB,SAAS,EAAE,CAAC;IAChB,CAAC;IAED,SAAS,CAAC,GAAG,EAAE;QACX,SAAS,CAAC,gBAAgB,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;QACnD,SAAS,CAAC,gBAAgB,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;QAEzD,OAAO,GAAG,EAAE;YACR,SAAS,CAAC,mBAAmB,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;YACtD,SAAS,CAAC,mBAAmB,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;YAE5D,YAAY,EAAE,CAAC;QACnB,CAAC,CAAA;IAEL,CAAC,EAAE,YAAY,CAAC,CAAC;AACrB,CAAC"}
1
+ {"version":3,"file":"use-connected.js","sourceRoot":"","sources":["../../src/hooks/use-connected.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAGvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,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AAS,CAAC,mBAAmB,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;YACtD,SAAS,CAAC,mBAAmB,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;YAC5D,YAAY,EAAE,CAAC;QACnB,CAAC,CAAC;IACN,CAAC,EAAE,YAAY,CAAC,CAAC;AACrB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/data-lit",
3
- "version": "0.10.13",
3
+ "version": "0.10.15",
4
4
  "description": "Adobe data Lit bindings - hooks, elements, decorators",
5
5
  "type": "module",
6
6
  "private": false,
@@ -12,7 +12,7 @@
12
12
  "access": "public"
13
13
  },
14
14
  "dependencies": {
15
- "@adobe/data": "0.10.13"
15
+ "@adobe/data": "0.10.15"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "lit": "^3.3.1"
@@ -17,18 +17,24 @@ import { useObservable } from "../use-observable.js";
17
17
  * `requestUpdate` on a detached element).
18
18
  */
19
19
 
20
- /** An Observe whose live subscriber count the test can inspect. */
20
+ /** An Observe whose live subscriber count and cumulative churn the test can inspect. */
21
21
  function createTrackedObservable(): {
22
22
  observable: Observe<number>;
23
23
  emit: (value: number) => void;
24
24
  subscriberCount: () => number;
25
+ totalSubscribes: () => number;
26
+ totalUnsubscribes: () => number;
25
27
  } {
26
28
  const subscribers = new Set<(value: number) => void>();
27
29
  let current = 0;
30
+ let subscribes = 0;
31
+ let unsubscribes = 0;
28
32
  const observable: Observe<number> = observer => {
33
+ subscribes++;
29
34
  subscribers.add(observer);
30
35
  observer(current);
31
36
  return () => {
37
+ unsubscribes++;
32
38
  subscribers.delete(observer);
33
39
  };
34
40
  };
@@ -39,9 +45,14 @@ function createTrackedObservable(): {
39
45
  for (const observer of subscribers) observer(value);
40
46
  },
41
47
  subscriberCount: () => subscribers.size,
48
+ totalSubscribes: () => subscribes,
49
+ totalUnsubscribes: () => unsubscribes,
42
50
  };
43
51
  }
44
52
 
53
+ /** Resolves after the current microtask checkpoint, so deferred teardown can run. */
54
+ const flushMicrotasks = (): Promise<void> => new Promise(resolve => queueMicrotask(resolve));
55
+
45
56
  class HookProbeElement extends LitElement {
46
57
  observable!: Observe<number>;
47
58
  constructor() {
@@ -76,6 +87,9 @@ describe("hook disposal on real DOM disconnect", () => {
76
87
  };
77
88
 
78
89
  el.remove();
90
+ // Teardown is deferred one microtask (so a re-parent MOVE can cancel it —
91
+ // see the move test below); a genuine unmount has no reconnect, so it runs.
92
+ await flushMicrotasks();
79
93
 
80
94
  // Disconnect ran the effect cleanup: the subscription is gone.
81
95
  expect(subscriberCount()).toBe(0);
@@ -85,4 +99,44 @@ describe("hook disposal on real DOM disconnect", () => {
85
99
  emit(99);
86
100
  expect(updatesAfterRemove).toBe(0);
87
101
  });
102
+
103
+ it("does NOT tear down and rebuild subscriptions when the element is merely MOVED", async () => {
104
+ const { observable, subscriberCount, totalSubscribes, totalUnsubscribes } = createTrackedObservable();
105
+
106
+ const parentA = document.createElement("div");
107
+ const parentB = document.createElement("div");
108
+ document.body.appendChild(parentA);
109
+ document.body.appendChild(parentB);
110
+
111
+ const el = document.createElement("hook-probe-element") as HookProbeElement;
112
+ el.observable = observable;
113
+ parentA.appendChild(el);
114
+ await el.updateComplete;
115
+ expect(subscriberCount()).toBe(1);
116
+ expect(totalSubscribes()).toBe(1);
117
+
118
+ let updatesDuringMove = 0;
119
+ const originalRequestUpdate = el.requestUpdate.bind(el);
120
+ el.requestUpdate = (...args: Parameters<HookProbeElement["requestUpdate"]>) => {
121
+ updatesDuringMove++;
122
+ originalRequestUpdate(...args);
123
+ };
124
+
125
+ // MOVE: appendChild to another already-connected parent fires
126
+ // disconnectedCallback then connectedCallback synchronously, in one task.
127
+ parentB.appendChild(el);
128
+ await flushMicrotasks();
129
+ await el.updateComplete;
130
+
131
+ // The element is still mounted and its subscription is intact...
132
+ expect(el.isConnected).toBe(true);
133
+ expect(subscriberCount()).toBe(1);
134
+ // ...and, crucially, the move neither disposed nor re-created it. Before the
135
+ // deferred-disposal fix, a move disposed and re-subscribed (2 subs / 1 unsub)
136
+ // and forced an extra re-render — the source of the studio-page lag, churn,
137
+ // and transient empty lists on project switch (FFP-111900).
138
+ expect(totalUnsubscribes()).toBe(0);
139
+ expect(totalSubscribes()).toBe(1);
140
+ expect(updatesDuringMove).toBe(0);
141
+ });
88
142
  });
@@ -7,8 +7,10 @@ import { installHooksController } from "./hooks-controller.js";
7
7
  /**
8
8
  * Minimal stand-in for a Lit host: an EventTarget that captures the controller
9
9
  * installed on it so the test can drive `hostConnected` / `hostDisconnected`
10
- * directly (a real Lit element would need a DOM). `addController` mirrors Lit's
11
- * contract of firing `hostConnected` synchronously when already connected.
10
+ * directly (a real Lit element would need a DOM). `connect()` / `disconnect()`
11
+ * flip `isConnected` BEFORE dispatching, mirroring the DOM (the callback runs
12
+ * after connectedness has already changed) — the controller's deferred
13
+ * finalization reads `isConnected` to tell a move from a genuine unmount.
12
14
  */
13
15
  class FakeHost extends EventTarget implements Component {
14
16
  isConnected = true;
@@ -21,17 +23,21 @@ class FakeHost extends EventTarget implements Component {
21
23
  addController(controller: ReactiveController) {
22
24
  this.controllers.push(controller);
23
25
  }
24
- // Simulate Lit dispatching lifecycle to every registered controller.
25
26
  connect() {
27
+ this.isConnected = true;
26
28
  for (const c of this.controllers) c.hostConnected?.();
27
29
  }
28
30
  disconnect() {
31
+ this.isConnected = false;
29
32
  for (const c of this.controllers) c.hostDisconnected?.();
30
33
  }
31
34
  }
32
35
 
36
+ /** Resolves after the current microtask checkpoint, so deferred finalization can run. */
37
+ const flushMicrotasks = (): Promise<void> => new Promise(resolve => queueMicrotask(resolve));
38
+
33
39
  describe("installHooksController", () => {
34
- it("disposes every effect hook and resets the cursor on disconnect", () => {
40
+ it("finalizes every effect hook and resets the cursor on a genuine disconnect", async () => {
35
41
  const host = new FakeHost();
36
42
  const disposeA = vi.fn();
37
43
  const disposeB = vi.fn();
@@ -45,6 +51,9 @@ describe("installHooksController", () => {
45
51
 
46
52
  installHooksController(host);
47
53
  host.disconnect();
54
+ // Finalization is deferred one microtask (so a move can cancel it); with no
55
+ // reconnect the host is still disconnected, so it runs on the next checkpoint.
56
+ await flushMicrotasks();
48
57
 
49
58
  expect(disposeA).toHaveBeenCalledTimes(1);
50
59
  expect(disposeB).toHaveBeenCalledTimes(1);
@@ -52,7 +61,7 @@ describe("installHooksController", () => {
52
61
  expect(host.hookIndex).toBe(0);
53
62
  });
54
63
 
55
- it("dispatches connected / disconnected events across the lifecycle", () => {
64
+ it("fires the connect / disconnect EDGE synchronously — no microtask needed", () => {
56
65
  const host = new FakeHost();
57
66
  const onConnected = vi.fn();
58
67
  const onDisconnected = vi.fn();
@@ -64,10 +73,68 @@ describe("installHooksController", () => {
64
73
  expect(onConnected).toHaveBeenCalledTimes(1);
65
74
 
66
75
  host.disconnect();
76
+ // No flush: the edge is synchronous so connectedness hooks (useConnected)
77
+ // tear down inline. Under the old defer-everything controller this event
78
+ // only fired on a later microtask.
67
79
  expect(onDisconnected).toHaveBeenCalledTimes(1);
68
80
  });
69
81
 
70
- it("forces a re-render only on RE-connect, not the first connect", () => {
82
+ it("fires disconnect THEN connect synchronously across a MOVE, and does NOT finalize", async () => {
83
+ const host = new FakeHost();
84
+ const dispose = vi.fn();
85
+ host.hooks = [{ dispose, dependencies: [] }];
86
+ host.hookIndex = 1;
87
+ const edges: string[] = [];
88
+ host.addEventListener("disconnected", () => edges.push("disconnect"));
89
+ host.addEventListener("connected", () => edges.push("connect"));
90
+
91
+ installHooksController(host);
92
+ host.requestUpdate.mockClear();
93
+
94
+ // Move: disconnect immediately followed by reconnect, same task.
95
+ host.disconnect();
96
+ host.connect();
97
+ expect(edges).toEqual(["disconnect", "connect"]); // both edges, synchronous, in order
98
+
99
+ await flushMicrotasks();
100
+ // The slot survived: not disposed, and no re-render was forced.
101
+ expect(dispose).not.toHaveBeenCalled();
102
+ expect(host.hooks).toHaveLength(1);
103
+ expect(host.requestUpdate).not.toHaveBeenCalled();
104
+ });
105
+
106
+ it("a rapid disconnect→connect→disconnect bounce finalizes exactly once (net state wins)", async () => {
107
+ const host = new FakeHost();
108
+ const dispose = vi.fn();
109
+ host.hooks = [{ dispose, dependencies: [] }];
110
+
111
+ installHooksController(host);
112
+ // Bounce within one task: only one finalization microtask is queued, and it
113
+ // reads the final isConnected (false) → finalizes once, not per disconnect.
114
+ host.disconnect();
115
+ host.connect();
116
+ host.disconnect();
117
+ await flushMicrotasks();
118
+
119
+ expect(dispose).toHaveBeenCalledTimes(1);
120
+ expect(host.hooks).toEqual([]);
121
+ });
122
+
123
+ it("gates finalization on isConnected — a slot that reconnects before the microtask survives", async () => {
124
+ const host = new FakeHost();
125
+ const dispose = vi.fn();
126
+ host.hooks = [{ dispose, dependencies: [] }];
127
+
128
+ installHooksController(host);
129
+ host.disconnect(); // schedules finalization; isConnected now false
130
+ host.connect(); // reconnects in the same task; isConnected back to true
131
+ await flushMicrotasks();
132
+
133
+ expect(dispose).not.toHaveBeenCalled();
134
+ expect(host.hooks).toHaveLength(1);
135
+ });
136
+
137
+ it("forces a re-render only on a genuine RE-connect, not the first connect or a move", async () => {
71
138
  const host = new FakeHost();
72
139
  installHooksController(host);
73
140
 
@@ -75,7 +142,8 @@ describe("installHooksController", () => {
75
142
  expect(host.requestUpdate).not.toHaveBeenCalled();
76
143
 
77
144
  host.disconnect();
78
- host.connect(); // reconnect
145
+ await flushMicrotasks(); // genuine unmount: finalization runs
146
+ host.connect(); // reconnect must re-initialize the finalized hooks
79
147
  expect(host.requestUpdate).toHaveBeenCalledTimes(1);
80
148
  });
81
149