@solidjs/signals 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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;
@@ -46,7 +40,7 @@ function schedule() {
46
40
  return;
47
41
  scheduled = true;
48
42
  if (!globalQueue.J)
49
- queueMicrotask(flushSync);
43
+ queueMicrotask(flush);
50
44
  }
51
45
  var pureQueue = [];
52
46
  var Queue = class {
@@ -55,21 +49,21 @@ var Queue = class {
55
49
  K = [[], []];
56
50
  E = [];
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.K[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
63
  } else if (this.K[type - 1].length) {
70
64
  const effects = this.K[type - 1];
71
65
  this.K[type - 1] = [];
72
- runEffectQueue(effects);
66
+ runQueue(effects, type);
73
67
  }
74
68
  for (let i = 0; i < this.E.length; i++) {
75
69
  this.E[i].run(type);
@@ -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,7 +157,7 @@ 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
163
  current.y();
@@ -222,15 +169,15 @@ var Owner = class {
222
169
  if (self)
223
170
  this.y();
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
176
  y() {
230
- if (this.s)
231
- this.s.m = null;
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,30 +272,33 @@ 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
277
  F;
323
278
  z;
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
- S = isEqual;
283
+ R = 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
- T = DEFAULT_FLAGS;
289
+ S = DEFAULT_FLAGS;
334
290
  A = -1;
335
291
  w = false;
336
292
  constructor(initialValue, compute2, options) {
337
- super(null, compute2 === null);
293
+ super(options == null ? void 0 : options.id, compute2 === null);
338
294
  this.z = 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
- this.S = options.equals;
299
+ this.R = 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
  }
@@ -391,7 +349,8 @@ 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.R === false || !this.R(this.g, newValue));
395
354
  if (valueChanged) {
396
355
  this.g = newValue;
397
356
  this.F = void 0;
@@ -399,12 +358,12 @@ var Computation = class extends Owner {
399
358
  const changedFlagsMask = this.f ^ flags, changedFlags = changedFlagsMask & flags;
400
359
  this.f = flags;
401
360
  this.A = getClock() + 1;
402
- if (this.e) {
403
- for (let i = 0; i < this.e.length; i++) {
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
  }
@@ -418,9 +377,9 @@ var Computation = class extends Owner {
418
377
  return;
419
378
  this.w = !!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
  }
@@ -433,7 +392,7 @@ var Computation = class extends Owner {
433
392
  Z(mask, newFlags2) {
434
393
  if (this.a >= STATE_DIRTY)
435
394
  return;
436
- if (mask & this.T) {
395
+ if (mask & this.S) {
437
396
  this.r(STATE_DIRTY);
438
397
  return;
439
398
  }
@@ -445,9 +404,9 @@ 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
  }
@@ -546,15 +505,16 @@ 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;
@@ -563,20 +523,28 @@ function update(node) {
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
  }
@@ -615,7 +583,7 @@ function isPending(fn, loadingValue) {
615
583
  if (!currentObserver)
616
584
  return pendingCheck(fn, loadingValue);
617
585
  const c = new Computation(void 0, () => pendingCheck(fn, loadingValue));
618
- c.T |= LOADING_BIT;
586
+ c.S |= LOADING_BIT;
619
587
  return c.read();
620
588
  }
621
589
  function latest(fn, fallback) {
@@ -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;
@@ -677,7 +645,7 @@ function runWithObserver(observer, run) {
677
645
  function compute(owner, fn, observer) {
678
646
  const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask, prevNotStale = notStale;
679
647
  currentObserver = observer;
680
- currentMask = (observer == null ? void 0 : observer.T) ?? DEFAULT_FLAGS;
648
+ currentMask = (observer == null ? void 0 : observer.S) ?? DEFAULT_FLAGS;
681
649
  notStale = true;
682
650
  try {
683
651
  return fn(observer ? observer.g : void 0);
@@ -688,94 +656,32 @@ 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 {
662
+ T;
756
663
  M;
757
- N;
758
664
  B;
759
665
  U = false;
760
- O;
761
- t;
666
+ N;
667
+ s;
762
668
  constructor(initialValue, compute2, effect, error, options) {
763
669
  super(initialValue, compute2, options);
764
- this.M = effect;
765
- this.N = error;
766
- this.O = initialValue;
767
- this.t = (options == null ? void 0 : options.render) ? EFFECT_RENDER : EFFECT_USER;
768
- if (this.t === EFFECT_RENDER) {
670
+ this.T = effect;
671
+ this.M = error;
672
+ this.N = initialValue;
673
+ this.s = (options == null ? void 0 : options.render) ? EFFECT_RENDER : EFFECT_USER;
674
+ if (this.s === EFFECT_RENDER) {
769
675
  this.z = (p) => getClock() > this.h.created && !(this.f & ERROR_BIT) ? latest(() => compute2(p)) : compute2(p);
770
676
  }
771
677
  this.x();
772
- !(options == null ? void 0 : options.defer) && (this.t === EFFECT_USER ? this.h.enqueue(this.t, this) : this.V());
678
+ !(options == null ? void 0 : options.defer) && (this.s === EFFECT_USER ? this.h.enqueue(this.s, this.V.bind(this)) : this.V(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
  }
@@ -789,18 +695,20 @@ var Effect = class extends Computation {
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.V.bind(this));
793
699
  this.a = state;
794
700
  }
795
701
  L(error) {
796
- var _a;
797
702
  this.F = error;
798
- (_a = this.B) == null ? void 0 : _a.call(this);
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.M ? this.M(error, () => {
708
+ var _a;
709
+ (_a = this.B) == null ? void 0 : _a.call(this);
710
+ this.B = void 0;
711
+ }) : console.error(error);
804
712
  } catch (e) {
805
713
  error = e;
806
714
  }
@@ -812,29 +720,35 @@ var Effect = class extends Computation {
812
720
  var _a;
813
721
  if (this.a === STATE_DISPOSED)
814
722
  return;
815
- this.M = void 0;
816
- this.O = void 0;
723
+ this.T = void 0;
817
724
  this.N = void 0;
725
+ this.M = void 0;
818
726
  (_a = this.B) == null ? void 0 : _a.call(this);
819
727
  this.B = void 0;
820
728
  super.y();
821
729
  }
822
- V() {
730
+ V(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.U && this.a !== STATE_DISPOSED) {
734
+ (_a = this.B) == null ? void 0 : _a.call(this);
735
+ try {
736
+ this.B = this.T(this.g, this.N);
737
+ } catch (e) {
738
+ if (!this.h.notify(this, ERROR_BIT, ERROR_BIT))
739
+ throw e;
740
+ } finally {
741
+ this.N = this.g;
742
+ this.U = false;
743
+ }
834
744
  }
835
- }
745
+ } else
746
+ this.a !== STATE_CLEAN && runTop(this);
836
747
  }
837
748
  };
749
+ function runComputation() {
750
+ this.a !== STATE_CLEAN && runTop(this);
751
+ }
838
752
  var EagerComputation = class extends Computation {
839
753
  constructor(initialValue, compute2, options) {
840
754
  super(initialValue, compute2, options);
@@ -844,11 +758,12 @@ var EagerComputation = class extends Computation {
844
758
  if (this.a >= state && !this.w)
845
759
  return;
846
760
  if (!skipQueue && (this.a === STATE_CLEAN || this.a === STATE_CHECK && this.w))
847
- this.h.enqueue(EFFECT_PURE, this);
761
+ this.h.enqueue(EFFECT_PURE, runComputation.bind(this));
848
762
  super.r(state, skipQueue);
849
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
  }
@@ -856,142 +771,22 @@ var ProjectionComputation = class extends Computation {
856
771
  if (this.a >= state && !this.w)
857
772
  return;
858
773
  if (!skipQueue && (this.a === STATE_CLEAN || this.a === STATE_CHECK && this.w))
859
- this.h.enqueue(EFFECT_PURE, this);
774
+ this.h.enqueue(EFFECT_PURE, runComputation.bind(this));
860
775
  super.r(state, true);
861
776
  this.w = !!skipQueue;
862
777
  }
863
778
  };
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;
879
- }
880
- };
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;
779
+ function runTop(node) {
780
+ const ancestors = [];
781
+ for (let current = node; current !== null; current = current.o) {
782
+ if (current.a !== STATE_CLEAN) {
783
+ ancestors.push(current);
916
784
  }
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
- }
928
- run(type) {
929
- if (!type || this.n.read())
930
- return;
931
- return super.run(type);
932
785
  }
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;
786
+ for (let i = ancestors.length - 1; i >= 0; i--) {
787
+ if (ancestors[i].a !== STATE_DISPOSED)
788
+ ancestors[i].x();
947
789
  }
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
790
  }
996
791
 
997
792
  // src/signals.ts
@@ -1007,7 +802,13 @@ function createSignal(first, second, third) {
1007
802
  });
1008
803
  return [() => memo()[0](), (value) => memo()[1](value)];
1009
804
  }
1010
- const node = new Computation(first, null, second);
805
+ const o = getOwner();
806
+ const needsId = (o == null ? void 0 : o.id) != null;
807
+ const node = new Computation(
808
+ first,
809
+ null,
810
+ needsId ? { id: o.getNextChildId(), ...second } : second
811
+ );
1011
812
  return [node.read.bind(node), node.write.bind(node)];
1012
813
  }
1013
814
  function createMemo(compute2, value, options) {
@@ -1034,10 +835,12 @@ function createMemo(compute2, value, options) {
1034
835
  };
1035
836
  }
1036
837
  function createAsync(compute2, value, options) {
838
+ let refreshing = false;
1037
839
  const node = new EagerComputation(
1038
840
  value,
1039
841
  (p) => {
1040
- const source = compute2(p);
842
+ const source = compute2(p, refreshing);
843
+ refreshing = false;
1041
844
  const isPromise = source instanceof Promise;
1042
845
  const iterator = source[Symbol.asyncIterator];
1043
846
  if (!isPromise && !iterator) {
@@ -1077,14 +880,20 @@ function createAsync(compute2, value, options) {
1077
880
  },
1078
881
  options
1079
882
  );
1080
- return node.wait.bind(node);
883
+ const read = node.wait.bind(node);
884
+ read.refresh = () => {
885
+ node.a = STATE_DIRTY;
886
+ refreshing = true;
887
+ node.x();
888
+ };
889
+ return read;
1081
890
  }
1082
- function createEffect(compute2, effect, error, value, options) {
891
+ function createEffect(compute2, effect, value, options) {
1083
892
  void new Effect(
1084
893
  value,
1085
894
  compute2,
1086
- effect,
1087
- error,
895
+ effect.effect ? effect.effect : effect,
896
+ effect.error,
1088
897
  options
1089
898
  );
1090
899
  }
@@ -1117,101 +926,98 @@ function resolve(fn) {
1117
926
  });
1118
927
  });
1119
928
  }
929
+ function tryCatch(fn) {
930
+ try {
931
+ const v = fn();
932
+ if (v instanceof Promise) {
933
+ return v.then(
934
+ (v2) => [void 0, v2],
935
+ (e) => {
936
+ if (e instanceof NotReadyError)
937
+ throw e;
938
+ return [e];
939
+ }
940
+ );
941
+ }
942
+ return [void 0, v];
943
+ } catch (e) {
944
+ if (e instanceof NotReadyError)
945
+ throw e;
946
+ return [e];
947
+ }
948
+ }
949
+ function transition(fn) {
950
+ }
951
+ function createOptimistic(initial, compute2, options) {
952
+ return [];
953
+ }
1120
954
 
1121
955
  // src/store/projection.ts
1122
956
  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);
957
+ let wrappedStore;
958
+ const node = new FirewallComputation(() => {
959
+ storeSetter(wrappedStore, fn);
1129
960
  });
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");
961
+ const wrappedMap = /* @__PURE__ */ new WeakMap();
962
+ const traps = {
963
+ ...storeTraps,
964
+ get(target, property, receiver) {
965
+ const o = getOwner();
966
+ (!o || o !== node) && node.wait();
967
+ return storeTraps.get(target, property, receiver);
1147
968
  }
1148
- });
1149
- wrapped.set(source, wrap3);
1150
- return wrap3;
969
+ };
970
+ function wrapProjection(source) {
971
+ var _a;
972
+ if (wrappedMap.has(source))
973
+ return wrappedMap.get(source);
974
+ if (((_a = source[$TARGET]) == null ? void 0 : _a[STORE_WRAP]) === wrapProjection)
975
+ return source;
976
+ const wrapped = createStoreProxy(source, traps, {
977
+ [STORE_WRAP]: wrapProjection,
978
+ [STORE_LOOKUP]: wrappedMap
979
+ });
980
+ wrappedMap.set(source, wrapped);
981
+ return wrapped;
982
+ }
983
+ return wrappedStore = wrapProjection(initialValue);
1151
984
  }
1152
985
 
1153
986
  // src/store/store.ts
1154
- var $RAW = Symbol(0);
1155
987
  var $TRACK = Symbol(0);
1156
988
  var $DEEP = Symbol(0);
1157
989
  var $TARGET = Symbol(0);
1158
990
  var $PROXY = Symbol(0);
991
+ var $DELETED = Symbol(0);
1159
992
  var PARENTS = /* @__PURE__ */ new WeakMap();
1160
993
  var STORE_VALUE = "v";
994
+ var STORE_OVERRIDE = "o";
1161
995
  var STORE_NODE = "n";
1162
996
  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
- }
997
+ var STORE_WRAP = "w";
998
+ var STORE_LOOKUP = "l";
999
+ function createStoreProxy(value, traps = storeTraps, extend) {
1000
+ let newTarget;
1001
+ if (Array.isArray(value)) {
1002
+ newTarget = [];
1003
+ newTarget.v = value;
1004
+ } else
1005
+ newTarget = { v: value };
1006
+ extend && Object.assign(newTarget, extend);
1007
+ return newTarget[$PROXY] = new Proxy(newTarget, traps);
1008
+ }
1009
+ var storeLookup = /* @__PURE__ */ new WeakMap();
1010
+ function wrap(value, target) {
1011
+ if (target == null ? void 0 : target[STORE_WRAP])
1012
+ return target[STORE_WRAP](value, target);
1013
+ let p = value[$PROXY] || storeLookup.get(value);
1014
+ if (!p)
1015
+ storeLookup.set(value, p = createStoreProxy(value));
1177
1016
  return p;
1178
1017
  }
1179
1018
  function isWrappable(obj) {
1180
1019
  return obj != null && typeof obj === "object" && !Object.isFrozen(obj);
1181
1020
  }
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
1021
  function getNodes(target, type) {
1216
1022
  let nodes = target[type];
1217
1023
  if (!nodes)
@@ -1228,31 +1034,38 @@ function getNode(nodes, property, value, equals = isEqual) {
1228
1034
  }
1229
1035
  });
1230
1036
  }
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
1037
  function trackSelf(target, symbol = $TRACK) {
1243
1038
  getObserver() && getNode(getNodes(target, STORE_NODE), symbol, void 0, false).read();
1244
1039
  }
1245
- function ownKeys(target) {
1246
- trackSelf(target);
1247
- return Reflect.ownKeys(target[STORE_VALUE]);
1040
+ function getKeys(source, override, enumerable = true) {
1041
+ const baseKeys = untrack(() => enumerable ? Object.keys(source) : Reflect.ownKeys(source));
1042
+ if (!override)
1043
+ return baseKeys;
1044
+ const keys = new Set(baseKeys);
1045
+ const overrides = Reflect.ownKeys(override);
1046
+ for (const key of overrides) {
1047
+ if (override[key] !== $DELETED)
1048
+ keys.add(key);
1049
+ else
1050
+ keys.delete(key);
1051
+ }
1052
+ return Array.from(keys);
1053
+ }
1054
+ function getPropertyDescriptor(source, override, property) {
1055
+ let value = source;
1056
+ if (override && property in override) {
1057
+ if (value[property] === $DELETED)
1058
+ return void 0;
1059
+ if (!(property in value))
1060
+ value = override;
1061
+ }
1062
+ return Reflect.getOwnPropertyDescriptor(value, property);
1248
1063
  }
1249
1064
  var Writing = null;
1250
- var proxyTraps = {
1065
+ var storeTraps = {
1251
1066
  get(target, property, receiver) {
1252
1067
  if (property === $TARGET)
1253
1068
  return target;
1254
- if (property === $RAW)
1255
- return target[STORE_VALUE];
1256
1069
  if (property === $PROXY)
1257
1070
  return receiver;
1258
1071
  if (property === $TRACK || property === $DEEP) {
@@ -1260,94 +1073,160 @@ var proxyTraps = {
1260
1073
  return receiver;
1261
1074
  }
1262
1075
  const nodes = getNodes(target, STORE_NODE);
1263
- const storeValue = target[STORE_VALUE];
1264
1076
  const tracked = nodes[property];
1077
+ const overridden = target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE];
1078
+ const proxySource = !!target[STORE_VALUE][$TARGET];
1079
+ const storeValue = overridden ? target[STORE_OVERRIDE] : target[STORE_VALUE];
1265
1080
  if (!tracked) {
1266
1081
  const desc = Object.getOwnPropertyDescriptor(storeValue, property);
1267
1082
  if (desc && desc.get)
1268
1083
  return desc.get.call(receiver);
1269
1084
  }
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];
1085
+ if (Writing == null ? void 0 : Writing.has(receiver)) {
1086
+ let value2 = tracked && (overridden || !proxySource) ? tracked.g : storeValue[property];
1087
+ value2 === $DELETED && (value2 = void 0);
1088
+ if (!isWrappable(value2))
1089
+ return value2;
1090
+ const wrapped = wrap(value2, target);
1091
+ Writing.add(wrapped);
1092
+ return wrapped;
1093
+ }
1094
+ let value = tracked ? overridden || !proxySource ? nodes[property].read() : (nodes[property].read(), storeValue[property]) : storeValue[property];
1095
+ value === $DELETED && (value = void 0);
1275
1096
  if (!tracked) {
1276
- if (typeof value === "function" && !storeValue.hasOwnProperty(property)) {
1097
+ if (!overridden && typeof value === "function" && !storeValue.hasOwnProperty(property)) {
1277
1098
  let proto;
1278
- return !Array.isArray(storeValue) && (proto = Object.getPrototypeOf(storeValue)) && proto !== Object.prototype ? value.bind(storeValue) : value;
1099
+ return !Array.isArray(target[STORE_VALUE]) && (proto = Object.getPrototypeOf(target[STORE_VALUE])) && proto !== Object.prototype ? value.bind(storeValue) : value;
1279
1100
  } else if (getObserver()) {
1280
- return getNode(nodes, property, isWrappable(value) ? wrap2(value) : value).read();
1101
+ return getNode(nodes, property, isWrappable(value) ? wrap(value, target) : value).read();
1281
1102
  }
1282
1103
  }
1283
- return isWrappable(value) ? wrap2(value) : value;
1104
+ return isWrappable(value) ? wrap(value, target) : value;
1284
1105
  },
1285
1106
  has(target, property) {
1286
- if (property === $RAW || property === $PROXY || property === $TRACK || property === "__proto__")
1107
+ if (property === $PROXY || property === $TRACK || property === "__proto__")
1287
1108
  return true;
1288
- const has = property in target[STORE_VALUE];
1109
+ const has = target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE] ? target[STORE_OVERRIDE][property] !== $DELETED : property in target[STORE_VALUE];
1289
1110
  getObserver() && getNode(getNodes(target, STORE_HAS), property, has).read();
1290
1111
  return has;
1291
1112
  },
1292
- set(target, property, value) {
1293
- (Writing == null ? void 0 : Writing.has(target[STORE_VALUE])) && setProperty(target[STORE_VALUE], property, unwrap(value, false));
1113
+ set(target, property, rawValue) {
1114
+ const store = target[$PROXY];
1115
+ if (Writing == null ? void 0 : Writing.has(target[$PROXY])) {
1116
+ untrack(() => {
1117
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1118
+ const state = target[STORE_VALUE];
1119
+ const base = state[property];
1120
+ const prev = ((_a = target[STORE_OVERRIDE]) == null ? void 0 : _a[property]) || base;
1121
+ const value = ((_b = rawValue == null ? void 0 : rawValue[$TARGET]) == null ? void 0 : _b[STORE_VALUE]) ?? rawValue;
1122
+ if (prev === value)
1123
+ return true;
1124
+ const len = ((_c = target[STORE_OVERRIDE]) == null ? void 0 : _c.length) || state.length;
1125
+ if (value !== void 0 && value === base)
1126
+ delete target[STORE_OVERRIDE][property];
1127
+ else
1128
+ (target[STORE_OVERRIDE] || (target[STORE_OVERRIDE] = /* @__PURE__ */ Object.create(null)))[property] = value;
1129
+ const wrappable = isWrappable(value);
1130
+ if (isWrappable(prev)) {
1131
+ const parents = PARENTS.get(prev);
1132
+ parents && (parents instanceof Set ? parents.delete(store) : PARENTS.delete(prev));
1133
+ }
1134
+ if (recursivelyNotify(store, storeLookup) && wrappable)
1135
+ recursivelyAddParent(value, store);
1136
+ (_e = (_d = target[STORE_HAS]) == null ? void 0 : _d[property]) == null ? void 0 : _e.write(true);
1137
+ const nodes = getNodes(target, STORE_NODE);
1138
+ (_f = nodes[property]) == null ? void 0 : _f.write(wrappable ? wrap(value, target) : value);
1139
+ if (Array.isArray(state)) {
1140
+ const index = parseInt(property) + 1;
1141
+ if (index > len)
1142
+ (_g = nodes.length) == null ? void 0 : _g.write(index);
1143
+ }
1144
+ (_h = nodes[$TRACK]) == null ? void 0 : _h.write(void 0);
1145
+ });
1146
+ }
1294
1147
  return true;
1295
1148
  },
1296
1149
  deleteProperty(target, property) {
1297
- (Writing == null ? void 0 : Writing.has(target[STORE_VALUE])) && setProperty(target[STORE_VALUE], property, void 0, true);
1150
+ var _a;
1151
+ if ((Writing == null ? void 0 : Writing.has(target[$PROXY])) && ((_a = target[STORE_OVERRIDE]) == null ? void 0 : _a[property]) !== $DELETED) {
1152
+ untrack(() => {
1153
+ var _a2, _b, _c, _d, _e;
1154
+ const prev = ((_a2 = target[STORE_OVERRIDE]) == null ? void 0 : _a2[property]) || target[STORE_VALUE][property];
1155
+ if (property in target[STORE_VALUE]) {
1156
+ (target[STORE_OVERRIDE] || (target[STORE_OVERRIDE] = /* @__PURE__ */ Object.create(null)))[property] = $DELETED;
1157
+ } else if (target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE]) {
1158
+ delete target[STORE_OVERRIDE][property];
1159
+ } else
1160
+ return true;
1161
+ if (isWrappable(prev)) {
1162
+ const parents = PARENTS.get(prev);
1163
+ parents && (parents instanceof Set ? parents.delete(target) : PARENTS.delete(prev));
1164
+ }
1165
+ (_c = (_b = target[STORE_HAS]) == null ? void 0 : _b[property]) == null ? void 0 : _c.write(false);
1166
+ const nodes = getNodes(target, STORE_NODE);
1167
+ (_d = nodes[property]) == null ? void 0 : _d.write(void 0);
1168
+ (_e = nodes[$TRACK]) == null ? void 0 : _e.write(void 0);
1169
+ });
1170
+ }
1298
1171
  return true;
1299
1172
  },
1300
- ownKeys,
1301
- getOwnPropertyDescriptor: proxyDescriptor,
1173
+ ownKeys(target) {
1174
+ trackSelf(target);
1175
+ return getKeys(target[STORE_VALUE], target[STORE_OVERRIDE], false);
1176
+ },
1177
+ getOwnPropertyDescriptor(target, property) {
1178
+ if (property === $PROXY)
1179
+ return { value: target[$PROXY], writable: true, configurable: true };
1180
+ return getPropertyDescriptor(target[STORE_VALUE], target[STORE_OVERRIDE], property);
1181
+ },
1302
1182
  getPrototypeOf(target) {
1303
1183
  return Object.getPrototypeOf(target[STORE_VALUE]);
1304
1184
  }
1305
1185
  };
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];
1186
+ function storeSetter(store, fn) {
1187
+ const prevWriting = Writing;
1188
+ Writing = /* @__PURE__ */ new Set();
1189
+ Writing.add(store);
1190
+ try {
1191
+ fn(store);
1192
+ } finally {
1193
+ Writing.clear();
1194
+ Writing = prevWriting;
1195
+ }
1196
+ }
1197
+ function createStore(first, second) {
1198
+ const derived = typeof first === "function", wrappedStore = derived ? createProjection(first, second) : wrap(first);
1199
+ return [wrappedStore, (fn) => storeSetter(wrappedStore, fn)];
1200
+ }
1201
+ function recursivelyNotify(state, lookup) {
1202
+ var _a;
1203
+ let target = state[$TARGET] || ((_a = lookup == null ? void 0 : lookup.get(state)) == null ? void 0 : _a[$TARGET]);
1338
1204
  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);
1205
+ if (target) {
1206
+ const deep2 = getNodes(target, STORE_NODE)[$DEEP];
1207
+ if (deep2) {
1208
+ deep2.write(void 0);
1209
+ notified = true;
1210
+ }
1211
+ lookup = target[STORE_LOOKUP] || lookup;
1212
+ }
1213
+ const parents = PARENTS.get((target == null ? void 0 : target[STORE_VALUE]) || state);
1341
1214
  if (!parents)
1342
1215
  return notified;
1343
1216
  if (parents instanceof Set) {
1344
1217
  for (let parent of parents)
1345
- notified = recursivelyNotify(parent) || notified;
1218
+ notified = recursivelyNotify(parent, lookup) || notified;
1346
1219
  } else
1347
- notified = recursivelyNotify(parents) || notified;
1220
+ notified = recursivelyNotify(parents, lookup) || notified;
1348
1221
  return notified;
1349
1222
  }
