@solidjs/signals 0.0.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.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/dev.js +1404 -0
- package/dist/node.cjs +1452 -0
- package/dist/prod.js +1403 -0
- package/dist/types/core/constants.d.ts +14 -0
- package/dist/types/core/core.d.ts +157 -0
- package/dist/types/core/effect.d.ts +27 -0
- package/dist/types/core/error.d.ts +11 -0
- package/dist/types/core/flags.d.ts +8 -0
- package/dist/types/core/index.d.ts +7 -0
- package/dist/types/core/owner.d.ts +90 -0
- package/dist/types/core/scheduler.d.ts +26 -0
- package/dist/types/core/suspense.d.ts +11 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/map.d.ts +12 -0
- package/dist/types/signals.d.ts +56 -0
- package/dist/types/store/index.d.ts +5 -0
- package/dist/types/store/reconcile.d.ts +1 -0
- package/dist/types/store/store.d.ts +40 -0
- package/dist/types/store/utilities.d.ts +29 -0
- package/dist/types/utils.d.ts +1 -0
- package/package.json +48 -0
package/dist/prod.js
ADDED
|
@@ -0,0 +1,1403 @@
|
|
|
1
|
+
// src/core/error.ts
|
|
2
|
+
var NotReadyError = class extends Error {
|
|
3
|
+
};
|
|
4
|
+
var NoOwnerError = class extends Error {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(
|
|
7
|
+
""
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var ContextNotFoundError = class extends Error {
|
|
12
|
+
constructor() {
|
|
13
|
+
super(
|
|
14
|
+
""
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/utils.ts
|
|
20
|
+
function isUndefined(value) {
|
|
21
|
+
return typeof value === "undefined";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/core/constants.ts
|
|
25
|
+
var STATE_CLEAN = 0;
|
|
26
|
+
var STATE_CHECK = 1;
|
|
27
|
+
var STATE_DIRTY = 2;
|
|
28
|
+
var STATE_DISPOSED = 3;
|
|
29
|
+
var EFFECT_PURE = 0;
|
|
30
|
+
var EFFECT_RENDER = 1;
|
|
31
|
+
var EFFECT_USER = 2;
|
|
32
|
+
var SUPPORTS_PROXY = typeof Proxy === "function";
|
|
33
|
+
|
|
34
|
+
// src/core/owner.ts
|
|
35
|
+
var currentOwner = null;
|
|
36
|
+
var defaultContext = {};
|
|
37
|
+
function getOwner() {
|
|
38
|
+
return currentOwner;
|
|
39
|
+
}
|
|
40
|
+
function setOwner(owner) {
|
|
41
|
+
const out = currentOwner;
|
|
42
|
+
currentOwner = owner;
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
var Owner = class {
|
|
46
|
+
// We flatten the owner tree into a linked list so that we don't need a pointer to .firstChild
|
|
47
|
+
// However, the children are actually added in reverse creation order
|
|
48
|
+
// See comment at the top of the file for an example of the _nextSibling traversal
|
|
49
|
+
q = null;
|
|
50
|
+
l = null;
|
|
51
|
+
o = null;
|
|
52
|
+
a = STATE_CLEAN;
|
|
53
|
+
g = null;
|
|
54
|
+
m = defaultContext;
|
|
55
|
+
h = null;
|
|
56
|
+
e = null;
|
|
57
|
+
constructor(signal = false) {
|
|
58
|
+
if (currentOwner && !signal)
|
|
59
|
+
currentOwner.append(this);
|
|
60
|
+
}
|
|
61
|
+
append(child) {
|
|
62
|
+
child.q = this;
|
|
63
|
+
child.o = this;
|
|
64
|
+
if (this.l)
|
|
65
|
+
this.l.o = child;
|
|
66
|
+
child.l = this.l;
|
|
67
|
+
this.l = child;
|
|
68
|
+
if (child.m !== this.m) {
|
|
69
|
+
child.m = { ...this.m, ...child.m };
|
|
70
|
+
}
|
|
71
|
+
if (this.h) {
|
|
72
|
+
child.h = !child.h ? this.h : [...child.h, ...this.h];
|
|
73
|
+
}
|
|
74
|
+
if (this.e)
|
|
75
|
+
child.e = this.e;
|
|
76
|
+
}
|
|
77
|
+
dispose(self = true) {
|
|
78
|
+
if (this.a === STATE_DISPOSED)
|
|
79
|
+
return;
|
|
80
|
+
let head = self ? this.o || this.q : this, current = this.l, next = null;
|
|
81
|
+
while (current && current.q === this) {
|
|
82
|
+
current.dispose(true);
|
|
83
|
+
current.w();
|
|
84
|
+
next = current.l;
|
|
85
|
+
current.l = null;
|
|
86
|
+
current = next;
|
|
87
|
+
}
|
|
88
|
+
if (self)
|
|
89
|
+
this.w();
|
|
90
|
+
if (current)
|
|
91
|
+
current.o = !self ? this : this.o;
|
|
92
|
+
if (head)
|
|
93
|
+
head.l = current;
|
|
94
|
+
}
|
|
95
|
+
w() {
|
|
96
|
+
if (this.o)
|
|
97
|
+
this.o.l = null;
|
|
98
|
+
this.q = null;
|
|
99
|
+
this.o = null;
|
|
100
|
+
this.m = defaultContext;
|
|
101
|
+
this.h = null;
|
|
102
|
+
this.a = STATE_DISPOSED;
|
|
103
|
+
this.emptyDisposal();
|
|
104
|
+
}
|
|
105
|
+
emptyDisposal() {
|
|
106
|
+
if (!this.g)
|
|
107
|
+
return;
|
|
108
|
+
if (Array.isArray(this.g)) {
|
|
109
|
+
for (let i = 0; i < this.g.length; i++) {
|
|
110
|
+
const callable = this.g[i];
|
|
111
|
+
callable.call(callable);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
this.g.call(this.g);
|
|
115
|
+
}
|
|
116
|
+
this.g = null;
|
|
117
|
+
}
|
|
118
|
+
handleError(error) {
|
|
119
|
+
if (!this.h)
|
|
120
|
+
throw error;
|
|
121
|
+
let i = 0, len = this.h.length;
|
|
122
|
+
for (i = 0; i < len; i++) {
|
|
123
|
+
try {
|
|
124
|
+
this.h[i](error);
|
|
125
|
+
break;
|
|
126
|
+
} catch (e) {
|
|
127
|
+
error = e;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (i === len)
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
function createContext(defaultValue, description) {
|
|
135
|
+
return { id: Symbol(description), defaultValue };
|
|
136
|
+
}
|
|
137
|
+
function getContext(context, owner = currentOwner) {
|
|
138
|
+
if (!owner) {
|
|
139
|
+
throw new NoOwnerError();
|
|
140
|
+
}
|
|
141
|
+
const value = hasContext(context, owner) ? owner.m[context.id] : context.defaultValue;
|
|
142
|
+
if (isUndefined(value)) {
|
|
143
|
+
throw new ContextNotFoundError();
|
|
144
|
+
}
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
function setContext(context, value, owner = currentOwner) {
|
|
148
|
+
if (!owner) {
|
|
149
|
+
throw new NoOwnerError();
|
|
150
|
+
}
|
|
151
|
+
owner.m = {
|
|
152
|
+
...owner.m,
|
|
153
|
+
[context.id]: isUndefined(value) ? context.defaultValue : value
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function hasContext(context, owner = currentOwner) {
|
|
157
|
+
return !isUndefined(owner?.m[context.id]);
|
|
158
|
+
}
|
|
159
|
+
function onCleanup(disposable) {
|
|
160
|
+
if (!currentOwner)
|
|
161
|
+
return;
|
|
162
|
+
const node = currentOwner;
|
|
163
|
+
if (!node.g) {
|
|
164
|
+
node.g = disposable;
|
|
165
|
+
} else if (Array.isArray(node.g)) {
|
|
166
|
+
node.g.push(disposable);
|
|
167
|
+
} else {
|
|
168
|
+
node.g = [node.g, disposable];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/core/flags.ts
|
|
173
|
+
var ERROR_OFFSET = 0;
|
|
174
|
+
var ERROR_BIT = 1 << ERROR_OFFSET;
|
|
175
|
+
var LOADING_OFFSET = 1;
|
|
176
|
+
var LOADING_BIT = 1 << LOADING_OFFSET;
|
|
177
|
+
var DEFAULT_FLAGS = ERROR_BIT;
|
|
178
|
+
|
|
179
|
+
// src/core/core.ts
|
|
180
|
+
var currentObserver = null;
|
|
181
|
+
var currentMask = DEFAULT_FLAGS;
|
|
182
|
+
var newSources = null;
|
|
183
|
+
var newSourcesIndex = 0;
|
|
184
|
+
var newFlags = 0;
|
|
185
|
+
var clock = 0;
|
|
186
|
+
var syncResolve = false;
|
|
187
|
+
var updateCheck = null;
|
|
188
|
+
function getObserver() {
|
|
189
|
+
return currentObserver;
|
|
190
|
+
}
|
|
191
|
+
function incrementClock() {
|
|
192
|
+
clock++;
|
|
193
|
+
}
|
|
194
|
+
var UNCHANGED = Symbol(0);
|
|
195
|
+
var Computation = class extends Owner {
|
|
196
|
+
b = null;
|
|
197
|
+
c = null;
|
|
198
|
+
d;
|
|
199
|
+
C;
|
|
200
|
+
// Used in __DEV__ mode, hopefully removed in production
|
|
201
|
+
U;
|
|
202
|
+
// Using false is an optimization as an alternative to _equals: () => false
|
|
203
|
+
// which could enable more efficient DIRTY notification
|
|
204
|
+
D = isEqual;
|
|
205
|
+
N;
|
|
206
|
+
/** Whether the computation is an error or has ancestors that are unresolved */
|
|
207
|
+
i = 0;
|
|
208
|
+
/** Which flags raised by sources are handled, vs. being passed through. */
|
|
209
|
+
A = DEFAULT_FLAGS;
|
|
210
|
+
E = null;
|
|
211
|
+
F = null;
|
|
212
|
+
G = -1;
|
|
213
|
+
constructor(initialValue, compute2, options) {
|
|
214
|
+
super(compute2 === null);
|
|
215
|
+
this.C = compute2;
|
|
216
|
+
this.a = compute2 ? STATE_DIRTY : STATE_CLEAN;
|
|
217
|
+
this.d = initialValue;
|
|
218
|
+
if (options?.equals !== void 0)
|
|
219
|
+
this.D = options.equals;
|
|
220
|
+
if (options?.unobserved)
|
|
221
|
+
this.N = options?.unobserved;
|
|
222
|
+
}
|
|
223
|
+
O() {
|
|
224
|
+
if (this.C)
|
|
225
|
+
this.r();
|
|
226
|
+
if (!this.b || this.b.length)
|
|
227
|
+
track(this);
|
|
228
|
+
newFlags |= this.i & ~currentMask;
|
|
229
|
+
if (this.i & ERROR_BIT) {
|
|
230
|
+
throw this.d;
|
|
231
|
+
} else {
|
|
232
|
+
return this.d;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Return the current value of this computation
|
|
237
|
+
* Automatically re-executes the surrounding computation when the value changes
|
|
238
|
+
*/
|
|
239
|
+
read() {
|
|
240
|
+
return this.O();
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Return the current value of this computation
|
|
244
|
+
* Automatically re-executes the surrounding computation when the value changes
|
|
245
|
+
*
|
|
246
|
+
* If the computation has any unresolved ancestors, this function waits for the value to resolve
|
|
247
|
+
* before continuing
|
|
248
|
+
*/
|
|
249
|
+
wait() {
|
|
250
|
+
if (!syncResolve && this.loading()) {
|
|
251
|
+
throw new NotReadyError();
|
|
252
|
+
}
|
|
253
|
+
return this.O();
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Return true if the computation is the value is dependent on an unresolved promise
|
|
257
|
+
* Triggers re-execution of the computation when the loading state changes
|
|
258
|
+
*
|
|
259
|
+
* This is useful especially when effects want to re-execute when a computation's
|
|
260
|
+
* loading state changes
|
|
261
|
+
*/
|
|
262
|
+
loading() {
|
|
263
|
+
if (this.F === null) {
|
|
264
|
+
this.F = loadingState(this);
|
|
265
|
+
}
|
|
266
|
+
return this.F.read();
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Return true if the computation is the computation threw an error
|
|
270
|
+
* Triggers re-execution of the computation when the error state changes
|
|
271
|
+
*/
|
|
272
|
+
error() {
|
|
273
|
+
if (this.E === null) {
|
|
274
|
+
this.E = errorState(this);
|
|
275
|
+
}
|
|
276
|
+
return this.E.read();
|
|
277
|
+
}
|
|
278
|
+
/** Update the computation with a new value. */
|
|
279
|
+
write(value, flags = 0, raw = false) {
|
|
280
|
+
const newValue = !raw && typeof value === "function" ? value(this.d) : value;
|
|
281
|
+
const valueChanged = newValue !== UNCHANGED && (!!(flags & ERROR_BIT) || this.D === false || !this.D(this.d, newValue));
|
|
282
|
+
if (valueChanged)
|
|
283
|
+
this.d = newValue;
|
|
284
|
+
const changedFlagsMask = this.i ^ flags, changedFlags = changedFlagsMask & flags;
|
|
285
|
+
this.i = flags;
|
|
286
|
+
this.G = clock + 1;
|
|
287
|
+
if (this.c) {
|
|
288
|
+
for (let i = 0; i < this.c.length; i++) {
|
|
289
|
+
if (valueChanged) {
|
|
290
|
+
this.c[i].s(STATE_DIRTY);
|
|
291
|
+
} else if (changedFlagsMask) {
|
|
292
|
+
this.c[i].P(changedFlagsMask, changedFlags);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return this.d;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Set the current node's state, and recursively mark all of this node's observers as STATE_CHECK
|
|
300
|
+
*/
|
|
301
|
+
s(state) {
|
|
302
|
+
if (this.a >= state)
|
|
303
|
+
return;
|
|
304
|
+
this.a = state;
|
|
305
|
+
if (this.c) {
|
|
306
|
+
for (let i = 0; i < this.c.length; i++) {
|
|
307
|
+
this.c[i].s(STATE_CHECK);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Notify the computation that one of its sources has changed flags.
|
|
313
|
+
*
|
|
314
|
+
* @param mask A bitmask for which flag(s) were changed.
|
|
315
|
+
* @param newFlags The source's new flags, masked to just the changed ones.
|
|
316
|
+
*/
|
|
317
|
+
P(mask, newFlags2) {
|
|
318
|
+
if (this.a >= STATE_DIRTY)
|
|
319
|
+
return;
|
|
320
|
+
if (mask & this.A) {
|
|
321
|
+
this.s(STATE_DIRTY);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (this.a >= STATE_CHECK)
|
|
325
|
+
return;
|
|
326
|
+
const prevFlags = this.i & mask;
|
|
327
|
+
const deltaFlags = prevFlags ^ newFlags2;
|
|
328
|
+
if (newFlags2 === prevFlags) ; else if (deltaFlags & prevFlags & mask) {
|
|
329
|
+
this.s(STATE_CHECK);
|
|
330
|
+
} else {
|
|
331
|
+
this.i ^= deltaFlags;
|
|
332
|
+
if (this.c) {
|
|
333
|
+
for (let i = 0; i < this.c.length; i++) {
|
|
334
|
+
this.c[i].P(mask, newFlags2);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
Q(error) {
|
|
340
|
+
this.write(error, this.i | ERROR_BIT);
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* This is the core part of the reactivity system, which makes sure that the values are updated
|
|
344
|
+
* before they are read. We've also adapted it to return the loading state of the computation,
|
|
345
|
+
* so that we can propagate that to the computation's observers.
|
|
346
|
+
*
|
|
347
|
+
* This function will ensure that the value and states we read from the computation are up to date
|
|
348
|
+
*/
|
|
349
|
+
r() {
|
|
350
|
+
if (this.a === STATE_DISPOSED) {
|
|
351
|
+
throw new Error("Tried to read a disposed computation");
|
|
352
|
+
}
|
|
353
|
+
if (this.a === STATE_CLEAN) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
let observerFlags = 0;
|
|
357
|
+
if (this.a === STATE_CHECK) {
|
|
358
|
+
for (let i = 0; i < this.b.length; i++) {
|
|
359
|
+
this.b[i].r();
|
|
360
|
+
observerFlags |= this.b[i].i;
|
|
361
|
+
if (this.a === STATE_DIRTY) {
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
if (this.a === STATE_DIRTY) {
|
|
367
|
+
update(this);
|
|
368
|
+
} else {
|
|
369
|
+
this.write(UNCHANGED, observerFlags);
|
|
370
|
+
this.a = STATE_CLEAN;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Remove ourselves from the owner graph and the computation graph
|
|
375
|
+
*/
|
|
376
|
+
w() {
|
|
377
|
+
if (this.a === STATE_DISPOSED)
|
|
378
|
+
return;
|
|
379
|
+
if (this.b)
|
|
380
|
+
removeSourceObservers(this, 0);
|
|
381
|
+
super.w();
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
function loadingState(node) {
|
|
385
|
+
const prevOwner = setOwner(node.q);
|
|
386
|
+
const options = void 0;
|
|
387
|
+
const computation = new Computation(
|
|
388
|
+
void 0,
|
|
389
|
+
() => {
|
|
390
|
+
track(node);
|
|
391
|
+
node.r();
|
|
392
|
+
return !!(node.i & LOADING_BIT);
|
|
393
|
+
},
|
|
394
|
+
options
|
|
395
|
+
);
|
|
396
|
+
computation.A = ERROR_BIT | LOADING_BIT;
|
|
397
|
+
setOwner(prevOwner);
|
|
398
|
+
return computation;
|
|
399
|
+
}
|
|
400
|
+
function errorState(node) {
|
|
401
|
+
const prevOwner = setOwner(node.q);
|
|
402
|
+
const options = void 0;
|
|
403
|
+
const computation = new Computation(
|
|
404
|
+
void 0,
|
|
405
|
+
() => {
|
|
406
|
+
track(node);
|
|
407
|
+
node.r();
|
|
408
|
+
return !!(node.i & ERROR_BIT);
|
|
409
|
+
},
|
|
410
|
+
options
|
|
411
|
+
);
|
|
412
|
+
computation.A = ERROR_BIT;
|
|
413
|
+
setOwner(prevOwner);
|
|
414
|
+
return computation;
|
|
415
|
+
}
|
|
416
|
+
function track(computation) {
|
|
417
|
+
if (currentObserver) {
|
|
418
|
+
if (!newSources && currentObserver.b && currentObserver.b[newSourcesIndex] === computation) {
|
|
419
|
+
newSourcesIndex++;
|
|
420
|
+
} else if (!newSources)
|
|
421
|
+
newSources = [computation];
|
|
422
|
+
else if (computation !== newSources[newSources.length - 1]) {
|
|
423
|
+
newSources.push(computation);
|
|
424
|
+
}
|
|
425
|
+
if (updateCheck) {
|
|
426
|
+
updateCheck.d = computation.G > currentObserver.G;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
function update(node) {
|
|
431
|
+
const prevSources = newSources, prevSourcesIndex = newSourcesIndex, prevFlags = newFlags;
|
|
432
|
+
newSources = null;
|
|
433
|
+
newSourcesIndex = 0;
|
|
434
|
+
newFlags = 0;
|
|
435
|
+
try {
|
|
436
|
+
node.dispose(false);
|
|
437
|
+
node.emptyDisposal();
|
|
438
|
+
const result = compute(node, node.C, node);
|
|
439
|
+
node.write(result, newFlags, true);
|
|
440
|
+
} catch (error) {
|
|
441
|
+
if (error instanceof NotReadyError) {
|
|
442
|
+
node.write(UNCHANGED, newFlags | LOADING_BIT);
|
|
443
|
+
} else {
|
|
444
|
+
node.Q(error);
|
|
445
|
+
}
|
|
446
|
+
} finally {
|
|
447
|
+
if (newSources) {
|
|
448
|
+
if (node.b)
|
|
449
|
+
removeSourceObservers(node, newSourcesIndex);
|
|
450
|
+
if (node.b && newSourcesIndex > 0) {
|
|
451
|
+
node.b.length = newSourcesIndex + newSources.length;
|
|
452
|
+
for (let i = 0; i < newSources.length; i++) {
|
|
453
|
+
node.b[newSourcesIndex + i] = newSources[i];
|
|
454
|
+
}
|
|
455
|
+
} else {
|
|
456
|
+
node.b = newSources;
|
|
457
|
+
}
|
|
458
|
+
let source;
|
|
459
|
+
for (let i = newSourcesIndex; i < node.b.length; i++) {
|
|
460
|
+
source = node.b[i];
|
|
461
|
+
if (!source.c)
|
|
462
|
+
source.c = [node];
|
|
463
|
+
else
|
|
464
|
+
source.c.push(node);
|
|
465
|
+
}
|
|
466
|
+
} else if (node.b && newSourcesIndex < node.b.length) {
|
|
467
|
+
removeSourceObservers(node, newSourcesIndex);
|
|
468
|
+
node.b.length = newSourcesIndex;
|
|
469
|
+
}
|
|
470
|
+
newSources = prevSources;
|
|
471
|
+
newSourcesIndex = prevSourcesIndex;
|
|
472
|
+
newFlags = prevFlags;
|
|
473
|
+
node.a = STATE_CLEAN;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function removeSourceObservers(node, index) {
|
|
477
|
+
let source;
|
|
478
|
+
let swap;
|
|
479
|
+
for (let i = index; i < node.b.length; i++) {
|
|
480
|
+
source = node.b[i];
|
|
481
|
+
if (source.c) {
|
|
482
|
+
swap = source.c.indexOf(node);
|
|
483
|
+
source.c[swap] = source.c[source.c.length - 1];
|
|
484
|
+
source.c.pop();
|
|
485
|
+
if (!source.c.length)
|
|
486
|
+
source.N?.();
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
function isEqual(a, b) {
|
|
491
|
+
return a === b;
|
|
492
|
+
}
|
|
493
|
+
function untrack(fn) {
|
|
494
|
+
if (currentObserver === null)
|
|
495
|
+
return fn();
|
|
496
|
+
return compute(getOwner(), fn, null);
|
|
497
|
+
}
|
|
498
|
+
function hasUpdated(fn) {
|
|
499
|
+
const current = updateCheck;
|
|
500
|
+
updateCheck = { d: false };
|
|
501
|
+
try {
|
|
502
|
+
fn();
|
|
503
|
+
return updateCheck.d;
|
|
504
|
+
} finally {
|
|
505
|
+
updateCheck = current;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
function isPending(fn) {
|
|
509
|
+
try {
|
|
510
|
+
fn();
|
|
511
|
+
return false;
|
|
512
|
+
} catch (e) {
|
|
513
|
+
return e instanceof NotReadyError;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
function latest(fn) {
|
|
517
|
+
const prevFlags = newFlags;
|
|
518
|
+
syncResolve = true;
|
|
519
|
+
try {
|
|
520
|
+
return fn();
|
|
521
|
+
} catch {
|
|
522
|
+
} finally {
|
|
523
|
+
newFlags = prevFlags;
|
|
524
|
+
syncResolve = false;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
function compute(owner, compute2, observer) {
|
|
528
|
+
const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask;
|
|
529
|
+
currentObserver = observer;
|
|
530
|
+
currentMask = observer?.A ?? DEFAULT_FLAGS;
|
|
531
|
+
try {
|
|
532
|
+
return compute2(observer ? observer.d : void 0);
|
|
533
|
+
} finally {
|
|
534
|
+
setOwner(prevOwner);
|
|
535
|
+
currentObserver = prevObserver;
|
|
536
|
+
currentMask = prevMask;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/core/scheduler.ts
|
|
541
|
+
var scheduled = false;
|
|
542
|
+
function schedule() {
|
|
543
|
+
if (scheduled)
|
|
544
|
+
return;
|
|
545
|
+
scheduled = true;
|
|
546
|
+
queueMicrotask(flushSync);
|
|
547
|
+
}
|
|
548
|
+
var Queue = class {
|
|
549
|
+
H = false;
|
|
550
|
+
t = [[], [], []];
|
|
551
|
+
y = [];
|
|
552
|
+
enqueue(type, node) {
|
|
553
|
+
this.t[0].push(node);
|
|
554
|
+
if (type)
|
|
555
|
+
this.t[type].push(node);
|
|
556
|
+
schedule();
|
|
557
|
+
}
|
|
558
|
+
run(type) {
|
|
559
|
+
if (this.t[type].length) {
|
|
560
|
+
if (type === EFFECT_PURE) {
|
|
561
|
+
runPureQueue(this.t[type]);
|
|
562
|
+
this.t[type] = [];
|
|
563
|
+
} else {
|
|
564
|
+
const effects = this.t[type];
|
|
565
|
+
this.t[type] = [];
|
|
566
|
+
runEffectQueue(effects);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
for (let i = 0; i < this.y.length; i++) {
|
|
570
|
+
this.y[i].run(type);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
flush() {
|
|
574
|
+
if (this.H)
|
|
575
|
+
return;
|
|
576
|
+
this.H = true;
|
|
577
|
+
try {
|
|
578
|
+
this.run(EFFECT_PURE);
|
|
579
|
+
incrementClock();
|
|
580
|
+
this.run(EFFECT_RENDER);
|
|
581
|
+
this.run(EFFECT_USER);
|
|
582
|
+
} finally {
|
|
583
|
+
this.H = false;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
addChild(child) {
|
|
587
|
+
this.y.push(child);
|
|
588
|
+
}
|
|
589
|
+
removeChild(child) {
|
|
590
|
+
const index = this.y.indexOf(child);
|
|
591
|
+
if (index >= 0)
|
|
592
|
+
this.y.splice(index, 1);
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
var globalQueue = new Queue();
|
|
596
|
+
function flushSync() {
|
|
597
|
+
globalQueue.flush();
|
|
598
|
+
scheduled = false;
|
|
599
|
+
}
|
|
600
|
+
function createBoundary(fn, queue) {
|
|
601
|
+
const owner = new Owner();
|
|
602
|
+
const parentQueue = owner.e || globalQueue;
|
|
603
|
+
parentQueue.addChild(owner.e = queue);
|
|
604
|
+
onCleanup(() => parentQueue.removeChild(owner.e));
|
|
605
|
+
return compute(owner, fn, null);
|
|
606
|
+
}
|
|
607
|
+
function runTop(node) {
|
|
608
|
+
const ancestors = [];
|
|
609
|
+
for (let current = node; current !== null; current = current.q) {
|
|
610
|
+
if (current.a !== STATE_CLEAN) {
|
|
611
|
+
ancestors.push(current);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
615
|
+
if (ancestors[i].a !== STATE_DISPOSED)
|
|
616
|
+
ancestors[i].r();
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
function runPureQueue(queue) {
|
|
620
|
+
for (let i = 0; i < queue.length; i++) {
|
|
621
|
+
if (queue[i].a !== STATE_CLEAN)
|
|
622
|
+
runTop(queue[i]);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
function runEffectQueue(queue) {
|
|
626
|
+
for (let i = 0; i < queue.length; i++) {
|
|
627
|
+
if (queue[i].I && queue[i].a !== STATE_DISPOSED) {
|
|
628
|
+
queue[i].J(queue[i].d, queue[i].B);
|
|
629
|
+
queue[i].I = false;
|
|
630
|
+
queue[i].B = queue[i].d;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// src/core/effect.ts
|
|
636
|
+
var Effect = class extends Computation {
|
|
637
|
+
J;
|
|
638
|
+
I = false;
|
|
639
|
+
B;
|
|
640
|
+
K;
|
|
641
|
+
e;
|
|
642
|
+
constructor(initialValue, compute2, effect, options) {
|
|
643
|
+
super(initialValue, compute2, options);
|
|
644
|
+
this.J = effect;
|
|
645
|
+
this.B = initialValue;
|
|
646
|
+
this.K = options?.render ? EFFECT_RENDER : EFFECT_USER;
|
|
647
|
+
this.e = getOwner()?.e || globalQueue;
|
|
648
|
+
this.e.enqueue(this.K, this);
|
|
649
|
+
this.r();
|
|
650
|
+
}
|
|
651
|
+
write(value, flags = 0) {
|
|
652
|
+
const currentFlags = this.i;
|
|
653
|
+
this.i = flags;
|
|
654
|
+
if ((flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
|
|
655
|
+
this.e.S?.(this);
|
|
656
|
+
}
|
|
657
|
+
if (value === UNCHANGED)
|
|
658
|
+
return this.d;
|
|
659
|
+
this.d = value;
|
|
660
|
+
this.I = true;
|
|
661
|
+
return value;
|
|
662
|
+
}
|
|
663
|
+
s(state) {
|
|
664
|
+
if (this.a >= state)
|
|
665
|
+
return;
|
|
666
|
+
if (this.a === STATE_CLEAN)
|
|
667
|
+
this.e.enqueue(this.K, this);
|
|
668
|
+
this.a = state;
|
|
669
|
+
}
|
|
670
|
+
Q(error) {
|
|
671
|
+
this.handleError(error);
|
|
672
|
+
}
|
|
673
|
+
w() {
|
|
674
|
+
this.J = void 0;
|
|
675
|
+
this.B = void 0;
|
|
676
|
+
super.w();
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
var EagerComputation = class extends Computation {
|
|
680
|
+
e;
|
|
681
|
+
constructor(initialValue, compute2, options) {
|
|
682
|
+
super(initialValue, compute2, options);
|
|
683
|
+
this.e = getOwner()?.e || globalQueue;
|
|
684
|
+
this.r();
|
|
685
|
+
}
|
|
686
|
+
s(state) {
|
|
687
|
+
if (this.a >= state)
|
|
688
|
+
return;
|
|
689
|
+
if (this.a === STATE_CLEAN)
|
|
690
|
+
this.e.enqueue(EFFECT_PURE, this);
|
|
691
|
+
super.s(state);
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
// src/core/suspense.ts
|
|
696
|
+
var SuspenseQueue = class extends Queue {
|
|
697
|
+
k = /* @__PURE__ */ new Set();
|
|
698
|
+
z = false;
|
|
699
|
+
L = new Computation(false, null);
|
|
700
|
+
run(type) {
|
|
701
|
+
if (type && this.z)
|
|
702
|
+
return;
|
|
703
|
+
super.run(type);
|
|
704
|
+
}
|
|
705
|
+
S(node) {
|
|
706
|
+
if (node.i & LOADING_BIT) {
|
|
707
|
+
this.k.add(node);
|
|
708
|
+
if (!this.z) {
|
|
709
|
+
this.z = true;
|
|
710
|
+
queueMicrotask(() => this.L.write(true));
|
|
711
|
+
}
|
|
712
|
+
} else {
|
|
713
|
+
this.k.delete(node);
|
|
714
|
+
if (this.k.size === 0) {
|
|
715
|
+
this.z = false;
|
|
716
|
+
queueMicrotask(() => this.L.write(false));
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
function createSuspense(fn, fallbackFn) {
|
|
722
|
+
const queue = new SuspenseQueue();
|
|
723
|
+
const tree = createBoundary(fn, queue);
|
|
724
|
+
const equality = new Computation(null, () => queue.L.read() || queue.z);
|
|
725
|
+
const comp = new Computation(null, () => equality.read() ? untrack(fallbackFn) : tree);
|
|
726
|
+
return comp.read.bind(comp);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// src/signals.ts
|
|
730
|
+
function createSignal(first, second, third) {
|
|
731
|
+
if (typeof first === "function") {
|
|
732
|
+
const memo = createMemo((p) => {
|
|
733
|
+
const node2 = new Computation(
|
|
734
|
+
first(p ? untrack(p[0]) : second),
|
|
735
|
+
null,
|
|
736
|
+
third
|
|
737
|
+
);
|
|
738
|
+
return [node2.read.bind(node2), node2.write.bind(node2)];
|
|
739
|
+
});
|
|
740
|
+
return [() => memo()[0](), (value) => memo()[1](value)];
|
|
741
|
+
}
|
|
742
|
+
const node = new Computation(first, null, second);
|
|
743
|
+
return [node.read.bind(node), node.write.bind(node)];
|
|
744
|
+
}
|
|
745
|
+
function createAsync(fn, initial, options) {
|
|
746
|
+
const lhs = new EagerComputation(
|
|
747
|
+
{
|
|
748
|
+
d: initial
|
|
749
|
+
},
|
|
750
|
+
(p) => {
|
|
751
|
+
const value = p?.d;
|
|
752
|
+
const source = fn(value);
|
|
753
|
+
const isPromise = source instanceof Promise;
|
|
754
|
+
const iterator = source[Symbol.asyncIterator];
|
|
755
|
+
if (!isPromise && !iterator) {
|
|
756
|
+
return {
|
|
757
|
+
wait() {
|
|
758
|
+
return source;
|
|
759
|
+
},
|
|
760
|
+
d: source
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
const signal = new Computation(value, null, options);
|
|
764
|
+
signal.write(UNCHANGED, LOADING_BIT);
|
|
765
|
+
if (isPromise) {
|
|
766
|
+
source.then(
|
|
767
|
+
(value2) => {
|
|
768
|
+
signal.write(value2, 0);
|
|
769
|
+
},
|
|
770
|
+
(error) => {
|
|
771
|
+
signal.write(error, ERROR_BIT);
|
|
772
|
+
}
|
|
773
|
+
);
|
|
774
|
+
} else {
|
|
775
|
+
let abort = false;
|
|
776
|
+
onCleanup(() => abort = true);
|
|
777
|
+
(async () => {
|
|
778
|
+
try {
|
|
779
|
+
for await (let value2 of source) {
|
|
780
|
+
if (abort)
|
|
781
|
+
return;
|
|
782
|
+
signal.write(value2, 0);
|
|
783
|
+
}
|
|
784
|
+
} catch (error) {
|
|
785
|
+
signal.write(error, ERROR_BIT);
|
|
786
|
+
}
|
|
787
|
+
})();
|
|
788
|
+
}
|
|
789
|
+
return signal;
|
|
790
|
+
}
|
|
791
|
+
);
|
|
792
|
+
return () => lhs.wait().wait();
|
|
793
|
+
}
|
|
794
|
+
function createMemo(compute2, initialValue, options) {
|
|
795
|
+
let node = new Computation(initialValue, compute2, options);
|
|
796
|
+
let value;
|
|
797
|
+
return () => {
|
|
798
|
+
if (node) {
|
|
799
|
+
value = node.wait();
|
|
800
|
+
if (!node.b?.length)
|
|
801
|
+
node = void 0;
|
|
802
|
+
}
|
|
803
|
+
return value;
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
function createEffect(compute2, effect, initialValue, options) {
|
|
807
|
+
void new Effect(
|
|
808
|
+
initialValue,
|
|
809
|
+
compute2,
|
|
810
|
+
effect,
|
|
811
|
+
void 0
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
function createRenderEffect(compute2, effect, initialValue, options) {
|
|
815
|
+
void new Effect(initialValue, compute2, effect, {
|
|
816
|
+
render: true,
|
|
817
|
+
...void 0
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
function createRoot(init) {
|
|
821
|
+
const owner = new Owner();
|
|
822
|
+
return compute(owner, !init.length ? init : () => init(() => owner.dispose()), null);
|
|
823
|
+
}
|
|
824
|
+
function runWithOwner(owner, run) {
|
|
825
|
+
try {
|
|
826
|
+
return compute(owner, run, null);
|
|
827
|
+
} catch (error) {
|
|
828
|
+
owner?.handleError(error);
|
|
829
|
+
return void 0;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
function catchError(fn, handler) {
|
|
833
|
+
const owner = new Owner();
|
|
834
|
+
owner.h = owner.h ? [handler, ...owner.h] : [handler];
|
|
835
|
+
try {
|
|
836
|
+
compute(owner, fn, null);
|
|
837
|
+
} catch (error) {
|
|
838
|
+
owner.handleError(error);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// src/store/store.ts
|
|
843
|
+
var $RAW = Symbol(0);
|
|
844
|
+
var $TRACK = Symbol(0);
|
|
845
|
+
var $TARGET = Symbol(0);
|
|
846
|
+
var $PROXY = Symbol(0);
|
|
847
|
+
var STORE_VALUE = "v";
|
|
848
|
+
var STORE_NODE = "n";
|
|
849
|
+
var STORE_HAS = "h";
|
|
850
|
+
function wrap(value) {
|
|
851
|
+
let p = value[$PROXY];
|
|
852
|
+
if (!p)
|
|
853
|
+
Object.defineProperty(value, $PROXY, {
|
|
854
|
+
value: p = new Proxy({ v: value }, proxyTraps),
|
|
855
|
+
writable: true
|
|
856
|
+
});
|
|
857
|
+
return p;
|
|
858
|
+
}
|
|
859
|
+
function isWrappable(obj) {
|
|
860
|
+
return obj != null && typeof obj === "object";
|
|
861
|
+
}
|
|
862
|
+
function unwrap(item, deep = true, set) {
|
|
863
|
+
let result, unwrapped, v, prop;
|
|
864
|
+
if (result = item != null && item[$RAW])
|
|
865
|
+
return result;
|
|
866
|
+
if (!deep)
|
|
867
|
+
return item;
|
|
868
|
+
if (!isWrappable(item) || set?.has(item))
|
|
869
|
+
return item;
|
|
870
|
+
if (!set)
|
|
871
|
+
set = /* @__PURE__ */ new Set();
|
|
872
|
+
set.add(item);
|
|
873
|
+
if (Array.isArray(item)) {
|
|
874
|
+
for (let i = 0, l = item.length; i < l; i++) {
|
|
875
|
+
v = item[i];
|
|
876
|
+
if ((unwrapped = unwrap(v, deep, set)) !== v)
|
|
877
|
+
item[i] = unwrapped;
|
|
878
|
+
}
|
|
879
|
+
} else {
|
|
880
|
+
if (!deep)
|
|
881
|
+
return item;
|
|
882
|
+
const keys = Object.keys(item);
|
|
883
|
+
for (let i = 0, l = keys.length; i < l; i++) {
|
|
884
|
+
prop = keys[i];
|
|
885
|
+
const desc = Object.getOwnPropertyDescriptor(item, prop);
|
|
886
|
+
if (desc.get)
|
|
887
|
+
continue;
|
|
888
|
+
v = item[prop];
|
|
889
|
+
if ((unwrapped = unwrap(v, deep, set)) !== v)
|
|
890
|
+
item[prop] = unwrapped;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
return item;
|
|
894
|
+
}
|
|
895
|
+
function getNodes(target, type) {
|
|
896
|
+
let nodes = target[type];
|
|
897
|
+
if (!nodes)
|
|
898
|
+
target[type] = nodes = /* @__PURE__ */ Object.create(null);
|
|
899
|
+
return nodes;
|
|
900
|
+
}
|
|
901
|
+
function getNode(nodes, property, value, equals = isEqual) {
|
|
902
|
+
if (nodes[property])
|
|
903
|
+
return nodes[property];
|
|
904
|
+
return nodes[property] = new Computation(value, null, {
|
|
905
|
+
equals,
|
|
906
|
+
unobserved() {
|
|
907
|
+
delete nodes[property];
|
|
908
|
+
}
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
function proxyDescriptor(target, property) {
|
|
912
|
+
const desc = Reflect.getOwnPropertyDescriptor(target[STORE_VALUE], property);
|
|
913
|
+
if (!desc || desc.get || !desc.configurable || property === $PROXY)
|
|
914
|
+
return desc;
|
|
915
|
+
delete desc.value;
|
|
916
|
+
delete desc.writable;
|
|
917
|
+
desc.get = () => target[STORE_VALUE][$PROXY][property];
|
|
918
|
+
return desc;
|
|
919
|
+
}
|
|
920
|
+
function trackSelf(target) {
|
|
921
|
+
getObserver() && getNode(getNodes(target, STORE_NODE), $TRACK, void 0, false).read();
|
|
922
|
+
}
|
|
923
|
+
function ownKeys(target) {
|
|
924
|
+
trackSelf(target);
|
|
925
|
+
return Reflect.ownKeys(target[STORE_VALUE]);
|
|
926
|
+
}
|
|
927
|
+
var Writing = /* @__PURE__ */ new Set();
|
|
928
|
+
var proxyTraps = {
|
|
929
|
+
get(target, property, receiver) {
|
|
930
|
+
if (property === $TARGET)
|
|
931
|
+
return target;
|
|
932
|
+
if (property === $RAW)
|
|
933
|
+
return target[STORE_VALUE];
|
|
934
|
+
if (property === $PROXY)
|
|
935
|
+
return receiver;
|
|
936
|
+
if (property === $TRACK) {
|
|
937
|
+
trackSelf(target);
|
|
938
|
+
return receiver;
|
|
939
|
+
}
|
|
940
|
+
const nodes = getNodes(target, STORE_NODE);
|
|
941
|
+
const storeValue = target[STORE_VALUE];
|
|
942
|
+
const tracked = nodes[property];
|
|
943
|
+
if (!tracked) {
|
|
944
|
+
const desc = Object.getOwnPropertyDescriptor(storeValue, property);
|
|
945
|
+
if (desc && desc.get)
|
|
946
|
+
return desc.get.call(receiver);
|
|
947
|
+
}
|
|
948
|
+
if (Writing.has(storeValue)) {
|
|
949
|
+
const value2 = tracked ? tracked.d : storeValue[property];
|
|
950
|
+
return isWrappable(value2) ? (Writing.add(value2[$RAW] || value2), wrap(value2)) : value2;
|
|
951
|
+
}
|
|
952
|
+
let value = tracked ? nodes[property].read() : storeValue[property];
|
|
953
|
+
if (!tracked) {
|
|
954
|
+
if (typeof value === "function" && !storeValue.hasOwnProperty(property)) {
|
|
955
|
+
let proto;
|
|
956
|
+
return !Array.isArray(storeValue) && (proto = Object.getPrototypeOf(storeValue)) && proto !== Object.prototype ? value.bind(storeValue) : value;
|
|
957
|
+
} else if (getObserver()) {
|
|
958
|
+
value = getNode(nodes, property, isWrappable(value) ? wrap(value) : value).read();
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
return isWrappable(value) ? wrap(value) : value;
|
|
962
|
+
},
|
|
963
|
+
has(target, property) {
|
|
964
|
+
if (property === $RAW || property === $PROXY || property === $TRACK || property === "__proto__")
|
|
965
|
+
return true;
|
|
966
|
+
const has = property in target[STORE_VALUE];
|
|
967
|
+
getObserver() && getNode(getNodes(target, STORE_HAS), property, has).read();
|
|
968
|
+
return has;
|
|
969
|
+
},
|
|
970
|
+
set(target, property, value) {
|
|
971
|
+
Writing.has(target[STORE_VALUE]) && setProperty(target[STORE_VALUE], property, unwrap(value, false));
|
|
972
|
+
return true;
|
|
973
|
+
},
|
|
974
|
+
deleteProperty(target, property) {
|
|
975
|
+
Writing.has(target[STORE_VALUE]) && setProperty(target[STORE_VALUE], property, void 0, true);
|
|
976
|
+
return true;
|
|
977
|
+
},
|
|
978
|
+
ownKeys,
|
|
979
|
+
getOwnPropertyDescriptor: proxyDescriptor
|
|
980
|
+
};
|
|
981
|
+
function setProperty(state, property, value, deleting = false) {
|
|
982
|
+
const prev = state[property];
|
|
983
|
+
if (!deleting && prev === value)
|
|
984
|
+
return;
|
|
985
|
+
const len = state.length;
|
|
986
|
+
if (deleting)
|
|
987
|
+
delete state[property];
|
|
988
|
+
else
|
|
989
|
+
state[property] = value;
|
|
990
|
+
const target = state[$PROXY]?.[$TARGET];
|
|
991
|
+
if (!target)
|
|
992
|
+
return;
|
|
993
|
+
if (deleting)
|
|
994
|
+
target[STORE_HAS]?.[property]?.write(false);
|
|
995
|
+
else
|
|
996
|
+
target[STORE_HAS]?.[property]?.write(true);
|
|
997
|
+
const nodes = getNodes(target, STORE_NODE);
|
|
998
|
+
let node;
|
|
999
|
+
if (node = nodes[property])
|
|
1000
|
+
node.write(isWrappable(value) ? wrap(value) : value);
|
|
1001
|
+
Array.isArray(state) && state.length !== len && (node = nodes.length) && node.write(state.length);
|
|
1002
|
+
(node = nodes[$TRACK]) && node.write(void 0);
|
|
1003
|
+
}
|
|
1004
|
+
function createStore(first, second) {
|
|
1005
|
+
const derived = typeof first === "function", store = derived ? second : first, unwrappedStore = unwrap(store, false);
|
|
1006
|
+
const wrappedStore = wrap(unwrappedStore);
|
|
1007
|
+
const setStore = (fn) => {
|
|
1008
|
+
try {
|
|
1009
|
+
Writing.add(unwrappedStore);
|
|
1010
|
+
fn(wrappedStore);
|
|
1011
|
+
} finally {
|
|
1012
|
+
Writing.clear();
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
if (derived) {
|
|
1016
|
+
new EagerComputation(void 0, () => setStore(first));
|
|
1017
|
+
}
|
|
1018
|
+
return [wrappedStore, setStore];
|
|
1019
|
+
}
|
|
1020
|
+
function createProjection(fn, initialValue = {}) {
|
|
1021
|
+
const [store] = createStore(fn, initialValue);
|
|
1022
|
+
return store;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// src/store/reconcile.ts
|
|
1026
|
+
function applyState(next, state, keyFn) {
|
|
1027
|
+
const target = state?.[$TARGET];
|
|
1028
|
+
if (!target)
|
|
1029
|
+
return;
|
|
1030
|
+
const previous = target[STORE_VALUE];
|
|
1031
|
+
if (next === previous)
|
|
1032
|
+
return;
|
|
1033
|
+
Object.defineProperty(next, $PROXY, {
|
|
1034
|
+
value: previous[$PROXY],
|
|
1035
|
+
writable: true
|
|
1036
|
+
});
|
|
1037
|
+
previous[$PROXY] = null;
|
|
1038
|
+
target[STORE_VALUE] = next;
|
|
1039
|
+
if (Array.isArray(previous)) {
|
|
1040
|
+
let changed = false;
|
|
1041
|
+
if (next.length && previous.length && next[0] && keyFn(next[0]) != null) {
|
|
1042
|
+
let i, j, start, end, newEnd, item, newIndicesNext, keyVal;
|
|
1043
|
+
for (start = 0, end = Math.min(previous.length, next.length); start < end && (previous[start] === next[start] || previous[start] && next[start] && keyFn(previous[start]) === keyFn(next[start])); start++) {
|
|
1044
|
+
applyState(next[start], wrap(previous[start]), keyFn);
|
|
1045
|
+
}
|
|
1046
|
+
const temp = new Array(next.length), newIndices = /* @__PURE__ */ new Map();
|
|
1047
|
+
for (end = previous.length - 1, newEnd = next.length - 1; end >= start && newEnd >= start && (previous[end] === next[newEnd] || previous[end] && next[newEnd] && keyFn(previous[end]) === keyFn(next[newEnd])); end--, newEnd--) {
|
|
1048
|
+
temp[newEnd] = previous[end];
|
|
1049
|
+
}
|
|
1050
|
+
if (start > newEnd || start > end) {
|
|
1051
|
+
for (j = start; j <= newEnd; j++) {
|
|
1052
|
+
changed = true;
|
|
1053
|
+
target[STORE_NODE][j]?.write(wrap(next[j]));
|
|
1054
|
+
}
|
|
1055
|
+
for (; j < next.length; j++) {
|
|
1056
|
+
changed = true;
|
|
1057
|
+
const wrapped = wrap(temp[j]);
|
|
1058
|
+
target[STORE_NODE][j]?.write(wrapped);
|
|
1059
|
+
applyState(next[j], wrapped, keyFn);
|
|
1060
|
+
}
|
|
1061
|
+
changed && target[STORE_NODE][$TRACK]?.write(void 0);
|
|
1062
|
+
previous.length !== next.length && target[STORE_NODE].length?.write(next.length);
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
newIndicesNext = new Array(newEnd + 1);
|
|
1066
|
+
for (j = newEnd; j >= start; j--) {
|
|
1067
|
+
item = next[j];
|
|
1068
|
+
keyVal = item ? keyFn(item) : item;
|
|
1069
|
+
i = newIndices.get(keyVal);
|
|
1070
|
+
newIndicesNext[j] = i === void 0 ? -1 : i;
|
|
1071
|
+
newIndices.set(keyVal, j);
|
|
1072
|
+
}
|
|
1073
|
+
for (i = start; i <= end; i++) {
|
|
1074
|
+
item = previous[i];
|
|
1075
|
+
keyVal = item ? keyFn(item) : item;
|
|
1076
|
+
j = newIndices.get(keyVal);
|
|
1077
|
+
if (j !== void 0 && j !== -1) {
|
|
1078
|
+
temp[j] = previous[i];
|
|
1079
|
+
j = newIndicesNext[j];
|
|
1080
|
+
newIndices.set(keyVal, j);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
for (j = start; j < next.length; j++) {
|
|
1084
|
+
if (j in temp) {
|
|
1085
|
+
const wrapped = wrap(temp[j]);
|
|
1086
|
+
target[STORE_NODE][j]?.write(wrapped);
|
|
1087
|
+
applyState(next[j], wrapped, keyFn);
|
|
1088
|
+
} else
|
|
1089
|
+
target[STORE_NODE][j]?.write(wrap(next[j]));
|
|
1090
|
+
}
|
|
1091
|
+
if (start < next.length)
|
|
1092
|
+
changed = true;
|
|
1093
|
+
} else if (previous.length && next.length) {
|
|
1094
|
+
for (let i = 0, len = next.length; i < len; i++) {
|
|
1095
|
+
isWrappable(previous[i]) && applyState(next[i], wrap(previous[i]), keyFn);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
if (previous.length !== next.length) {
|
|
1099
|
+
changed = true;
|
|
1100
|
+
target[STORE_NODE].length?.write(next.length);
|
|
1101
|
+
}
|
|
1102
|
+
changed && target[STORE_NODE][$TRACK]?.write(void 0);
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
let nodes = target[STORE_NODE];
|
|
1106
|
+
if (nodes) {
|
|
1107
|
+
const keys = Object.keys(nodes);
|
|
1108
|
+
for (let i = 0, len = keys.length; i < len; i++) {
|
|
1109
|
+
const node = nodes[keys[i]];
|
|
1110
|
+
const previousValue = unwrap(previous[keys[i]], false);
|
|
1111
|
+
let nextValue = unwrap(next[keys[i]], false);
|
|
1112
|
+
if (previousValue === nextValue)
|
|
1113
|
+
continue;
|
|
1114
|
+
if (!previousValue || !isWrappable(previousValue) || keyFn(previousValue) != null && keyFn(previousValue) !== keyFn(nextValue))
|
|
1115
|
+
node.write(isWrappable(nextValue) ? wrap(nextValue) : nextValue);
|
|
1116
|
+
else
|
|
1117
|
+
applyState(nextValue, wrap(previousValue), keyFn);
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
if (nodes = target[STORE_HAS]) {
|
|
1121
|
+
const keys = Object.keys(nodes);
|
|
1122
|
+
for (let i = 0, len = keys.length; i < len; i++) {
|
|
1123
|
+
nodes[keys[i]].write(keys[i] in next);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
function reconcile(value, key) {
|
|
1128
|
+
return (state) => {
|
|
1129
|
+
const keyFn = typeof key === "string" ? (item) => item[key] : key;
|
|
1130
|
+
if (keyFn(value) !== keyFn(state))
|
|
1131
|
+
throw new Error("Cannot reconcile states with different identity");
|
|
1132
|
+
applyState(value, state, keyFn);
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
// src/store/utilities.ts
|
|
1137
|
+
function trueFn() {
|
|
1138
|
+
return true;
|
|
1139
|
+
}
|
|
1140
|
+
var propTraps = {
|
|
1141
|
+
get(_, property, receiver) {
|
|
1142
|
+
if (property === $PROXY)
|
|
1143
|
+
return receiver;
|
|
1144
|
+
return _.get(property);
|
|
1145
|
+
},
|
|
1146
|
+
has(_, property) {
|
|
1147
|
+
if (property === $PROXY)
|
|
1148
|
+
return true;
|
|
1149
|
+
return _.has(property);
|
|
1150
|
+
},
|
|
1151
|
+
set: trueFn,
|
|
1152
|
+
deleteProperty: trueFn,
|
|
1153
|
+
getOwnPropertyDescriptor(_, property) {
|
|
1154
|
+
return {
|
|
1155
|
+
configurable: true,
|
|
1156
|
+
enumerable: true,
|
|
1157
|
+
get() {
|
|
1158
|
+
return _.get(property);
|
|
1159
|
+
},
|
|
1160
|
+
set: trueFn,
|
|
1161
|
+
deleteProperty: trueFn
|
|
1162
|
+
};
|
|
1163
|
+
},
|
|
1164
|
+
ownKeys(_) {
|
|
1165
|
+
return _.keys();
|
|
1166
|
+
}
|
|
1167
|
+
};
|
|
1168
|
+
function resolveSource(s) {
|
|
1169
|
+
return !(s = typeof s === "function" ? s() : s) ? {} : s;
|
|
1170
|
+
}
|
|
1171
|
+
var $SOURCES = Symbol(0);
|
|
1172
|
+
function merge(...sources) {
|
|
1173
|
+
if (sources.length === 1)
|
|
1174
|
+
return sources[0];
|
|
1175
|
+
let proxy = false;
|
|
1176
|
+
const flattened = [];
|
|
1177
|
+
for (let i = 0; i < sources.length; i++) {
|
|
1178
|
+
const s = sources[i];
|
|
1179
|
+
proxy = proxy || !!s && $PROXY in s;
|
|
1180
|
+
const childSources = !!s && s[$SOURCES];
|
|
1181
|
+
if (childSources)
|
|
1182
|
+
flattened.push(...childSources);
|
|
1183
|
+
else
|
|
1184
|
+
flattened.push(
|
|
1185
|
+
typeof s === "function" ? (proxy = true, createMemo(s)) : s
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
if (SUPPORTS_PROXY && proxy) {
|
|
1189
|
+
return new Proxy(
|
|
1190
|
+
{
|
|
1191
|
+
get(property) {
|
|
1192
|
+
if (property === $SOURCES)
|
|
1193
|
+
return flattened;
|
|
1194
|
+
for (let i = flattened.length - 1; i >= 0; i--) {
|
|
1195
|
+
const s = resolveSource(flattened[i]);
|
|
1196
|
+
if (property in s)
|
|
1197
|
+
return s[property];
|
|
1198
|
+
}
|
|
1199
|
+
},
|
|
1200
|
+
has(property) {
|
|
1201
|
+
for (let i = flattened.length - 1; i >= 0; i--) {
|
|
1202
|
+
if (property in resolveSource(flattened[i]))
|
|
1203
|
+
return true;
|
|
1204
|
+
}
|
|
1205
|
+
return false;
|
|
1206
|
+
},
|
|
1207
|
+
keys() {
|
|
1208
|
+
const keys = [];
|
|
1209
|
+
for (let i = 0; i < flattened.length; i++)
|
|
1210
|
+
keys.push(...Object.keys(resolveSource(flattened[i])));
|
|
1211
|
+
return [...new Set(keys)];
|
|
1212
|
+
}
|
|
1213
|
+
},
|
|
1214
|
+
propTraps
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
1217
|
+
const defined = /* @__PURE__ */ Object.create(null);
|
|
1218
|
+
let nonTargetKey = false;
|
|
1219
|
+
let lastIndex = flattened.length - 1;
|
|
1220
|
+
for (let i = lastIndex; i >= 0; i--) {
|
|
1221
|
+
const source = flattened[i];
|
|
1222
|
+
if (!source) {
|
|
1223
|
+
i === lastIndex && lastIndex--;
|
|
1224
|
+
continue;
|
|
1225
|
+
}
|
|
1226
|
+
const sourceKeys = Object.getOwnPropertyNames(source);
|
|
1227
|
+
for (let j = sourceKeys.length - 1; j >= 0; j--) {
|
|
1228
|
+
const key = sourceKeys[j];
|
|
1229
|
+
if (key === "__proto__" || key === "constructor")
|
|
1230
|
+
continue;
|
|
1231
|
+
if (!defined[key]) {
|
|
1232
|
+
nonTargetKey = nonTargetKey || i !== lastIndex;
|
|
1233
|
+
const desc = Object.getOwnPropertyDescriptor(source, key);
|
|
1234
|
+
defined[key] = desc.get ? {
|
|
1235
|
+
enumerable: true,
|
|
1236
|
+
configurable: true,
|
|
1237
|
+
get: desc.get.bind(source)
|
|
1238
|
+
} : desc;
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
if (!nonTargetKey)
|
|
1243
|
+
return flattened[lastIndex];
|
|
1244
|
+
const target = {};
|
|
1245
|
+
const definedKeys = Object.keys(defined);
|
|
1246
|
+
for (let i = definedKeys.length - 1; i >= 0; i--) {
|
|
1247
|
+
const key = definedKeys[i], desc = defined[key];
|
|
1248
|
+
if (desc.get)
|
|
1249
|
+
Object.defineProperty(target, key, desc);
|
|
1250
|
+
else
|
|
1251
|
+
target[key] = desc.value;
|
|
1252
|
+
}
|
|
1253
|
+
target[$SOURCES] = flattened;
|
|
1254
|
+
return target;
|
|
1255
|
+
}
|
|
1256
|
+
function omit(props, ...keys) {
|
|
1257
|
+
const blocked = new Set(keys);
|
|
1258
|
+
if (SUPPORTS_PROXY && $PROXY in props) {
|
|
1259
|
+
return new Proxy(
|
|
1260
|
+
{
|
|
1261
|
+
get(property) {
|
|
1262
|
+
return blocked.has(property) ? void 0 : props[property];
|
|
1263
|
+
},
|
|
1264
|
+
has(property) {
|
|
1265
|
+
return !blocked.has(property) && property in props;
|
|
1266
|
+
},
|
|
1267
|
+
keys() {
|
|
1268
|
+
return Object.keys(props).filter((k) => !blocked.has(k));
|
|
1269
|
+
}
|
|
1270
|
+
},
|
|
1271
|
+
propTraps
|
|
1272
|
+
);
|
|
1273
|
+
}
|
|
1274
|
+
const result = {};
|
|
1275
|
+
for (const propName of Object.getOwnPropertyNames(props)) {
|
|
1276
|
+
if (!blocked.has(propName)) {
|
|
1277
|
+
const desc = Object.getOwnPropertyDescriptor(props, propName);
|
|
1278
|
+
!desc.get && !desc.set && desc.enumerable && desc.writable && desc.configurable ? result[propName] = desc.value : Object.defineProperty(result, propName, desc);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
return result;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
// src/map.ts
|
|
1285
|
+
function mapArray(list, map, options) {
|
|
1286
|
+
const keyFn = typeof options?.keyed === "function" ? options.keyed : void 0;
|
|
1287
|
+
return Computation.prototype.read.bind(
|
|
1288
|
+
new Computation(
|
|
1289
|
+
[],
|
|
1290
|
+
updateKeyedMap.bind({
|
|
1291
|
+
R: new Owner(),
|
|
1292
|
+
u: 0,
|
|
1293
|
+
T: list,
|
|
1294
|
+
p: [],
|
|
1295
|
+
M: map,
|
|
1296
|
+
n: [],
|
|
1297
|
+
k: [],
|
|
1298
|
+
x: keyFn,
|
|
1299
|
+
f: keyFn || options?.keyed === false ? [] : void 0,
|
|
1300
|
+
j: map.length > 1 ? [] : void 0
|
|
1301
|
+
}),
|
|
1302
|
+
options
|
|
1303
|
+
)
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
function updateKeyedMap() {
|
|
1307
|
+
const newItems = this.T() || [], newLen = newItems.length;
|
|
1308
|
+
newItems[$TRACK];
|
|
1309
|
+
runWithOwner(this.R, () => {
|
|
1310
|
+
let i, j, mapper = this.f ? () => {
|
|
1311
|
+
this.f[j] = new Computation(newItems[j], null);
|
|
1312
|
+
this.j[j] = new Computation(j, null);
|
|
1313
|
+
return this.M(
|
|
1314
|
+
Computation.prototype.read.bind(this.f[j]),
|
|
1315
|
+
Computation.prototype.read.bind(this.j[j])
|
|
1316
|
+
);
|
|
1317
|
+
} : this.j ? () => {
|
|
1318
|
+
const item = newItems[j];
|
|
1319
|
+
this.j[j] = new Computation(j, null);
|
|
1320
|
+
return this.M(() => item, Computation.prototype.read.bind(this.j[j]));
|
|
1321
|
+
} : () => {
|
|
1322
|
+
const item = newItems[j];
|
|
1323
|
+
return this.M(() => item);
|
|
1324
|
+
};
|
|
1325
|
+
if (newLen === 0) {
|
|
1326
|
+
if (this.u !== 0) {
|
|
1327
|
+
this.R.dispose(false);
|
|
1328
|
+
this.k = [];
|
|
1329
|
+
this.p = [];
|
|
1330
|
+
this.n = [];
|
|
1331
|
+
this.u = 0;
|
|
1332
|
+
this.f && (this.f = []);
|
|
1333
|
+
this.j && (this.j = []);
|
|
1334
|
+
}
|
|
1335
|
+
} else if (this.u === 0) {
|
|
1336
|
+
this.n = new Array(newLen);
|
|
1337
|
+
for (j = 0; j < newLen; j++) {
|
|
1338
|
+
this.p[j] = newItems[j];
|
|
1339
|
+
this.n[j] = compute(this.k[j] = new Owner(), mapper, null);
|
|
1340
|
+
}
|
|
1341
|
+
this.u = newLen;
|
|
1342
|
+
} else {
|
|
1343
|
+
let start, end, newEnd, item, key, newIndices, newIndicesNext, temp = new Array(newLen), tempNodes = new Array(newLen), tempRows = this.f ? new Array(newLen) : void 0, tempIndexes = this.j ? new Array(newLen) : void 0;
|
|
1344
|
+
for (start = 0, end = Math.min(this.u, newLen); start < end && (this.p[start] === newItems[start] || this.f && compare(this.x, this.p[start], newItems[start])); start++) {
|
|
1345
|
+
if (this.f)
|
|
1346
|
+
this.f[start].write(newItems[start]);
|
|
1347
|
+
}
|
|
1348
|
+
for (end = this.u - 1, newEnd = newLen - 1; end >= start && newEnd >= start && (this.p[end] === newItems[newEnd] || this.f && compare(this.x, this.p[end], newItems[newEnd])); end--, newEnd--) {
|
|
1349
|
+
temp[newEnd] = this.n[end];
|
|
1350
|
+
tempNodes[newEnd] = this.k[end];
|
|
1351
|
+
tempRows && (tempRows[newEnd] = this.f[end]);
|
|
1352
|
+
tempIndexes && (tempIndexes[newEnd] = this.j[end]);
|
|
1353
|
+
}
|
|
1354
|
+
newIndices = /* @__PURE__ */ new Map();
|
|
1355
|
+
newIndicesNext = new Array(newEnd + 1);
|
|
1356
|
+
for (j = newEnd; j >= start; j--) {
|
|
1357
|
+
item = newItems[j];
|
|
1358
|
+
key = this.x ? this.x(item) : item;
|
|
1359
|
+
i = newIndices.get(key);
|
|
1360
|
+
newIndicesNext[j] = i === void 0 ? -1 : i;
|
|
1361
|
+
newIndices.set(key, j);
|
|
1362
|
+
}
|
|
1363
|
+
for (i = start; i <= end; i++) {
|
|
1364
|
+
item = this.p[i];
|
|
1365
|
+
key = this.x ? this.x(item) : item;
|
|
1366
|
+
j = newIndices.get(key);
|
|
1367
|
+
if (j !== void 0 && j !== -1) {
|
|
1368
|
+
temp[j] = this.n[i];
|
|
1369
|
+
tempNodes[j] = this.k[i];
|
|
1370
|
+
tempRows && (tempRows[j] = this.f[i]);
|
|
1371
|
+
tempIndexes && (tempIndexes[j] = this.j[i]);
|
|
1372
|
+
j = newIndicesNext[j];
|
|
1373
|
+
newIndices.set(key, j);
|
|
1374
|
+
} else
|
|
1375
|
+
this.k[i].dispose();
|
|
1376
|
+
}
|
|
1377
|
+
for (j = start; j < newLen; j++) {
|
|
1378
|
+
if (j in temp) {
|
|
1379
|
+
this.n[j] = temp[j];
|
|
1380
|
+
this.k[j] = tempNodes[j];
|
|
1381
|
+
if (tempRows) {
|
|
1382
|
+
this.f[j] = tempRows[j];
|
|
1383
|
+
this.f[j].write(newItems[j]);
|
|
1384
|
+
}
|
|
1385
|
+
if (tempIndexes) {
|
|
1386
|
+
this.j[j] = tempIndexes[j];
|
|
1387
|
+
this.j[j].write(j);
|
|
1388
|
+
}
|
|
1389
|
+
} else {
|
|
1390
|
+
this.n[j] = compute(this.k[j] = new Owner(), mapper, null);
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
this.n = this.n.slice(0, this.u = newLen);
|
|
1394
|
+
this.p = newItems.slice(0);
|
|
1395
|
+
}
|
|
1396
|
+
});
|
|
1397
|
+
return this.n;
|
|
1398
|
+
}
|
|
1399
|
+
function compare(key, a, b) {
|
|
1400
|
+
return key ? key(a) === key(b) : true;
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
export { $PROXY, $RAW, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, catchError, createAsync, createBoundary, createContext, createEffect, createMemo, createProjection, createRenderEffect, createRoot, createSignal, createStore, createSuspense, flushSync, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual, isPending, isWrappable, latest, mapArray, merge, omit, onCleanup, reconcile, runWithOwner, setContext, untrack, unwrap };
|