@solidjs/signals 0.3.2 → 0.4.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/dist/node.cjs CHANGED
@@ -15,12 +15,6 @@ var ContextNotFoundError = class extends Error {
15
15
  );
16
16
  }
17
17
  };
18
- var EffectError = class extends Error {
19
- constructor(effect, cause) {
20
- super("");
21
- this.cause = cause;
22
- }
23
- };
24
18
 
25
19
  // src/core/constants.ts
26
20
  var STATE_CLEAN = 0;
@@ -45,40 +39,40 @@ function schedule() {
45
39
  if (scheduled)
46
40
  return;
47
41
  scheduled = true;
48
- if (!globalQueue.J)
49
- queueMicrotask(flushSync);
42
+ if (!globalQueue.K)
43
+ queueMicrotask(flush);
50
44
  }
51
45
  var pureQueue = [];
52
46
  var Queue = class {
53
47
  o = null;
54
- J = false;
55
- K = [[], []];
56
- E = [];
48
+ K = false;
49
+ L = [[], []];
50
+ F = [];
57
51
  created = clock;
58
- enqueue(type, node) {
59
- pureQueue.push(node);
52
+ enqueue(type, fn) {
53
+ pureQueue.push(fn);
60
54
  if (type)
61
- this.K[type - 1].push(node);
55
+ this.L[type - 1].push(fn);
62
56
  schedule();
63
57
  }
64
58
  run(type) {
65
59
  if (type === EFFECT_PURE) {
66
- pureQueue.length && runPureQueue(pureQueue);
60
+ pureQueue.length && runQueue(pureQueue, type);
67
61
  pureQueue = [];
68
62
  return;
69
- } else if (this.K[type - 1].length) {
70
- const effects = this.K[type - 1];
71
- this.K[type - 1] = [];
72
- runEffectQueue(effects);
63
+ } else if (this.L[type - 1].length) {
64
+ const effects = this.L[type - 1];
65
+ this.L[type - 1] = [];
66
+ runQueue(effects, type);
73
67
  }
74
- for (let i = 0; i < this.E.length; i++) {
75
- this.E[i].run(type);
68
+ for (let i = 0; i < this.F.length; i++) {
69
+ this.F[i].run(type);
76
70
  }
77
71
  }
78
72
  flush() {
79
- if (this.J)
73
+ if (this.K)
80
74
  return;
81
- this.J = true;
75
+ this.K = true;
82
76
  try {
83
77
  this.run(EFFECT_PURE);
84
78
  incrementClock();
@@ -86,17 +80,17 @@ var Queue = class {
86
80
  this.run(EFFECT_RENDER);
87
81
  this.run(EFFECT_USER);
88
82
  } finally {
89
- this.J = false;
83
+ this.K = false;
90
84
  }
91
85
  }
92
86
  addChild(child) {
93
- this.E.push(child);
87
+ this.F.push(child);
94
88
  child.o = this;
95
89
  }
96
90
  removeChild(child) {
97
- const index = this.E.indexOf(child);
91
+ const index = this.F.indexOf(child);
98
92
  if (index >= 0)
99
- this.E.splice(index, 1);
93
+ this.F.splice(index, 1);
100
94
  }
101
95
  notify(...args) {
102
96
  if (this.o)
@@ -105,57 +99,14 @@ var Queue = class {
105
99
  }
106
100
  };
107
101
  var globalQueue = new Queue();
108
- function flushSync() {
102
+ function flush() {
109
103
  while (scheduled) {
110
104
  globalQueue.flush();
111
105
  }
112
106
  }
113
- function runTop(node) {
114
- const ancestors = [];
115
- for (let current = node; current !== null; current = current.o) {
116
- if (current.a !== STATE_CLEAN) {
117
- ancestors.push(current);
118
- }
119
- }
120
- for (let i = ancestors.length - 1; i >= 0; i--) {
121
- if (ancestors[i].a !== STATE_DISPOSED)
122
- ancestors[i].x();
123
- }
124
- }
125
- function runPureQueue(queue) {
126
- for (let i = 0; i < queue.length; i++) {
127
- if (queue[i].a !== STATE_CLEAN)
128
- runTop(queue[i]);
129
- }
130
- }
131
- function runEffectQueue(queue) {
107
+ function runQueue(queue, type) {
132
108
  for (let i = 0; i < queue.length; i++)
133
- queue[i].V();
134
- }
135
-
136
- // src/core/utils.ts
137
- function isUndefined(value) {
138
- return typeof value === "undefined";
139
- }
140
- function tryCatch(fn) {
141
- try {
142
- const v = fn();
143
- if (v instanceof Promise) {
144
- return v.then(
145
- (v2) => [void 0, v2],
146
- (e) => {
147
- if (e instanceof NotReadyError)
148
- throw e;
149
- return [e];
150
- }
151
- );
152
- }
153
- return [void 0, v];
154
- } catch (e) {
155
- if (e instanceof NotReadyError)
156
- throw e;
157
- return [e];
158
- }
109
+ queue[i](type);
159
110
  }
160
111
 
161
112
  // src/core/owner.ts
@@ -169,17 +120,13 @@ function setOwner(owner) {
169
120
  currentOwner = owner;
170
121
  return out;
171
122
  }
172
- function formatId(prefix, id) {
173
- const num = id.toString(36), len = num.length - 1;
174
- return prefix + (len ? String.fromCharCode(64 + len) : "") + num;
175
- }
176
123
  var Owner = class {
177
124
  // We flatten the owner tree into a linked list so that we don't need a pointer to .firstChild
178
125
  // However, the children are actually added in reverse creation order
179
126
  // See comment at the top of the file for an example of the _nextSibling traversal
180
127
  o = null;
181
128
  m = null;
182
- s = null;
129
+ t = null;
183
130
  a = STATE_CLEAN;
184
131
  l = null;
185
132
  p = defaultContext;
@@ -189,18 +136,18 @@ var Owner = class {
189
136
  constructor(id = null, skipAppend = false) {
190
137
  this.id = id;
191
138
  if (currentOwner) {
192
- if (id == null && currentOwner.id != null)
193
- this.id = currentOwner.getNextChildId();
194
139
  !skipAppend && currentOwner.append(this);
195
140
  }
196
141
  }
197
142
  append(child) {
198
143
  child.o = this;
199
- child.s = this;
144
+ child.t = this;
200
145
  if (this.m)
201
- this.m.s = child;
146
+ this.m.t = child;
202
147
  child.m = this.m;
203
148
  this.m = child;
149
+ if (this.id != null && child.id == null)
150
+ child.id = this.getNextChildId();
204
151
  if (child.p !== this.p) {
205
152
  child.p = { ...this.p, ...child.p };
206
153
  }
@@ -210,27 +157,27 @@ var Owner = class {
210
157
  dispose(self = true) {
211
158
  if (this.a === STATE_DISPOSED)
212
159
  return;
213
- let head = self ? this.s || this.o : this, current = this.m, next = null;
160
+ let head = self ? this.t || this.o : this, current = this.m, next = null;
214
161
  while (current && current.o === this) {
215
162
  current.dispose(true);
216
- current.y();
163
+ current.z();
217
164
  next = current.m;
218
165
  current.m = null;
219
166
  current = next;
220
167
  }
221
168
  this.W = 0;
222
169
  if (self)
223
- this.y();
170
+ this.z();
224
171
  if (current)
225
- current.s = !self ? this : this.s;
172
+ current.t = !self ? this : this.t;
226
173
  if (head)
227
174
  head.m = current;
228
175
  }
229
- y() {
230
- if (this.s)
231
- this.s.m = null;
176
+ z() {
177
+ if (this.t)
178
+ this.t.m = null;
232
179
  this.o = null;
233
- this.s = null;
180
+ this.t = null;
234
181
  this.p = defaultContext;
235
182
  this.a = STATE_DISPOSED;
236
183
  this.emptyDisposal();
@@ -292,6 +239,13 @@ function onCleanup(fn) {
292
239
  }
293
240
  return fn;
294
241
  }
242
+ function formatId(prefix, id) {
243
+ const num = id.toString(36), len = num.length - 1;
244
+ return prefix + (len ? String.fromCharCode(64 + len) : "") + num;
245
+ }
246
+ function isUndefined(value) {
247
+ return typeof value === "undefined";
248
+ }
295
249
 
296
250
  // src/core/flags.ts
297
251
  var ERROR_OFFSET = 0;
@@ -308,6 +262,7 @@ var currentMask = DEFAULT_FLAGS;
308
262
  var newSources = null;
309
263
  var newSourcesIndex = 0;
310
264
  var newFlags = 0;
265
+ var unobserved = [];
311
266
  var notStale = false;
312
267
  var updateCheck = null;
313
268
  var staleCheck = null;
@@ -317,44 +272,47 @@ function getObserver() {
317
272
  var UNCHANGED = Symbol(0);
318
273
  var Computation = class extends Owner {
319
274
  c = null;
320
- e = null;
275
+ d = null;
321
276
  g;
322
- F;
323
- z;
277
+ G;
278
+ A;
324
279
  // Used in __DEV__ mode, hopefully removed in production
325
- ba;
280
+ ca;
326
281
  // Using false is an optimization as an alternative to _equals: () => false
327
282
  // which could enable more efficient DIRTY notification
328
283
  S = isEqual;
329
284
  X;
285
+ _ = false;
330
286
  /** Whether the computation is an error or has ancestors that are unresolved */
331
287
  f = 0;
332
288
  /** Which flags raised by sources are handled, vs. being passed through. */
333
289
  T = DEFAULT_FLAGS;
334
- A = -1;
335
- w = false;
290
+ B = -1;
291
+ x = false;
336
292
  constructor(initialValue, compute2, options) {
337
- super(null, compute2 === null);
338
- this.z = compute2;
293
+ super(options == null ? void 0 : options.id, compute2 === null);
294
+ this.A = compute2;
339
295
  this.a = compute2 ? STATE_DIRTY : STATE_CLEAN;
340
296
  this.f = compute2 && initialValue === void 0 ? UNINITIALIZED_BIT : 0;
341
297
  this.g = initialValue;
342
298
  if ((options == null ? void 0 : options.equals) !== void 0)
343
299
  this.S = options.equals;
300
+ if (options == null ? void 0 : options.pureWrite)
301
+ this._ = true;
344
302
  if (options == null ? void 0 : options.unobserved)
345
303
  this.X = options == null ? void 0 : options.unobserved;
346
304
  }
347
305
  Y() {
348
- if (this.z) {
349
- if (this.f & ERROR_BIT && this.A <= getClock())
306
+ if (this.A) {
307
+ if (this.f & ERROR_BIT && this.B <= getClock())
350
308
  update(this);
351
309
  else
352
- this.x();
310
+ this.y();
353
311
  }
354
312
  track(this);
355
313
  newFlags |= this.f & ~currentMask;
356
314
  if (this.f & ERROR_BIT) {
357
- throw this.F;
315
+ throw this.G;
358
316
  } else {
359
317
  return this.g;
360
318
  }
@@ -374,10 +332,10 @@ var Computation = class extends Owner {
374
332
  * before continuing
375
333
  */
376
334
  wait() {
377
- if (this.z && this.f & ERROR_BIT && this.A <= getClock()) {
335
+ if (this.A && this.f & ERROR_BIT && this.B <= getClock()) {
378
336
  update(this);
379
337
  } else {
380
- this.x();
338
+ this.y();
381
339
  }
382
340
  track(this);
383
341
  if ((notStale || this.f & UNINITIALIZED_BIT) && this.f & LOADING_BIT) {
@@ -391,20 +349,21 @@ var Computation = class extends Owner {
391
349
  /** Update the computation with a new value. */
392
350
  write(value, flags = 0, raw = false) {
393
351
  const newValue = !raw && typeof value === "function" ? value(this.g) : value;
394
- const valueChanged = newValue !== UNCHANGED && (!!(this.f & UNINITIALIZED_BIT) || this.f & LOADING_BIT & ~flags || this.S === false || !this.S(this.g, newValue));
352
+ const valueChanged = newValue !== UNCHANGED && (!!(this.f & UNINITIALIZED_BIT) || // this._stateFlags & LOADING_BIT & ~flags ||
353
+ this.S === false || !this.S(this.g, newValue));
395
354
  if (valueChanged) {
396
355
  this.g = newValue;
397
- this.F = void 0;
356
+ this.G = void 0;
398
357
  }
399
358
  const changedFlagsMask = this.f ^ flags, changedFlags = changedFlagsMask & flags;
400
359
  this.f = flags;
401
- this.A = getClock() + 1;
402
- if (this.e) {
403
- for (let i = 0; i < this.e.length; i++) {
360
+ this.B = getClock() + 1;
361
+ if (this.d) {
362
+ for (let i = 0; i < this.d.length; i++) {
404
363
  if (valueChanged) {
405
- this.e[i].r(STATE_DIRTY);
364
+ this.d[i].r(STATE_DIRTY);
406
365
  } else if (changedFlagsMask) {
407
- this.e[i].Z(changedFlagsMask, changedFlags);
366
+ this.d[i].Z(changedFlagsMask, changedFlags);
408
367
  }
409
368
  }
410
369
  }
@@ -414,13 +373,13 @@ var Computation = class extends Owner {
414
373
  * Set the current node's state, and recursively mark all of this node's observers as STATE_CHECK
415
374
  */
416
375
  r(state, skipQueue) {
417
- if (this.a >= state && !this.w)
376
+ if (this.a >= state && !this.x)
418
377
  return;
419
- this.w = !!skipQueue;
378
+ this.x = !!skipQueue;
420
379
  this.a = state;
421
- if (this.e) {
422
- for (let i = 0; i < this.e.length; i++) {
423
- this.e[i].r(STATE_CHECK, skipQueue);
380
+ if (this.d) {
381
+ for (let i = 0; i < this.d.length; i++) {
382
+ this.d[i].r(STATE_CHECK, skipQueue);
424
383
  }
425
384
  }
426
385
  }
@@ -445,15 +404,15 @@ var Computation = class extends Owner {
445
404
  this.r(STATE_CHECK);
446
405
  } else {
447
406
  this.f ^= deltaFlags;
448
- if (this.e) {
449
- for (let i = 0; i < this.e.length; i++) {
450
- this.e[i].Z(mask, newFlags2);
407
+ if (this.d) {
408
+ for (let i = 0; i < this.d.length; i++) {
409
+ this.d[i].Z(mask, newFlags2);
451
410
  }
452
411
  }
453
412
  }
454
413
  }
455
- L(error) {
456
- this.F = error;
414
+ M(error) {
415
+ this.G = error;
457
416
  this.write(UNCHANGED, this.f & ~LOADING_BIT | ERROR_BIT | UNINITIALIZED_BIT);
458
417
  }
459
418
  /**
@@ -463,8 +422,8 @@ var Computation = class extends Owner {
463
422
  *
464
423
  * This function will ensure that the value and states we read from the computation are up to date
465
424
  */
466
- x() {
467
- if (!this.z) {
425
+ y() {
426
+ if (!this.A) {
468
427
  return;
469
428
  }
470
429
  if (this.a === STATE_DISPOSED) {
@@ -476,7 +435,7 @@ var Computation = class extends Owner {
476
435
  let observerFlags = 0;
477
436
  if (this.a === STATE_CHECK) {
478
437
  for (let i = 0; i < this.c.length; i++) {
479
- this.c[i].x();
438
+ this.c[i].y();
480
439
  observerFlags |= this.c[i].f;
481
440
  if (this.a === STATE_DIRTY) {
482
441
  break;
@@ -493,12 +452,12 @@ var Computation = class extends Owner {
493
452
  /**
494
453
  * Remove ourselves from the owner graph and the computation graph
495
454
  */
496
- y() {
455
+ z() {
497
456
  if (this.a === STATE_DISPOSED)
498
457
  return;
499
458
  if (this.c)
500
459
  removeSourceObservers(this, 0);
501
- super.y();
460
+ super.z();
502
461
  }
503
462
  };
504
463
  function track(computation) {
@@ -511,7 +470,7 @@ function track(computation) {
511
470
  newSources.push(computation);
512
471
  }
513
472
  if (updateCheck) {
514
- updateCheck.g = computation.A > currentObserver.A;
473
+ updateCheck.g = computation.B > currentObserver.B;
515
474
  }
516
475
  }
517
476
  }
@@ -523,13 +482,13 @@ function update(node) {
523
482
  try {
524
483
  node.dispose(false);
525
484
  node.emptyDisposal();
526
- const result = compute(node, node.z, node);
485
+ const result = compute(node, node.A, node);
527
486
  node.write(result, newFlags, true);
528
487
  } catch (error) {
529
488
  if (error instanceof NotReadyError) {
530
489
  node.write(UNCHANGED, newFlags | LOADING_BIT | node.f & UNINITIALIZED_BIT);
531
490
  } else {
532
- node.L(error);
491
+ node.M(error);
533
492
  }
534
493
  } finally {
535
494
  if (newSources) {
@@ -546,37 +505,46 @@ function update(node) {
546
505
  let source;
547
506
  for (let i = newSourcesIndex; i < node.c.length; i++) {
548
507
  source = node.c[i];
549
- if (!source.e)
550
- source.e = [node];
508
+ if (!source.d)
509
+ source.d = [node];
551
510
  else
552
- source.e.push(node);
511
+ source.d.push(node);
553
512
  }
554
513
  } else if (node.c && newSourcesIndex < node.c.length) {
555
514
  removeSourceObservers(node, newSourcesIndex);
556
515
  node.c.length = newSourcesIndex;
557
516
  }
517
+ unobserved.length && notifyUnobserved();
558
518
  newSources = prevSources;
559
519
  newSourcesIndex = prevSourcesIndex;
560
520
  newFlags = prevFlags;
561
- node.A = getClock() + 1;
521
+ node.B = getClock() + 1;
562
522
  node.a = STATE_CLEAN;
563
523
  }
564
524
  }
565
525
  function removeSourceObservers(node, index) {
566
- var _a;
567
526
  let source;
568
527
  let swap;
569
528
  for (let i = index; i < node.c.length; i++) {
570
529
  source = node.c[i];
571
- if (source.e) {
572
- swap = source.e.indexOf(node);
573
- source.e[swap] = source.e[source.e.length - 1];
574
- source.e.pop();
575
- if (!source.e.length)
576
- (_a = source.X) == null ? void 0 : _a.call(source);
530
+ if (source.d) {
531
+ swap = source.d.indexOf(node);
532
+ source.d[swap] = source.d[source.d.length - 1];
533
+ source.d.pop();
534
+ if (!source.d.length)
535
+ unobserved.push(source);
577
536
  }
578
537
  }
579
538
  }
539
+ function notifyUnobserved() {
540
+ var _a, _b;
541
+ for (let i = 0; i < unobserved.length; i++) {
542
+ const source = unobserved[i];
543
+ if (!source.d || !source.d.length)
544
+ (_b = (_a = unobserved[i]).X) == null ? void 0 : _b.call(_a);
545
+ }
546
+ unobserved = [];
547
+ }
580
548
  function isEqual(a, b) {
581
549
  return a === b;
582
550
  }
@@ -648,7 +616,7 @@ function runWithObserver(observer, run) {
648
616
  newFlags | LOADING_BIT | observer.f & UNINITIALIZED_BIT
649
617
  );
650
618
  } else {
651
- observer.L(error);
619
+ observer.M(error);
652
620
  }
653
621
  } finally {
654
622
  if (newSources) {
@@ -663,10 +631,10 @@ function runWithObserver(observer, run) {
663
631
  let source;
664
632
  for (let i = newSourcesIndex; i < observer.c.length; i++) {
665
633
  source = observer.c[i];
666
- if (!source.e)
667
- source.e = [observer];
634
+ if (!source.d)
635
+ source.d = [observer];
668
636
  else
669
- source.e.push(observer);
637
+ source.d.push(observer);
670
638
  }
671
639
  }
672
640
  newSources = prevSources;
@@ -688,119 +656,59 @@ function compute(owner, fn, observer) {
688
656
  notStale = prevNotStale;
689
657
  }
690
658
  }
691
- function flatten(children, options) {
692
- try {
693
- if (typeof children === "function" && !children.length) {
694
- if (options == null ? void 0 : options.doNotUnwrap)
695
- return children;
696
- do {
697
- children = children();
698
- } while (typeof children === "function" && !children.length);
699
- }
700
- if ((options == null ? void 0 : options.skipNonRendered) && (children == null || children === true || children === false || children === ""))
701
- return;
702
- if (Array.isArray(children)) {
703
- let results = [];
704
- if (flattenArray(children, results, options)) {
705
- return () => {
706
- let nested = [];
707
- flattenArray(results, nested, { ...options, doNotUnwrap: false });
708
- return nested;
709
- };
710
- }
711
- return results;
712
- }
713
- return children;
714
- } catch (e) {
715
- if ((options == null ? void 0 : options.skipNonRendered) && e instanceof NotReadyError) {
716
- newFlags |= LOADING_BIT;
717
- return void 0;
718
- }
719
- throw e;
720
- }
721
- }
722
- function flattenArray(children, results = [], options) {
723
- let notReady = null;
724
- let needsUnwrap = false;
725
- for (let i = 0; i < children.length; i++) {
726
- try {
727
- let child = children[i];
728
- if (typeof child === "function" && !child.length) {
729
- if (options == null ? void 0 : options.doNotUnwrap) {
730
- results.push(child);
731
- needsUnwrap = true;
732
- continue;
733
- }
734
- do {
735
- child = child();
736
- } while (typeof child === "function" && !child.length);
737
- }
738
- if (Array.isArray(child)) {
739
- needsUnwrap = flattenArray(child, results, options);
740
- } else if ((options == null ? void 0 : options.skipNonRendered) && (child == null || child === true || child === false || child === "")) {
741
- } else
742
- results.push(child);
743
- } catch (e) {
744
- if (!(e instanceof NotReadyError))
745
- throw e;
746
- notReady = e;
747
- }
748
- }
749
- if (notReady)
750
- throw notReady;
751
- return needsUnwrap;
752
- }
753
659
 
754
660
  // src/core/effect.ts
755
661
  var Effect = class extends Computation {
756
- M;
662
+ U;
757
663
  N;
758
- B;
759
- U = false;
664
+ C;
665
+ V = false;
760
666
  O;
761
- t;
667
+ s;
762
668
  constructor(initialValue, compute2, effect, error, options) {
763
669
  super(initialValue, compute2, options);
764
- this.M = effect;
670
+ this.U = effect;
765
671
  this.N = error;
766
672
  this.O = initialValue;
767
- this.t = (options == null ? void 0 : options.render) ? EFFECT_RENDER : EFFECT_USER;
768
- if (this.t === EFFECT_RENDER) {
769
- this.z = (p) => getClock() > this.h.created && !(this.f & ERROR_BIT) ? latest(() => compute2(p)) : compute2(p);
673
+ this.s = (options == null ? void 0 : options.render) ? EFFECT_RENDER : EFFECT_USER;
674
+ if (this.s === EFFECT_RENDER) {
675
+ this.A = (p) => getClock() > this.h.created && !(this.f & ERROR_BIT) ? latest(() => compute2(p)) : compute2(p);
770
676
  }
771
- this.x();
772
- !(options == null ? void 0 : options.defer) && (this.t === EFFECT_USER ? this.h.enqueue(this.t, this) : this.V());
677
+ this.y();
678
+ !(options == null ? void 0 : options.defer) && (this.s === EFFECT_USER ? this.h.enqueue(this.s, this.u.bind(this)) : this.u(this.s));
773
679
  }
774
680
  write(value, flags = 0) {
775
681
  if (this.a == STATE_DIRTY) {
776
682
  this.f;
777
683
  this.f = flags;
778
- if (this.t === EFFECT_RENDER) {
684
+ if (this.s === EFFECT_RENDER) {
779
685
  this.h.notify(this, LOADING_BIT | ERROR_BIT, flags);
780
686
  }
781
687
  }
782
688
  if (value === UNCHANGED)
783
689
  return this.g;
784
690
  this.g = value;
785
- this.U = true;
691
+ this.V = true;
786
692
  return value;
787
693
  }
788
694
  r(state, skipQueue) {
789
695
  if (this.a >= state || skipQueue)
790
696
  return;
791
697
  if (this.a === STATE_CLEAN)
792
- this.h.enqueue(this.t, this);
698
+ this.h.enqueue(this.s, this.u.bind(this));
793
699
  this.a = state;
794
700
  }
795
- L(error) {
796
- var _a;
797
- this.F = error;
798
- (_a = this.B) == null ? void 0 : _a.call(this);
701
+ M(error) {
702
+ this.G = error;
799
703
  this.h.notify(this, LOADING_BIT, 0);
800
704
  this.f = ERROR_BIT;
801
- if (this.t === EFFECT_USER) {
705
+ if (this.s === EFFECT_USER) {
802
706
  try {
803
- return this.N ? this.B = this.N(error) : console.error(new EffectError(this.M, error));
707
+ return this.N ? this.N(error, () => {
708
+ var _a;
709
+ (_a = this.C) == null ? void 0 : _a.call(this);
710
+ this.C = void 0;
711
+ }) : console.error(error);
804
712
  } catch (e) {
805
713
  error = e;
806
714
  }
@@ -808,190 +716,80 @@ var Effect = class extends Computation {
808
716
  if (!this.h.notify(this, ERROR_BIT, ERROR_BIT))
809
717
  throw error;
810
718
  }
811
- y() {
719
+ z() {
812
720
  var _a;
813
721
  if (this.a === STATE_DISPOSED)
814
722
  return;
815
- this.M = void 0;
723
+ this.U = void 0;
816
724
  this.O = void 0;
817
725
  this.N = void 0;
818
- (_a = this.B) == null ? void 0 : _a.call(this);
819
- this.B = void 0;
820
- super.y();
726
+ (_a = this.C) == null ? void 0 : _a.call(this);
727
+ this.C = void 0;
728
+ super.z();
821
729
  }
822
- V() {
730
+ u(type) {
823
731
  var _a;
824
- if (this.U && this.a !== STATE_DISPOSED) {
825
- (_a = this.B) == null ? void 0 : _a.call(this);
826
- try {
827
- this.B = this.M(this.g, this.O);
828
- } catch (e) {
829
- if (!this.h.notify(this, ERROR_BIT, ERROR_BIT))
830
- throw e;
831
- } finally {
832
- this.O = this.g;
833
- this.U = false;
732
+ if (type) {
733
+ if (this.V && this.a !== STATE_DISPOSED) {
734
+ (_a = this.C) == null ? void 0 : _a.call(this);
735
+ try {
736
+ this.C = this.U(this.g, this.O);
737
+ } catch (e) {
738
+ if (!this.h.notify(this, ERROR_BIT, ERROR_BIT))
739
+ throw e;
740
+ } finally {
741
+ this.O = this.g;
742
+ this.V = false;
743
+ }
834
744
  }
835
- }
745
+ } else
746
+ this.a !== STATE_CLEAN && runTop(this);
836
747
  }
837
748
  };
838
749
  var EagerComputation = class extends Computation {
839
750
  constructor(initialValue, compute2, options) {
840
751
  super(initialValue, compute2, options);
841
- !(options == null ? void 0 : options.defer) && this.x();
752
+ !(options == null ? void 0 : options.defer) && this.y();
842
753
  }
843
754
  r(state, skipQueue) {
844
- if (this.a >= state && !this.w)
755
+ if (this.a >= state && !this.x)
845
756
  return;
846
- if (!skipQueue && (this.a === STATE_CLEAN || this.a === STATE_CHECK && this.w))
847
- this.h.enqueue(EFFECT_PURE, this);
757
+ if (!skipQueue && (this.a === STATE_CLEAN || this.a === STATE_CHECK && this.x))
758
+ this.h.enqueue(EFFECT_PURE, this.u.bind(this));
848
759
  super.r(state, skipQueue);
849
760
  }
761
+ u() {
762
+ this.a !== STATE_CLEAN && runTop(this);
763
+ }
850
764
  };
851
- var ProjectionComputation = class extends Computation {
765
+ var FirewallComputation = class extends Computation {
766
+ firewall = true;
852
767
  constructor(compute2) {
853
768
  super(void 0, compute2);
854
769
  }
855
770
  r(state, skipQueue) {
856
- if (this.a >= state && !this.w)
771
+ if (this.a >= state && !this.x)
857
772
  return;
858
- if (!skipQueue && (this.a === STATE_CLEAN || this.a === STATE_CHECK && this.w))
859
- this.h.enqueue(EFFECT_PURE, this);
773
+ if (!skipQueue && (this.a === STATE_CLEAN || this.a === STATE_CHECK && this.x))
774
+ this.h.enqueue(EFFECT_PURE, this.u.bind(this));
860
775
  super.r(state, true);
861
- this.w = !!skipQueue;
776
+ this.x = !!skipQueue;
862
777
  }
863
- };
864
-
865
- // src/core/boundaries.ts
866
- var BoundaryComputation = class extends EagerComputation {
867
- G;
868
- constructor(compute2, propagationMask) {
869
- super(void 0, compute2, { defer: true });
870
- this.G = propagationMask;
871
- }
872
- write(value, flags) {
873
- super.write(value, flags & ~this.G);
874
- if (this.G & LOADING_BIT && !(this.f & UNINITIALIZED_BIT)) {
875
- flags &= ~LOADING_BIT;
876
- }
877
- this.h.notify(this, this.G, flags);
878
- return this.g;
778
+ u() {
779
+ this.a !== STATE_CLEAN && runTop(this);
879
780
  }
880
781
  };
881
- function createBoundChildren(owner, fn, queue, mask) {
882
- const parentQueue = owner.h;
883
- parentQueue.addChild(owner.h = queue);
884
- onCleanup(() => parentQueue.removeChild(owner.h));
885
- return compute(
886
- owner,
887
- () => {
888
- const c = new Computation(void 0, fn);
889
- return new BoundaryComputation(() => flatten(c.wait()), mask);
890
- },
891
- null
892
- );
893
- }
894
- var ConditionalQueue = class extends Queue {
895
- n;
896
- P = /* @__PURE__ */ new Set();
897
- Q = /* @__PURE__ */ new Set();
898
- constructor(disabled) {
899
- super();
900
- this.n = disabled;
901
- }
902
- run(type) {
903
- if (!type || this.n.read())
904
- return;
905
- return super.run(type);
906
- }
907
- notify(node, type, flags) {
908
- if (this.n.read()) {
909
- if (type === LOADING_BIT) {
910
- flags & LOADING_BIT ? this.Q.add(node) : this.Q.delete(node);
911
- }
912
- if (type === ERROR_BIT) {
913
- flags & ERROR_BIT ? this.P.add(node) : this.P.delete(node);
914
- }
915
- return true;
782
+ function runTop(node) {
783
+ const ancestors = [];
784
+ for (let current = node; current !== null; current = current.o) {
785
+ if (current.a !== STATE_CLEAN) {
786
+ ancestors.push(current);
916
787
  }
917
- return super.notify(node, type, flags);
918
- }
919
- };
920
- var CollectionQueue = class extends Queue {
921
- R;
922
- b = /* @__PURE__ */ new Set();
923
- n = new Computation(false, null);
924
- constructor(type) {
925
- super();
926
- this.R = type;
927
788
  }
928
- run(type) {
929
- if (!type || this.n.read())
930
- return;
931
- return super.run(type);
932
- }
933
- notify(node, type, flags) {
934
- if (!(type & this.R))
935
- return super.notify(node, type, flags);
936
- if (flags & this.R) {
937
- this.b.add(node);
938
- if (this.b.size === 1)
939
- this.n.write(true);
940
- } else {
941
- this.b.delete(node);
942
- if (this.b.size === 0)
943
- this.n.write(false);
944
- }
945
- type &= ~this.R;
946
- return type ? super.notify(node, type, flags) : true;
789
+ for (let i = ancestors.length - 1; i >= 0; i--) {
790
+ if (ancestors[i].a !== STATE_DISPOSED)
791
+ ancestors[i].y();
947
792
  }
948
- };
949
- function createBoundary(fn, condition) {
950
- const owner = new Owner();
951
- const queue = new ConditionalQueue(new Computation(void 0, () => condition() === "hidden" /* HIDDEN */));
952
- const tree = createBoundChildren(owner, fn, queue, 0);
953
- new EagerComputation(void 0, () => {
954
- const disabled = queue.n.read();
955
- tree.G = disabled ? ERROR_BIT | LOADING_BIT : 0;
956
- if (!disabled) {
957
- queue.Q.forEach((node) => queue.notify(node, LOADING_BIT, LOADING_BIT));
958
- queue.P.forEach((node) => queue.notify(node, ERROR_BIT, ERROR_BIT));
959
- queue.Q.clear();
960
- queue.P.clear();
961
- }
962
- });
963
- return () => queue.n.read() ? void 0 : tree.read();
964
- }
965
- function createCollectionBoundary(type, fn, fallback) {
966
- const owner = new Owner();
967
- const queue = new CollectionQueue(type);
968
- const tree = createBoundChildren(owner, fn, queue, type);
969
- const decision = new Computation(void 0, () => {
970
- if (!queue.n.read()) {
971
- const resolved = tree.read();
972
- if (!queue.n.read())
973
- return resolved;
974
- }
975
- return fallback(queue);
976
- });
977
- return decision.read.bind(decision);
978
- }
979
- function createSuspense(fn, fallback) {
980
- return createCollectionBoundary(LOADING_BIT, fn, () => fallback());
981
- }
982
- function createErrorBoundary(fn, fallback) {
983
- return createCollectionBoundary(
984
- ERROR_BIT,
985
- fn,
986
- (queue) => fallback(queue.b.values().next().value.F, () => {
987
- var _a;
988
- incrementClock();
989
- for (let node of queue.b) {
990
- node.a = STATE_DIRTY;
991
- (_a = node.h) == null ? void 0 : _a.enqueue(node.t, node);
992
- }
993
- })
994
- );
995
793
  }
996
794
 
997
795
  // src/signals.ts
@@ -1007,7 +805,13 @@ function createSignal(first, second, third) {
1007
805
  });
1008
806
  return [() => memo()[0](), (value) => memo()[1](value)];
1009
807
  }
1010
- const node = new Computation(first, null, second);
808
+ const o = getOwner();
809
+ const needsId = (o == null ? void 0 : o.id) != null;
810
+ const node = new Computation(
811
+ first,
812
+ null,
813
+ needsId ? { id: o.getNextChildId(), ...second } : second
814
+ );
1011
815
  return [node.read.bind(node), node.write.bind(node)];
1012
816
  }
1013
817
  function createMemo(compute2, value, options) {
@@ -1034,10 +838,12 @@ function createMemo(compute2, value, options) {
1034
838
  };
1035
839
  }
1036
840
  function createAsync(compute2, value, options) {
841
+ let refreshing = false;
1037
842
  const node = new EagerComputation(
1038
843
  value,
1039
844
  (p) => {
1040
- const source = compute2(p);
845
+ const source = compute2(p, refreshing);
846
+ refreshing = false;
1041
847
  const isPromise = source instanceof Promise;
1042
848
  const iterator = source[Symbol.asyncIterator];
1043
849
  if (!isPromise && !iterator) {
@@ -1055,7 +861,7 @@ function createAsync(compute2, value, options) {
1055
861
  (error) => {
1056
862
  if (abort)
1057
863
  return;
1058
- node.L(error);
864
+ node.M(error);
1059
865
  }
1060
866
  );
1061
867
  } else {
@@ -1077,14 +883,20 @@ function createAsync(compute2, value, options) {
1077
883
  },
1078
884
  options
1079
885
  );
1080
- return node.wait.bind(node);
886
+ const read = node.wait.bind(node);
887
+ read.refresh = () => {
888
+ node.a = STATE_DIRTY;
889
+ refreshing = true;
890
+ node.y();
891
+ };
892
+ return read;
1081
893
  }
1082
- function createEffect(compute2, effect, error, value, options) {
894
+ function createEffect(compute2, effect, value, options) {
1083
895
  void new Effect(
1084
896
  value,
1085
897
  compute2,
1086
- effect,
1087
- error,
898
+ effect.effect ? effect.effect : effect,
899
+ effect.error,
1088
900
  options
1089
901
  );
1090
902
  }
@@ -1117,101 +929,98 @@ function resolve(fn) {
1117
929
  });
1118
930
  });
1119
931
  }
932
+ function tryCatch(fn) {
933
+ try {
934
+ const v = fn();
935
+ if (v instanceof Promise) {
936
+ return v.then(
937
+ (v2) => [void 0, v2],
938
+ (e) => {
939
+ if (e instanceof NotReadyError)
940
+ throw e;
941
+ return [e];
942
+ }
943
+ );
944
+ }
945
+ return [void 0, v];
946
+ } catch (e) {
947
+ if (e instanceof NotReadyError)
948
+ throw e;
949
+ return [e];
950
+ }
951
+ }
952
+ function transition(fn) {
953
+ }
954
+ function createOptimistic(initial, compute2, options) {
955
+ return [];
956
+ }
1120
957
 
1121
958
  // src/store/projection.ts
1122
959
  function createProjection(fn, initialValue = {}) {
1123
- const [store] = createStore(fn, initialValue);
1124
- return store;
1125
- }
1126
- function wrapProjection(fn, store, setStore) {
1127
- const node = new ProjectionComputation(() => {
1128
- setStore(fn);
960
+ let wrappedStore;
961
+ const node = new FirewallComputation(() => {
962
+ storeSetter(wrappedStore, fn);
1129
963
  });
1130
- const wrapped = /* @__PURE__ */ new WeakMap();
1131
- return [wrap(store, node, wrapped), setStore];
1132
- }
1133
- function wrap(source, node, wrapped) {
1134
- if (wrapped.has(source))
1135
- return wrapped.get(source);
1136
- const wrap3 = new Proxy(source, {
1137
- get(target, property) {
1138
- node.read();
1139
- const v = target[property];
1140
- return isWrappable(v) ? wrap3(v, node, wrapped) : v;
1141
- },
1142
- set() {
1143
- throw new Error("Projections are readonly");
1144
- },
1145
- deleteProperty() {
1146
- throw new Error("Projections are readonly");
964
+ const wrappedMap = /* @__PURE__ */ new WeakMap();
965
+ const traps = {
966
+ ...storeTraps,
967
+ get(target, property, receiver) {
968
+ const o = getOwner();
969
+ (!o || o !== node) && node.wait();
970
+ return storeTraps.get(target, property, receiver);
1147
971
  }
1148
- });
1149
- wrapped.set(source, wrap3);
1150
- return wrap3;
972
+ };
973
+ function wrapProjection(source) {
974
+ var _a;
975
+ if (wrappedMap.has(source))
976
+ return wrappedMap.get(source);
977
+ if (((_a = source[$TARGET]) == null ? void 0 : _a[STORE_WRAP]) === wrapProjection)
978
+ return source;
979
+ const wrapped = createStoreProxy(source, traps, {
980
+ [STORE_WRAP]: wrapProjection,
981
+ [STORE_LOOKUP]: wrappedMap
982
+ });
983
+ wrappedMap.set(source, wrapped);
984
+ return wrapped;
985
+ }
986
+ return wrappedStore = wrapProjection(initialValue);
1151
987
  }
1152
988
 
1153
989
  // src/store/store.ts
1154
- var $RAW = Symbol(0);
1155
990
  var $TRACK = Symbol(0);
1156
991
  var $DEEP = Symbol(0);
1157
992
  var $TARGET = Symbol(0);
1158
993
  var $PROXY = Symbol(0);
994
+ var $DELETED = Symbol(0);
1159
995
  var PARENTS = /* @__PURE__ */ new WeakMap();
1160
996
  var STORE_VALUE = "v";
997
+ var STORE_OVERRIDE = "o";
1161
998
  var STORE_NODE = "n";
1162
999
  var STORE_HAS = "h";
1163
- function wrap2(value) {
1164
- let p = value[$PROXY];
1165
- if (!p) {
1166
- let target;
1167
- if (Array.isArray(value)) {
1168
- target = [];
1169
- target.v = value;
1170
- } else
1171
- target = { v: value };
1172
- Object.defineProperty(value, $PROXY, {
1173
- value: p = new Proxy(target, proxyTraps),
1174
- writable: true
1175
- });
1176
- }
1000
+ var STORE_WRAP = "w";
1001
+ var STORE_LOOKUP = "l";
1002
+ function createStoreProxy(value, traps = storeTraps, extend) {
1003
+ let newTarget;
1004
+ if (Array.isArray(value)) {
1005
+ newTarget = [];
1006
+ newTarget.v = value;
1007
+ } else
1008
+ newTarget = { v: value };
1009
+ extend && Object.assign(newTarget, extend);
1010
+ return newTarget[$PROXY] = new Proxy(newTarget, traps);
1011
+ }
1012
+ var storeLookup = /* @__PURE__ */ new WeakMap();
1013
+ function wrap(value, target) {
1014
+ if (target == null ? void 0 : target[STORE_WRAP])
1015
+ return target[STORE_WRAP](value, target);
1016
+ let p = value[$PROXY] || storeLookup.get(value);
1017
+ if (!p)
1018
+ storeLookup.set(value, p = createStoreProxy(value));
1177
1019
  return p;
1178
1020
  }
1179
1021
  function isWrappable(obj) {
1180
1022
  return obj != null && typeof obj === "object" && !Object.isFrozen(obj);
1181
1023
  }
1182
- function unwrap(item, deep2 = true, set) {
1183
- let result, unwrapped, v, prop;
1184
- if (result = item != null && item[$RAW])
1185
- return result;
1186
- if (!deep2)
1187
- return item;
1188
- if (!isWrappable(item) || (set == null ? void 0 : set.has(item)))
1189
- return item;
1190
- if (!set)
1191
- set = /* @__PURE__ */ new Set();
1192
- set.add(item);
1193
- if (Array.isArray(item)) {
1194
- for (let i = 0, l = item.length; i < l; i++) {
1195
- v = item[i];
1196
- if ((unwrapped = unwrap(v, deep2, set)) !== v)
1197
- item[i] = unwrapped;
1198
- }
1199
- } else {
1200
- if (!deep2)
1201
- return item;
1202
- const keys = Object.keys(item);
1203
- for (let i = 0, l = keys.length; i < l; i++) {
1204
- prop = keys[i];
1205
- const desc = Object.getOwnPropertyDescriptor(item, prop);
1206
- if (desc.get)
1207
- continue;
1208
- v = item[prop];
1209
- if ((unwrapped = unwrap(v, deep2, set)) !== v)
1210
- item[prop] = unwrapped;
1211
- }
1212
- }
1213
- return item;
1214
- }
1215
1024
  function getNodes(target, type) {
1216
1025
  let nodes = target[type];
1217
1026
  if (!nodes)
@@ -1228,31 +1037,38 @@ function getNode(nodes, property, value, equals = isEqual) {
1228
1037
  }
1229
1038
  });
1230
1039
  }
1231
- function proxyDescriptor(target, property) {
1232
- if (property === $PROXY)
1233
- return { value: target[$PROXY], writable: true, configurable: true };
1234
- const desc = Reflect.getOwnPropertyDescriptor(target[STORE_VALUE], property);
1235
- if (!desc || desc.get || !desc.configurable)
1236
- return desc;
1237
- delete desc.value;
1238
- delete desc.writable;
1239
- desc.get = () => target[STORE_VALUE][$PROXY][property];
1240
- return desc;
1241
- }
1242
1040
  function trackSelf(target, symbol = $TRACK) {
1243
1041
  getObserver() && getNode(getNodes(target, STORE_NODE), symbol, void 0, false).read();
1244
1042
  }
1245
- function ownKeys(target) {
1246
- trackSelf(target);
1247
- return Reflect.ownKeys(target[STORE_VALUE]);
1043
+ function getKeys(source, override, enumerable = true) {
1044
+ const baseKeys = untrack(() => enumerable ? Object.keys(source) : Reflect.ownKeys(source));
1045
+ if (!override)
1046
+ return baseKeys;
1047
+ const keys = new Set(baseKeys);
1048
+ const overrides = Reflect.ownKeys(override);
1049
+ for (const key of overrides) {
1050
+ if (override[key] !== $DELETED)
1051
+ keys.add(key);
1052
+ else
1053
+ keys.delete(key);
1054
+ }
1055
+ return Array.from(keys);
1056
+ }
1057
+ function getPropertyDescriptor(source, override, property) {
1058
+ let value = source;
1059
+ if (override && property in override) {
1060
+ if (value[property] === $DELETED)
1061
+ return void 0;
1062
+ if (!(property in value))
1063
+ value = override;
1064
+ }
1065
+ return Reflect.getOwnPropertyDescriptor(value, property);
1248
1066
  }
1249
1067
  var Writing = null;
1250
- var proxyTraps = {
1068
+ var storeTraps = {
1251
1069
  get(target, property, receiver) {
1252
1070
  if (property === $TARGET)
1253
1071
  return target;
1254
- if (property === $RAW)
1255
- return target[STORE_VALUE];
1256
1072
  if (property === $PROXY)
1257
1073
  return receiver;
1258
1074
  if (property === $TRACK || property === $DEEP) {
@@ -1260,94 +1076,160 @@ var proxyTraps = {
1260
1076
  return receiver;
1261
1077
  }
1262
1078
  const nodes = getNodes(target, STORE_NODE);
1263
- const storeValue = target[STORE_VALUE];
1264
1079
  const tracked = nodes[property];
1080
+ const overridden = target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE];
1081
+ const proxySource = !!target[STORE_VALUE][$TARGET];
1082
+ const storeValue = overridden ? target[STORE_OVERRIDE] : target[STORE_VALUE];
1265
1083
  if (!tracked) {
1266
1084
  const desc = Object.getOwnPropertyDescriptor(storeValue, property);
1267
1085
  if (desc && desc.get)
1268
1086
  return desc.get.call(receiver);
1269
1087
  }
1270
- if (Writing == null ? void 0 : Writing.has(storeValue)) {
1271
- const value2 = tracked ? tracked.g : storeValue[property];
1272
- return isWrappable(value2) ? (Writing.add(value2[$RAW] || value2), wrap2(value2)) : value2;
1273
- }
1274
- let value = tracked ? nodes[property].read() : storeValue[property];
1088
+ if (Writing == null ? void 0 : Writing.has(receiver)) {
1089
+ let value2 = tracked && (overridden || !proxySource) ? tracked.g : storeValue[property];
1090
+ value2 === $DELETED && (value2 = void 0);
1091
+ if (!isWrappable(value2))
1092
+ return value2;
1093
+ const wrapped = wrap(value2, target);
1094
+ Writing.add(wrapped);
1095
+ return wrapped;
1096
+ }
1097
+ let value = tracked ? overridden || !proxySource ? nodes[property].read() : (nodes[property].read(), storeValue[property]) : storeValue[property];
1098
+ value === $DELETED && (value = void 0);
1275
1099
  if (!tracked) {
1276
- if (typeof value === "function" && !storeValue.hasOwnProperty(property)) {
1100
+ if (!overridden && typeof value === "function" && !storeValue.hasOwnProperty(property)) {
1277
1101
  let proto;
1278
- return !Array.isArray(storeValue) && (proto = Object.getPrototypeOf(storeValue)) && proto !== Object.prototype ? value.bind(storeValue) : value;
1102
+ return !Array.isArray(target[STORE_VALUE]) && (proto = Object.getPrototypeOf(target[STORE_VALUE])) && proto !== Object.prototype ? value.bind(storeValue) : value;
1279
1103
  } else if (getObserver()) {
1280
- return getNode(nodes, property, isWrappable(value) ? wrap2(value) : value).read();
1104
+ return getNode(nodes, property, isWrappable(value) ? wrap(value, target) : value).read();
1281
1105
  }
1282
1106
  }
1283
- return isWrappable(value) ? wrap2(value) : value;
1107
+ return isWrappable(value) ? wrap(value, target) : value;
1284
1108
  },
1285
1109
  has(target, property) {
1286
- if (property === $RAW || property === $PROXY || property === $TRACK || property === "__proto__")
1110
+ if (property === $PROXY || property === $TRACK || property === "__proto__")
1287
1111
  return true;
1288
- const has = property in target[STORE_VALUE];
1112
+ const has = target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE] ? target[STORE_OVERRIDE][property] !== $DELETED : property in target[STORE_VALUE];
1289
1113
  getObserver() && getNode(getNodes(target, STORE_HAS), property, has).read();
1290
1114
  return has;
1291
1115
  },
1292
- set(target, property, value) {
1293
- (Writing == null ? void 0 : Writing.has(target[STORE_VALUE])) && setProperty(target[STORE_VALUE], property, unwrap(value, false));
1116
+ set(target, property, rawValue) {
1117
+ const store = target[$PROXY];
1118
+ if (Writing == null ? void 0 : Writing.has(target[$PROXY])) {
1119
+ untrack(() => {
1120
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1121
+ const state = target[STORE_VALUE];
1122
+ const base = state[property];
1123
+ const prev = ((_a = target[STORE_OVERRIDE]) == null ? void 0 : _a[property]) || base;
1124
+ const value = ((_b = rawValue == null ? void 0 : rawValue[$TARGET]) == null ? void 0 : _b[STORE_VALUE]) ?? rawValue;
1125
+ if (prev === value)
1126
+ return true;
1127
+ const len = ((_c = target[STORE_OVERRIDE]) == null ? void 0 : _c.length) || state.length;
1128
+ if (value !== void 0 && value === base)
1129
+ delete target[STORE_OVERRIDE][property];
1130
+ else
1131
+ (target[STORE_OVERRIDE] || (target[STORE_OVERRIDE] = /* @__PURE__ */ Object.create(null)))[property] = value;
1132
+ const wrappable = isWrappable(value);
1133
+ if (isWrappable(prev)) {
1134
+ const parents = PARENTS.get(prev);
1135
+ parents && (parents instanceof Set ? parents.delete(store) : PARENTS.delete(prev));
1136
+ }
1137
+ if (recursivelyNotify(store, storeLookup) && wrappable)
1138
+ recursivelyAddParent(value, store);
1139
+ (_e = (_d = target[STORE_HAS]) == null ? void 0 : _d[property]) == null ? void 0 : _e.write(true);
1140
+ const nodes = getNodes(target, STORE_NODE);
1141
+ (_f = nodes[property]) == null ? void 0 : _f.write(wrappable ? wrap(value, target) : value);
1142
+ if (Array.isArray(state)) {
1143
+ const index = parseInt(property) + 1;
1144
+ if (index > len)
1145
+ (_g = nodes.length) == null ? void 0 : _g.write(index);
1146
+ }
1147
+ (_h = nodes[$TRACK]) == null ? void 0 : _h.write(void 0);
1148
+ });
1149
+ }
1294
1150
  return true;
1295
1151
  },
1296
1152
  deleteProperty(target, property) {
1297
- (Writing == null ? void 0 : Writing.has(target[STORE_VALUE])) && setProperty(target[STORE_VALUE], property, void 0, true);
1153
+ var _a;
1154
+ if ((Writing == null ? void 0 : Writing.has(target[$PROXY])) && ((_a = target[STORE_OVERRIDE]) == null ? void 0 : _a[property]) !== $DELETED) {
1155
+ untrack(() => {
1156
+ var _a2, _b, _c, _d, _e;
1157
+ const prev = ((_a2 = target[STORE_OVERRIDE]) == null ? void 0 : _a2[property]) || target[STORE_VALUE][property];
1158
+ if (property in target[STORE_VALUE]) {
1159
+ (target[STORE_OVERRIDE] || (target[STORE_OVERRIDE] = /* @__PURE__ */ Object.create(null)))[property] = $DELETED;
1160
+ } else if (target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE]) {
1161
+ delete target[STORE_OVERRIDE][property];
1162
+ } else
1163
+ return true;
1164
+ if (isWrappable(prev)) {
1165
+ const parents = PARENTS.get(prev);
1166
+ parents && (parents instanceof Set ? parents.delete(target) : PARENTS.delete(prev));
1167
+ }
1168
+ (_c = (_b = target[STORE_HAS]) == null ? void 0 : _b[property]) == null ? void 0 : _c.write(false);
1169
+ const nodes = getNodes(target, STORE_NODE);
1170
+ (_d = nodes[property]) == null ? void 0 : _d.write(void 0);
1171
+ (_e = nodes[$TRACK]) == null ? void 0 : _e.write(void 0);
1172
+ });
1173
+ }
1298
1174
  return true;
1299
1175
  },
1300
- ownKeys,
1301
- getOwnPropertyDescriptor: proxyDescriptor,
1176
+ ownKeys(target) {
1177
+ trackSelf(target);
1178
+ return getKeys(target[STORE_VALUE], target[STORE_OVERRIDE], false);
1179
+ },
1180
+ getOwnPropertyDescriptor(target, property) {
1181
+ if (property === $PROXY)
1182
+ return { value: target[$PROXY], writable: true, configurable: true };
1183
+ return getPropertyDescriptor(target[STORE_VALUE], target[STORE_OVERRIDE], property);
1184
+ },
1302
1185
  getPrototypeOf(target) {
1303
1186
  return Object.getPrototypeOf(target[STORE_VALUE]);
1304
1187
  }
1305
1188
  };
1306
- function setProperty(state, property, value, deleting = false) {
1307
- var _a, _b, _c, _d, _e, _f, _g, _h;
1308
- const prev = state[property];
1309
- if (!deleting && prev === value)
1310
- return;
1311
- const len = state.length;
1312
- if (deleting)
1313
- delete state[property];
1314
- else
1315
- state[property] = value;
1316
- const wrappable = isWrappable(value);
1317
- if (isWrappable(prev)) {
1318
- const parents = PARENTS.get(prev);
1319
- parents && (parents instanceof Set ? parents.delete(state) : PARENTS.delete(prev));
1320
- }
1321
- if (recursivelyNotify(state) && wrappable)
1322
- recursivelyAddParent(value[$RAW] || value, state);
1323
- const target = (_a = state[$PROXY]) == null ? void 0 : _a[$TARGET];
1324
- if (!target)
1325
- return;
1326
- if (deleting)
1327
- (_c = (_b = target[STORE_HAS]) == null ? void 0 : _b[property]) == null ? void 0 : _c.write(false);
1328
- else
1329
- (_e = (_d = target[STORE_HAS]) == null ? void 0 : _d[property]) == null ? void 0 : _e.write(true);
1330
- const nodes = getNodes(target, STORE_NODE);
1331
- (_f = nodes[property]) == null ? void 0 : _f.write(wrappable ? wrap2(value) : value);
1332
- Array.isArray(state) && state.length !== len && ((_g = nodes.length) == null ? void 0 : _g.write(state.length));
1333
- (_h = nodes[$TRACK]) == null ? void 0 : _h.write(void 0);
1334
- }
1335
- function recursivelyNotify(state) {
1336
- var _a, _b;
1337
- let target = (_a = state[$PROXY]) == null ? void 0 : _a[$TARGET];
1189
+ function storeSetter(store, fn) {
1190
+ const prevWriting = Writing;
1191
+ Writing = /* @__PURE__ */ new Set();
1192
+ Writing.add(store);
1193
+ try {
1194
+ fn(store);
1195
+ } finally {
1196
+ Writing.clear();
1197
+ Writing = prevWriting;
1198
+ }
1199
+ }
1200
+ function createStore(first, second) {
1201
+ const derived = typeof first === "function", wrappedStore = derived ? createProjection(first, second) : wrap(first);
1202
+ return [wrappedStore, (fn) => storeSetter(wrappedStore, fn)];
1203
+ }
1204
+ function recursivelyNotify(state, lookup) {
1205
+ var _a;
1206
+ let target = state[$TARGET] || ((_a = lookup == null ? void 0 : lookup.get(state)) == null ? void 0 : _a[$TARGET]);
1338
1207
  let notified = false;
1339
- target && ((_b = getNodes(target, STORE_NODE)[$DEEP]) == null ? void 0 : _b.write(void 0), notified = true);
1340
- const parents = PARENTS.get(state);
1208
+ if (target) {
1209
+ const deep2 = getNodes(target, STORE_NODE)[$DEEP];
1210
+ if (deep2) {
1211
+ deep2.write(void 0);
1212
+ notified = true;
1213
+ }
1214
+ lookup = target[STORE_LOOKUP] || lookup;
1215
+ }
1216
+ const parents = PARENTS.get((target == null ? void 0 : target[STORE_VALUE]) || state);
1341
1217
  if (!parents)
1342
1218
  return notified;
1343
1219
  if (parents instanceof Set) {
1344
1220
  for (let parent of parents)
1345
- notified = recursivelyNotify(parent) || notified;
1221
+ notified = recursivelyNotify(parent, lookup) || notified;
1346
1222
  } else
1347
- notified = recursivelyNotify(parents) || notified;
1223
+ notified = recursivelyNotify(parents, lookup) || notified;
1348
1224
  return notified;
1349
1225
  }
1350
1226
  function recursivelyAddParent(state, parent) {
1227
+ let override;
1228
+ const target = state[$TARGET];
1229
+ if (target) {
1230
+ override = target[STORE_OVERRIDE];
1231
+ state = target[STORE_VALUE];
1232
+ }
1351
1233
  if (parent) {
1352
1234
  let parents = PARENTS.get(state);
1353
1235
  if (!parents)
@@ -1362,81 +1244,75 @@ function recursivelyAddParent(state, parent) {
1362
1244
  return;
1363
1245
  }
1364
1246
  if (Array.isArray(state)) {
1365
- for (let i = 0; i < state.length; i++) {
1366
- const item = state[i];
1367
- isWrappable(item) && recursivelyAddParent(item[$RAW] || item, state);
1247
+ const len = (override == null ? void 0 : override.length) || state.length;
1248
+ for (let i = 0; i < len; i++) {
1249
+ const item = override && i in override ? override[i] : state[i];
1250
+ isWrappable(item) && recursivelyAddParent(item, state);
1368
1251
  }
1369
1252
  } else {
1370
- const keys = Object.keys(state);
1253
+ const keys = getKeys(state, override);
1371
1254
  for (let i = 0; i < keys.length; i++) {
1372
- const item = state[keys[i]];
1373
- isWrappable(item) && recursivelyAddParent(item[$RAW] || item, state);
1255
+ const key = keys[i];
1256
+ const item = override && key in override ? override[key] : state[key];
1257
+ isWrappable(item) && recursivelyAddParent(item, state);
1374
1258
  }
1375
1259
  }
1376
1260
  }
1377
- function createStore(first, second) {
1378
- const derived = typeof first === "function", store = derived ? second : first;
1379
- const unwrappedStore = unwrap(store);
1380
- let wrappedStore = wrap2(unwrappedStore);
1381
- const setStore = (fn) => {
1382
- const prevWriting = Writing;
1383
- Writing = /* @__PURE__ */ new Set();
1384
- Writing.add(unwrappedStore);
1385
- try {
1386
- fn(wrappedStore);
1387
- } finally {
1388
- Writing.clear();
1389
- Writing = prevWriting;
1390
- }
1391
- };
1392
- if (derived)
1393
- return wrapProjection(first, wrappedStore, setStore);
1394
- return [wrappedStore, setStore];
1395
- }
1396
1261
  function deep(store) {
1397
- recursivelyAddParent(store[$RAW] || store);
1262
+ recursivelyAddParent(store);
1398
1263
  return store[$DEEP];
1399
1264
  }
1400
1265
 
1401
1266
  // src/store/reconcile.ts
1402
- function applyState(next, state, keyFn) {
1267
+ function unwrap(value) {
1268
+ var _a;
1269
+ return ((_a = value == null ? void 0 : value[$TARGET]) == null ? void 0 : _a[STORE_NODE]) ?? value;
1270
+ }
1271
+ function getOverrideValue(value, override, key) {
1272
+ return override && key in override ? override[key] : value[key];
1273
+ }
1274
+ function getAllKeys(value, override, next) {
1275
+ const keys = getKeys(value, override);
1276
+ const nextKeys = Object.keys(next);
1277
+ return Array.from(/* @__PURE__ */ new Set([...keys, ...nextKeys]));
1278
+ }
1279
+ function applyState(next, state, keyFn, all) {
1403
1280
  var _a, _b, _c, _d, _e, _f, _g, _h;
1404
1281
  const target = state == null ? void 0 : state[$TARGET];
1405
1282
  if (!target)
1406
1283
  return;
1407
1284
  const previous = target[STORE_VALUE];
1408
- if (next === previous)
1285
+ const override = target[STORE_OVERRIDE];
1286
+ if (next === previous && !override)
1409
1287
  return;
1410
- Object.defineProperty(next, $PROXY, {
1411
- value: previous[$PROXY],
1412
- writable: true
1413
- });
1414
- previous[$PROXY] = null;
1288
+ (target[STORE_LOOKUP] || storeLookup).set(next, target[$PROXY]);
1415
1289
  target[STORE_VALUE] = next;
1290
+ target[STORE_OVERRIDE] = void 0;
1416
1291
  if (Array.isArray(previous)) {
1417
1292
  let changed = false;
1418
- if (next.length && previous.length && next[0] && keyFn(next[0]) != null) {
1293
+ const prevLength = getOverrideValue(previous, override, "length");
1294
+ if (next.length && prevLength && next[0] && keyFn(next[0]) != null) {
1419
1295
  let i, j, start, end, newEnd, item, newIndicesNext, keyVal;
1420
- 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++) {
1421
- applyState(next[start], wrap2(previous[start]), keyFn);
1296
+ for (start = 0, end = Math.min(prevLength, next.length); start < end && ((item = getOverrideValue(previous, override, start)) === next[start] || item && next[start] && keyFn(item) === keyFn(next[start])); start++) {
1297
+ applyState(next[start], wrap(item, target), keyFn, all);
1422
1298
  }
1423
1299
  const temp = new Array(next.length), newIndices = /* @__PURE__ */ new Map();
1424
- 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--) {
1425
- temp[newEnd] = previous[end];
1300
+ for (end = prevLength - 1, newEnd = next.length - 1; end >= start && newEnd >= start && ((item = getOverrideValue(previous, override, end)) === next[newEnd] || item && next[newEnd] && keyFn(item) === keyFn(next[newEnd])); end--, newEnd--) {
1301
+ temp[newEnd] = item;
1426
1302
  }
1427
1303
  if (start > newEnd || start > end) {
1428
1304
  for (j = start; j <= newEnd; j++) {
1429
1305
  changed = true;
1430
- (_a = target[STORE_NODE][j]) == null ? void 0 : _a.write(wrap2(next[j]));
1306
+ (_a = target[STORE_NODE][j]) == null ? void 0 : _a.write(wrap(next[j], target));
1431
1307
  }
1432
1308
  for (; j < next.length; j++) {
1433
1309
  changed = true;
1434
- const wrapped = wrap2(temp[j]);
1310
+ const wrapped = wrap(temp[j], target);
1435
1311
  (_b = target[STORE_NODE][j]) == null ? void 0 : _b.write(wrapped);
1436
- applyState(next[j], wrapped, keyFn);
1312
+ applyState(next[j], wrapped, keyFn, all);
1437
1313
  }
1438
1314
  changed && ((_c = target[STORE_NODE][$TRACK]) == null ? void 0 : _c.write(void 0));
1439
- previous.length !== next.length && ((_d = target[STORE_NODE].length) == null ? void 0 : _d.write(next.length));
1315
+ prevLength !== next.length && ((_d = target[STORE_NODE].length) == null ? void 0 : _d.write(next.length));
1440
1316
  return;
1441
1317
  }
1442
1318
  newIndicesNext = new Array(newEnd + 1);
@@ -1448,31 +1324,32 @@ function applyState(next, state, keyFn) {
1448
1324
  newIndices.set(keyVal, j);
1449
1325
  }
1450
1326
  for (i = start; i <= end; i++) {
1451
- item = previous[i];
1327
+ item = getOverrideValue(previous, override, i);
1452
1328
  keyVal = item ? keyFn(item) : item;
1453
1329
  j = newIndices.get(keyVal);
1454
1330
  if (j !== void 0 && j !== -1) {
1455
- temp[j] = previous[i];
1331
+ temp[j] = item;
1456
1332
  j = newIndicesNext[j];
1457
1333
  newIndices.set(keyVal, j);
1458
1334
  }
1459
1335
  }
1460
1336
  for (j = start; j < next.length; j++) {
1461
1337
  if (j in temp) {
1462
- const wrapped = wrap2(temp[j]);
1338
+ const wrapped = wrap(temp[j], target);
1463
1339
  (_e = target[STORE_NODE][j]) == null ? void 0 : _e.write(wrapped);
1464
- applyState(next[j], wrapped, keyFn);
1340
+ applyState(next[j], wrapped, keyFn, all);
1465
1341
  } else
1466
- (_f = target[STORE_NODE][j]) == null ? void 0 : _f.write(wrap2(next[j]));
1342
+ (_f = target[STORE_NODE][j]) == null ? void 0 : _f.write(wrap(next[j], target));
1467
1343
  }
1468
1344
  if (start < next.length)
1469
1345
  changed = true;
1470
- } else if (previous.length && next.length) {
1346
+ } else if (prevLength && next.length) {
1471
1347
  for (let i = 0, len = next.length; i < len; i++) {
1472
- isWrappable(previous[i]) && applyState(next[i], wrap2(previous[i]), keyFn);
1348
+ const item = getOverrideValue(previous, override, i);
1349
+ isWrappable(item) && applyState(next[i], wrap(item, target), keyFn, all);
1473
1350
  }
1474
1351
  }
1475
- if (previous.length !== next.length) {
1352
+ if (prevLength !== next.length) {
1476
1353
  changed = true;
1477
1354
  (_g = target[STORE_NODE].length) == null ? void 0 : _g.write(next.length);
1478
1355
  }
@@ -1481,17 +1358,20 @@ function applyState(next, state, keyFn) {
1481
1358
  }
1482
1359
  let nodes = target[STORE_NODE];
1483
1360
  if (nodes) {
1484
- const keys = Object.keys(nodes);
1361
+ const tracked = nodes[$TRACK];
1362
+ const keys = tracked || all ? getAllKeys(previous, override, next) : Object.keys(nodes);
1485
1363
  for (let i = 0, len = keys.length; i < len; i++) {
1486
- const node = nodes[keys[i]];
1487
- const previousValue = unwrap(previous[keys[i]], false);
1488
- let nextValue = unwrap(next[keys[i]], false);
1364
+ const key = keys[i];
1365
+ const node = nodes[key];
1366
+ const previousValue = unwrap(getOverrideValue(previous, override, key));
1367
+ let nextValue = unwrap(next[key]);
1489
1368
  if (previousValue === nextValue)
1490
1369
  continue;
1491
- if (!previousValue || !isWrappable(previousValue) || keyFn(previousValue) != null && keyFn(previousValue) !== keyFn(nextValue))
1492
- node.write(isWrappable(nextValue) ? wrap2(nextValue) : nextValue);
1493
- else
1494
- applyState(nextValue, wrap2(previousValue), keyFn);
1370
+ if (!previousValue || !isWrappable(previousValue) || keyFn(previousValue) != null && keyFn(previousValue) !== keyFn(nextValue)) {
1371
+ tracked == null ? void 0 : tracked.write(void 0);
1372
+ node == null ? void 0 : node.write(isWrappable(nextValue) ? wrap(nextValue, target) : nextValue);
1373
+ } else
1374
+ applyState(nextValue, wrap(previousValue, target), keyFn, all);
1495
1375
  }
1496
1376
  }
1497
1377
  if (nodes = target[STORE_HAS]) {
@@ -1501,17 +1381,70 @@ function applyState(next, state, keyFn) {
1501
1381
  }
1502
1382
  }
1503
1383
  }
1504
- function reconcile(value, key) {
1384
+ function reconcile(value, key, all = false) {
1505
1385
  return (state) => {
1506
1386
  const keyFn = typeof key === "string" ? (item) => item[key] : key;
1507
- if (keyFn(value) !== keyFn(state))
1387
+ const eq = keyFn(state);
1388
+ if (eq !== void 0 && keyFn(value) !== keyFn(state))
1508
1389
  throw new Error("Cannot reconcile states with different identity");
1509
- applyState(value, state, keyFn);
1510
- return state;
1390
+ applyState(value, state, keyFn, all);
1511
1391
  };
1512
1392
  }
1513
1393
 
1514
1394
  // src/store/utils.ts
1395
+ function snapshot(item, map, lookup) {
1396
+ var _a;
1397
+ let target, isArray, override, result, unwrapped, v;
1398
+ if (!isWrappable(item))
1399
+ return item;
1400
+ if (map && map.has(item))
1401
+ return map.get(item);
1402
+ if (!map)
1403
+ map = /* @__PURE__ */ new Map();
1404
+ if (target = item[$TARGET] || ((_a = lookup == null ? void 0 : lookup.get(item)) == null ? void 0 : _a[$TARGET])) {
1405
+ override = target[STORE_OVERRIDE];
1406
+ isArray = Array.isArray(target[STORE_VALUE]);
1407
+ map.set(
1408
+ item,
1409
+ override ? result = isArray ? [] : Object.create(Object.getPrototypeOf(target[STORE_VALUE])) : target[STORE_VALUE]
1410
+ );
1411
+ item = target[STORE_VALUE];
1412
+ lookup = storeLookup;
1413
+ } else {
1414
+ isArray = Array.isArray(item);
1415
+ map.set(item, item);
1416
+ }
1417
+ if (isArray) {
1418
+ const len = (override == null ? void 0 : override.length) || item.length;
1419
+ for (let i = 0; i < len; i++) {
1420
+ v = override && i in override ? override[i] : item[i];
1421
+ if (v === $DELETED)
1422
+ continue;
1423
+ if ((unwrapped = snapshot(v, map, lookup)) !== v || result) {
1424
+ if (!result)
1425
+ map.set(item, result = [...item]);
1426
+ result[i] = unwrapped;
1427
+ }
1428
+ }
1429
+ } else {
1430
+ const keys = getKeys(item, override);
1431
+ for (let i = 0, l = keys.length; i < l; i++) {
1432
+ let prop = keys[i];
1433
+ const desc = getPropertyDescriptor(item, override, prop);
1434
+ if (desc.get)
1435
+ continue;
1436
+ v = override && prop in override ? override[prop] : item[prop];
1437
+ if ((unwrapped = snapshot(v, map, lookup)) !== item[prop] || result) {
1438
+ if (!result) {
1439
+ result = Object.create(Object.getPrototypeOf(item));
1440
+ Object.assign(result, item);
1441
+ }
1442
+ result[prop] = unwrapped;
1443
+ }
1444
+ }
1445
+ }
1446
+ return result || item;
1447
+ }
1515
1448
  function trueFn() {
1516
1449
  return true;
1517
1450
  }
@@ -1665,48 +1598,49 @@ function mapArray(list, map, options) {
1665
1598
  return updateKeyedMap.bind({
1666
1599
  H: new Owner(),
1667
1600
  i: 0,
1668
- _: list,
1669
- u: [],
1670
- C: map,
1671
- d: [],
1601
+ $: list,
1602
+ w: [],
1603
+ D: map,
1604
+ e: [],
1672
1605
  b: [],
1673
- D: keyFn,
1606
+ E: keyFn,
1674
1607
  j: keyFn || (options == null ? void 0 : options.keyed) === false ? [] : void 0,
1675
1608
  k: map.length > 1 ? [] : void 0,
1676
1609
  I: options == null ? void 0 : options.fallback
1677
1610
  });
1678
1611
  }
1612
+ var pureOptions = { pureWrite: true };
1679
1613
  function updateKeyedMap() {
1680
- const newItems = this._() || [], newLen = newItems.length;
1614
+ const newItems = this.$() || [], newLen = newItems.length;
1681
1615
  newItems[$TRACK];
1682
1616
  runWithOwner(this.H, () => {
1683
1617
  let i, j, mapper = this.j ? () => {
1684
- this.j[j] = new Computation(newItems[j], null);
1685
- this.k && (this.k[j] = new Computation(j, null));
1686
- return this.C(
1618
+ this.j[j] = new Computation(newItems[j], null, pureOptions);
1619
+ this.k && (this.k[j] = new Computation(j, null, pureOptions));
1620
+ return this.D(
1687
1621
  Computation.prototype.read.bind(this.j[j]),
1688
1622
  this.k ? Computation.prototype.read.bind(this.k[j]) : void 0
1689
1623
  );
1690
1624
  } : this.k ? () => {
1691
1625
  const item = newItems[j];
1692
- this.k[j] = new Computation(j, null);
1693
- return this.C(() => item, Computation.prototype.read.bind(this.k[j]));
1626
+ this.k[j] = new Computation(j, null, pureOptions);
1627
+ return this.D(() => item, Computation.prototype.read.bind(this.k[j]));
1694
1628
  } : () => {
1695
1629
  const item = newItems[j];
1696
- return this.C(() => item);
1630
+ return this.D(() => item);
1697
1631
  };
1698
1632
  if (newLen === 0) {
1699
1633
  if (this.i !== 0) {
1700
1634
  this.H.dispose(false);
1701
1635
  this.b = [];
1702
- this.u = [];
1703
- this.d = [];
1636
+ this.w = [];
1637
+ this.e = [];
1704
1638
  this.i = 0;
1705
1639
  this.j && (this.j = []);
1706
1640
  this.k && (this.k = []);
1707
1641
  }
1708
- if (this.I && !this.d[0]) {
1709
- this.d[0] = compute(
1642
+ if (this.I && !this.e[0]) {
1643
+ this.e[0] = compute(
1710
1644
  this.b[0] = new Owner(),
1711
1645
  this.I,
1712
1646
  null
@@ -1715,20 +1649,20 @@ function updateKeyedMap() {
1715
1649
  } else if (this.i === 0) {
1716
1650
  if (this.b[0])
1717
1651
  this.b[0].dispose();
1718
- this.d = new Array(newLen);
1652
+ this.e = new Array(newLen);
1719
1653
  for (j = 0; j < newLen; j++) {
1720
- this.u[j] = newItems[j];
1721
- this.d[j] = compute(this.b[j] = new Owner(), mapper, null);
1654
+ this.w[j] = newItems[j];
1655
+ this.e[j] = compute(this.b[j] = new Owner(), mapper, null);
1722
1656
  }
1723
1657
  this.i = newLen;
1724
1658
  } else {
1725
1659
  let start, end, newEnd, item, key, newIndices, newIndicesNext, temp = new Array(newLen), tempNodes = new Array(newLen), tempRows = this.j ? new Array(newLen) : void 0, tempIndexes = this.k ? new Array(newLen) : void 0;
1726
- for (start = 0, end = Math.min(this.i, newLen); start < end && (this.u[start] === newItems[start] || this.j && compare(this.D, this.u[start], newItems[start])); start++) {
1660
+ for (start = 0, end = Math.min(this.i, newLen); start < end && (this.w[start] === newItems[start] || this.j && compare(this.E, this.w[start], newItems[start])); start++) {
1727
1661
  if (this.j)
1728
1662
  this.j[start].write(newItems[start]);
1729
1663
  }
1730
- for (end = this.i - 1, newEnd = newLen - 1; end >= start && newEnd >= start && (this.u[end] === newItems[newEnd] || this.j && compare(this.D, this.u[end], newItems[newEnd])); end--, newEnd--) {
1731
- temp[newEnd] = this.d[end];
1664
+ for (end = this.i - 1, newEnd = newLen - 1; end >= start && newEnd >= start && (this.w[end] === newItems[newEnd] || this.j && compare(this.E, this.w[end], newItems[newEnd])); end--, newEnd--) {
1665
+ temp[newEnd] = this.e[end];
1732
1666
  tempNodes[newEnd] = this.b[end];
1733
1667
  tempRows && (tempRows[newEnd] = this.j[end]);
1734
1668
  tempIndexes && (tempIndexes[newEnd] = this.k[end]);
@@ -1737,17 +1671,17 @@ function updateKeyedMap() {
1737
1671
  newIndicesNext = new Array(newEnd + 1);
1738
1672
  for (j = newEnd; j >= start; j--) {
1739
1673
  item = newItems[j];
1740
- key = this.D ? this.D(item) : item;
1674
+ key = this.E ? this.E(item) : item;
1741
1675
  i = newIndices.get(key);
1742
1676
  newIndicesNext[j] = i === void 0 ? -1 : i;
1743
1677
  newIndices.set(key, j);
1744
1678
  }
1745
1679
  for (i = start; i <= end; i++) {
1746
- item = this.u[i];
1747
- key = this.D ? this.D(item) : item;
1680
+ item = this.w[i];
1681
+ key = this.E ? this.E(item) : item;
1748
1682
  j = newIndices.get(key);
1749
1683
  if (j !== void 0 && j !== -1) {
1750
- temp[j] = this.d[i];
1684
+ temp[j] = this.e[i];
1751
1685
  tempNodes[j] = this.b[i];
1752
1686
  tempRows && (tempRows[j] = this.j[i]);
1753
1687
  tempIndexes && (tempIndexes[j] = this.k[i]);
@@ -1758,7 +1692,7 @@ function updateKeyedMap() {
1758
1692
  }
1759
1693
  for (j = start; j < newLen; j++) {
1760
1694
  if (j in temp) {
1761
- this.d[j] = temp[j];
1695
+ this.e[j] = temp[j];
1762
1696
  this.b[j] = tempNodes[j];
1763
1697
  if (tempRows) {
1764
1698
  this.j[j] = tempRows[j];
@@ -1769,42 +1703,42 @@ function updateKeyedMap() {
1769
1703
  this.k[j].write(j);
1770
1704
  }
1771
1705
  } else {
1772
- this.d[j] = compute(this.b[j] = new Owner(), mapper, null);
1706
+ this.e[j] = compute(this.b[j] = new Owner(), mapper, null);
1773
1707
  }
1774
1708
  }
1775
- this.d = this.d.slice(0, this.i = newLen);
1776
- this.u = newItems.slice(0);
1709
+ this.e = this.e.slice(0, this.i = newLen);
1710
+ this.w = newItems.slice(0);
1777
1711
  }
1778
1712
  });
1779
- return this.d;
1713
+ return this.e;
1780
1714
  }
1781
1715
  function repeat(count, map, options) {
1782
1716
  return updateRepeat.bind({
1783
1717
  H: new Owner(),
1784
1718
  i: 0,
1785
1719
  q: 0,
1786
- $: count,
1787
- C: map,
1720
+ aa: count,
1721
+ D: map,
1788
1722
  b: [],
1789
- d: [],
1790
- aa: options == null ? void 0 : options.from,
1723
+ e: [],
1724
+ ba: options == null ? void 0 : options.from,
1791
1725
  I: options == null ? void 0 : options.fallback
1792
1726
  });
1793
1727
  }
1794
1728
  function updateRepeat() {
1795
1729
  var _a;
1796
- const newLen = this.$();
1797
- const from = ((_a = this.aa) == null ? void 0 : _a.call(this)) || 0;
1730
+ const newLen = this.aa();
1731
+ const from = ((_a = this.ba) == null ? void 0 : _a.call(this)) || 0;
1798
1732
  runWithOwner(this.H, () => {
1799
1733
  if (newLen === 0) {
1800
1734
  if (this.i !== 0) {
1801
1735
  this.H.dispose(false);
1802
1736
  this.b = [];
1803
- this.d = [];
1737
+ this.e = [];
1804
1738
  this.i = 0;
1805
1739
  }
1806
- if (this.I && !this.d[0]) {
1807
- this.d[0] = compute(
1740
+ if (this.I && !this.e[0]) {
1741
+ this.e[0] = compute(
1808
1742
  this.b[0] = new Owner(),
1809
1743
  this.I,
1810
1744
  null
@@ -1823,43 +1757,237 @@ function updateRepeat() {
1823
1757
  while (i < from && i < this.i)
1824
1758
  this.b[i++].dispose();
1825
1759
  this.b.splice(0, from - this.q);
1826
- this.d.splice(0, from - this.q);
1760
+ this.e.splice(0, from - this.q);
1827
1761
  } else if (this.q > from) {
1828
1762
  let i = prevTo - this.q - 1;
1829
1763
  let difference = this.q - from;
1830
- this.b.length = this.d.length = newLen;
1764
+ this.b.length = this.e.length = newLen;
1831
1765
  while (i >= difference) {
1832
1766
  this.b[i] = this.b[i - difference];
1833
- this.d[i] = this.d[i - difference];
1767
+ this.e[i] = this.e[i - difference];
1834
1768
  i--;
1835
1769
  }
1836
1770
  for (let i2 = 0; i2 < difference; i2++) {
1837
- this.d[i2] = compute(
1771
+ this.e[i2] = compute(
1838
1772
  this.b[i2] = new Owner(),
1839
- () => this.C(i2 + from),
1773
+ () => this.D(i2 + from),
1840
1774
  null
1841
1775
  );
1842
1776
  }
1843
1777
  }
1844
1778
  for (let i = prevTo; i < to; i++) {
1845
- this.d[i - from] = compute(
1779
+ this.e[i - from] = compute(
1846
1780
  this.b[i - from] = new Owner(),
1847
- () => this.C(i),
1781
+ () => this.D(i),
1848
1782
  null
1849
1783
  );
1850
1784
  }
1851
- this.d = this.d.slice(0, newLen);
1785
+ this.e = this.e.slice(0, newLen);
1852
1786
  this.q = from;
1853
1787
  this.i = newLen;
1854
1788
  });
1855
- return this.d;
1789
+ return this.e;
1856
1790
  }
1857
1791
  function compare(key, a, b) {
1858
1792
  return key ? key(a) === key(b) : true;
1859
1793
  }
1860
1794
 
1795
+ // src/boundaries.ts
1796
+ var BoundaryComputation = class extends EagerComputation {
1797
+ J;
1798
+ constructor(compute2, propagationMask) {
1799
+ super(void 0, compute2, { defer: true });
1800
+ this.J = propagationMask;
1801
+ }
1802
+ write(value, flags) {
1803
+ super.write(value, flags & ~this.J);
1804
+ if (this.J & LOADING_BIT && !(this.f & UNINITIALIZED_BIT)) {
1805
+ flags &= ~LOADING_BIT;
1806
+ }
1807
+ this.h.notify(this, this.J, flags);
1808
+ return this.g;
1809
+ }
1810
+ };
1811
+ function createBoundChildren(owner, fn, queue, mask) {
1812
+ const parentQueue = owner.h;
1813
+ parentQueue.addChild(owner.h = queue);
1814
+ onCleanup(() => parentQueue.removeChild(owner.h));
1815
+ return compute(
1816
+ owner,
1817
+ () => {
1818
+ const c = new Computation(void 0, fn);
1819
+ return new BoundaryComputation(() => flatten(c.wait()), mask);
1820
+ },
1821
+ null
1822
+ );
1823
+ }
1824
+ var ConditionalQueue = class extends Queue {
1825
+ n;
1826
+ P = /* @__PURE__ */ new Set();
1827
+ Q = /* @__PURE__ */ new Set();
1828
+ constructor(disabled) {
1829
+ super();
1830
+ this.n = disabled;
1831
+ }
1832
+ run(type) {
1833
+ if (!type || this.n.read())
1834
+ return;
1835
+ return super.run(type);
1836
+ }
1837
+ notify(node, type, flags) {
1838
+ if (this.n.read()) {
1839
+ if (type & LOADING_BIT) {
1840
+ if (flags & LOADING_BIT) {
1841
+ this.Q.add(node);
1842
+ type &= ~LOADING_BIT;
1843
+ } else if (this.Q.delete(node))
1844
+ type &= ~LOADING_BIT;
1845
+ }
1846
+ if (type & ERROR_BIT) {
1847
+ if (flags & ERROR_BIT) {
1848
+ this.P.add(node);
1849
+ type &= ~ERROR_BIT;
1850
+ } else if (this.P.delete(node))
1851
+ type &= ~ERROR_BIT;
1852
+ }
1853
+ }
1854
+ return type ? super.notify(node, type, flags) : true;
1855
+ }
1856
+ };
1857
+ var CollectionQueue = class extends Queue {
1858
+ R;
1859
+ b = /* @__PURE__ */ new Set();
1860
+ n = new Computation(false, null, { pureWrite: true });
1861
+ constructor(type) {
1862
+ super();
1863
+ this.R = type;
1864
+ }
1865
+ run(type) {
1866
+ if (!type || this.n.read())
1867
+ return;
1868
+ return super.run(type);
1869
+ }
1870
+ notify(node, type, flags) {
1871
+ if (!(type & this.R))
1872
+ return super.notify(node, type, flags);
1873
+ if (flags & this.R) {
1874
+ this.b.add(node);
1875
+ if (this.b.size === 1)
1876
+ this.n.write(true);
1877
+ } else {
1878
+ this.b.delete(node);
1879
+ if (this.b.size === 0)
1880
+ this.n.write(false);
1881
+ }
1882
+ type &= ~this.R;
1883
+ return type ? super.notify(node, type, flags) : true;
1884
+ }
1885
+ };
1886
+ function createBoundary(fn, condition) {
1887
+ const owner = new Owner();
1888
+ const queue = new ConditionalQueue(
1889
+ new Computation(void 0, () => condition() === "hidden" /* HIDDEN */)
1890
+ );
1891
+ const tree = createBoundChildren(owner, fn, queue, 0);
1892
+ new EagerComputation(void 0, () => {
1893
+ const disabled = queue.n.read();
1894
+ tree.J = disabled ? ERROR_BIT | LOADING_BIT : 0;
1895
+ if (!disabled) {
1896
+ queue.Q.forEach((node) => queue.notify(node, LOADING_BIT, LOADING_BIT));
1897
+ queue.P.forEach((node) => queue.notify(node, ERROR_BIT, ERROR_BIT));
1898
+ queue.Q.clear();
1899
+ queue.P.clear();
1900
+ }
1901
+ });
1902
+ return () => queue.n.read() ? void 0 : tree.read();
1903
+ }
1904
+ function createCollectionBoundary(type, fn, fallback) {
1905
+ const owner = new Owner();
1906
+ const queue = new CollectionQueue(type);
1907
+ const tree = createBoundChildren(owner, fn, queue, type);
1908
+ const decision = new Computation(void 0, () => {
1909
+ if (!queue.n.read()) {
1910
+ const resolved = tree.read();
1911
+ if (!queue.n.read())
1912
+ return resolved;
1913
+ }
1914
+ return fallback(queue);
1915
+ });
1916
+ return decision.read.bind(decision);
1917
+ }
1918
+ function createSuspense(fn, fallback) {
1919
+ return createCollectionBoundary(LOADING_BIT, fn, () => fallback());
1920
+ }
1921
+ function createErrorBoundary(fn, fallback) {
1922
+ return createCollectionBoundary(
1923
+ ERROR_BIT,
1924
+ fn,
1925
+ (queue) => fallback(queue.b.values().next().value.G, () => {
1926
+ var _a;
1927
+ incrementClock();
1928
+ for (let node of queue.b) {
1929
+ node.a = STATE_DIRTY;
1930
+ (_a = node.h) == null ? void 0 : _a.enqueue(node.s, node.u.bind(node));
1931
+ }
1932
+ })
1933
+ );
1934
+ }
1935
+ function flatten(children, options) {
1936
+ if (typeof children === "function" && !children.length) {
1937
+ if (options == null ? void 0 : options.doNotUnwrap)
1938
+ return children;
1939
+ do {
1940
+ children = children();
1941
+ } while (typeof children === "function" && !children.length);
1942
+ }
1943
+ if ((options == null ? void 0 : options.skipNonRendered) && (children == null || children === true || children === false || children === ""))
1944
+ return;
1945
+ if (Array.isArray(children)) {
1946
+ let results = [];
1947
+ if (flattenArray(children, results, options)) {
1948
+ return () => {
1949
+ let nested = [];
1950
+ flattenArray(results, nested, { ...options, doNotUnwrap: false });
1951
+ return nested;
1952
+ };
1953
+ }
1954
+ return results;
1955
+ }
1956
+ return children;
1957
+ }
1958
+ function flattenArray(children, results = [], options) {
1959
+ let notReady = null;
1960
+ let needsUnwrap = false;
1961
+ for (let i = 0; i < children.length; i++) {
1962
+ try {
1963
+ let child = children[i];
1964
+ if (typeof child === "function" && !child.length) {
1965
+ if (options == null ? void 0 : options.doNotUnwrap) {
1966
+ results.push(child);
1967
+ needsUnwrap = true;
1968
+ continue;
1969
+ }
1970
+ do {
1971
+ child = child();
1972
+ } while (typeof child === "function" && !child.length);
1973
+ }
1974
+ if (Array.isArray(child)) {
1975
+ needsUnwrap = flattenArray(child, results, options);
1976
+ } else if ((options == null ? void 0 : options.skipNonRendered) && (child == null || child === true || child === false || child === "")) {
1977
+ } else
1978
+ results.push(child);
1979
+ } catch (e) {
1980
+ if (!(e instanceof NotReadyError))
1981
+ throw e;
1982
+ notReady = e;
1983
+ }
1984
+ }
1985
+ if (notReady)
1986
+ throw notReady;
1987
+ return needsUnwrap;
1988
+ }
1989
+
1861
1990
  exports.$PROXY = $PROXY;
1862
- exports.$RAW = $RAW;
1863
1991
  exports.$TARGET = $TARGET;
1864
1992
  exports.$TRACK = $TRACK;
1865
1993
  exports.Computation = Computation;
@@ -1875,6 +2003,7 @@ exports.createContext = createContext;
1875
2003
  exports.createEffect = createEffect;
1876
2004
  exports.createErrorBoundary = createErrorBoundary;
1877
2005
  exports.createMemo = createMemo;
2006
+ exports.createOptimistic = createOptimistic;
1878
2007
  exports.createProjection = createProjection;
1879
2008
  exports.createRenderEffect = createRenderEffect;
1880
2009
  exports.createRoot = createRoot;
@@ -1883,7 +2012,7 @@ exports.createStore = createStore;
1883
2012
  exports.createSuspense = createSuspense;
1884
2013
  exports.deep = deep;
1885
2014
  exports.flatten = flatten;
1886
- exports.flushSync = flushSync;
2015
+ exports.flush = flush;
1887
2016
  exports.getContext = getContext;
1888
2017
  exports.getObserver = getObserver;
1889
2018
  exports.getOwner = getOwner;
@@ -1903,6 +2032,7 @@ exports.resolve = resolve;
1903
2032
  exports.runWithObserver = runWithObserver;
1904
2033
  exports.runWithOwner = runWithOwner;
1905
2034
  exports.setContext = setContext;
2035
+ exports.snapshot = snapshot;
2036
+ exports.transition = transition;
1906
2037
  exports.tryCatch = tryCatch;
1907
2038
  exports.untrack = untrack;
1908
- exports.unwrap = unwrap;