1350
1223
  function recursivelyAddParent(state, parent) {
1224
+ let override;
1225
+ const target = state[$TARGET];
1226
+ if (target) {
1227
+ override = target[STORE_OVERRIDE];
1228
+ state = target[STORE_VALUE];
1229
+ }
1351
1230
  if (parent) {
1352
1231
  let parents = PARENTS.get(state);
1353
1232
  if (!parents)
@@ -1362,81 +1241,75 @@ function recursivelyAddParent(state, parent) {
1362
1241
  return;
1363
1242
  }
1364
1243
  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);
1244
+ const len = (override == null ? void 0 : override.length) || state.length;
1245
+ for (let i = 0; i < len; i++) {
1246
+ const item = override && i in override ? override[i] : state[i];
1247
+ isWrappable(item) && recursivelyAddParent(item, state);
1368
1248
  }
1369
1249
  } else {
1370
- const keys = Object.keys(state);
1250
+ const keys = getKeys(state, override);
1371
1251
  for (let i = 0; i < keys.length; i++) {
1372
- const item = state[keys[i]];
1373
- isWrappable(item) && recursivelyAddParent(item[$RAW] || item, state);
1252
+ const key = keys[i];
1253
+ const item = override && key in override ? override[key] : state[key];
1254
+ isWrappable(item) && recursivelyAddParent(item, state);
1374
1255
  }
1375
1256
  }
