@solidjs/signals 2.0.0-beta.8 → 2.0.0-rc.0
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/README.md +13 -9
- package/dist/dev.js +6078 -1440
- package/dist/node.cjs +7684 -3248
- package/dist/prod/affects.js +126 -0
- package/dist/prod/boundaries.js +572 -0
- package/dist/prod/core/action.js +139 -0
- package/dist/prod/core/async.js +553 -0
- package/dist/prod/core/constants.js +92 -0
- package/dist/prod/core/context.js +67 -0
- package/dist/prod/core/core.js +817 -0
- package/dist/prod/core/dev.js +3 -0
- package/dist/prod/core/effect.js +145 -0
- package/dist/prod/core/error.js +68 -0
- package/dist/prod/core/external.js +98 -0
- package/dist/prod/core/graph.js +104 -0
- package/dist/prod/core/heap.js +140 -0
- package/dist/prod/core/invariants.js +42 -0
- package/dist/prod/core/lanes.js +140 -0
- package/dist/prod/core/optimistic.js +245 -0
- package/dist/prod/core/owner.js +302 -0
- package/dist/prod/core/scheduler.js +749 -0
- package/dist/prod/core/verdict.js +369 -0
- package/dist/prod/index.js +45 -0
- package/dist/prod/map.js +317 -0
- package/dist/prod/signals.js +375 -0
- package/dist/prod/store/optimistic.js +217 -0
- package/dist/prod/store/projection.js +231 -0
- package/dist/prod/store/reconcile.js +707 -0
- package/dist/prod/store/store.js +1081 -0
- package/dist/prod/store/storePath.js +103 -0
- package/dist/prod/store/utils.js +328 -0
- package/dist/types/affects.d.ts +47 -0
- package/dist/types/boundaries.d.ts +93 -11
- package/dist/types/core/action.d.ts +63 -0
- package/dist/types/core/async.d.ts +16 -1
- package/dist/types/core/constants.d.ts +46 -0
- package/dist/types/core/context.d.ts +10 -2
- package/dist/types/core/core.d.ts +111 -17
- package/dist/types/core/dev.d.ts +17 -2
- package/dist/types/core/effect.d.ts +1 -2
- package/dist/types/core/error.d.ts +33 -0
- package/dist/types/core/external.d.ts +0 -11
- package/dist/types/core/graph.d.ts +2 -1
- package/dist/types/core/heap.d.ts +8 -0
- package/dist/types/core/index.d.ts +3 -2
- package/dist/types/core/invariants.d.ts +59 -0
- package/dist/types/core/lanes.d.ts +2 -0
- package/dist/types/core/optimistic.d.ts +6 -0
- package/dist/types/core/owner.d.ts +101 -3
- package/dist/types/core/scheduler.d.ts +97 -9
- package/dist/types/core/types.d.ts +68 -6
- package/dist/types/core/verdict.d.ts +2 -0
- package/dist/types/index.d.ts +4 -3
- package/dist/types/map.d.ts +50 -5
- package/dist/types/signals.d.ts +424 -19
- package/dist/types/store/index.d.ts +1 -1
- package/dist/types/store/optimistic.d.ts +39 -13
- package/dist/types/store/projection.d.ts +48 -18
- package/dist/types/store/reconcile.d.ts +46 -1
- package/dist/types/store/store.d.ts +164 -19
- package/dist/types/store/storePath.d.ts +28 -0
- package/dist/types/store/utils.d.ts +78 -7
- package/dist/types-cjs/affects.d.cts +47 -0
- package/dist/types-cjs/boundaries.d.cts +93 -11
- package/dist/types-cjs/core/action.d.cts +63 -0
- package/dist/types-cjs/core/async.d.cts +16 -1
- package/dist/types-cjs/core/constants.d.cts +46 -0
- package/dist/types-cjs/core/context.d.cts +10 -2
- package/dist/types-cjs/core/core.d.cts +111 -17
- package/dist/types-cjs/core/dev.d.cts +17 -2
- package/dist/types-cjs/core/effect.d.cts +1 -2
- package/dist/types-cjs/core/error.d.cts +33 -0
- package/dist/types-cjs/core/external.d.cts +0 -11
- package/dist/types-cjs/core/graph.d.cts +2 -1
- package/dist/types-cjs/core/heap.d.cts +8 -0
- package/dist/types-cjs/core/index.d.cts +3 -2
- package/dist/types-cjs/core/invariants.d.cts +59 -0
- package/dist/types-cjs/core/lanes.d.cts +2 -0
- package/dist/types-cjs/core/optimistic.d.cts +6 -0
- package/dist/types-cjs/core/owner.d.cts +101 -3
- package/dist/types-cjs/core/scheduler.d.cts +97 -9
- package/dist/types-cjs/core/types.d.cts +68 -6
- package/dist/types-cjs/core/verdict.d.cts +2 -0
- package/dist/types-cjs/index.d.cts +4 -3
- package/dist/types-cjs/map.d.cts +50 -5
- package/dist/types-cjs/signals.d.cts +424 -19
- package/dist/types-cjs/store/index.d.cts +1 -1
- package/dist/types-cjs/store/optimistic.d.cts +39 -13
- package/dist/types-cjs/store/projection.d.cts +48 -18
- package/dist/types-cjs/store/reconcile.d.cts +46 -1
- package/dist/types-cjs/store/store.d.cts +164 -19
- package/dist/types-cjs/store/storePath.d.cts +28 -0
- package/dist/types-cjs/store/utils.d.cts +78 -7
- package/package.json +14 -11
- package/dist/prod.js +0 -3577
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { STATUS_PENDING, CONFIG_AUTO_DISPOSE, NOT_PENDING } from "../core/constants.js";
|
|
2
|
+
|
|
3
|
+
import { computed } from "../core/core.js";
|
|
4
|
+
|
|
5
|
+
import { GlobalQueue, schedule, currentTransition, insertSubs, setProjectionWriteActive, projectionWriteActive } from "../core/scheduler.js";
|
|
6
|
+
|
|
7
|
+
import "../core/invariants.js";
|
|
8
|
+
|
|
9
|
+
import "../core/verdict.js";
|
|
10
|
+
|
|
11
|
+
import "../core/effect.js";
|
|
12
|
+
|
|
13
|
+
import { installOptimisticEngine } from "../core/optimistic.js";
|
|
14
|
+
|
|
15
|
+
import { runProjectionComputed } from "./projection.js";
|
|
16
|
+
|
|
17
|
+
import { $TARGET, STORE_FIREWALL, storeSetter, STORE_OPTIMISTIC_OVERRIDE, STORE_NODE, STORE_OPTIMISTIC_OWNERS, getOverlayLayer, STORE_VALUE, $DELETED, isWrappable, wrap, visibleNodeValue, $TRACK, notifySelf, STORE_WRAP, createStoreProxy, storeTraps, STORE_LOOKUP, STORE_SHALLOW, markRawIngest, STORE_OPTIMISTIC } from "./store.js";
|
|
18
|
+
|
|
19
|
+
function createOptimisticStore(e, t, i) {
|
|
20
|
+
// Register clear function with scheduler; store nodes marked
|
|
21
|
+
// STORE_OPTIMISTIC take the engine's write path, so install it before any
|
|
22
|
+
// node can be created.
|
|
23
|
+
installOptimisticEngine();
|
|
24
|
+
if (!GlobalQueue.Dt) {
|
|
25
|
+
GlobalQueue.Dt = clearOptimisticStores;
|
|
26
|
+
// Store half of the engine's override blockage (#2951): signal-form
|
|
27
|
+
// createOptimistic carries the pending async and the override on ONE node,
|
|
28
|
+
// so transitionBlocked sees both; a derived optimistic STORE splits them —
|
|
29
|
+
// the layer sits on store targets while the in-flight truth lives on the
|
|
30
|
+
// firewall computed. Without this, the transition adopting a bare store
|
|
31
|
+
// write settled in the same flush that started the refetch and its settle
|
|
32
|
+
// consumed the layer mid-flight (follow-up writes then drafted from base,
|
|
33
|
+
// clobbering instead of composing). Optimistic state clears when truth
|
|
34
|
+
// lands or its transaction ends — never mid-refetch. Wrapped here (engine
|
|
35
|
+
// is already installed above) so store-free apps never carry the check.
|
|
36
|
+
const e = GlobalQueue.Tn;
|
|
37
|
+
GlobalQueue.Tn = t => {
|
|
38
|
+
for (const e of t.cn) {
|
|
39
|
+
if ((e[$TARGET]?.[STORE_FIREWALL]?.S ?? 0) & STATUS_PENDING) return true;
|
|
40
|
+
}
|
|
41
|
+
return e(t);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const r = typeof e === "function";
|
|
45
|
+
// Plain form: the second slot carries options.
|
|
46
|
+
if (!r && i === undefined) i = t;
|
|
47
|
+
const o = r ? t : e;
|
|
48
|
+
const n = r ? e : undefined;
|
|
49
|
+
// Create optimistic projection store
|
|
50
|
+
const {store: c} = createOptimisticProjectionInternal(n, o, i);
|
|
51
|
+
return [ c, e => storeSetter(c, e) ];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Clear the optimistic overrides of a settling batch of stores and notify
|
|
55
|
+
// signals. Owns the whole batch (iterate + clear + reschedule) so the
|
|
56
|
+
// scheduler's flush tail carries only a size-guarded hook call. The
|
|
57
|
+
// completing transition scopes each clear to its own layer keys (#2899).
|
|
58
|
+
function clearOptimisticStores(e, t) {
|
|
59
|
+
for (const i of e) {
|
|
60
|
+
const e = i[$TARGET];
|
|
61
|
+
if (e?.[STORE_OPTIMISTIC_OVERRIDE]) clearOptimisticOverride(e, t);
|
|
62
|
+
}
|
|
63
|
+
e.clear();
|
|
64
|
+
schedule();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Consume optimistic layer entries and reset their backing nodes to base.
|
|
69
|
+
* With `completing` (settle path, #2899) only entries the settling
|
|
70
|
+
* transaction owns are consumed — the layer is store-wide but concurrent
|
|
71
|
+
* actions revert independently, so keys stamped by a still-in-flight
|
|
72
|
+
* transition survive (node-level overrides already have this granularity via
|
|
73
|
+
* _optimisticNodes; this is the layer's half). `null` consumes ambient
|
|
74
|
+
* (transaction-less) entries at plain flush end. Omitted (projection landing:
|
|
75
|
+
* fresh authoritative data) consumes everything — the correction supersedes
|
|
76
|
+
* every tentative layer.
|
|
77
|
+
*/ function clearOptimisticOverride(e, t) {
|
|
78
|
+
const i = e[STORE_OPTIMISTIC_OVERRIDE];
|
|
79
|
+
if (!i) return;
|
|
80
|
+
const r = e[STORE_NODE];
|
|
81
|
+
const o = e[STORE_OPTIMISTIC_OWNERS];
|
|
82
|
+
const n = t !== undefined;
|
|
83
|
+
let c = false;
|
|
84
|
+
let s = false;
|
|
85
|
+
// Use projectionWriteActive to bypass optimistic signal behavior (no lane creation)
|
|
86
|
+
// This ensures reversion effects go to regular queues, not lane queues
|
|
87
|
+
const O = projectionWriteActive;
|
|
88
|
+
setProjectionWriteActive(true);
|
|
89
|
+
try {
|
|
90
|
+
for (const O of Reflect.ownKeys(i)) {
|
|
91
|
+
if (n) {
|
|
92
|
+
let e = o?.[O] ?? null;
|
|
93
|
+
// Resolve merge chains (entangled actions settle as one); path-compress
|
|
94
|
+
// so later keys skip the walk. A dead owner (`_done === true`) settled
|
|
95
|
+
// through some other path — never strand its entry. A null owner is an
|
|
96
|
+
// ambient write: its batch belongs to whichever transaction adopted it
|
|
97
|
+
// (initTransition mid-batch) or to the plain flush, so it clears on
|
|
98
|
+
// whichever clear call reaches this store first.
|
|
99
|
+
if (e) {
|
|
100
|
+
if (typeof e.fn === "object") e = o[O] = currentTransition(e);
|
|
101
|
+
if (e !== t && e.fn !== true) {
|
|
102
|
+
s = true;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
delete i[O];
|
|
108
|
+
if (o) delete o[O];
|
|
109
|
+
c = true;
|
|
110
|
+
const T = r?.[O];
|
|
111
|
+
if (T) {
|
|
112
|
+
// Clear lane association so effects go to regular queue
|
|
113
|
+
T.Ke = undefined;
|
|
114
|
+
// Re-read from base — this key left the optimistic layer above, so the
|
|
115
|
+
// overlay resolves to STORE_OVERRIDE or STORE_VALUE.
|
|
116
|
+
const t = getOverlayLayer(e, O);
|
|
117
|
+
const i = t ? t[O] : e[STORE_VALUE][O];
|
|
118
|
+
const r = i === $DELETED ? undefined : i;
|
|
119
|
+
const o = isWrappable(r) ? wrap(r, e) : r;
|
|
120
|
+
const n = visibleNodeValue(T);
|
|
121
|
+
T._e = NOT_PENDING;
|
|
122
|
+
T.sn = null;
|
|
123
|
+
T.De = NOT_PENDING;
|
|
124
|
+
T.Ue = o;
|
|
125
|
+
if (!T.be || !T.be(n, o)) {
|
|
126
|
+
insertSubs(T, true);
|
|
127
|
+
schedule();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (!s) {
|
|
132
|
+
// Assignment, not delete: StoreNode targets share one pre-initialized
|
|
133
|
+
// hidden class (see createStoreProxy) and a delete would demote it.
|
|
134
|
+
e[STORE_OPTIMISTIC_OVERRIDE] = undefined;
|
|
135
|
+
e[STORE_OPTIMISTIC_OWNERS] = undefined;
|
|
136
|
+
}
|
|
137
|
+
// Notify $TRACK
|
|
138
|
+
if (c && r?.[$TRACK]) {
|
|
139
|
+
r[$TRACK].Ke = undefined;
|
|
140
|
+
notifySelf(e);
|
|
141
|
+
}
|
|
142
|
+
} finally {
|
|
143
|
+
setProjectionWriteActive(O);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function createOptimisticProjectionInternal(e, t, i) {
|
|
148
|
+
let r;
|
|
149
|
+
const o = new WeakMap;
|
|
150
|
+
const n = !!i?.shallow;
|
|
151
|
+
const wrapper = e => {
|
|
152
|
+
e[STORE_WRAP] = wrapProjection;
|
|
153
|
+
e[STORE_LOOKUP] = o;
|
|
154
|
+
if (n) {
|
|
155
|
+
e[STORE_SHALLOW] = true;
|
|
156
|
+
markRawIngest(e[STORE_VALUE]);
|
|
157
|
+
}
|
|
158
|
+
e[STORE_OPTIMISTIC] = true;
|
|
159
|
+
// Mark as optimistic store
|
|
160
|
+
Object.defineProperty(e, STORE_FIREWALL, {
|
|
161
|
+
get() {
|
|
162
|
+
return r;
|
|
163
|
+
},
|
|
164
|
+
configurable: true
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
const wrapProjection = e => {
|
|
168
|
+
if (o.has(e)) return o.get(e);
|
|
169
|
+
if (e[$TARGET]?.[STORE_WRAP] === wrapProjection) return e;
|
|
170
|
+
const t = createStoreProxy(e, storeTraps, wrapper);
|
|
171
|
+
o.set(e, t);
|
|
172
|
+
return t;
|
|
173
|
+
};
|
|
174
|
+
const c = wrapProjection(t);
|
|
175
|
+
// If there's a projection function, create a computed to drive it
|
|
176
|
+
if (e) {
|
|
177
|
+
// All writes inside firewall recompute must go to STORE_OVERRIDE (base), not
|
|
178
|
+
// STORE_OPTIMISTIC_OVERRIDE. The outer wrap covers the sync body (including
|
|
179
|
+
// `fn(draft)` and the initial commit); `wrapCommit` re-applies the flag for
|
|
180
|
+
// async yields because they fire outside any enclosing try/finally. It also
|
|
181
|
+
// consumes stale optimistic overlays once fresh projected data lands.
|
|
182
|
+
const clearProjectionOverride = () => {
|
|
183
|
+
const e = c[$TARGET];
|
|
184
|
+
if (e?.[STORE_OPTIMISTIC_OVERRIDE]) clearOptimisticOverride(e);
|
|
185
|
+
};
|
|
186
|
+
const wrapCommit = e => {
|
|
187
|
+
const t = projectionWriteActive;
|
|
188
|
+
setProjectionWriteActive(true);
|
|
189
|
+
try {
|
|
190
|
+
e();
|
|
191
|
+
clearProjectionOverride();
|
|
192
|
+
} finally {
|
|
193
|
+
setProjectionWriteActive(t);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
// seedLoadingValue: born-committed firewall, same as createProjection.
|
|
197
|
+
let t;
|
|
198
|
+
if (i?.seedLoadingValue) t = {
|
|
199
|
+
loadingValue: undefined
|
|
200
|
+
};
|
|
201
|
+
r = computed(() => {
|
|
202
|
+
setProjectionWriteActive(true);
|
|
203
|
+
try {
|
|
204
|
+
runProjectionComputed(c, e, i?.key === undefined ? "id" : i.key, wrapCommit, clearProjectionOverride);
|
|
205
|
+
} finally {
|
|
206
|
+
setProjectionWriteActive(false);
|
|
207
|
+
}
|
|
208
|
+
}, t);
|
|
209
|
+
r.T &= ~CONFIG_AUTO_DISPOSE;
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
store: c,
|
|
213
|
+
node: r
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export { createOptimisticStore };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { computed } from "../core/core.js";
|
|
2
|
+
|
|
3
|
+
import { getOwner } from "../core/owner.js";
|
|
4
|
+
|
|
5
|
+
import { setProjectionWriteActive } from "../core/scheduler.js";
|
|
6
|
+
|
|
7
|
+
import { handleAsync } from "../core/async.js";
|
|
8
|
+
|
|
9
|
+
import "../core/verdict.js";
|
|
10
|
+
|
|
11
|
+
import "../core/effect.js";
|
|
12
|
+
|
|
13
|
+
import { CONFIG_AUTO_DISPOSE } from "../core/constants.js";
|
|
14
|
+
|
|
15
|
+
import { reconcileState } from "./reconcile.js";
|
|
16
|
+
|
|
17
|
+
import { $TARGET, STORE_VALUE, storeSetter, STORE_WRAP, createStoreProxy, storeTraps, setWriteOverride, STORE_LOOKUP, STORE_SHALLOW, markRawIngest, STORE_FIREWALL } from "./store.js";
|
|
18
|
+
|
|
19
|
+
function createProjectionInternal(e, t, r) {
|
|
20
|
+
let o;
|
|
21
|
+
const n = new WeakMap;
|
|
22
|
+
// A shallow projection's children are raw and never wrapped, so the
|
|
23
|
+
// wrapper only ever runs for the root — flagging unconditionally is safe.
|
|
24
|
+
const i = !!r?.shallow;
|
|
25
|
+
const wrapper = e => {
|
|
26
|
+
e[STORE_WRAP] = wrapProjection;
|
|
27
|
+
e[STORE_LOOKUP] = n;
|
|
28
|
+
if (i) {
|
|
29
|
+
e[STORE_SHALLOW] = true;
|
|
30
|
+
markRawIngest(e[STORE_VALUE]);
|
|
31
|
+
}
|
|
32
|
+
Object.defineProperty(e, STORE_FIREWALL, {
|
|
33
|
+
get() {
|
|
34
|
+
return o;
|
|
35
|
+
},
|
|
36
|
+
configurable: true
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const wrapProjection = e => {
|
|
40
|
+
if (n.has(e)) return n.get(e);
|
|
41
|
+
if (e[$TARGET]?.[STORE_WRAP] === wrapProjection) return e;
|
|
42
|
+
const t = createStoreProxy(e, storeTraps, wrapper);
|
|
43
|
+
n.set(e, t);
|
|
44
|
+
return t;
|
|
45
|
+
};
|
|
46
|
+
const c = wrapProjection(t);
|
|
47
|
+
// seedLoadingValue: the firewall is born committed (the seed is commit #0);
|
|
48
|
+
// the internal handleAsync serves it during the derive's first flight. The
|
|
49
|
+
// node's own value channel is void, so the loading value itself is
|
|
50
|
+
// `undefined` — presence of the key is what flips the mode.
|
|
51
|
+
let s;
|
|
52
|
+
if (r?.seedLoadingValue) s = {
|
|
53
|
+
loadingValue: undefined
|
|
54
|
+
};
|
|
55
|
+
o = computed(() => {
|
|
56
|
+
if (!o) o = getOwner();
|
|
57
|
+
runProjectionComputed(c, e, r?.key === undefined ? "id" : r.key);
|
|
58
|
+
}, s);
|
|
59
|
+
o.T &= ~CONFIG_AUTO_DISPOSE;
|
|
60
|
+
return {
|
|
61
|
+
store: c,
|
|
62
|
+
node: o
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Creates a derived (projected) store. Like `createMemo` but for stores: the
|
|
68
|
+
* derive function receives a mutable draft and either mutates it in place
|
|
69
|
+
* (canonical) or returns a new value. Either way the result is reconciled
|
|
70
|
+
* against the previous draft by `options.key` (default `"id"`), so surviving
|
|
71
|
+
* items keep their proxy identity — only added/removed items are
|
|
72
|
+
* created/disposed.
|
|
73
|
+
*
|
|
74
|
+
* If the derive returns a different entity than the one currently held (the
|
|
75
|
+
* `/users/1` → `/users/2` shape), the store swaps to it rather than merging,
|
|
76
|
+
* and nothing below it is treated as surviving.
|
|
77
|
+
*
|
|
78
|
+
* Returns the projected store directly (no setter — reads only).
|
|
79
|
+
*
|
|
80
|
+
* Use this when you want the structural-sharing / per-property tracking
|
|
81
|
+
* behaviour of a store on top of a derived computation. For simple read-only
|
|
82
|
+
* derivations, `createMemo` is lighter.
|
|
83
|
+
*
|
|
84
|
+
* @param fn receives the current draft; mutate it in place or return new
|
|
85
|
+
* data. Return is convenient for filter/derive shapes where mutation is
|
|
86
|
+
* awkward.
|
|
87
|
+
* @param seed the backing store value to wrap and reconcile into
|
|
88
|
+
* @param options `ProjectionOptions` — `name`, `key`. `key` defaults to
|
|
89
|
+
* `"id"`; specify it only when your data uses a different identity field
|
|
90
|
+
* (e.g. `{ key: "uuid" }` or `{ key: u => u.slug }`), or `null` to merge
|
|
91
|
+
* positionally with no keyed pass.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* // Mutation form — update individual fields on the draft.
|
|
96
|
+
* const summary = createProjection<{ total: number; active: number }>(
|
|
97
|
+
* draft => {
|
|
98
|
+
* draft.total = users().length;
|
|
99
|
+
* draft.active = users().filter(u => u.active).length;
|
|
100
|
+
* },
|
|
101
|
+
* { total: 0, active: 0 }
|
|
102
|
+
* );
|
|
103
|
+
*
|
|
104
|
+
* // Return form — produce a derived collection. Reconciled by `id` so each
|
|
105
|
+
* // surviving user keeps the same store identity across recomputes.
|
|
106
|
+
* const activeUsers = createProjection<User[]>(
|
|
107
|
+
* () => allUsers().filter(u => u.active),
|
|
108
|
+
* []
|
|
109
|
+
* );
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @see {@link https://github.com/solidjs/x-reactivity#createprojection}
|
|
113
|
+
*/ function createProjection(e, t, r) {
|
|
114
|
+
return createProjectionInternal(e, t, r).store;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Shared projection computed body used by both `createProjection` and the derived
|
|
119
|
+
* form of `createOptimisticStore`. Encapsulates the write-trap draft, `storeSetter`
|
|
120
|
+
* wrapping, the `handleAsync` subscription with a setter callback, and the commit
|
|
121
|
+
* path (which must always go through `storeSetter` so the `writeOnly` guard is
|
|
122
|
+
* engaged during `reconcile`'s property reads).
|
|
123
|
+
*
|
|
124
|
+
* `wrapCommit` is invoked for every commit (sync return and each async yield) and
|
|
125
|
+
* lets callers layer extra context around the write — e.g. the optimistic store
|
|
126
|
+
* re-enters `setProjectionWriteActive` so reconciles target `STORE_OVERRIDE`
|
|
127
|
+
* instead of `STORE_OPTIMISTIC_OVERRIDE` even when an async yield fires outside
|
|
128
|
+
* the outer `setProjectionWriteActive` scope.
|
|
129
|
+
*/ function runProjectionComputed(e, t, r, o, n) {
|
|
130
|
+
const i = getOwner();
|
|
131
|
+
let c = false;
|
|
132
|
+
let s;
|
|
133
|
+
// Open loading window (seedLoadingValue): the observable store IS commit #0
|
|
134
|
+
// for the whole first flight, so the derive works a detached shadow of the
|
|
135
|
+
// seed — draft writes (pre-await, or between yields) land on the shadow and
|
|
136
|
+
// cannot tear through to readers (#2988; store reads resolve from the live
|
|
137
|
+
// backing, and the born-committed firewall removed the status gate that hid
|
|
138
|
+
// windowless drafts). Every commit point — sync return, each yield, the
|
|
139
|
+
// async landing — reconciles the shadow through the normal commit path, so
|
|
140
|
+
// a fully-sync derive still lands immediately (commit #0 superseded before
|
|
141
|
+
// any observer runs, same as a sync answer superseding loadingValue). The
|
|
142
|
+
// JSON round-trip matches the server's frozen-seed copy (seedLock): a
|
|
143
|
+
// loading-window seed is renderable data by contract. Optimistic note:
|
|
144
|
+
// onDraftWrite (override clearing) shifts from write-time to commit-time
|
|
145
|
+
// for the shadow run — an invisible draft write must not clobber a visible
|
|
146
|
+
// optimistic override mid-window.
|
|
147
|
+
const u = i.Ee ? JSON.parse(JSON.stringify(e[$TARGET][STORE_VALUE])) : null;
|
|
148
|
+
const a = new Proxy(e, createWriteTraps(() => !c || i.Te === s, n));
|
|
149
|
+
storeSetter(a, n => {
|
|
150
|
+
s = t(u ?? n);
|
|
151
|
+
c = true;
|
|
152
|
+
const commit = t => {
|
|
153
|
+
// Shadow run: a void/self return is the mutation form — the shadow
|
|
154
|
+
// carries the writes and is what commits. Commit a detached snapshot,
|
|
155
|
+
// never the shadow itself: reconcile adopts a new root value by
|
|
156
|
+
// identity, and handing it the live shadow would fuse the draft to the
|
|
157
|
+
// observable store — later shadow writes would mutate the backing
|
|
158
|
+
// silently and the next yield would diff the shadow against itself.
|
|
159
|
+
if (u && (t === undefined || t === u)) t = JSON.parse(JSON.stringify(u));
|
|
160
|
+
if (t === n || t === undefined) return;
|
|
161
|
+
const write = () => storeSetter(e, e => reconcileState(t, e, r, true));
|
|
162
|
+
o ? o(write) : write();
|
|
163
|
+
};
|
|
164
|
+
const a = handleAsync(i, s, commit);
|
|
165
|
+
// A still-open window after handleAsync means the return was the
|
|
166
|
+
// commit-#0 fall-through, not a landing — real landings arrive through
|
|
167
|
+
// the setter. A closed one is a genuine sync landing (windowless nodes
|
|
168
|
+
// were never open); commit it.
|
|
169
|
+
if (!i.Ee) commit(a);
|
|
170
|
+
});
|
|
171
|
+
return i;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function createWriteTraps(e, t) {
|
|
175
|
+
const r = {
|
|
176
|
+
get(e, t) {
|
|
177
|
+
let o;
|
|
178
|
+
setWriteOverride(true);
|
|
179
|
+
setProjectionWriteActive(true);
|
|
180
|
+
try {
|
|
181
|
+
o = e[t];
|
|
182
|
+
} finally {
|
|
183
|
+
setWriteOverride(false);
|
|
184
|
+
setProjectionWriteActive(false);
|
|
185
|
+
}
|
|
186
|
+
if (t === $TARGET) return o;
|
|
187
|
+
return typeof o === "object" && o !== null ? new Proxy(o, r) : o;
|
|
188
|
+
},
|
|
189
|
+
has(e, t) {
|
|
190
|
+
let r;
|
|
191
|
+
setWriteOverride(true);
|
|
192
|
+
setProjectionWriteActive(true);
|
|
193
|
+
try {
|
|
194
|
+
r = t in e;
|
|
195
|
+
} finally {
|
|
196
|
+
setWriteOverride(false);
|
|
197
|
+
setProjectionWriteActive(false);
|
|
198
|
+
}
|
|
199
|
+
return r;
|
|
200
|
+
},
|
|
201
|
+
set(r, o, n) {
|
|
202
|
+
if (e && !e()) return true;
|
|
203
|
+
setWriteOverride(true);
|
|
204
|
+
setProjectionWriteActive(true);
|
|
205
|
+
try {
|
|
206
|
+
r[o] = n;
|
|
207
|
+
t?.();
|
|
208
|
+
} finally {
|
|
209
|
+
setWriteOverride(false);
|
|
210
|
+
setProjectionWriteActive(false);
|
|
211
|
+
}
|
|
212
|
+
return true;
|
|
213
|
+
},
|
|
214
|
+
deleteProperty(r, o) {
|
|
215
|
+
if (e && !e()) return true;
|
|
216
|
+
setWriteOverride(true);
|
|
217
|
+
setProjectionWriteActive(true);
|
|
218
|
+
try {
|
|
219
|
+
delete r[o];
|
|
220
|
+
t?.();
|
|
221
|
+
} finally {
|
|
222
|
+
setWriteOverride(false);
|
|
223
|
+
setProjectionWriteActive(false);
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
return r;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export { createProjection, createProjectionInternal, createWriteTraps, runProjectionComputed };
|