@solidjs/signals 2.0.0-rc.2 → 2.0.0-rc.20
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/dist/dev-shared.js +6505 -0
- package/dist/dev.attribution.js +3484 -0
- package/dist/dev.js +10227 -9508
- package/dist/observe/affects-marks.js +86 -0
- package/dist/observe/affects.js +112 -0
- package/dist/observe/attribution.js +7 -0
- package/dist/observe/boundaries.js +696 -0
- package/dist/observe/core/action.js +182 -0
- package/dist/observe/core/async.js +824 -0
- package/dist/observe/core/attribution-costs.js +66 -0
- package/dist/observe/core/attribution-feedback.js +280 -0
- package/dist/observe/core/attribution-hooks.js +83 -0
- package/dist/observe/core/attribution-queries.js +43 -0
- package/dist/observe/core/attribution.js +3079 -0
- package/dist/observe/core/constants.js +269 -0
- package/dist/observe/core/context.js +63 -0
- package/dist/observe/core/copies.js +10 -0
- package/dist/observe/core/core.js +1707 -0
- package/dist/observe/core/dev.js +394 -0
- package/dist/observe/core/effect.js +193 -0
- package/dist/observe/core/error-hooks.js +71 -0
- package/dist/observe/core/error.js +82 -0
- package/dist/observe/core/external.js +97 -0
- package/dist/observe/core/graph.js +162 -0
- package/dist/observe/core/heap.js +124 -0
- package/dist/observe/core/identities.js +62 -0
- package/dist/observe/core/invariants.js +12 -0
- package/dist/observe/core/lanes.js +972 -0
- package/dist/observe/core/owner.js +371 -0
- package/dist/observe/core/scheduler.js +1157 -0
- package/dist/observe/core/truth.js +47 -0
- package/dist/observe/core/verdict.js +470 -0
- package/dist/observe/deferred.js +186 -0
- package/dist/observe/flatten.js +79 -0
- package/dist/observe/from.js +248 -0
- package/dist/observe/index.js +71 -0
- package/dist/observe/map.js +593 -0
- package/dist/observe/reveal.js +212 -0
- package/dist/observe/signals.js +717 -0
- package/dist/observe/store/affects.js +142 -0
- package/dist/observe/store/edges.js +39 -0
- package/dist/observe/store/hooks.js +16 -0
- package/dist/observe/store/index.js +47 -0
- package/dist/observe/store/optimistic.js +713 -0
- package/dist/observe/store/projection.js +345 -0
- package/dist/observe/store/reconcile.js +281 -0
- package/dist/observe/store/store.js +2127 -0
- package/dist/observe/store/storePath.js +103 -0
- package/dist/observe/store/target.js +46 -0
- package/dist/observe/store/types.js +83 -0
- package/dist/observe/store/utils.js +1063 -0
- package/dist/prod/affects-marks.js +86 -0
- package/dist/prod/affects.js +98 -112
- package/dist/prod/attribution.js +45 -0
- package/dist/prod/boundaries.js +578 -468
- package/dist/prod/core/action.js +92 -59
- package/dist/prod/core/async.js +522 -336
- package/dist/prod/core/attribution-hooks.js +3 -0
- package/dist/prod/core/constants.js +168 -44
- package/dist/prod/core/context.js +12 -16
- package/dist/prod/core/copies.js +10 -0
- package/dist/prod/core/core.js +1345 -627
- package/dist/prod/core/dev.js +123 -1
- package/dist/prod/core/effect.js +113 -80
- package/dist/prod/core/error-hooks.js +71 -0
- package/dist/prod/core/error.js +18 -4
- package/dist/prod/core/external.js +6 -7
- package/dist/prod/core/graph.js +119 -72
- package/dist/prod/core/heap.js +85 -109
- package/dist/prod/core/identities.js +62 -0
- package/dist/prod/core/invariants.js +1 -32
- package/dist/prod/core/lanes.js +931 -132
- package/dist/prod/core/owner.js +192 -129
- package/dist/prod/core/scheduler.js +948 -642
- package/dist/prod/core/truth.js +47 -0
- package/dist/prod/core/verdict.js +409 -420
- package/dist/prod/deferred.js +184 -0
- package/dist/prod/flatten.js +77 -0
- package/dist/prod/from.js +248 -0
- package/dist/prod/index.js +40 -16
- package/dist/prod/map.js +481 -211
- package/dist/prod/reveal.js +212 -0
- package/dist/prod/signals.js +401 -84
- package/dist/prod/store/affects.js +142 -0
- package/dist/prod/store/edges.js +39 -0
- package/dist/prod/store/hooks.js +16 -0
- package/dist/prod/store/index.js +27 -18
- package/dist/prod/store/optimistic.js +707 -0
- package/dist/prod/store/projection.js +331 -0
- package/dist/prod/store/reconcile.js +281 -0
- package/dist/prod/store/store.js +1952 -267
- package/dist/prod/store/storePath.js +1 -1
- package/dist/prod/store/target.js +43 -0
- package/dist/prod/store/types.js +83 -0
- package/dist/prod/store/utils.js +997 -138
- package/dist/types/affects-marks.d.ts +14 -0
- package/dist/types/affects.d.ts +12 -30
- package/dist/types/attribution.d.ts +19 -0
- package/dist/types/attribution.prod.d.ts +22 -0
- package/dist/types/boundaries.d.ts +99 -124
- package/dist/types/carved.d.ts +1 -0
- package/dist/types/core/action.d.ts +23 -9
- package/dist/types/core/async.d.ts +34 -6
- package/dist/types/core/attribution-costs.d.ts +35 -0
- package/dist/types/core/attribution-feedback.d.ts +133 -0
- package/dist/types/core/attribution-hooks.d.ts +307 -8
- package/dist/types/core/attribution-queries.d.ts +24 -0
- package/dist/types/core/attribution.d.ts +802 -72
- package/dist/types/core/constants.d.ts +195 -38
- package/dist/types/core/copies.d.ts +54 -0
- package/dist/types/core/core.d.ts +295 -65
- package/dist/types/core/dev.d.ts +509 -22
- package/dist/types/core/effect.d.ts +1 -1
- package/dist/types/core/error-hooks.d.ts +71 -0
- package/dist/types/core/error.d.ts +18 -1
- package/dist/types/core/graph.d.ts +40 -1
- package/dist/types/core/heap.d.ts +5 -5
- package/dist/types/core/identities.d.ts +17 -0
- package/dist/types/core/index.d.ts +11 -7
- package/dist/types/core/invariants.d.ts +1 -26
- package/dist/types/core/lanes.d.ts +74 -43
- package/dist/types/core/owner.d.ts +27 -9
- package/dist/types/core/scheduler.d.ts +353 -102
- package/dist/types/core/truth.d.ts +18 -0
- package/dist/types/core/types.d.ts +97 -71
- package/dist/types/core/verdict.d.ts +35 -0
- package/dist/types/deferred.d.ts +28 -0
- package/dist/types/flatten.d.ts +8 -0
- package/dist/types/from.d.ts +47 -0
- package/dist/types/index.d.ts +33 -7
- package/dist/types/map.d.ts +4 -2
- package/dist/types/reveal.d.ts +19 -0
- package/dist/types/signals.d.ts +196 -45
- package/dist/types/store/affects.d.ts +12 -0
- package/dist/types/store/edges.d.ts +9 -0
- package/dist/types/store/hooks.d.ts +62 -0
- package/dist/types/store/index.d.ts +42 -15
- package/dist/types/store/optimistic.d.ts +24 -45
- package/dist/types/store/projection.d.ts +17 -68
- package/dist/types/store/reconcile.d.ts +3 -46
- package/dist/types/store/store.d.ts +133 -136
- package/dist/types/store/storePath.d.ts +1 -1
- package/dist/types/store/target.d.ts +166 -0
- package/dist/types/store/types.d.ts +105 -0
- package/dist/types/store/utils.d.ts +188 -0
- package/package.json +31 -26
- package/dist/node.cjs +0 -8745
- package/dist/prod/core/optimistic.js +0 -265
- package/dist/prod/store/next/optimistic.js +0 -385
- package/dist/prod/store/next/projection.js +0 -172
- package/dist/prod/store/next/reconcile.js +0 -331
- package/dist/prod/store/next/store.js +0 -1499
- package/dist/prod/store/next/target.js +0 -20
- package/dist/types/core/optimistic.d.ts +0 -6
- package/dist/types/store/next/optimistic.d.ts +0 -20
- package/dist/types/store/next/projection.d.ts +0 -8
- package/dist/types/store/next/reconcile.d.ts +0 -3
- package/dist/types/store/next/store.d.ts +0 -77
- package/dist/types/store/next/target.d.ts +0 -117
- package/dist/types-cjs/affects.d.cts +0 -47
- package/dist/types-cjs/boundaries.d.cts +0 -175
- package/dist/types-cjs/core/action.d.cts +0 -64
- package/dist/types-cjs/core/async.d.cts +0 -21
- package/dist/types-cjs/core/attribution-hooks.d.cts +0 -52
- package/dist/types-cjs/core/attribution.d.cts +0 -186
- package/dist/types-cjs/core/constants.d.cts +0 -97
- package/dist/types-cjs/core/context.d.cts +0 -36
- package/dist/types-cjs/core/core.d.cts +0 -163
- package/dist/types-cjs/core/dev.d.cts +0 -81
- package/dist/types-cjs/core/effect.d.cts +0 -30
- package/dist/types-cjs/core/error.d.cts +0 -47
- package/dist/types-cjs/core/external.d.cts +0 -15
- package/dist/types-cjs/core/graph.d.cts +0 -6
- package/dist/types-cjs/core/heap.d.cts +0 -22
- package/dist/types-cjs/core/index.d.cts +0 -13
- package/dist/types-cjs/core/invariants.d.cts +0 -59
- package/dist/types-cjs/core/lanes.d.cts +0 -44
- package/dist/types-cjs/core/optimistic.d.cts +0 -6
- package/dist/types-cjs/core/owner.d.cts +0 -124
- package/dist/types-cjs/core/scheduler.d.cts +0 -180
- package/dist/types-cjs/core/types.d.cts +0 -192
- package/dist/types-cjs/core/verdict.d.cts +0 -2
- package/dist/types-cjs/index.d.cts +0 -10
- package/dist/types-cjs/map.d.cts +0 -69
- package/dist/types-cjs/package.json +0 -3
- package/dist/types-cjs/signals.d.cts +0 -624
- package/dist/types-cjs/store/index.d.cts +0 -19
- package/dist/types-cjs/store/next/optimistic.d.cts +0 -20
- package/dist/types-cjs/store/next/projection.d.cts +0 -8
- package/dist/types-cjs/store/next/reconcile.d.cts +0 -3
- package/dist/types-cjs/store/next/store.d.cts +0 -77
- package/dist/types-cjs/store/next/target.d.cts +0 -117
- package/dist/types-cjs/store/optimistic.d.cts +0 -45
- package/dist/types-cjs/store/projection.d.cts +0 -70
- package/dist/types-cjs/store/reconcile.d.cts +0 -46
- package/dist/types-cjs/store/store.d.cts +0 -138
- package/dist/types-cjs/store/storePath.d.cts +0 -58
- package/dist/types-cjs/store/utils.d.cts +0 -74
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown by a tracked read whose value is currently pending (an async memo /
|
|
3
|
+
* `createSignal(asyncFn)` / projection / store derivation that hasn't settled
|
|
4
|
+
* yet). Surfacing through the reactive graph is what suspends the consumer
|
|
5
|
+
* scope — the nearest enclosing `<Loading>` boundary catches the throw and
|
|
6
|
+
* renders its fallback until the source resolves.
|
|
7
|
+
*
|
|
8
|
+
* App code rarely catches this directly; `<Loading>` is the canonical
|
|
9
|
+
* handler. The error type is exposed for advanced cases — e.g. interop layers
|
|
10
|
+
* that bridge Solid's pending-throw protocol to a different async strategy,
|
|
11
|
+
* or tests that want to assert on the suspension shape.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // Advanced: distinguish "not ready yet" from a real error in custom
|
|
16
|
+
* // boundary plumbing. App code should rely on `<Loading>` / `<Errored>`.
|
|
17
|
+
* try {
|
|
18
|
+
* const value = readReactiveSource();
|
|
19
|
+
* } catch (err) {
|
|
20
|
+
* if (err instanceof NotReadyError) throw err; // re-throw to suspend
|
|
21
|
+
* reportError(err);
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
class NotReadyError extends Error {
|
|
26
|
+
constructor(r) {
|
|
27
|
+
// Control-flow throw: it happens on every read of a pending source, so in
|
|
28
|
+
// production skip V8's eager stack capture (proportional to stack depth —
|
|
29
|
+
// real cost under SSR) by zeroing the V8-specific stackTraceLimit around
|
|
30
|
+
// super(). Dev keeps the stack for debuggability; non-V8 engines (no
|
|
31
|
+
// stackTraceLimit) take the plain path.
|
|
32
|
+
const o = Error;
|
|
33
|
+
const t = o.stackTraceLimit;
|
|
34
|
+
if (t !== undefined) o.stackTraceLimit = 0;
|
|
35
|
+
super();
|
|
36
|
+
if (t !== undefined) o.stackTraceLimit = t;
|
|
37
|
+
this.source = r;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class StatusError extends Error {
|
|
42
|
+
constructor(r, o) {
|
|
43
|
+
// Best-effort message: formatting must never replace the original (#4112).
|
|
44
|
+
try {
|
|
45
|
+
var t = String(o instanceof Error ? o.message : o);
|
|
46
|
+
} catch {}
|
|
47
|
+
super(t, {
|
|
48
|
+
cause: o
|
|
49
|
+
});
|
|
50
|
+
this.source = r;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Return the user's error from an internal status wrapper. */ function unwrapStatusError(r) {
|
|
55
|
+
return r instanceof StatusError ? r.cause : r;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Rejection value of `until(fn, { timeout })` when the predicate does not turn
|
|
60
|
+
* truthy within the window. Inside an `action()`, the rejection is thrown back
|
|
61
|
+
* in at the `yield` point — catchable there, or the action fails and its
|
|
62
|
+
* optimistic state reverts.
|
|
63
|
+
*/ class TimeoutError extends Error {
|
|
64
|
+
constructor(r = "Timed out waiting for condition") {
|
|
65
|
+
super(r);
|
|
66
|
+
this.name = "TimeoutError";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
class NoOwnerError extends Error {
|
|
71
|
+
constructor() {
|
|
72
|
+
super("");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
class ContextNotFoundError extends Error {
|
|
77
|
+
constructor() {
|
|
78
|
+
super("");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { ContextNotFoundError, NoOwnerError, NotReadyError, StatusError, TimeoutError, unwrapStatusError };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { signal, setSignal, read } from "./core.js";
|
|
2
|
+
|
|
3
|
+
import { cleanup } from "./owner.js";
|
|
4
|
+
|
|
5
|
+
import { setExternalHooks } from "./scheduler.js";
|
|
6
|
+
|
|
7
|
+
let externalSourceConfig = null;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Registers a factory that bridges external reactive systems (e.g. MobX, Vue refs)
|
|
11
|
+
* into Solid's tracking graph. Every computation will be wrapped so that the
|
|
12
|
+
* external library can track its own dependencies alongside Solid's.
|
|
13
|
+
*
|
|
14
|
+
* Multiple calls pipe together: each new factory wraps the previous one.
|
|
15
|
+
*
|
|
16
|
+
* @param config.factory receives `(fn, trigger)` — wrap fn execution in external tracking,
|
|
17
|
+
* call trigger when external deps change. Return `{ track, dispose }`.
|
|
18
|
+
* @param config.untrack optional wrapper for `untrack` — disables external tracking too.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* // Bridge an external "subscribe / notify" library into Solid's graph.
|
|
23
|
+
* // `factory` wraps every Solid compute so the external library can attach
|
|
24
|
+
* // its own dependency tracker; `trigger` re-runs the compute on external
|
|
25
|
+
* // change. `untrack` mirrors Solid's `untrack()` into the external library
|
|
26
|
+
* // so that reads inside `untrack(...)` don't get tracked twice.
|
|
27
|
+
* enableExternalSource({
|
|
28
|
+
* factory: (compute, trigger) => {
|
|
29
|
+
* const sub = externalLib.subscribe(trigger);
|
|
30
|
+
* return {
|
|
31
|
+
* track: prev => externalLib.run(() => compute(prev)),
|
|
32
|
+
* dispose: () => sub.unsubscribe()
|
|
33
|
+
* };
|
|
34
|
+
* },
|
|
35
|
+
* untrack: fn => externalLib.untracked(fn)
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
// Wires a freshly created computed through the active external-source bridge.
|
|
40
|
+
// Lives here (installed as a scheduler hook while a config is active) rather than
|
|
41
|
+
// inline in core: esbuild cannot literal-track the mutable config binding the
|
|
42
|
+
// way rollup does, so an inline `if (externalSourceConfig)` block ships in
|
|
43
|
+
// every bundle even though only enableExternalSource() can make it reachable.
|
|
44
|
+
function wireExternalSource(e) {
|
|
45
|
+
const n = signal(undefined, {
|
|
46
|
+
equals: false,
|
|
47
|
+
ownedWrite: true
|
|
48
|
+
});
|
|
49
|
+
const r = externalSourceConfig.factory(e.X, () => {
|
|
50
|
+
setSignal(n, undefined);
|
|
51
|
+
});
|
|
52
|
+
cleanup(() => r.dispose());
|
|
53
|
+
e.X = e => {
|
|
54
|
+
read(n);
|
|
55
|
+
return r.track(e);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function externalUntrack(e) {
|
|
60
|
+
return externalSourceConfig.untrack(e);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The hooks mirror the config's liveness exactly (installed on enable,
|
|
64
|
+
// removed on reset) so core's presence checks stay equivalent to the old
|
|
65
|
+
// `externalSourceConfig` truthiness checks.
|
|
66
|
+
function syncExternalHooks() {
|
|
67
|
+
if (externalSourceConfig) setExternalHooks(wireExternalSource, externalUntrack); else setExternalHooks(undefined, undefined);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function enableExternalSource(e) {
|
|
71
|
+
const {factory: n, untrack: r = e => e()} = e;
|
|
72
|
+
if (externalSourceConfig) {
|
|
73
|
+
const {factory: e, untrack: o} = externalSourceConfig;
|
|
74
|
+
externalSourceConfig = {
|
|
75
|
+
factory: (r, o) => {
|
|
76
|
+
const t = e(r, o);
|
|
77
|
+
const a = n(e => t.track(e), o);
|
|
78
|
+
return {
|
|
79
|
+
track: e => a.track(e),
|
|
80
|
+
dispose() {
|
|
81
|
+
a.dispose();
|
|
82
|
+
t.dispose();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
untrack: e => o(() => r(e))
|
|
87
|
+
};
|
|
88
|
+
} else {
|
|
89
|
+
externalSourceConfig = {
|
|
90
|
+
factory: n,
|
|
91
|
+
untrack: r
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
syncExternalHooks();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export { enableExternalSource, externalSourceConfig };
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { NewSet, REACTIVE_RECOMPUTING_DEPS, CONFIG_AUTO_DISPOSE, STATUS_PENDING, REACTIVE_DISPOSED, REACTIVE_ZOMBIE, CONFIG_SLOT_NODE } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
import { deleteFromHeap } from "./heap.js";
|
|
4
|
+
|
|
5
|
+
import { disposeChildren, runCleanup } from "./owner.js";
|
|
6
|
+
|
|
7
|
+
import { bumpNotifyEpoch, dirtyQueue } from "./scheduler.js";
|
|
8
|
+
|
|
9
|
+
/** The store's shared slot-node release handler (store/store.ts installs it
|
|
10
|
+
* once) — a live binding read directly by the sweep site: no wrapper frame,
|
|
11
|
+
* no null check (a CONFIG_SLOT_NODE node existing implies the store module
|
|
12
|
+
* loaded and registered it). */ let slotUnobservedHook;
|
|
13
|
+
|
|
14
|
+
function setSlotUnobserved(e) {
|
|
15
|
+
slotUnobservedHook = e;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// https://github.com/stackblitz/alien-signals/blob/v2.0.3/src/system.ts#L100
|
|
19
|
+
function unlinkSubs(e) {
|
|
20
|
+
const n = e.S;
|
|
21
|
+
const l = e.G;
|
|
22
|
+
const o = e.t;
|
|
23
|
+
const s = e.en;
|
|
24
|
+
if (o !== null) o.en = s; else n.Ot = s;
|
|
25
|
+
if (s !== null) s.t = o; else {
|
|
26
|
+
n.o = o;
|
|
27
|
+
if (o === null) {
|
|
28
|
+
// Store slot nodes dispatch to the ONE shared hook — no per-node
|
|
29
|
+
// unobserved closure, no NodeExtension to hold it.
|
|
30
|
+
// A user hook that throws is collected like a cleanup (owner.ts): the
|
|
31
|
+
// unlink and the teardown around it carry on.
|
|
32
|
+
if (n.l & CONFIG_SLOT_NODE) slotUnobservedHook(n); else if (n.T?.Gt) runCleanup(n.T.Gt);
|
|
33
|
+
// No more subscribers; only tear down if CONFIG_AUTO_DISPOSE is set.
|
|
34
|
+
// A pending node is exempt: its in-flight async work is an observer —
|
|
35
|
+
// tearing down would orphan the work and re-execute it on the next
|
|
36
|
+
// read. The settle path runs this same last-one-out check when that
|
|
37
|
+
// observer releases (the untracked-read dormancy sweep guards on
|
|
38
|
+
// pending identically).
|
|
39
|
+
// During a pass's teardown of its unfinished frame, the release waits
|
|
40
|
+
// for the pass to end (`deferredReleases`, #4142).
|
|
41
|
+
const e = n;
|
|
42
|
+
e.X && e.l & CONFIG_AUTO_DISPOSE && !(e.R & REACTIVE_ZOMBIE) && !(e.m & STATUS_PENDING) && (deferredReleases ? deferredReleases.push(e) : unobserved(e));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return l;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function trimStaleDeps(e) {
|
|
49
|
+
const n = e._;
|
|
50
|
+
let l = n !== null ? n.G : e.C;
|
|
51
|
+
if (l !== null) {
|
|
52
|
+
do {
|
|
53
|
+
l = unlinkSubs(l);
|
|
54
|
+
} while (l !== null);
|
|
55
|
+
if (n !== null) n.G = null; else e.C = null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Shared by unobserved() and the disposeChildren child loop. The truthy guard
|
|
60
|
+
// (not `!== null`) matters: plain Owners in a child chain have no _deps field,
|
|
61
|
+
// and skipping early also avoids adding one (hidden-class churn) via the
|
|
62
|
+
// null-out below.
|
|
63
|
+
function clearDeps(e) {
|
|
64
|
+
let n = e.C;
|
|
65
|
+
if (!n) return;
|
|
66
|
+
do {
|
|
67
|
+
n = unlinkSubs(n);
|
|
68
|
+
} while (n !== null);
|
|
69
|
+
e.C = null;
|
|
70
|
+
e._ = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Last-one-out releases held back while a pass tears down the frame of an
|
|
75
|
+
* earlier pass that never committed (#4142). The pass is about to rebuild
|
|
76
|
+
* the same children, which read the same lazy nodes again. Released on the
|
|
77
|
+
* spot, a lazy async memo whose only reader was the old frame's child memo
|
|
78
|
+
* was torn down, re-ran on the new child's read, went pending, and the cycle
|
|
79
|
+
* repeated forever. `recompute` collects them around that teardown and
|
|
80
|
+
* releases what is still unobserved when the pass ends.
|
|
81
|
+
*/ let deferredReleases = null;
|
|
82
|
+
|
|
83
|
+
const deferReleases = e => deferredReleases = e;
|
|
84
|
+
|
|
85
|
+
function unobserved(e) {
|
|
86
|
+
deleteFromHeap(e, dirtyQueue);
|
|
87
|
+
clearDeps(e);
|
|
88
|
+
disposeChildren(e, true);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Deferred dormancy for never-observed auto-dispose computeds (#3078).
|
|
93
|
+
*
|
|
94
|
+
* An untracked top-level read of a subscriber-less observation-lifecycle memo
|
|
95
|
+
* used to call unobserved() inline at the end of read(). That kept the leak
|
|
96
|
+
* closed (the compute links the memo into its deps' sub lists — without a
|
|
97
|
+
* teardown point a never-observed memo is retained by its sources forever;
|
|
98
|
+
* upstream alien-signals has exactly this retention), but it made reads
|
|
99
|
+
* destructive: each read disposed the node, the next read revived it with a
|
|
100
|
+
* full recompute in whatever ambient transition/lane context happened to be
|
|
101
|
+
* current, so consecutive reads could return different answers with no write
|
|
102
|
+
* in between.
|
|
103
|
+
*
|
|
104
|
+
* Instead, reads queue the node here and the scheduler sweeps at the top of
|
|
105
|
+
* the next flush (before runHeap, so a same-tick dirtying is reclaimed
|
|
106
|
+
* instead of recomputed). Reads become idempotent within a tick (the node
|
|
107
|
+
* stays alive and serves its cache, uniform with observed memos) while
|
|
108
|
+
* reclamation still happens within one microtask — the enqueue site arms
|
|
109
|
+
* schedule(), so a flush is guaranteed even when no other work is queued.
|
|
110
|
+
*/ const dormantNodes = new NewSet;
|
|
111
|
+
|
|
112
|
+
function sweepDormant() {
|
|
113
|
+
if (dormantNodes.size === 0) return;
|
|
114
|
+
for (const e of dormantNodes) {
|
|
115
|
+
// Re-validate at sweep time: the node may have gained a subscriber (its
|
|
116
|
+
// lifecycle is the unlinkSubs cascade now), gone pending (in-flight async
|
|
117
|
+
// is an observer; the settle path re-runs last-one-out), lost its
|
|
118
|
+
// AUTO_DISPOSE bit (owner teardown strips it, #3024), or already been
|
|
119
|
+
// torn down.
|
|
120
|
+
if (!e.o && e.l & CONFIG_AUTO_DISPOSE && !(e.m & STATUS_PENDING) && !(e.R & (REACTIVE_DISPOSED | REACTIVE_ZOMBIE))) {
|
|
121
|
+
unobserved(e);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
dormantNodes.clear();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// https://github.com/stackblitz/alien-signals/blob/v2.0.3/src/system.ts#L52
|
|
128
|
+
function link(e, n) {
|
|
129
|
+
const l = n._;
|
|
130
|
+
if (l !== null && l.S === e) return;
|
|
131
|
+
let o = null;
|
|
132
|
+
const s = n.R & REACTIVE_RECOMPUTING_DEPS;
|
|
133
|
+
if (s) {
|
|
134
|
+
o = l !== null ? l.G : n.C;
|
|
135
|
+
if (o !== null && o.S === e) {
|
|
136
|
+
o.de = n.Ne;
|
|
137
|
+
n._ = o;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// A link stamped with the current pass generation was created or reused
|
|
142
|
+
// in-order during this recompute, i.e. it already sits in the validated
|
|
143
|
+
// [deps.._depsTail] prefix — the O(1) equivalent of scanning the dep list
|
|
144
|
+
// (the old alien-signals `isValidLink` walk, O(n²) when a computation
|
|
145
|
+
// re-reads earlier deps non-consecutively, e.g. store leaf reads).
|
|
146
|
+
const r = e.Ot;
|
|
147
|
+
if (r !== null && r.u === n && (!s || r.de === n.Ne)) return;
|
|
148
|
+
const t = n._ = e.Ot = {
|
|
149
|
+
S: e,
|
|
150
|
+
u: n,
|
|
151
|
+
G: o,
|
|
152
|
+
en: r,
|
|
153
|
+
t: null,
|
|
154
|
+
de: n.Ne
|
|
155
|
+
};
|
|
156
|
+
if (l !== null) l.G = t; else n.C = t;
|
|
157
|
+
if (r !== null) r.t = t; else e.o = t;
|
|
158
|
+
// New subscriber edge: staged-rewrite skips (§12d) must not miss it.
|
|
159
|
+
bumpNotifyEpoch();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export { clearDeps, deferReleases, deferredReleases, dormantNodes, link, setSlotUnobserved, slotUnobservedHook, sweepDormant, trimStaleDeps, unlinkSubs, unobserved };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import "./core.js";
|
|
2
|
+
|
|
3
|
+
import { REACTIVE_IN_HEAP, REACTIVE_RECOMPUTING_DEPS, REACTIVE_CHECK, REACTIVE_DIRTY, REACTIVE_IN_HEAP_HEIGHT } from "./constants.js";
|
|
4
|
+
|
|
5
|
+
import { dirtyQueue } from "./scheduler.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Schedule one subscriber to re-run on the next flush: inserted into the
|
|
9
|
+
* dirty heap with the `_min` cursor pulled down. Tracked effects ride the
|
|
10
|
+
* heap too — the heap visit is their (empty) compute phase, which hands the
|
|
11
|
+
* callback to the user queue once the pass has committed (see
|
|
12
|
+
* GlobalQueue._update, #3291).
|
|
13
|
+
*/ function enqueueSub(e) {
|
|
14
|
+
if (dirtyQueue.Et > e.lt) dirtyQueue.Et = e.lt;
|
|
15
|
+
insertIntoHeap(e, dirtyQueue);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function actualInsertIntoHeap(e, t) {
|
|
19
|
+
const E = (e._parent?.Pt ? e._parent.Ht?.lt : e._parent?.lt) ?? -1;
|
|
20
|
+
if (E >= e.lt) e.lt = E + 1;
|
|
21
|
+
const n = e.lt;
|
|
22
|
+
const I = t.jt[n];
|
|
23
|
+
if (I === undefined) t.jt[n] = e; else {
|
|
24
|
+
const t = I.Ct;
|
|
25
|
+
t.Rt = e;
|
|
26
|
+
e.Ct = t;
|
|
27
|
+
I.Ct = e;
|
|
28
|
+
}
|
|
29
|
+
if (n > t.yt) t.yt = n;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function insertIntoHeap(e, t) {
|
|
33
|
+
let E = e.R;
|
|
34
|
+
// RECOMPUTING refusals are not always losses: a genuinely missed wake (a
|
|
35
|
+
// write to a link this pass already validated) is latched link-side in
|
|
36
|
+
// insertSubs as REACTIVE_MISSED_WAKE for recompute's tail (#3037).
|
|
37
|
+
if (E & (REACTIVE_IN_HEAP | REACTIVE_RECOMPUTING_DEPS)) return;
|
|
38
|
+
if (E & REACTIVE_CHECK) {
|
|
39
|
+
e.R = E & -4 | REACTIVE_DIRTY | REACTIVE_IN_HEAP;
|
|
40
|
+
} else {
|
|
41
|
+
e.R = E | REACTIVE_IN_HEAP;
|
|
42
|
+
// An unmarked node entering an already-marked heap is marked on the
|
|
43
|
+
// spot, keeping the markHeap memo valid. `_marked` is only reset by
|
|
44
|
+
// runHeap, so a write between two mid-tick pulls (read-time markHeap +
|
|
45
|
+
// updateIfNecessary) would otherwise leave this node unmarked and every
|
|
46
|
+
// downstream pull stale until the next flush (#2922: the second
|
|
47
|
+
// `latest()` returned the first write's value). Invalidating the memo
|
|
48
|
+
// instead re-walked the WHOLE heap on the next pull — with N effects
|
|
49
|
+
// parked in the heap for a synchronous mount (each row writing a ref
|
|
50
|
+
// signal its effect subscribes to), mounting N rows was O(N²) (#3350).
|
|
51
|
+
// markNode's own guard skips an already-DIRTY node.
|
|
52
|
+
if (t.p) markNode(e);
|
|
53
|
+
}
|
|
54
|
+
if (!(E & REACTIVE_IN_HEAP_HEIGHT)) actualInsertIntoHeap(e, t);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function insertIntoHeapHeight(e, t) {
|
|
58
|
+
let E = e.R;
|
|
59
|
+
if (E & (REACTIVE_IN_HEAP | REACTIVE_RECOMPUTING_DEPS | REACTIVE_IN_HEAP_HEIGHT)) return;
|
|
60
|
+
e.R = E | REACTIVE_IN_HEAP_HEIGHT;
|
|
61
|
+
actualInsertIntoHeap(e, t);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function deleteFromHeap(e, t) {
|
|
65
|
+
const E = e.R;
|
|
66
|
+
if (!(E & (REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT))) return;
|
|
67
|
+
e.R = E & -25;
|
|
68
|
+
const n = e.lt;
|
|
69
|
+
if (e.Ct === e) t.jt[n] = undefined; else {
|
|
70
|
+
const E = e.Rt;
|
|
71
|
+
const I = t.jt[n];
|
|
72
|
+
const i = E ?? I;
|
|
73
|
+
if (e === I) t.jt[n] = E; else e.Ct.Rt = E;
|
|
74
|
+
i.Ct = e.Ct;
|
|
75
|
+
}
|
|
76
|
+
e.Ct = e;
|
|
77
|
+
e.Rt = undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function markHeap(e) {
|
|
81
|
+
if (e.p) return;
|
|
82
|
+
e.p = true;
|
|
83
|
+
for (let t = 0; t <= e.yt; t++) {
|
|
84
|
+
for (let E = e.jt[t]; E !== undefined; E = E.Rt) {
|
|
85
|
+
if (E.R & REACTIVE_IN_HEAP) markNode(E);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function markNode(e, t = REACTIVE_DIRTY) {
|
|
91
|
+
const E = e.R;
|
|
92
|
+
if ((E & (REACTIVE_CHECK | REACTIVE_DIRTY)) >= t) return;
|
|
93
|
+
e.R = E & -4 | t;
|
|
94
|
+
for (let t = e.o; t !== null; t = t.t) {
|
|
95
|
+
markNode(t.u, REACTIVE_CHECK);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function runHeap(e, t) {
|
|
100
|
+
e.p = false;
|
|
101
|
+
for (e.Et = 0; e.Et <= e.yt; e.Et++) {
|
|
102
|
+
let E = e.jt[e.Et];
|
|
103
|
+
while (E !== undefined) {
|
|
104
|
+
if (E.R & REACTIVE_IN_HEAP) t(E); else adjustHeight(E, e);
|
|
105
|
+
E = e.jt[e.Et];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
e.yt = 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function adjustHeight(e, t) {
|
|
112
|
+
deleteFromHeap(e, t);
|
|
113
|
+
let E = e.lt;
|
|
114
|
+
for (let t = e.C; t; t = t.G) {
|
|
115
|
+
const e = t.S;
|
|
116
|
+
if (e.X && e.lt >= E) E = e.lt + 1;
|
|
117
|
+
}
|
|
118
|
+
if (e.lt !== E) {
|
|
119
|
+
e.lt = E;
|
|
120
|
+
for (let E = e.o; E !== null; E = E.t) insertIntoHeapHeight(E.u, t);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { deleteFromHeap, enqueueSub, insertIntoHeap, insertIntoHeapHeight, markHeap, markNode, runHeap };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { CONFIG_TRANSPARENT } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
import "./dev.js";
|
|
4
|
+
|
|
5
|
+
import { getOwner } from "./owner.js";
|
|
6
|
+
|
|
7
|
+
// Deferred hydration, dependency edges: plain-signal identities.
|
|
8
|
+
// While on (`setSignalIdentities`; the server-components client turns it on
|
|
9
|
+
// before hydrating), a plain signal created under an id-bearing owner is
|
|
10
|
+
// named `<owner id>~<n>`, n counting the plain signals (and plain store
|
|
11
|
+
// roots: `createStore` over an object, store/edges.ts) created under that
|
|
12
|
+
// owner in its current pass (transparent owners defer to the nearest
|
|
13
|
+
// id-bearing one, as child ids do). The server names the same signal the
|
|
14
|
+
// same way and restarts the count on every re-run, so an edge it shipped (a
|
|
15
|
+
// slot fill read it) matches the client node; the write hook receives the
|
|
16
|
+
// node and looks its name up (`signalName`). The count restarts when the owner's pass
|
|
17
|
+
// generation (`_depGen`, bumped by every recompute) moves on. Only the
|
|
18
|
+
// frames client imports this module, so nothing here ships without it.
|
|
19
|
+
// Names live in a side table, not on the node: frames reads them across the
|
|
20
|
+
// package boundary, where a mangled field would not survive.
|
|
21
|
+
let counts = null;
|
|
22
|
+
|
|
23
|
+
const names = new WeakMap;
|
|
24
|
+
|
|
25
|
+
/** @internal The identity of a plain signal named while identities were on. */ function signalName(n) {
|
|
26
|
+
return n === undefined ? undefined : names.get(n);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function identify(n) {
|
|
30
|
+
let e = getOwner();
|
|
31
|
+
if (!e) return;
|
|
32
|
+
while (e.l & CONFIG_TRANSPARENT && e._parent) e = e._parent;
|
|
33
|
+
if (e.id == null) return;
|
|
34
|
+
const t = e.Ne;
|
|
35
|
+
let i = counts.get(e);
|
|
36
|
+
if (i === undefined || i[0] !== t) counts.set(e, i = [ t, 0 ]);
|
|
37
|
+
const s = e.id + "~" + i[1]++;
|
|
38
|
+
// A store root named twice (one object made into two stores, or a store
|
|
39
|
+
// handed to `createStore`) is one target under two server names: its
|
|
40
|
+
// writes cannot be attributed (an empty name).
|
|
41
|
+
names.set(n, names.has(n) ? "" : s);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @internal Name plain signals from now on (`on`), or stop. */ function setSignalIdentities(n) {
|
|
45
|
+
counts = n ? counts || new WeakMap : null;
|
|
46
|
+
signalHook = n ? identify : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @internal Called with every plain signal `createSignal` makes, and every
|
|
51
|
+
* plain store root `createStore` makes, while set (the namer above). Never
|
|
52
|
+
* set unless `setSignalIdentities` is imported, so a bundle without it keeps
|
|
53
|
+
* only a null check.
|
|
54
|
+
*/ let signalHook = null;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @internal Dev only: `watch` is called with every node read while set (the
|
|
58
|
+
* dev hook `onRead`) — the frames client's identity-drift check watches the
|
|
59
|
+
* named signals a deferred occurrence reads as it mounts. A no-op in prod.
|
|
60
|
+
*/ function setReadWatch(n) {}
|
|
61
|
+
|
|
62
|
+
export { setReadWatch, setSignalIdentities, signalHook, signalName };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "./dev.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Quiescence checks. Run only when the system is fully drained: nothing
|
|
5
|
+
* scheduled, no active/stashed transitions, no live lanes. At that point no
|
|
6
|
+
* transition-scoped state may survive, and the lazily-created companions must
|
|
7
|
+
* agree with a fresh computation of their owner's state.
|
|
8
|
+
*/ function devCheckQuiescent(e) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { devCheckQuiescent };
|