@sigil-dev/runtime 0.9.15 → 0.9.17

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.
package/index.test.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { describe, expect, test } from "bun:test";
2
2
  import {
3
+ applyDirective,
3
4
  batch,
4
5
  claim,
5
6
  claimComment,
6
7
  claimText,
8
+ collectEffects,
7
9
  createEffect,
8
10
  createEffectPre,
9
11
  createInspectEffect,
@@ -1952,3 +1954,136 @@ describe("object signal read from another module", () => {
1952
1954
  expect(store.peek()).toEqual({ a: 2 });
1953
1955
  });
1954
1956
  });
1957
+
1958
+ describe("collectEffects", () => {
1959
+ test("holds effects until flush, then runs them once", () => {
1960
+ const ran: string[] = [];
1961
+ const { value, flush } = collectEffects(() => {
1962
+ createEffect(() => {
1963
+ ran.push("a");
1964
+ });
1965
+ return 7;
1966
+ });
1967
+ expect(value).toBe(7);
1968
+ expect(ran).toEqual([]);
1969
+ flush();
1970
+ expect(ran).toEqual(["a"]);
1971
+ flush();
1972
+ expect(ran).toEqual(["a"]);
1973
+ });
1974
+
1975
+ test("an effect disposed before the flush never runs", () => {
1976
+ let ran = 0;
1977
+ let dispose = () => {};
1978
+ const { flush } = collectEffects(() => {
1979
+ dispose = withEffectScope(() => {
1980
+ createEffect(() => {
1981
+ ran++;
1982
+ });
1983
+ });
1984
+ });
1985
+ dispose();
1986
+ flush();
1987
+ expect(ran).toBe(0);
1988
+ });
1989
+
1990
+ test("two collections keep their own effects", () => {
1991
+ const ran: string[] = [];
1992
+ const first = collectEffects(() =>
1993
+ createEffect(() => void ran.push("first")),
1994
+ );
1995
+ const second = collectEffects(() =>
1996
+ createEffect(() => void ran.push("second")),
1997
+ );
1998
+ second.flush();
1999
+ expect(ran).toEqual(["second"]);
2000
+ first.flush();
2001
+ expect(ran).toEqual(["second", "first"]);
2002
+ });
2003
+
2004
+ test("an effect that throws doesn't stop the others", () => {
2005
+ const ran: string[] = [];
2006
+ const { flush } = collectEffects(() => {
2007
+ createEffect(() => {
2008
+ throw new Error("boom");
2009
+ });
2010
+ createEffect(() => void ran.push("after"));
2011
+ });
2012
+ const original = globalThis.queueMicrotask;
2013
+ const reported: unknown[] = [];
2014
+ globalThis.queueMicrotask = (fn: () => void) => {
2015
+ try {
2016
+ fn();
2017
+ } catch (e) {
2018
+ reported.push(e);
2019
+ }
2020
+ };
2021
+ try {
2022
+ flush();
2023
+ } finally {
2024
+ globalThis.queueMicrotask = original;
2025
+ }
2026
+ expect(ran).toEqual(["after"]);
2027
+ expect(String(reported[0])).toContain("boom");
2028
+ });
2029
+
2030
+ test("effects outside it run straight away again", () => {
2031
+ collectEffects(() => {});
2032
+ let ran = 0;
2033
+ createEffect(() => {
2034
+ ran++;
2035
+ });
2036
+ expect(ran).toBe(1);
2037
+ });
2038
+ });
2039
+
2040
+ describe("applyDirective", () => {
2041
+ test("sees its element in the document when the build is collected", () => {
2042
+ const host = document.createElement("div");
2043
+ document.body.appendChild(host);
2044
+ let connected = null as boolean | null;
2045
+ const { value: el, flush } = collectEffects(() => {
2046
+ const el = document.createElement("section");
2047
+ applyDirective(el, (node) => {
2048
+ connected = node.isConnected;
2049
+ return undefined;
2050
+ });
2051
+ return el;
2052
+ });
2053
+ expect(connected).toBeNull();
2054
+ host.appendChild(el);
2055
+ flush();
2056
+ expect(connected).toBe(true);
2057
+ host.remove();
2058
+ });
2059
+
2060
+ test("passes params, and its cleanup runs with the scope", () => {
2061
+ const calls: unknown[] = [];
2062
+ const dispose = withEffectScope(() => {
2063
+ applyDirective(document.createElement("div"), [
2064
+ (_node: Element, params: unknown) => {
2065
+ calls.push(params);
2066
+ return () => calls.push("cleanup");
2067
+ },
2068
+ { a: 1 },
2069
+ ]);
2070
+ });
2071
+ expect(calls).toEqual([{ a: 1 }]);
2072
+ dispose();
2073
+ expect(calls).toEqual([{ a: 1 }, "cleanup"]);
2074
+ });
2075
+
2076
+ test("is not set up again when a signal it read changes", () => {
2077
+ const n = createSignal(0);
2078
+ let runs = 0;
2079
+ withEffectScope(() => {
2080
+ applyDirective(document.createElement("div"), () => {
2081
+ n();
2082
+ runs++;
2083
+ return undefined;
2084
+ });
2085
+ });
2086
+ n.set(1);
2087
+ expect(runs).toBe(1);
2088
+ });
2089
+ });
package/index.ts CHANGED
@@ -425,15 +425,62 @@ export function createProfilerEffect(
425
425
  // Deferred effect mode for hydration: effects created while the flag is set
426
426
  // are queued instead of run immediately, then flushed after DOM insertion.
427
427
  const deferredEffectQueue: (() => void)[] = [];
428
+ // the queue of the innermost collectEffects() call, when there is one
429
+ let collectingQueue: (() => void)[] | null = null;
428
430
 
429
431
  export function flushDeferredEffects(): void {
430
432
  const pending = deferredEffectQueue.splice(0);
431
433
  for (const run of pending) run();
432
434
  }
433
435
 
436
+ /**
437
+ * Run fn() with every effect it creates held back, and return its value with
438
+ * a flush() that runs them. For building DOM that is inserted later: a client
439
+ * navigation builds the next page while the old one is still showing, and an
440
+ * effect (or a use: directive) that measures or queries its element needs it
441
+ * in the document. The queue belongs to this call, so two navigations in
442
+ * flight cannot run each other's effects; an effect disposed before the
443
+ * flush (its page was replaced first) never runs.
444
+ */
445
+ export function collectEffects<T>(fn: () => T): {
446
+ value: T;
447
+ flush: () => void;
448
+ } {
449
+ const g = globalThis as any;
450
+ const prevFlag = g.__sigilDeferEffects;
451
+ const prevQueue = collectingQueue;
452
+ const queue: (() => void)[] = [];
453
+ collectingQueue = queue;
454
+ g.__sigilDeferEffects = true;
455
+ let value: T;
456
+ try {
457
+ value = fn();
458
+ } finally {
459
+ g.__sigilDeferEffects = prevFlag;
460
+ collectingQueue = prevQueue;
461
+ }
462
+ return {
463
+ value,
464
+ // one effect that throws must not stop the rest (or the page swap the
465
+ // flush runs in); the error is still reported, as an uncaught one
466
+ flush: () => {
467
+ for (const run of queue.splice(0)) {
468
+ try {
469
+ run();
470
+ } catch (e) {
471
+ queueMicrotask(() => {
472
+ throw e;
473
+ });
474
+ }
475
+ }
476
+ },
477
+ };
478
+ }
479
+
434
480
  // biome-ignore lint/suspicious/noConfusingVoidType: please shut up its a weird type
435
481
  export function createEffect(fn: () => void | (() => void)): () => void {
436
482
  let cleanup: (() => void) | undefined;
483
+ let disposed = false;
437
484
  const ownDeps = new Set<Set<() => void>>();
438
485
 
439
486
  const effect = () => {
@@ -457,12 +504,15 @@ export function createEffect(fn: () => void | (() => void)): () => void {
457
504
  // In deferred mode (hydration), queue the effect instead of running immediately.
458
505
  // This ensures the DOM is inserted before effect callbacks query it.
459
506
  if ((globalThis as any).__sigilDeferEffects) {
460
- deferredEffectQueue.push(() => effect());
507
+ (collectingQueue ?? deferredEffectQueue).push(() => {
508
+ if (!disposed) effect();
509
+ });
461
510
  } else {
462
511
  effect();
463
512
  }
464
513
 
465
514
  const dispose = () => {
515
+ disposed = true;
466
516
  for (const depSet of ownDeps) depSet.delete(effect);
467
517
  ownDeps.clear();
468
518
  if (cleanup) cleanup();
@@ -1175,6 +1225,11 @@ function attachHydrated(
1175
1225
  * Apply a use: directive to an element.
1176
1226
  * The directive function receives (element, params) and may return a cleanup function.
1177
1227
  * Cleanup runs when the enclosing $effect re-runs or the component is destroyed.
1228
+ *
1229
+ * It runs as an effect, so it waits with the others while hydration or a
1230
+ * client navigation holds them back: the element is in the document by the
1231
+ * time the directive sees it. Untracked, so a signal it happens to read does
1232
+ * not set it up a second time.
1178
1233
  */
1179
1234
  export type Directive = (
1180
1235
  node: Element,
@@ -1188,10 +1243,10 @@ export function applyDirective(
1188
1243
  const [fn, params] = Array.isArray(directive)
1189
1244
  ? directive
1190
1245
  : [directive, undefined];
1191
- const cleanup = fn(node, params);
1192
- if (typeof cleanup === "function") {
1193
- createEffect(() => () => cleanup());
1194
- }
1246
+ createEffect(() => {
1247
+ const cleanup = untrack(() => fn(node, params));
1248
+ return typeof cleanup === "function" ? cleanup : undefined;
1249
+ });
1195
1250
  }
1196
1251
 
1197
1252
  export type ContextKey<T> = symbol & { __type: T };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@sigil-dev/runtime",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "0.9.15",
5
+ "version": "0.9.17",
6
6
  "description": "Runtime for the Sigil framework",
7
7
  "private": false,
8
8
  "devDependencies": {
package/test-preload.ts CHANGED
@@ -7,3 +7,5 @@ const window = new Window();
7
7
  (globalThis as any).Node = window.Node;
8
8
  (globalThis as any).HTMLElement = window.HTMLElement;
9
9
  (globalThis as any).Comment = window.Comment;
10
+ // events the tests dispatch must be the DOM's own: happy-dom refuses Bun's
11
+ (globalThis as any).Event = window.Event;