1376
1257
  }
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
1258
  function deep(store) {
1397
- recursivelyAddParent(store[$RAW] || store);
1259
+ recursivelyAddParent(store);
1398
1260
  return store[$DEEP];
1399
1261
  }
1400
1262
 
1401
1263
  // src/store/reconcile.ts
1402
- function applyState(next, state, keyFn) {
1264
+ function unwrap(value) {
1265
+ var _a;
1266
+ return ((_a = value == null ? void 0 : value[$TARGET]) == null ? void 0 : _a[STORE_NODE]) ?? value;
1267
+ }
1268
+ function getOverrideValue(value, override, key) {
1269
+ return override && key in override ? override[key] : value[key];
1270
+ }
1271
+ function getAllKeys(value, override, next) {
1272
+ const keys = getKeys(value, override);
1273
+ const nextKeys = Object.keys(next);
1274
+ return Array.from(/* @__PURE__ */ new Set([...keys, ...nextKeys]));
1275
+ }
1276
+ function applyState(next, state, keyFn, all) {
1403
1277
  var _a, _b, _c, _d, _e, _f, _g, _h;
1404
1278
  const target = state == null ? void 0 : state[$TARGET];
1405
1279
  if (!target)
1406
1280
  return;
1407
1281
  const previous = target[STORE_VALUE];
1408
- if (next === previous)
1282
+ const override = target[STORE_OVERRIDE];
1283
+ if (next === previous && !override)
1409
1284
  return;
1410
- Object.defineProperty(next, $PROXY, {
1411
- value: previous[$PROXY],
1412
- writable: true
1413
- });
1414
- previous[$PROXY] = null;
1285
+ (target[STORE_LOOKUP] || storeLookup).set(next, target[$PROXY]);
1415
1286
  target[STORE_VALUE] = next;
1287
+ target[STORE_OVERRIDE] = void 0;
1416
1288
  if (Array.isArray(previous)) {
1417
1289
  let changed = false;
1418
- if (next.length && previous.length && next[0] && keyFn(next[0]) != null) {
1290
+ const prevLength = getOverrideValue(previous, override, "length");
1291
+ if (next.length && prevLength && next[0] && keyFn(next[0]) != null) {
1419
1292
  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);
1293
+ 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++) {
1294
+ applyState(next[start], wrap(item, target), keyFn, all);
1422
1295
  }
1423
1296
  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];
1297
+ 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--) {
1298
+ temp[newEnd] = item;
1426
1299
  }
1427
1300
  if (start > newEnd || start > end) {
1428
1301
  for (j = start; j <= newEnd; j++) {
1429
1302
  changed = true;
1430
- (_a = target[STORE_NODE][j]) == null ? void 0 : _a.write(wrap2(next[j]));
1303
+ (_a = target[STORE_NODE][j]) == null ? void 0 : _a.write(wrap(next[j], target));
1431
1304
  }
1432
1305
  for (; j < next.length; j++) {
1433
1306
  changed = true;
1434
- const wrapped = wrap2(temp[j]);
1307
+ const wrapped = wrap(temp[j], target);
1435
1308
  (_b = target[STORE_NODE][j]) == null ? void 0 : _b.write(wrapped);
1436
- applyState(next[j], wrapped, keyFn);
1309
+ applyState(next[j], wrapped, keyFn, all);
1437
1310
  }
1438
1311
  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));
