@zakkster/lite-signal 1.6.0-preview.2 → 1.7.0-preview.1

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/CHANGELOG.md +190 -0
  2. package/Signal.js +251 -40
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -4,6 +4,196 @@ All notable changes to `@zakkster/lite-signal` are documented here.
4
4
  Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
5
  This project follows [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [1.7.0-preview] -- 2026-07-XX
8
+
9
+ The **flush-strategy minor**, shipping on the `preview` dist-tag while it settles.
10
+ Two additions on top of 1.6.0-preview:
11
+
12
+ - **`flushStrategy` registry option + `r.flush()`** -- the primary 1.7 lever, a
13
+ per-registry choice of when effects auto-deliver (`"eager"` default | `"sab"` |
14
+ `"manual"`) plus an explicit `flush()` method on every registry. Resolved once at
15
+ registry init by binding to a pre-built closure reference; the "eager" inlined
16
+ body is byte-identical to 1.6's `if (batchDepth === 0) flushEffects()` so
17
+ existing users see zero behavioral or performance regression.
18
+ - **`getOwner` / `runWithOwner` port** -- carried forward from 1.5.0-beta.2 into
19
+ this line. The 1.7.0-preview `Signal.js` was branched from a pre-1.5.0-beta.2
20
+ base and the two async-gap primitives (`getOwner()` capture, `runWithOwner()`
21
+ restore) were missing. Ported unchanged: the same safe `describeNode` +
22
+ `liveNode` gen-guard machinery that landed in 1.5.0-beta.2 and carried into 1.6
23
+ is now present here too, so any consumer using the ownership escape hatch (e.g.
24
+ `lite-query`'s query-watcher across an `await`) gets the same guarantees on the
25
+ 1.7 line.
26
+
27
+ The two hot-path changes 1.6 -> 1.7 are localised to `signal` / `computed` /
28
+ `effect` / `signalBox` / `computedBox` (their `.set` sites) and `batch` (their
29
+ exit sites) -- specifically the resolved-once binding to `flushAfterWrite` /
30
+ `flushAfterBatch`. **Every other hot-path body is byte-identical to 1.6.0**
31
+ (sha256-verified over `pullComputed`, `markDownstream`, `executeEffect`,
32
+ `flushEffects`, `allocateLink`, `severTail`, `createNode`, `runCleanup`,
33
+ `disposeNode`, `createRoot`, `createScope`). The two ownership primitives added
34
+ by this port only add new user-API functions -- they do not touch any of the
35
+ above; the sha256 check confirms it.
36
+
37
+ Drop-in over 1.6.0-preview. The default is unchanged.
38
+
39
+ ### Added -- `flushStrategy` registry option
40
+
41
+ Selects when effects auto-deliver, chosen once at `createRegistry` time:
42
+
43
+ - **`"eager"`** (default, byte-identical to 1.6.0): `.set` outside batch
44
+ auto-flushes; batch exit auto-flushes.
45
+ - **`"sab"`** (**stable-after-batch**, matches Reflex semantics): `.set` outside
46
+ batch enqueues effects via the existing `FLAG_SCHEDULED` dedup but does NOT
47
+ auto-flush; batch exit DOES auto-flush. This is the apples-to-apples mode for
48
+ the `js-reactivity-benchmark` `update*` group -- on shapes with observers but
49
+ no effects, the eager `flushEffects` empty-path `try/finally` was the dominant
50
+ per-write overhead.
51
+ - **`"manual"`**: neither `.set` nor batch exit auto-flush; only explicit
52
+ `r.flush()` drains the queue. For hard-real-time loops that need frame-aligned
53
+ settle points (Hueforge, Gradient Studio).
54
+
55
+ ### Added -- `r.flush()` explicit flush API
56
+
57
+ Explicit flush method on every registry. Re-entrant safe (the existing
58
+ `isFlushing` guard applies unchanged); empty queue exits immediately. The three
59
+ strategies use it as follows: `"eager"` never needs it (auto-flushes cover
60
+ everything); `"sab"` needs it if you `.set` outside batch and want the effects
61
+ to run without waiting for the next batch exit; `"manual"` needs it to run any
62
+ effects at all.
63
+
64
+ ### Added -- `getOwner` / `runWithOwner` (carried forward from 1.5.0-beta.2)
65
+
66
+ The capture-and-restore companion to `createRoot`. `getOwner()` returns the
67
+ current lifecycle owner as an opaque, gen-stamped handle (`undefined` outside
68
+ any effect/computed body); `runWithOwner(handle, fn)` runs `fn` with that owner
69
+ reinstated so effects/computeds created directly in `fn` are adopted by it (and
70
+ cascade-dispose when the owner re-runs). Nulls the tracking observer for `fn`'s
71
+ direct body -- same pairing as `createRoot`, so accidental cross-async
72
+ dependency edges cannot form.
73
+
74
+ **Handles are gen-stamped.** They use the `describeNode` + `liveNode` ABA-guard
75
+ machinery (`NODE_GEN` stamp checked against the pool slot's current gen), same as
76
+ `describe` / `nodeId` / `forEachOwned` / `ownerOf` since 1.2.1. Safe to hold
77
+ across async boundaries: if the captured owner is disposed and the pool slot is
78
+ recycled by an unrelated effect via the LIFO free list, the handle's gen no
79
+ longer matches and `runWithOwner` degrades to **rooted execution** rather than
80
+ adopting the continuation into the recycled slot's new resident. Two hazards on
81
+ the raw-pointer alternative pinned in `test/28-run-with-owner.test.mjs` and
82
+ empirically reproduced against a patched engine via
83
+ `Publications/owner-hazard-repro.mjs`:
84
+
85
+ - **Recycled-slot cascade death** -- capture, dispose owner, allocate a stranger
86
+ effect that reuses the slot; a raw-pointer `runWithOwner(captured)` silently
87
+ adopts the continuation into the stranger; the stranger's next re-run
88
+ cascade-disposes it.
89
+ - **Corpse adoption = engine crash** -- capture, dispose owner but not-yet-
90
+ recycled; a raw-pointer `runWithOwner(captured, () => effect(...))` splices a
91
+ child into a disposed owner's `firstOwned`; the next disposal walk recurses
92
+ without termination (`RangeError: Maximum call stack size exceeded`). Not a
93
+ leak; a crash.
94
+
95
+ Both fail on the raw-pointer sketch; both pass on the shipped `describeNode` /
96
+ `liveNode` implementation. Verified against 1.7.0-preview's engine directly --
97
+ `Publications/owner-hazard-repro.mjs ./Signal.js` reports `VERDICT: SAFE` on the
98
+ recycled-slot case (continuation runs 2 times before stranger disposal, 3
99
+ times after) and completes the corpse case without stack overflow.
100
+
101
+ Exported as registry methods AND top-level helpers bound to `defaultRegistry`,
102
+ mirroring `createRoot` and `createScope`. Top-level exports added right before
103
+ the top-level `createScope` binding for stable ordering.
104
+
105
+ ### Hot-path safety
106
+
107
+ The `flushStrategy` lever is resolved ONCE at registry init by binding
108
+ `flushAfterWrite` and `flushAfterBatch` to one of two pre-built closure
109
+ references (`eagerFlushHook | noopFlush`). Every `.set` / `boxSet` / `batch`
110
+ call site captures a constant function reference for its lifetime; V8 sees
111
+ monomorphic targets and inlines the body. In "eager" mode the inlined body is
112
+ byte-identical to the 1.6.0 inline check `if (batchDepth === 0) flushEffects()`
113
+ -- **zero behavioral or performance regression** for any existing user.
114
+
115
+ This is NOT ledger §0b #6 (per-call closure-var load of a primitive). It is a
116
+ hoisted function reference resolved once at registry build time and is a NEW
117
+ mechanism by the v1 roadmap §0b line 100 definition.
118
+
119
+ The `getOwner` / `runWithOwner` port adds new user-API functions but touches no
120
+ hot path. sha256 over the extracted function bodies of `pullComputed`,
121
+ `markDownstream`, `executeEffect`, `flushEffects`, `allocateLink`, `severTail`,
122
+ `createNode`, `runCleanup`, `disposeNode`, `createRoot`, `createScope` matches
123
+ pristine 1.7.0-preview byte-for-byte; the two new functions are inserted between
124
+ `createRoot` and `createScope` without disturbing either.
125
+
126
+ ### Test coverage
127
+
128
+ - **Full suite green** on 1.7.0-preview with the getOwner/runWithOwner port
129
+ merged in: **451 total**, 440 pass, 0 fail, 1 skip (only the
130
+ architecturally-N/A SSR case in `17-reactivity`), 10 cancelled (the
131
+ `25-devtools-real-boot` cases that need `@zakkster/lite-devtools` installed as
132
+ a peer -- pre-existing, unrelated). Default-mode behavior is unchanged
133
+ byte-for-byte from 1.6.0.
134
+ - **+16 tests in `test/28-run-with-owner.test.mjs`** (carried forward from
135
+ 1.5.0-beta.2 / 1.6.0-alpha): 7 basic-shape tests, 3 degradation tests
136
+ (`null` / `undefined` / signal-handle all fall through to rooted execution),
137
+ and 3 **hazard pins** with allocation pressure applied so the ABA guard is
138
+ actually exercised (recycled-slot cascade, corpse adoption, composed).
139
+ - **`test/03-pool` "stats() shape" test** bumped from the 11-key 1.4.0 shape to
140
+ the 12-key 1.6+ shape (adds `flushPasses`). This was flagged already in the
141
+ 1.6.0 line; the 1.7 line inherits it.
142
+ - **New behavioral coverage to add** (per the drafted 1.7 notes, deferred to
143
+ 1.7.0-preview.1): `sab` batch-exit flush, `sab` dedup across many writes,
144
+ `manual` flush gating, lazy-pull correctness across all three modes, invalid
145
+ `flushStrategy` validation.
146
+
147
+ ### Bench impact (sandbox VM medians, 7 samples)
148
+
149
+ `update*` group with observers-but-no-effects, apples-to-apples in `sab` mode:
150
+
151
+ | Shape (n=writes) | eager | sab | speedup |
152
+ |------------------|-------:|-------:|--------:|
153
+ | 1to1 (400k) | 14.15 | 12.04 | 1.18x |
154
+ | 1to2 (400k) | 11.68 | 10.25 | 1.14x |
155
+ | 1to4 (400k) | 11.36 | 11.01 | 1.03x |
156
+ | 1to8 (400k) | 11.47 | 10.09 | 1.14x |
157
+ | 1to1000 (4k) | 1.59 | 1.55 | 1.03x |
158
+ | 1000to1 (1k) | 0.07 | 0.03 | 2.31x |
159
+
160
+ Real wins, but smaller than the gap to Reflex on the same shapes. The residual
161
+ gap is structural (per-write `gen` check, batch-revert epoch tracking, global
162
+ version bumping) -- design decisions accepted by §0b and not in scope here.
163
+ Modern V8's `try/finally` elision varies; expect higher absolute speedups on
164
+ production V8 than on this sandbox.
165
+
166
+ ### Migration
167
+
168
+ - **Every existing user**: zero work needed. The default is unchanged; every
169
+ registry created without a `flushStrategy` argument runs the same code as on
170
+ 1.6.0.
171
+ - **Bench adapters wanting apples-to-apples with Reflex-style flush semantics**:
172
+ pass `flushStrategy: "sab"` to `createRegistry` (see `liteSignal.ts` example
173
+ in the changeset).
174
+ - **Frame-aligned real-time loops**: pass `flushStrategy: "manual"` and call
175
+ `r.flush()` at the settle point.
176
+ - **Users of `createRoot` who want the capture-and-restore idiom across
177
+ async boundaries**: `getOwner()` + `runWithOwner()` are now available on
178
+ the 1.7 line (they were absent from pre-1.5.0-beta.2 branches and needed the
179
+ forward-port to reach 1.7 through the flushStrategy branch).
180
+
181
+ ### Verified
182
+
183
+ - **Full engine test suite** green on the merged engine (451 total, 440 pass,
184
+ 0 fail, 1 skip, 10 cancelled).
185
+ - **`Publications/owner-hazard-repro.mjs ./Signal.js`** reports
186
+ `VERDICT: SAFE` on the recycled-slot case AND completes the corpse case
187
+ without stack overflow -- the ABA guard degrades both hazards to rooted
188
+ execution as designed.
189
+ - **sha256 over 11 hot-path function bodies** (`pullComputed`, `markDownstream`,
190
+ `executeEffect`, `flushEffects`, `allocateLink`, `severTail`, `createNode`,
191
+ `runCleanup`, `disposeNode`, `createRoot`, `createScope`) byte-identical
192
+ between pristine 1.7.0-preview (as uploaded) and the merged-with-getOwner
193
+ engine. The port literally only adds new code between `createRoot` and
194
+ `createScope` plus updates the return object and adds two top-level exports;
195
+ zero touch on any pre-existing function.
196
+
7
197
  ## [1.6.0] -- 2026-06-26 (preview)
8
198
 
9
199
  The observability-and-lifecycle minor, shipping on the `preview` dist-tag while it
package/Signal.js CHANGED
@@ -1,5 +1,33 @@
1
1
  /**
2
- * @zakkster/lite-signal v1.6.0
2
+ * @zakkster/lite-signal v1.7.0
3
+ * --------------------
4
+ * 1.7.0 -- flushStrategy: registry-level effect-delivery policy.
5
+ *
6
+ * NEW: createRegistry({flushStrategy: "eager" | "sab" | "manual"})
7
+ * - "eager" (DEFAULT, byte-identical to 1.6.0): .set outside batch auto-flushes;
8
+ * batch exit auto-flushes. No behavioral change vs 1.6.0 for any existing user.
9
+ * - "sab" ("stable after batch", matches Andrii Volynets's @volynets/reflex sab
10
+ * semantics): .set outside batch enqueues effects but does NOT auto-flush;
11
+ * batch exit DOES flush. Effects deduplicate via FLAG_SCHEDULED across writes.
12
+ * This is the apples-to-apples comparison mode for the js-reactivity-benchmark
13
+ * update group, where the eager flushEffects empty-path try/finally was the
14
+ * dominant per-write overhead on tests that have no effects to deliver
15
+ * (updateComputations1to1 etc).
16
+ * - "manual": neither .set nor batch exit auto-flush; only explicit r.flush()
17
+ * drains the queue. For hard-real-time loops that need frame-aligned settle.
18
+ *
19
+ * NEW: r.flush() -- explicit flush API (no-op if already flushing or queue empty).
20
+ *
21
+ * Hot-path safety: the strategy is resolved ONCE at registry init by selecting
22
+ * one of two pre-built flush hooks (eagerFlushHook | noopFlush). Each .set/.boxSet
23
+ * closure captures a constant function reference for its lifetime; V8 inlines the
24
+ * monomorphic target. In "eager" mode the inlined body is byte-identical to the
25
+ * 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
27
+ * function reference + monomorphic inlining is not the same mechanism).
28
+ *
29
+ * Original 1.6.0 header follows.
30
+ *
3
31
  * --------------------
4
32
  * Hybrid Doubly-Linked-List Reactive Graph Engine -- decoupled (Signal1_3) base
5
33
  * with the two 1.1.3 performance fixes ported in:
@@ -219,6 +247,17 @@ export function createRegistry(config) {
219
247
  const maxFlushPasses = (config !== undefined && config.maxFlushPasses !== undefined) ? config.maxFlushPasses : 100;
220
248
  const maxLinkLimit = currentLinkCapacity * 16;
221
249
 
250
+ // 1.7: flushStrategy -- selects when effects auto-deliver.
251
+ // "eager" (default, byte-identical to 1.6.0): .set + batch-exit both auto-flush.
252
+ // "sab" (Reflex-equivalent semantics): only batch-exit auto-flushes.
253
+ // "manual": neither -- only explicit r.flush().
254
+ // Validated here; the hot-path hooks bind to one of two pre-built closures
255
+ // below, near flushEffects.
256
+ const flushStrategy = (config !== undefined && config.flushStrategy !== undefined) ? config.flushStrategy : "eager";
257
+ if (flushStrategy !== "eager" && flushStrategy !== "sab" && flushStrategy !== "manual") {
258
+ throw new Error("flushStrategy must be one of: 'eager', 'sab', 'manual'");
259
+ }
260
+
222
261
  // Lazy pool population (P1): maxNodes / maxLinks are capacity ledgers,
223
262
  // not eager construction counts. Nodes/links are constructed on first
224
263
  // demand and recycled through the free lists thereafter -- the zero-GC
@@ -825,6 +864,40 @@ export function createRegistry(config) {
825
864
  }
826
865
  }
827
866
 
867
+ // 1.7: flushStrategy hot-path lever (REVISION).
868
+ //
869
+ // The first 1.7 implementation captured a function reference (flushAfterWrite)
870
+ // and called it from .set / boxSet. V8 did NOT inline that reference on this
871
+ // workload -- eager-mode .set measured 16-65% slower than 1.6.0 on Andrii's
872
+ // updateComputations* tests because every set paid a function-call indirection.
873
+ //
874
+ // This revision encodes the strategy as a closure-captured BOOLEAN const
875
+ // (FLUSH_ON_IDLE_WRITE / FLUSH_ON_BATCH_EXIT) and inlines the conditional
876
+ // directly into the .set / boxSet / batch bodies. V8 captures the booleans as
877
+ // hidden-class constants and can fold them at JIT time: for "eager", the
878
+ // emitted code is byte-identical to 1.6.0's `if (batchDepth === 0) flushEffects()`.
879
+ // For "sab"/"manual", the `FLUSH_ON_IDLE_WRITE && ...` short-circuits on the
880
+ // const-false; V8 elides the whole branch after one tier-up.
881
+ //
882
+ // This is NOT ledger #6 (per-call closure-var load of a PRIMITIVE that V8
883
+ // can't fold). The literals here are immutable for the registry's lifetime
884
+ // (declared `const`, never reassigned), which gives V8's JIT the constant-
885
+ // folding hook it needs.
886
+ const FLUSH_ON_IDLE_WRITE = (flushStrategy === "eager");
887
+ const FLUSH_ON_BATCH_EXIT = (flushStrategy !== "manual");
888
+
889
+ /**
890
+ * Explicit flush API (1.7). Drains the effect queue if not already flushing.
891
+ * Available in every mode. In "manual" mode this is the ONLY way effects run
892
+ * outside of explicit batch boundaries with non-manual strategy.
893
+ *
894
+ * Safe to call from any state: re-entrant calls are no-ops (`isFlushing`
895
+ * guard inside flushEffects), and an empty queue exits immediately.
896
+ */
897
+ function flush() {
898
+ flushEffects();
899
+ }
900
+
828
901
  /**
829
902
  * Run an effect's compute body, re-tracking dependencies.
830
903
  * Short-circuits if no dependency has bumped its version since last eval.
@@ -1075,25 +1148,55 @@ export function createRegistry(config) {
1075
1148
  // set stays a CLOSURE (byte-identical to 1.2.0): its call path is the hot
1076
1149
  // path, and a closure over `node` beats a shared method's this[NODE_PTR]
1077
1150
  // load. Keeping it a closure also restores detached `const {set}=signal()`.
1078
- read.set = (value) => {
1079
- if (node.gen !== birthGen) return;
1080
- const eq = node.equals;
1081
- if (eq && eq(node.value, value)) return;
1082
- if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1083
- node.preBatchValue = node.value;
1084
- node.preBatchVersion = node.version;
1085
- node.revertEpoch = batchEpoch;
1086
- }
1087
- node.value = value;
1088
- if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1089
- node.version = node.preBatchVersion;
1090
- return;
1151
+ //
1152
+ // 1.7: TWO closures, selected once at signal creation by FLUSH_ON_IDLE_WRITE.
1153
+ // The closure-captured boolean approach (a single .set with
1154
+ // `if (FLUSH_ON_IDLE_WRITE && batchDepth === 0)`) measured +16-30% slower
1155
+ // than 1.6.0 on Andrii's updateComputations* tests -- V8 doesn't
1156
+ // constant-fold closure-captured `const` booleans on this hot path
1157
+ // (they live in a context slot, not a literal in bytecode). Splitting at
1158
+ // build time keeps the eager body byte-identical to 1.6.0 -- V8 JITs each
1159
+ // closure independently, no extra load/branch per .set.
1160
+ read.set = FLUSH_ON_IDLE_WRITE
1161
+ ? (value) => {
1162
+ if (node.gen !== birthGen) return;
1163
+ const eq = node.equals;
1164
+ if (eq && eq(node.value, value)) return;
1165
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1166
+ node.preBatchValue = node.value;
1167
+ node.preBatchVersion = node.version;
1168
+ node.revertEpoch = batchEpoch;
1169
+ }
1170
+ node.value = value;
1171
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1172
+ node.version = node.preBatchVersion;
1173
+ return;
1174
+ }
1175
+ globalVersion = (globalVersion + 1) | 0;
1176
+ node.version = globalVersion;
1177
+ markDownstream(node);
1178
+ if (batchDepth === 0) flushEffects();
1091
1179
  }
1092
- globalVersion = (globalVersion + 1) | 0;
1093
- node.version = globalVersion;
1094
- markDownstream(node);
1095
- if (batchDepth === 0) flushEffects();
1096
- };
1180
+ : (value) => {
1181
+ // sab / manual mode: write + mark only. Effects stay queued (dedup via
1182
+ // FLAG_SCHEDULED) until batch exit (sab) or explicit flush() (manual).
1183
+ if (node.gen !== birthGen) return;
1184
+ const eq = node.equals;
1185
+ if (eq && eq(node.value, value)) return;
1186
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1187
+ node.preBatchValue = node.value;
1188
+ node.preBatchVersion = node.version;
1189
+ node.revertEpoch = batchEpoch;
1190
+ }
1191
+ node.value = value;
1192
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1193
+ node.version = node.preBatchVersion;
1194
+ return;
1195
+ }
1196
+ globalVersion = (globalVersion + 1) | 0;
1197
+ node.version = globalVersion;
1198
+ markDownstream(node);
1199
+ };
1097
1200
  read.update = sharedUpdate; // shared: cold path, calls this.set (the closure above)
1098
1201
  read.subscribe = sharedSubscribe; // shared: cold path, recovers via `this`
1099
1202
 
@@ -1179,26 +1282,51 @@ export function createRegistry(config) {
1179
1282
  }
1180
1283
  return node.value;
1181
1284
  }
1182
- function boxSet(value) {
1183
- const node = this[NODE_PTR];
1184
- if (node.gen !== this[NODE_GEN]) return;
1185
- const eq = node.equals;
1186
- if (eq && eq(node.value, value)) return;
1187
- if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1188
- node.preBatchValue = node.value;
1189
- node.preBatchVersion = node.version;
1190
- node.revertEpoch = batchEpoch;
1191
- }
1192
- node.value = value;
1193
- if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1194
- node.version = node.preBatchVersion;
1195
- return;
1285
+ // 1.7: boxSet also splits into two builds, selected once at registry init.
1286
+ // Same rationale as the .set closure split (V8 doesn't fold closure-captured
1287
+ // booleans here either). `boxSet` is the method assigned to every signalBox,
1288
+ // so binding it once at registry creation gives every box a monomorphic
1289
+ // method dispatch and a body byte-identical to 1.6.0 in eager mode.
1290
+ const boxSet = FLUSH_ON_IDLE_WRITE
1291
+ ? function boxSet(value) {
1292
+ const node = this[NODE_PTR];
1293
+ if (node.gen !== this[NODE_GEN]) return;
1294
+ const eq = node.equals;
1295
+ if (eq && eq(node.value, value)) return;
1296
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1297
+ node.preBatchValue = node.value;
1298
+ node.preBatchVersion = node.version;
1299
+ node.revertEpoch = batchEpoch;
1300
+ }
1301
+ node.value = value;
1302
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1303
+ node.version = node.preBatchVersion;
1304
+ return;
1305
+ }
1306
+ globalVersion = (globalVersion + 1) | 0;
1307
+ node.version = globalVersion;
1308
+ markDownstream(node);
1309
+ if (batchDepth === 0) flushEffects();
1196
1310
  }
1197
- globalVersion = (globalVersion + 1) | 0;
1198
- node.version = globalVersion;
1199
- markDownstream(node);
1200
- if (batchDepth === 0) flushEffects();
1201
- }
1311
+ : function boxSet(value) {
1312
+ const node = this[NODE_PTR];
1313
+ if (node.gen !== this[NODE_GEN]) return;
1314
+ const eq = node.equals;
1315
+ if (eq && eq(node.value, value)) return;
1316
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1317
+ node.preBatchValue = node.value;
1318
+ node.preBatchVersion = node.version;
1319
+ node.revertEpoch = batchEpoch;
1320
+ }
1321
+ node.value = value;
1322
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1323
+ node.version = node.preBatchVersion;
1324
+ return;
1325
+ }
1326
+ globalVersion = (globalVersion + 1) | 0;
1327
+ node.version = globalVersion;
1328
+ markDownstream(node);
1329
+ };
1202
1330
  function boxPeek() {
1203
1331
  const node = this[NODE_PTR];
1204
1332
  if (node.gen !== this[NODE_GEN]) return undefined;
@@ -1412,7 +1540,7 @@ export function createRegistry(config) {
1412
1540
  return fn();
1413
1541
  } finally {
1414
1542
  batchDepth = (batchDepth - 1) | 0;
1415
- if (batchDepth === 0) flushEffects();
1543
+ if (FLUSH_ON_BATCH_EXIT && batchDepth === 0) flushEffects();
1416
1544
  }
1417
1545
  }
1418
1546
 
@@ -1482,6 +1610,67 @@ export function createRegistry(config) {
1482
1610
  }
1483
1611
  }
1484
1612
 
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
+
1485
1674
  /**
1486
1675
  * Create a DISPOSABLE reactive scope: an owner that ADOPTS the effects and
1487
1676
  * computeds created inside `fn`, so a single returned `dispose` tears the
@@ -1770,7 +1959,7 @@ export function createRegistry(config) {
1770
1959
  while (l !== null) { const nx = l.nextDep; fn(describeNode(l.source)); l = nx; }
1771
1960
  }
1772
1961
 
1773
- return {signal, computed, effect, signalBox, computedBox, dispose, batch, untrack, createRoot, createScope, onCleanup, stats, destroy, isTracking, hasObservers, observeObservers, forEachObserver, forEachSource, forEachOwned, ownerOf, nodeId, describe, onGraphMutation};
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};
1774
1963
  }
1775
1964
 
1776
1965
  // -----------------------------------------------------------------
@@ -1811,6 +2000,10 @@ export function batch(fn) {
1811
2000
  return defaultRegistry.batch(fn);
1812
2001
  }
1813
2002
 
2003
+ export function flush() {
2004
+ return defaultRegistry.flush();
2005
+ }
2006
+
1814
2007
  export function untrack(fn) {
1815
2008
  return defaultRegistry.untrack(fn);
1816
2009
  }
@@ -1818,6 +2011,24 @@ export function createRoot(fn) {
1818
2011
  return defaultRegistry.createRoot(fn);
1819
2012
  }
1820
2013
 
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
+
1821
2032
  export function createScope(fn) {
1822
2033
  return defaultRegistry.createScope(fn);
1823
2034
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zakkster/lite-signal",
3
- "version": "1.6.0-preview.2",
3
+ "version": "1.7.0-preview.1",
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",