@sigil-dev/runtime 0.9.15 → 0.9.16

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.
Files changed (3) hide show
  1. package/index.test.ts +109 -0
  2. package/index.ts +50 -5
  3. package/package.json +1 -1
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,110 @@ 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("effects outside it run straight away again", () => {
2005
+ collectEffects(() => {});
2006
+ let ran = 0;
2007
+ createEffect(() => {
2008
+ ran++;
2009
+ });
2010
+ expect(ran).toBe(1);
2011
+ });
2012
+ });
2013
+
2014
+ describe("applyDirective", () => {
2015
+ test("sees its element in the document when the build is collected", () => {
2016
+ const host = document.createElement("div");
2017
+ document.body.appendChild(host);
2018
+ let connected = null as boolean | null;
2019
+ const { value: el, flush } = collectEffects(() => {
2020
+ const el = document.createElement("section");
2021
+ applyDirective(el, (node) => {
2022
+ connected = node.isConnected;
2023
+ return undefined;
2024
+ });
2025
+ return el;
2026
+ });
2027
+ expect(connected).toBeNull();
2028
+ host.appendChild(el);
2029
+ flush();
2030
+ expect(connected).toBe(true);
2031
+ host.remove();
2032
+ });
2033
+
2034
+ test("passes params, and its cleanup runs with the scope", () => {
2035
+ const calls: unknown[] = [];
2036
+ const dispose = withEffectScope(() => {
2037
+ applyDirective(document.createElement("div"), [
2038
+ (_node: Element, params: unknown) => {
2039
+ calls.push(params);
2040
+ return () => calls.push("cleanup");
2041
+ },
2042
+ { a: 1 },
2043
+ ]);
2044
+ });
2045
+ expect(calls).toEqual([{ a: 1 }]);
2046
+ dispose();
2047
+ expect(calls).toEqual([{ a: 1 }, "cleanup"]);
2048
+ });
2049
+
2050
+ test("is not set up again when a signal it read changes", () => {
2051
+ const n = createSignal(0);
2052
+ let runs = 0;
2053
+ withEffectScope(() => {
2054
+ applyDirective(document.createElement("div"), () => {
2055
+ n();
2056
+ runs++;
2057
+ return undefined;
2058
+ });
2059
+ });
2060
+ n.set(1);
2061
+ expect(runs).toBe(1);
2062
+ });
2063
+ });
package/index.ts CHANGED
@@ -425,15 +425,52 @@ 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
+ flush: () => {
465
+ for (const run of queue.splice(0)) run();
466
+ },
467
+ };
468
+ }
469
+
434
470
  // biome-ignore lint/suspicious/noConfusingVoidType: please shut up its a weird type
435
471
  export function createEffect(fn: () => void | (() => void)): () => void {
436
472
  let cleanup: (() => void) | undefined;
473
+ let disposed = false;
437
474
  const ownDeps = new Set<Set<() => void>>();
438
475
 
439
476
  const effect = () => {
@@ -457,12 +494,15 @@ export function createEffect(fn: () => void | (() => void)): () => void {
457
494
  // In deferred mode (hydration), queue the effect instead of running immediately.
458
495
  // This ensures the DOM is inserted before effect callbacks query it.
459
496
  if ((globalThis as any).__sigilDeferEffects) {
460
- deferredEffectQueue.push(() => effect());
497
+ (collectingQueue ?? deferredEffectQueue).push(() => {
498
+ if (!disposed) effect();
499
+ });
461
500
  } else {
462
501
  effect();
463
502
  }
464
503
 
465
504
  const dispose = () => {
505
+ disposed = true;
466
506
  for (const depSet of ownDeps) depSet.delete(effect);
467
507
  ownDeps.clear();
468
508
  if (cleanup) cleanup();
@@ -1175,6 +1215,11 @@ function attachHydrated(
1175
1215
  * Apply a use: directive to an element.
1176
1216
  * The directive function receives (element, params) and may return a cleanup function.
1177
1217
  * Cleanup runs when the enclosing $effect re-runs or the component is destroyed.
1218
+ *
1219
+ * It runs as an effect, so it waits with the others while hydration or a
1220
+ * client navigation holds them back: the element is in the document by the
1221
+ * time the directive sees it. Untracked, so a signal it happens to read does
1222
+ * not set it up a second time.
1178
1223
  */
1179
1224
  export type Directive = (
1180
1225
  node: Element,
@@ -1188,10 +1233,10 @@ export function applyDirective(
1188
1233
  const [fn, params] = Array.isArray(directive)
1189
1234
  ? directive
1190
1235
  : [directive, undefined];
1191
- const cleanup = fn(node, params);
1192
- if (typeof cleanup === "function") {
1193
- createEffect(() => () => cleanup());
1194
- }
1236
+ createEffect(() => {
1237
+ const cleanup = untrack(() => fn(node, params));
1238
+ return typeof cleanup === "function" ? cleanup : undefined;
1239
+ });
1195
1240
  }
1196
1241
 
1197
1242
  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.16",
6
6
  "description": "Runtime for the Sigil framework",
7
7
  "private": false,
8
8
  "devDependencies": {