1312
+ prevLength !== next.length && ((_d = target[STORE_NODE].length) == null ? void 0 : _d.write(next.length));
1440
1313
  return;
1441
1314
  }
1442
1315
  newIndicesNext = new Array(newEnd + 1);
@@ -1448,31 +1321,32 @@ function applyState(next, state, keyFn) {
1448
1321
  newIndices.set(keyVal, j);
1449
1322
  }
1450
1323
  for (i = start; i <= end; i++) {
1451
- item = previous[i];
1324
+ item = getOverrideValue(previous, override, i);
1452
1325
  keyVal = item ? keyFn(item) : item;
1453
1326
  j = newIndices.get(keyVal);
1454
1327
  if (j !== void 0 && j !== -1) {
1455
- temp[j] = previous[i];
1328
+ temp[j] = item;
1456
1329
  j = newIndicesNext[j];
1457
1330
  newIndices.set(keyVal, j);
1458
1331
  }
1459
1332
  }
1460
1333
  for (j = start; j < next.length; j++) {
1461
1334
  if (j in temp) {
1462
- const wrapped = wrap2(temp[j]);
1335
+ const wrapped = wrap(temp[j], target);
1463
1336
  (_e = target[STORE_NODE][j]) == null ? void 0 : _e.write(wrapped);
1464
- applyState(next[j], wrapped, keyFn);
1337
+ applyState(next[j], wrapped, keyFn, all);
1465
1338
  } else
1466
- (_f = target[STORE_NODE][j]) == null ? void 0 : _f.write(wrap2(next[j]));
1339
+ (_f = target[STORE_NODE][j]) == null ? void 0 : _f.write(wrap(next[j], target));
1467
1340
  }
