@zakkster/lite-signal 1.7.0-preview.1 → 1.8.0-experimental

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/Signal.js CHANGED
@@ -1,5 +1,42 @@
1
1
  /**
2
- * @zakkster/lite-signal v1.7.0
2
+ * @zakkster/lite-signal v1.8.0
3
+ * Copyright (c) Zahary Shinikchiev <shinikchiev@yahoo.com>
4
+ * MIT License
5
+ * --------------------
6
+ * 1.8.0 -- effect cleanup return (Reflex-study pattern A).
7
+ *
8
+ * NEW: effect(() => { ...; return cleanup }) -- an effect body may return a
9
+ * cleanup function (Solid/Vue/Reflex-compatible), which runs before the next
10
+ * re-run and on dispose. Composes with imperative onCleanup(fn) in call
11
+ * order. Non-function returns are ignored. Zero cost on bodies returning
12
+ * undefined (one typeof check per effect run, off the mark/pull path).
13
+ * Computeds are untouched: a computed returning a function keeps treating it
14
+ * as the VALUE.
15
+ *
16
+ * REJECTED during this cycle (ledger #13, measured): the "lean node shape"
17
+ * batch-revert side stack (Reflex pattern C). Moving the on-node
18
+ * {preBatchValue, preBatchVersion, revertEpoch} triple into a registry-level
19
+ * capture stack -- parallel arrays AND an interleaved stride-3 variant --
20
+ * held the full behavioral contract (439/439, conformance clean) and was
21
+ * VersionMatrix-neutral, but regressed capture-dense batched writes +5-9%
22
+ * and revert-heavy batches +20% in cold-process A/B (n=3, consistent).
23
+ * Family (c) loses in BOTH directions: these fields are not cold -- the
24
+ * first batched write is a hot edge, and the on-node triple already rides
25
+ * node.value's cache line, which no side structure can beat. The build is
26
+ * archived (engines/rejected/1_8_0-sidestack.js) with its probe.
27
+ *
28
+ * ALSO MEASURED (not a 1.8.0 property): the recursive-pull depth plateau is
29
+ * V8-scope-shape-sensitive -- adding ANY context slots to createRegistry
30
+ * (four dummy consts suffice) drops the cold-pull overflow point from ~4096
31
+ * to ~2559 on Node 22 by parking the probe on interpreted-size frames.
32
+ * Ceiling scales linearly with --stack-size on every build. Public claims
33
+ * quoting "~4k depth" should say "plateau-dependent, 2.5k-4k, tunable".
34
+ *
35
+ * 1.7.0 flushStrategy (eager | sab | manual) + r.flush() carry forward
36
+ * unchanged, including the two-closure .set/boxSet build.
37
+ *
38
+ * Original 1.7.0 header follows.
39
+ *
3
40
  * --------------------
4
41
  * 1.7.0 -- flushStrategy: registry-level effect-delivery policy.
5
42
  *
@@ -23,7 +60,7 @@
23
60
  * closure captures a constant function reference for its lifetime; V8 inlines the
24
61
  * monomorphic target. In "eager" mode the inlined body is byte-identical to the
25
62
  * 1.6.0 inline check (`if (batchDepth === 0) flushEffects()`), preserving the
26
- * §0b ledger #6 verdict (per-call closure-var loads are forbidden -- a hoisted
63
+ * section 0b ledger #6 verdict (per-call closure-var loads are forbidden -- a hoisted
27
64
  * function reference + monomorphic inlining is not the same mechanism).
28
65
  *
29
66
  * Original 1.6.0 header follows.
@@ -953,7 +990,22 @@ export function createRegistry(config) {
953
990
  const savedGen = node.gen;
954
991
  if (mutationHook !== null) mutationHook(5, node.id, 0);
955
992
  try {
956
- node.computeFn();
993
+ // 1.8.0 (Reflex pattern A): an effect body may RETURN a cleanup
994
+ // function -- Solid/Vue-compatible ergonomics for the existing
995
+ // imperative onCleanup(fn). The returned fn runs before the next
996
+ // re-run and on dispose, appended with the exact onCleanup append
997
+ // semantics (single fn -> pair array -> push), so the two
998
+ // registration styles compose in call order. Non-function returns
999
+ // are ignored; a self-disposed body (gen advanced) registers
1000
+ // nothing -- the slot may already host a new resident. One typeof
1001
+ // per effect run, entirely off the mark/pull path.
1002
+ const ret = node.computeFn();
1003
+ if (typeof ret === "function" && node.gen === savedGen) {
1004
+ const existing = node.cleanupFn;
1005
+ if (existing === undefined) node.cleanupFn = ret;
1006
+ else if (typeof existing === "function") node.cleanupFn = [existing, ret];
1007
+ else existing.push(ret);
1008
+ }
957
1009
  } finally {
958
1010
  if (node.gen === savedGen) {
959
1011
  severTail(node);
@@ -1610,67 +1662,6 @@ export function createRegistry(config) {
1610
1662
  }
1611
1663
  }
1612
1664
 
1613
- /**
1614
- * Capture the currently-active owner as an opaque, gen-stamped handle
1615
- * (`undefined` if called outside any effect/computed body). Companion to
1616
- * `runWithOwner`: capture the lifecycle scope now, restore it later --
1617
- * typically across an async boundary. Shipped in 1.5.0-beta.2, carried
1618
- * forward unchanged into 1.6 and 1.7.
1619
- *
1620
- * The handle is a `describeNode` descriptor stamped with the pool slot's
1621
- * generation counter. It is safe to hold across async boundaries: if the
1622
- * captured owner is disposed and its slot is subsequently recycled by an
1623
- * unrelated effect/computed, the gen no longer matches and `runWithOwner`
1624
- * treats the handle as stale (degrades to rooted execution). Same ABA guard
1625
- * used by `describe` / `nodeId` / `forEachOwned` / `ownerOf`.
1626
- *
1627
- * The handle does NOT keep the owner alive -- it is a snapshot, not a
1628
- * reference. Node lifetime is controlled by the owner tree, not by
1629
- * outstanding handles.
1630
- */
1631
- function getOwner() {
1632
- return currentOwner !== null ? describeNode(currentOwner) : undefined;
1633
- }
1634
-
1635
- /**
1636
- * Run `fn` with `ownerHandle` as the current lifecycle owner. Effects
1637
- * or computeds created directly in `fn` are adopted by that owner and
1638
- * cascade-dispose when the owner re-runs or is disposed. Shipped in
1639
- * 1.5.0-beta.2, carried forward unchanged into 1.6 and 1.7.
1640
- *
1641
- * Degradation semantics for handles that cannot be re-attached:
1642
- * - `null` / `undefined` -> rooted execution (createRoot semantics for the
1643
- * ownership axis; created children survive, caller disposes them).
1644
- * - stale handle (owner disposed, slot recycled) -> rooted execution. Never
1645
- * adopts into the unrelated new resident of the recycled slot; never
1646
- * adopts into a corpse (a disposed owner whose slot is not yet recycled).
1647
- * - handle for a live signal (not an effect/computed) -> rooted execution
1648
- * (signals cannot own).
1649
- *
1650
- * Nulls the tracking observer for the duration of `fn` (the same pairing
1651
- * as `createRoot`): reads directly inside `fn` do NOT link into any
1652
- * enclosing observer. Inner effect/computed bodies still establish their
1653
- * own owner + observer scopes.
1654
- *
1655
- * Returns whatever `fn` returns.
1656
- */
1657
- function runWithOwner(ownerHandle, fn) {
1658
- const node = liveNode(ownerHandle);
1659
- const prevOwner = currentOwner;
1660
- const prevObserver = currentObserver;
1661
- const prevTracking = isTrackingDeps;
1662
- currentOwner = (node !== undefined && (node.flags & (FLAG_COMPUTED | FLAG_EFFECT)) !== 0) ? node : null;
1663
- currentObserver = null;
1664
- isTrackingDeps = false;
1665
- try {
1666
- return fn();
1667
- } finally {
1668
- currentOwner = prevOwner;
1669
- currentObserver = prevObserver;
1670
- isTrackingDeps = prevTracking;
1671
- }
1672
- }
1673
-
1674
1665
  /**
1675
1666
  * Create a DISPOSABLE reactive scope: an owner that ADOPTS the effects and
1676
1667
  * computeds created inside `fn`, so a single returned `dispose` tears the
@@ -1959,7 +1950,7 @@ export function createRegistry(config) {
1959
1950
  while (l !== null) { const nx = l.nextDep; fn(describeNode(l.source)); l = nx; }
1960
1951
  }
1961
1952
 
1962
- return {signal, computed, effect, signalBox, computedBox, dispose, batch, flush, untrack, createRoot, getOwner, runWithOwner, createScope, onCleanup, stats, destroy, isTracking, hasObservers, observeObservers, forEachObserver, forEachSource, forEachOwned, ownerOf, nodeId, describe, onGraphMutation};
1953
+ return {signal, computed, effect, signalBox, computedBox, dispose, batch, flush, untrack, createRoot, createScope, onCleanup, stats, destroy, isTracking, hasObservers, observeObservers, forEachObserver, forEachSource, forEachOwned, ownerOf, nodeId, describe, onGraphMutation};
1963
1954
  }
1964
1955
 
1965
1956
  // -----------------------------------------------------------------
@@ -2011,24 +2002,6 @@ export function createRoot(fn) {
2011
2002
  return defaultRegistry.createRoot(fn);
2012
2003
  }
2013
2004
 
2014
- /**
2015
- * Capture the currently-active owner as an opaque, gen-stamped handle
2016
- * (undefined outside any effect/computed body). Companion to
2017
- * {@link runWithOwner}; safe to hold across async boundaries via the
2018
- * NODE_GEN ABA guard.
2019
- */
2020
- export function getOwner() {
2021
- return defaultRegistry.getOwner();
2022
- }
2023
-
2024
- /**
2025
- * Run `fn` with `ownerHandle` as the current lifecycle owner. Stale, null,
2026
- * undefined, or non-tracker handles degrade to rooted execution.
2027
- */
2028
- export function runWithOwner(ownerHandle, fn) {
2029
- return defaultRegistry.runWithOwner(ownerHandle, fn);
2030
- }
2031
-
2032
2005
  export function createScope(fn) {
2033
2006
  return defaultRegistry.createScope(fn);
2034
2007
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zakkster/lite-signal",
3
- "version": "1.7.0-preview.1",
3
+ "version": "1.8.0-experimental",
4
4
  "description": "Zero-GC reactive graph. Monomorphic object pool, versioned push-pull propagation, 32-bit modular versioning. Built for hot paths and long-running processes.",
5
5
  "author": "Zahary Shinikchiev <shinikchiev@yahoo.com>",
6
6
  "license": "MIT",