1468
1341
  if (start < next.length)
1469
1342
  changed = true;
1470
- } else if (previous.length && next.length) {
1343
+ } else if (prevLength && next.length) {
1471
1344
  for (let i = 0, len = next.length; i < len; i++) {
1472
- isWrappable(previous[i]) && applyState(next[i], wrap2(previous[i]), keyFn);
1345
+ const item = getOverrideValue(previous, override, i);
1346
+ isWrappable(item) && applyState(next[i], wrap(item, target), keyFn, all);
1473
1347
  }
1474
1348
  }
1475
- if (previous.length !== next.length) {
1349
+ if (prevLength !== next.length) {
1476
1350
  changed = true;
1477
1351
  (_g = target[STORE_NODE].length) == null ? void 0 : _g.write(next.length);
1478
1352
  }
@@ -1481,17 +1355,20 @@ function applyState(next, state, keyFn) {
1481
1355
  }
1482
1356
  let nodes = target[STORE_NODE];
1483
1357
  if (nodes) {
1484
- const keys = Object.keys(nodes);
1358
+ const tracked = nodes[$TRACK];
1359
+ const keys = tracked || all ? getAllKeys(previous, override, next) : Object.keys(nodes);
1485
1360
  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);
1361
+ const key = keys[i];
1362
+ const node = nodes[key];
1363
+ const previousValue = unwrap(getOverrideValue(previous, override, key));
1364
+ let nextValue = unwrap(next[key]);
1489
1365
  if (previousValue === nextValue)
1490
1366
  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);
1367
+ if (!previousValue || !isWrappable(previousValue) || keyFn(previousValue) != null && keyFn(previousValue) !== keyFn(nextValue)) {
1368
+ tracked == null ? void 0 : tracked.write(void 0);
1369
+ node == null ? void 0 : node.write(isWrappable(nextValue) ? wrap(nextValue, target) : nextValue);
1370
+ } else
1371
+ applyState(nextValue, wrap(previousValue, target), keyFn, all);
1495
1372
  }
1496
1373
  }
1497
1374
  if (nodes = target[STORE_HAS]) {
@@ -1501,17 +1378,70 @@ function applyState(next, state, keyFn) {
1501
1378
  }
1502
1379
  }
1503
1380
  }
1504
- function reconcile(value, key) {
1381
+ function reconcile(value, key, all = false) {
1505
1382
  return (state) => {
1506
1383
  const keyFn = typeof key === "string" ? (item) => item[key] : key;
1507
- if (keyFn(value) !== keyFn(state))
1384
+ const eq = keyFn(state);
1385
+ if (eq !== void 0 && keyFn(value) !== keyFn(state))
1508
1386
  throw new Error("Cannot reconcile states with different identity");
1509
- applyState(value, state, keyFn);
1510
- return state;
1387
+ applyState(value, state, keyFn, all);
1511
1388
  };
1512
1389
  }
1513
1390
 
1514
1391
  // src/store/utils.ts
1392
+ function snapshot(item, map, lookup) {
1393
+ var _a;
1394
+ let target, isArray, override, result, unwrapped, v;
1395
+ if (!isWrappable(item))
1396
+ return item;
1397
+ if (map && map.has(item))
1398
+ return map.get(item);
1399
+ if (!map)
1400
+ map = /* @__PURE__ */ new Map();
1401
+ if (target = item[$TARGET] || ((_a = lookup == null ? void 0 : lookup.get(item)) == null ? void 0 : _a[$TARGET])) {
1402
+ override = target[STORE_OVERRIDE];
1403
+ isArray = Array.isArray(target[STORE_VALUE]);
1404
+ map.set(
1405
+ item,
1406
+ override ? result = isArray ? [] : Object.create(Object.getPrototypeOf(target[STORE_VALUE])) : target[STORE_VALUE]
1407
+ );
1408
+ item = target[STORE_VALUE];
1409
+ lookup = storeLookup;
1410
+ } else {
1411
+ isArray = Array.isArray(item);
1412
+ map.set(item, item);
1413
+ }
1414
+ if (isArray) {
1415
+ const len = (override == null ? void 0 : override.length) || item.length;
1416
+ for (let i = 0; i < len; i++) {
1417
+ v = override && i in override ? override[i] : item[i];
1418
+ if (v === $DELETED)
1419
+ continue;
1420
+ if ((unwrapped = snapshot(v, map, lookup)) !== v || result) {
1421
+ if (!result)
1422
+ map.set(item, result = [...item]);
1423
+ result[i] = unwrapped;
1424
+ }
1425
+ }
1426
+ } else {
1427
+ const keys = getKeys(item, override);
1428
+ for (let i = 0, l = keys.length; i < l; i++) {
1429
+ let prop = keys[i];
1430
+ const desc = getPropertyDescriptor(item, override, prop);
1431
+ if (desc.get)
1432
+ continue;
1433
+ v = override && prop in override ? override[prop] : item[prop];
1434
+ if ((unwrapped = snapshot(v, map, lookup)) !== item[prop] || result) {
1435
+ if (!result) {
1436
+ result = Object.create(Object.getPrototypeOf(item));
1437
+ Object.assign(result, item);
1438
+ }
1439
+ result[prop] = unwrapped;
1440
+ }
1441
+ }
1442
+ }
1443
+ return result || item;
1444
+ }
1515
1445
  function trueFn() {
1516
1446
  return true;
1517
1447
  }
@@ -1663,33 +1593,34 @@ function omit(props, ...keys) {
1663
1593
  function mapArray(list, map, options) {
1664
1594
  const keyFn = typeof (options == null ? void 0 : options.keyed) === "function" ? options.keyed : void 0;
1665
1595
  return updateKeyedMap.bind({
1666
- H: new Owner(),
1596
+ G: new Owner(),
1667
1597
  i: 0,
1668
- _: list,
1598
+ $: list,
1669
1599
  u: [],
1670
1600
  C: map,
1671
- d: [],
1601
+ e: [],
1672
1602
  b: [],
1673
1603
  D: keyFn,
1674
1604
  j: keyFn || (options == null ? void 0 : options.keyed) === false ? [] : void 0,
1675
1605
  k: map.length > 1 ? [] : void 0,
1676
- I: options == null ? void 0 : options.fallback
1606
+ H: options == null ? void 0 : options.fallback
1677
1607
  });
1678
1608
  }
1609
+ var pureOptions = { pureWrite: true };
1679
1610
  function updateKeyedMap() {
1680
- const newItems = this._() || [], newLen = newItems.length;
1611
+ const newItems = this.$() || [], newLen = newItems.length;
1681
1612
  newItems[$TRACK];
1682
- runWithOwner(this.H, () => {
1613
+ runWithOwner(this.G, () => {
1683
1614
  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));
1615
+ this.j[j] = new Computation(newItems[j], null, pureOptions);
1616
+ this.k && (this.k[j] = new Computation(j, null, pureOptions));
1686
1617
  return this.C(
1687
1618
  Computation.prototype.read.bind(this.j[j]),
1688
1619
  this.k ? Computation.prototype.read.bind(this.k[j]) : void 0
1689
1620
  );
1690
1621
  } : this.k ? () => {
1691
1622
  const item = newItems[j];
1692
- this.k[j] = new Computation(j, null);
1623
+ this.k[j] = new Computation(j, null, pureOptions);
1693
1624
  return this.C(() => item, Computation.prototype.read.bind(this.k[j]));
1694
1625
  } : () => {
1695
1626
  const item = newItems[j];
@@ -1697,28 +1628,28 @@ function updateKeyedMap() {
1697
1628
  };
1698
1629
  if (newLen === 0) {
1699
1630
  if (this.i !== 0) {
1700
- this.H.dispose(false);
1631
+ this.G.dispose(false);
1701
1632
  this.b = [];
1702
1633
  this.u = [];
1703
- this.d = [];
1634
+ this.e = [];
1704
1635
  this.i = 0;
1705
1636
  this.j && (this.j = []);
1706
1637
  this.k && (this.k = []);
1707
1638
  }
1708
- if (this.I && !this.d[0]) {
1709
- this.d[0] = compute(
1639
+ if (this.H && !this.e[0]) {
1640
+ this.e[0] = compute(
1710
1641
  this.b[0] = new Owner(),
1711
- this.I,
1642
+ this.H,
1712
1643
  null
1713
1644
  );
1714
1645
  }
1715
1646
  } else if (this.i === 0) {
1716
1647
  if (this.b[0])
1717
1648
  this.b[0].dispose();
1718
- this.d = new Array(newLen);
1649
+ this.e = new Array(newLen);
1719
1650
  for (j = 0; j < newLen; j++) {
1720
1651
  this.u[j] = newItems[j];
1721
- this.d[j] = compute(this.b[j] = new Owner(), mapper, null);
1652
+ this.e[j] = compute(this.b[j] = new Owner(), mapper, null);
1722
1653
  }
1723
1654
  this.i = newLen;
1724
1655
  } else {
@@ -1728,7 +1659,7 @@ function updateKeyedMap() {
1728
1659
  this.j[start].write(newItems[start]);
1729
1660
  }
1730
1661
  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];
1662
+ temp[newEnd] = this.e[end];
1732
1663
  tempNodes[newEnd] = this.b[end];
1733
1664
  tempRows && (tempRows[newEnd] = this.j[end]);
1734
1665
  tempIndexes && (tempIndexes[newEnd] = this.k[end]);
@@ -1747,7 +1678,7 @@ function updateKeyedMap() {
1747
1678
  key = this.D ? this.D(item) : item;
1748
1679
  j = newIndices.get(key);
1749
1680
  if (j !== void 0 && j !== -1) {
1750
- temp[j] = this.d[i];
1681
+ temp[j] = this.e[i];
1751
1682
  tempNodes[j] = this.b[i];
1752
1683
  tempRows && (tempRows[j] = this.j[i]);
1753
1684
  tempIndexes && (tempIndexes[j] = this.k[i]);
@@ -1758,7 +1689,7 @@ function updateKeyedMap() {
1758
1689
  }
1759
1690
  for (j = start; j < newLen; j++) {
1760
1691
  if (j in temp) {
1761
- this.d[j] = temp[j];
1692
+ this.e[j] = temp[j];
1762
1693
  this.b[j] = tempNodes[j];
1763
1694
  if (tempRows) {
1764
1695
  this.j[j] = tempRows[j];
@@ -1769,44 +1700,44 @@ function updateKeyedMap() {
1769
1700
  this.k[j].write(j);
1770
1701
  }
1771
1702
  } else {
1772
- this.d[j] = compute(this.b[j] = new Owner(), mapper, null);
1703
+ this.e[j] = compute(this.b[j] = new Owner(), mapper, null);
1773
1704
  }
1774
1705
  }
1775
- this.d = this.d.slice(0, this.i = newLen);
1706
+ this.e = this.e.slice(0, this.i = newLen);
1776
1707
  this.u = newItems.slice(0);
1777
1708
  }
1778
1709
  });
1779
- return this.d;
1710
+ return this.e;
1780
1711
  }
1781
1712
  function repeat(count, map, options) {
1782
1713
  return updateRepeat.bind({
1783
- H: new Owner(),
1714
+ G: new Owner(),
1784
1715
  i: 0,
1785
1716
  q: 0,
1786
- $: count,
1717
+ aa: count,
1787
1718
  C: map,
1788
1719
  b: [],
1789
- d: [],
1790
- aa: options == null ? void 0 : options.from,
1791
- I: options == null ? void 0 : options.fallback
1720
+ e: [],
1721
+ ba: options == null ? void 0 : options.from,
1722
+ H: options == null ? void 0 : options.fallback
1792
1723
  });
1793
1724
  }
1794
1725
  function updateRepeat() {
1795
1726
  var _a;
1796
- const newLen = this.$();
1797
- const from = ((_a = this.aa) == null ? void 0 : _a.call(this)) || 0;
1798
- runWithOwner(this.H, () => {
1727
+ const newLen = this.aa();
1728
+ const from = ((_a = this.ba) == null ? void 0 : _a.call(this)) || 0;
1729
+ runWithOwner(this.G, () => {
1799
1730
  if (newLen === 0) {
1800
1731
  if (this.i !== 0) {
1801
- this.H.dispose(false);
1732
+ this.G.dispose(false);
1802
1733
  this.b = [];
1803
- this.d = [];
1734
+ this.e = [];
1804
1735
  this.i = 0;
1805
1736
  }
1806
- if (this.I && !this.d[0]) {
1807
- this.d[0] = compute(
1737
+ if (this.H && !this.e[0]) {
1738
+ this.e[0] = compute(
1808
1739
  this.b[0] = new Owner(),
1809
- this.I,
1740
+ this.H,
1810
1741
  null
1811
1742
  );
1812
1743
  }
@@ -1823,18 +1754,18 @@ function updateRepeat() {
1823
1754
  while (i < from && i < this.i)
1824
1755
  this.b[i++].dispose();
1825
1756
  this.b.splice(0, from - this.q);
1826
- this.d.splice(0, from - this.q);
1757
+ this.e.splice(0, from - this.q);
1827
1758
  } else if (this.q > from) {
1828
1759
  let i = prevTo - this.q - 1;
1829
1760
  let difference = this.q - from;
1830
- this.b.length = this.d.length = newLen;
1761
+ this.b.length = this.e.length = newLen;
1831
1762
  while (i >= difference) {
1832
1763
  this.b[i] = this.b[i - difference];
1833
- this.d[i] = this.d[i - difference];
1764
+ this.e[i] = this.e[i - difference];
1834
1765
  i--;
1835
1766
  }
1836
1767
  for (let i2 = 0; i2 < difference; i2++) {
1837
- this.d[i2] = compute(
1768
+ this.e[i2] = compute(
1838
1769
  this.b[i2] = new Owner(),
1839
1770
  () => this.C(i2 + from),
1840
1771
  null
@@ -1842,24 +1773,218 @@ function updateRepeat() {
1842
1773
  }
1843
1774
  }
1844
1775
  for (let i = prevTo; i < to; i++) {
1845
- this.d[i - from] = compute(
1776
+ this.e[i - from] = compute(
1846
1777
  this.b[i - from] = new Owner(),
1847
1778
  () => this.C(i),
1848
1779
  null
1849
1780
  );
1850
1781
  }
1851
- this.d = this.d.slice(0, newLen);
1782
+ this.e = this.e.slice(0, newLen);
1852
1783
  this.q = from;
1853
1784
  this.i = newLen;
1854
1785
  });
1855
- return this.d;
1786
+ return this.e;
1856
1787
  }
1857
1788
  function compare(key, a, b) {
1858
1789
  return key ? key(a) === key(b) : true;
1859
1790
  }
1860
1791
 
1792
+ // src/boundaries.ts
1793
+ var BoundaryComputation = class extends EagerComputation {
1794
+ I;
1795
+ constructor(compute2, propagationMask) {
1796
+ super(void 0, compute2, { defer: true });
1797
+ this.I = propagationMask;
1798
+ }
1799
+ write(value, flags) {
1800
+ super.write(value, flags & ~this.I);
1801
+ if (this.I & LOADING_BIT && !(this.f & UNINITIALIZED_BIT)) {
1802
+ flags &= ~LOADING_BIT;
1803
+ }
1804
+ this.h.notify(this, this.I, flags);
1805
+ return this.g;
1806
+ }
1807
+ };
1808
+ function createBoundChildren(owner, fn, queue, mask) {
1809
+ const parentQueue = owner.h;
1810
+ parentQueue.addChild(owner.h = queue);
1811
+ onCleanup(() => parentQueue.removeChild(owner.h));
1812
+ return compute(
1813
+ owner,
1814
+ () => {
1815
+ const c = new Computation(void 0, fn);
1816
+ return new BoundaryComputation(() => flatten(c.wait()), mask);
1817
+ },
1818
+ null
1819
+ );
1820
+ }
1821
+ var ConditionalQueue = class extends Queue {
1822
+ n;
1823
+ O = /* @__PURE__ */ new Set();
1824
+ P = /* @__PURE__ */ new Set();
1825
+ constructor(disabled) {
1826
+ super();
1827
+ this.n = disabled;
1828
+ }
1829
+ run(type) {
1830
+ if (!type || this.n.read())
1831
+ return;
1832
+ return super.run(type);
1833
+ }
1834
+ notify(node, type, flags) {
1835
+ if (this.n.read()) {
1836
+ if (type & LOADING_BIT) {
1837
+ if (flags & LOADING_BIT) {
1838
+ this.P.add(node);
1839
+ type &= ~LOADING_BIT;
1840
+ } else if (this.P.delete(node))
1841
+ type &= ~LOADING_BIT;
1842
+ }
1843
+ if (type & ERROR_BIT) {
1844
+ if (flags & ERROR_BIT) {
1845
+ this.O.add(node);
1846
+ type &= ~ERROR_BIT;
1847
+ } else if (this.O.delete(node))
1848
+ type &= ~ERROR_BIT;
1849
+ }
1850
+ }
1851
+ return type ? super.notify(node, type, flags) : true;
1852
+ }
1853
+ };
1854
+ var CollectionQueue = class extends Queue {
1855
+ Q;
1856
+ b = /* @__PURE__ */ new Set();
1857
+ n = new Computation(false, null, { pureWrite: true });
1858
+ constructor(type) {
1859
+ super();
1860
+ this.Q = type;
1861
+ }
1862
+ run(type) {
1863
+ if (!type || this.n.read())
1864
+ return;
1865
+ return super.run(type);
1866
+ }
1867
+ notify(node, type, flags) {
1868
+ if (!(type & this.Q))
1869
+ return super.notify(node, type, flags);
1870
+ if (flags & this.Q) {
1871
+ this.b.add(node);
1872
+ if (this.b.size === 1)
1873
+ this.n.write(true);
1874
+ } else {
1875
+ this.b.delete(node);
1876
+ if (this.b.size === 0)
1877
+ this.n.write(false);
1878
+ }
1879
+ type &= ~this.Q;
1880
+ return type ? super.notify(node, type, flags) : true;
1881
+ }
1882
+ };
1883
+ function createBoundary(fn, condition) {
1884
+ const owner = new Owner();
1885
+ const queue = new ConditionalQueue(
1886
+ new Computation(void 0, () => condition() === "hidden" /* HIDDEN */)
1887
+ );
1888
+ const tree = createBoundChildren(owner, fn, queue, 0);
1889
+ new EagerComputation(void 0, () => {
1890
+ const disabled = queue.n.read();
1891
+ tree.I = disabled ? ERROR_BIT | LOADING_BIT : 0;
1892
+ if (!disabled) {
1893
+ queue.P.forEach((node) => queue.notify(node, LOADING_BIT, LOADING_BIT));
1894
+ queue.O.forEach((node) => queue.notify(node, ERROR_BIT, ERROR_BIT));
1895
+ queue.P.clear();
1896
+ queue.O.clear();
1897
+ }
1898
+ });
1899
+ return () => queue.n.read() ? void 0 : tree.read();
1900
+ }
1901
+ function createCollectionBoundary(type, fn, fallback) {
1902
+ const owner = new Owner();
1903
+ const queue = new CollectionQueue(type);
1904
+ const tree = createBoundChildren(owner, fn, queue, type);
1905
+ const decision = new Computation(void 0, () => {
1906
+ if (!queue.n.read()) {
1907
+ const resolved = tree.read();
1908
+ if (!queue.n.read())
1909
+ return resolved;
1910
+ }
1911
+ return fallback(queue);
1912
+ });
1913
+ return decision.read.bind(decision);
1914
+ }
1915
+ function createSuspense(fn, fallback) {
1916
+ return createCollectionBoundary(LOADING_BIT, fn, () => fallback());
1917
+ }
1918
+ function createErrorBoundary(fn, fallback) {
1919
+ return createCollectionBoundary(
1920
+ ERROR_BIT,
1921
+ fn,
1922
+ (queue) => fallback(queue.b.values().next().value.F, () => {
1923
+ var _a;
1924
+ incrementClock();
1925
+ for (let node of queue.b) {
1926
+ node.a = STATE_DIRTY;
1927
+ (_a = node.h) == null ? void 0 : _a.enqueue(node.s, node);
1928
+ }
1929
+ })
1930
+ );
1931
+ }
1932
+ function flatten(children, options) {
1933
+ if (typeof children === "function" && !children.length) {
1934
+ if (options == null ? void 0 : options.doNotUnwrap)
1935
+ return children;
1936
+ do {
1937
+ children = children();
1938
+ } while (typeof children === "function" && !children.length);
1939
+ }
1940
+ if ((options == null ? void 0 : options.skipNonRendered) && (children == null || children === true || children === false || children === ""))
1941
+ return;
1942
+ if (Array.isArray(children)) {
1943
+ let results = [];
1944
+ if (flattenArray(children, results, options)) {
1945
+ return () => {
1946
+ let nested = [];
1947
+ flattenArray(results, nested, { ...options, doNotUnwrap: false });
1948
+ return nested;
1949
+ };
1950
+ }
1951
+ return results;
1952
+ }
1953
+ return children;
1954
+ }
1955
+ function flattenArray(children, results = [], options) {
1956
+ let notReady = null;
1957
+ let needsUnwrap = false;
1958
+ for (let i = 0; i < children.length; i++) {
1959
+ try {
1960
+ let child = children[i];
1961
+ if (typeof child === "function" && !child.length) {
1962
+ if (options == null ? void 0 : options.doNotUnwrap) {
1963
+ results.push(child);
1964
+ needsUnwrap = true;
1965
+ continue;
1966
+ }
1967
+ do {
1968
+ child = child();
1969
+ } while (typeof child === "function" && !child.length);
1970
+ }
1971
+ if (Array.isArray(child)) {
1972
+ needsUnwrap = flattenArray(child, results, options);
1973
+ } else if ((options == null ? void 0 : options.skipNonRendered) && (child == null || child === true || child === false || child === "")) {
1974
+ } else
1975
+ results.push(child);
1976
+ } catch (e) {
1977
+ if (!(e instanceof NotReadyError))
1978
+ throw e;
1979
+ notReady = e;
1980
+ }
1981
+ }
1982
+ if (notReady)
1983
+ throw notReady;
1984
+ return needsUnwrap;
1985
+ }
1986
+
1861
1987
  exports.$PROXY = $PROXY;
1862
- exports.$RAW = $RAW;
1863
1988
  exports.$TARGET = $TARGET;
1864
1989
  exports.$TRACK = $TRACK;
1865
1990
  exports.Computation = Computation;
@@ -1875,6 +2000,7 @@ exports.createContext = createContext;
1875
2000
  exports.createEffect = createEffect;
1876
2001
  exports.createErrorBoundary = createErrorBoundary;
1877
2002
  exports.createMemo = createMemo;
2003
+ exports.createOptimistic = createOptimistic;
1878
2004
  exports.createProjection = createProjection;
1879
2005
  exports.createRenderEffect = createRenderEffect;
1880
2006
  exports.createRoot = createRoot;
@@ -1883,7 +2009,7 @@ exports.createStore = createStore;
1883
2009
  exports.createSuspense = createSuspense;
1884
2010
  exports.deep = deep;
1885
2011
  exports.flatten = flatten;
1886
- exports.flushSync = flushSync;
2012
+ exports.flush = flush;
1887
2013
  exports.getContext = getContext;
1888
2014
  exports.getObserver = getObserver;
1889
2015
  exports.getOwner = getOwner;
@@ -1903,6 +2029,7 @@ exports.resolve = resolve;
1903
2029
  exports.runWithObserver = runWithObserver;
1904
2030
  exports.runWithOwner = runWithOwner;
1905
2031
  exports.setContext = setContext;
2032
+ exports.snapshot = snapshot;
2033
+ exports.transition = transition;
1906
2034
  exports.tryCatch = tryCatch;
1907
2035
  exports.untrack = untrack;
1908
- exports.unwrap = unwrap;