@solidjs/signals 0.0.3 → 0.0.5

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/prod.js CHANGED
@@ -3,9 +3,7 @@ var NotReadyError = class extends Error {
3
3
  };
4
4
  var NoOwnerError = class extends Error {
5
5
  constructor() {
6
- super(
7
- ""
8
- );
6
+ super("");
9
7
  }
10
8
  };
11
9
  var ContextNotFoundError = class extends Error {
@@ -16,21 +14,76 @@ var ContextNotFoundError = class extends Error {
16
14
  }
17
15
  };
18
16
 
19
- // src/utils.ts
20
- function isUndefined(value) {
21
- return typeof value === "undefined";
22
- }
23
-
24
17
  // src/core/constants.ts
25
18
  var STATE_CLEAN = 0;
26
19
  var STATE_CHECK = 1;
27
20
  var STATE_DIRTY = 2;
28
- var STATE_DISPOSED = 3;
21
+ var STATE_UNINITIALIZED = 3;
22
+ var STATE_DISPOSED = 4;
29
23
  var EFFECT_PURE = 0;
30
24
  var EFFECT_RENDER = 1;
31
25
  var EFFECT_USER = 2;
32
26
  var SUPPORTS_PROXY = typeof Proxy === "function";
33
27
 
28
+ // src/core/utils.ts
29
+ function isUndefined(value) {
30
+ return typeof value === "undefined";
31
+ }
32
+ function flatten(children, options) {
33
+ if (typeof children === "function" && !children.length) {
34
+ if (options?.doNotUnwrap)
35
+ return children;
36
+ do {
37
+ children = children();
38
+ } while (typeof children === "function" && !children.length);
39
+ }
40
+ if (options?.skipNonRendered && (children == null || children === true || children === false || children === ""))
41
+ return;
42
+ if (Array.isArray(children)) {
43
+ let results = [];
44
+ if (flattenArray(children, results, options)) {
45
+ return () => {
46
+ let nested = [];
47
+ flattenArray(results, nested, { ...options, doNotUnwrap: false });
48
+ return nested;
49
+ };
50
+ }
51
+ return results;
52
+ }
53
+ return children;
54
+ }
55
+ function flattenArray(children, results = [], options) {
56
+ let notReady = null;
57
+ let needsUnwrap = false;
58
+ for (let i = 0; i < children.length; i++) {
59
+ try {
60
+ let child = children[i];
61
+ if (typeof child === "function" && !child.length) {
62
+ if (options?.doNotUnwrap) {
63
+ results.push(child);
64
+ needsUnwrap = true;
65
+ continue;
66
+ }
67
+ do {
68
+ child = child();
69
+ } while (typeof child === "function" && !child.length);
70
+ }
71
+ if (Array.isArray(child)) {
72
+ needsUnwrap = flattenArray(child, results, options);
73
+ } else if (options?.skipNonRendered && (child == null || child === true || child === false || child === "")) {
74
+ } else
75
+ results.push(child);
76
+ } catch (e) {
77
+ if (!(e instanceof NotReadyError))
78
+ throw e;
79
+ notReady = e;
80
+ }
81
+ }
82
+ if (notReady)
83
+ throw notReady;
84
+ return needsUnwrap;
85
+ }
86
+
34
87
  // src/core/owner.ts
35
88
  var currentOwner = null;
36
89
  var defaultContext = {};
@@ -46,30 +99,30 @@ var Owner = class {
46
99
  // We flatten the owner tree into a linked list so that we don't need a pointer to .firstChild
47
100
  // However, the children are actually added in reverse creation order
48
101
  // See comment at the top of the file for an example of the _nextSibling traversal
49
- r = null;
50
- m = null;
51
102
  o = null;
103
+ m = null;
104
+ p = null;
52
105
  a = STATE_CLEAN;
53
- h = null;
54
- n = defaultContext;
55
106
  i = null;
107
+ n = defaultContext;
108
+ j = null;
56
109
  e = null;
57
110
  constructor(signal = false) {
58
111
  if (currentOwner && !signal)
59
112
  currentOwner.append(this);
60
113
  }
61
114
  append(child) {
62
- child.r = this;
63
115
  child.o = this;
116
+ child.p = this;
64
117
  if (this.m)
65
- this.m.o = child;
118
+ this.m.p = child;
66
119
  child.m = this.m;
67
120
  this.m = child;
68
121
  if (child.n !== this.n) {
69
122
  child.n = { ...this.n, ...child.n };
70
123
  }
71
- if (this.i) {
72
- child.i = !child.i ? this.i : [...child.i, ...this.i];
124
+ if (this.j) {
125
+ child.j = !child.j ? this.j : [...child.j, ...this.j];
73
126
  }
74
127
  if (this.e)
75
128
  child.e = this.e;
@@ -77,8 +130,8 @@ var Owner = class {
77
130
  dispose(self = true) {
78
131
  if (this.a === STATE_DISPOSED)
79
132
  return;
80
- let head = self ? this.o || this.r : this, current = this.m, next = null;
81
- while (current && current.r === this) {
133
+ let head = self ? this.p || this.o : this, current = this.m, next = null;
134
+ while (current && current.o === this) {
82
135
  current.dispose(true);
83
136
  current.x();
84
137
  next = current.m;
@@ -88,40 +141,40 @@ var Owner = class {
88
141
  if (self)
89
142
  this.x();
90
143
  if (current)
91
- current.o = !self ? this : this.o;
144
+ current.p = !self ? this : this.p;
92
145
  if (head)
93
146
  head.m = current;
94
147
  }
95
148
  x() {
96
- if (this.o)
97
- this.o.m = null;
98
- this.r = null;
149
+ if (this.p)
150
+ this.p.m = null;
99
151
  this.o = null;
152
+ this.p = null;
100
153
  this.n = defaultContext;
101
- this.i = null;
154
+ this.j = null;
102
155
  this.a = STATE_DISPOSED;
103
156
  this.emptyDisposal();
104
157
  }
105
158
  emptyDisposal() {
106
- if (!this.h)
159
+ if (!this.i)
107
160
  return;
108
- if (Array.isArray(this.h)) {
109
- for (let i = 0; i < this.h.length; i++) {
110
- const callable = this.h[i];
161
+ if (Array.isArray(this.i)) {
162
+ for (let i = 0; i < this.i.length; i++) {
163
+ const callable = this.i[i];
111
164
  callable.call(callable);
112
165
  }
113
166
  } else {
114
- this.h.call(this.h);
167
+ this.i.call(this.i);
115
168
  }
116
- this.h = null;
169
+ this.i = null;
117
170
  }
118
171
  handleError(error) {
119
- if (!this.i)
172
+ if (!this.j)
120
173
  throw error;
121
- let i = 0, len = this.i.length;
174
+ let i = 0, len = this.j.length;
122
175
  for (i = 0; i < len; i++) {
123
176
  try {
124
- this.i[i](error);
177
+ this.j[i](error);
125
178
  break;
126
179
  } catch (e) {
127
180
  error = e;
@@ -156,17 +209,18 @@ function setContext(context, value, owner = currentOwner) {
156
209
  function hasContext(context, owner = currentOwner) {
157
210
  return !isUndefined(owner?.n[context.id]);
158
211
  }
159
- function onCleanup(disposable) {
212
+ function onCleanup(fn) {
160
213
  if (!currentOwner)
161
- return;
214
+ return fn;
162
215
  const node = currentOwner;
163
- if (!node.h) {
164
- node.h = disposable;
165
- } else if (Array.isArray(node.h)) {
166
- node.h.push(disposable);
216
+ if (!node.i) {
217
+ node.i = fn;
218
+ } else if (Array.isArray(node.i)) {
219
+ node.i.push(fn);
167
220
  } else {
168
- node.h = [node.h, disposable];
221
+ node.i = [node.i, fn];
169
222
  }
223
+ return fn;
170
224
  }
171
225
 
172
226
  // src/core/flags.ts
@@ -193,40 +247,40 @@ function incrementClock() {
193
247
  }
194
248
  var UNCHANGED = Symbol(0);
195
249
  var Computation = class extends Owner {
196
- b = null;
197
250
  c = null;
251
+ b = null;
198
252
  d;
199
- D;
253
+ B;
200
254
  // Used in __DEV__ mode, hopefully removed in production
201
255
  V;
202
256
  // Using false is an optimization as an alternative to _equals: () => false
203
257
  // which could enable more efficient DIRTY notification
204
- E = isEqual;
258
+ F = isEqual;
205
259
  N;
206
260
  /** Whether the computation is an error or has ancestors that are unresolved */
207
- j = 0;
261
+ f = 0;
208
262
  /** Which flags raised by sources are handled, vs. being passed through. */
209
- A = DEFAULT_FLAGS;
210
- F = null;
263
+ C = DEFAULT_FLAGS;
211
264
  G = null;
212
- H = -1;
265
+ H = null;
266
+ I = -1;
213
267
  constructor(initialValue, compute2, options) {
214
268
  super(compute2 === null);
215
- this.D = compute2;
216
- this.a = compute2 ? STATE_DIRTY : STATE_CLEAN;
269
+ this.B = compute2;
270
+ this.a = compute2 ? STATE_UNINITIALIZED : STATE_CLEAN;
217
271
  this.d = initialValue;
218
272
  if (options?.equals !== void 0)
219
- this.E = options.equals;
273
+ this.F = options.equals;
220
274
  if (options?.unobserved)
221
275
  this.N = options?.unobserved;
222
276
  }
223
277
  O() {
224
- if (this.D)
278
+ if (this.B)
225
279
  this.s();
226
- if (!this.b || this.b.length)
280
+ if (!this.B || this.c?.length)
227
281
  track(this);
228
- newFlags |= this.j & ~currentMask;
229
- if (this.j & ERROR_BIT) {
282
+ newFlags |= this.f & ~currentMask;
283
+ if (this.f & ERROR_BIT) {
230
284
  throw this.d;
231
285
  } else {
232
286
  return this.d;
@@ -260,36 +314,36 @@ var Computation = class extends Owner {
260
314
  * loading state changes
261
315
  */
262
316
  loading() {
263
- if (this.G === null) {
264
- this.G = loadingState(this);
317
+ if (this.H === null) {
318
+ this.H = loadingState(this);
265
319
  }
266
- return this.G.read();
320
+ return this.H.read();
267
321
  }
268
322
  /**
269
323
  * Return true if the computation is the computation threw an error
270
324
  * Triggers re-execution of the computation when the error state changes
271
325
  */
272
326
  error() {
273
- if (this.F === null) {
274
- this.F = errorState(this);
327
+ if (this.G === null) {
328
+ this.G = errorState(this);
275
329
  }
276
- return this.F.read();
330
+ return this.G.read();
277
331
  }
278
332
  /** Update the computation with a new value. */
279
333
  write(value, flags = 0, raw = false) {
280
334
  const newValue = !raw && typeof value === "function" ? value(this.d) : value;
281
- const valueChanged = newValue !== UNCHANGED && (!!(flags & ERROR_BIT) || this.E === false || !this.E(this.d, newValue));
335
+ const valueChanged = newValue !== UNCHANGED && (!!(flags & ERROR_BIT) || this.a === STATE_UNINITIALIZED || this.F === false || !this.F(this.d, newValue));
282
336
  if (valueChanged)
283
337
  this.d = newValue;
284
- const changedFlagsMask = this.j ^ flags, changedFlags = changedFlagsMask & flags;
285
- this.j = flags;
286
- this.H = clock + 1;
287
- if (this.c) {
288
- for (let i = 0; i < this.c.length; i++) {
338
+ const changedFlagsMask = this.f ^ flags, changedFlags = changedFlagsMask & flags;
339
+ this.f = flags;
340
+ this.I = clock + 1;
341
+ if (this.b) {
342
+ for (let i = 0; i < this.b.length; i++) {
289
343
  if (valueChanged) {
290
- this.c[i].t(STATE_DIRTY);
344
+ this.b[i].t(STATE_DIRTY);
291
345
  } else if (changedFlagsMask) {
292
- this.c[i].P(changedFlagsMask, changedFlags);
346
+ this.b[i].P(changedFlagsMask, changedFlags);
293
347
  }
294
348
  }
295
349
  }
@@ -302,9 +356,9 @@ var Computation = class extends Owner {
302
356
  if (this.a >= state)
303
357
  return;
304
358
  this.a = state;
305
- if (this.c) {
306
- for (let i = 0; i < this.c.length; i++) {
307
- this.c[i].t(STATE_CHECK);
359
+ if (this.b) {
360
+ for (let i = 0; i < this.b.length; i++) {
361
+ this.b[i].t(STATE_CHECK);
308
362
  }
309
363
  }
310
364
  }
@@ -317,27 +371,27 @@ var Computation = class extends Owner {
317
371
  P(mask, newFlags2) {
318
372
  if (this.a >= STATE_DIRTY)
319
373
  return;
320
- if (mask & this.A) {
374
+ if (mask & this.C) {
321
375
  this.t(STATE_DIRTY);
322
376
  return;
323
377
  }
324
378
  if (this.a >= STATE_CHECK)
325
379
  return;
326
- const prevFlags = this.j & mask;
380
+ const prevFlags = this.f & mask;
327
381
  const deltaFlags = prevFlags ^ newFlags2;
328
382
  if (newFlags2 === prevFlags) ; else if (deltaFlags & prevFlags & mask) {
329
383
  this.t(STATE_CHECK);
330
384
  } else {
331
- this.j ^= deltaFlags;
332
- if (this.c) {
333
- for (let i = 0; i < this.c.length; i++) {
334
- this.c[i].P(mask, newFlags2);
385
+ this.f ^= deltaFlags;
386
+ if (this.b) {
387
+ for (let i = 0; i < this.b.length; i++) {
388
+ this.b[i].P(mask, newFlags2);
335
389
  }
336
390
  }
337
391
  }
338
392
  }
339
393
  Q(error) {
340
- this.write(error, this.j | ERROR_BIT);
394
+ this.write(error, this.f | ERROR_BIT);
341
395
  }
342
396
  /**
343
397
  * This is the core part of the reactivity system, which makes sure that the values are updated
@@ -355,15 +409,15 @@ var Computation = class extends Owner {
355
409
  }
356
410
  let observerFlags = 0;
357
411
  if (this.a === STATE_CHECK) {
358
- for (let i = 0; i < this.b.length; i++) {
359
- this.b[i].s();
360
- observerFlags |= this.b[i].j;
412
+ for (let i = 0; i < this.c.length; i++) {
413
+ this.c[i].s();
414
+ observerFlags |= this.c[i].f;
361
415
  if (this.a === STATE_DIRTY) {
362
416
  break;
363
417
  }
364
418
  }
365
419
  }
366
- if (this.a === STATE_DIRTY) {
420
+ if (this.a === STATE_DIRTY || this.a === STATE_UNINITIALIZED) {
367
421
  update(this);
368
422
  } else {
369
423
  this.write(UNCHANGED, observerFlags);
@@ -376,46 +430,46 @@ var Computation = class extends Owner {
376
430
  x() {
377
431
  if (this.a === STATE_DISPOSED)
378
432
  return;
379
- if (this.b)
433
+ if (this.c)
380
434
  removeSourceObservers(this, 0);
381
435
  super.x();
382
436
  }
383
437
  };
384
438
  function loadingState(node) {
385
- const prevOwner = setOwner(node.r);
439
+ const prevOwner = setOwner(node.o);
386
440
  const options = void 0;
387
441
  const computation = new Computation(
388
442
  void 0,
389
443
  () => {
390
444
  track(node);
391
445
  node.s();
392
- return !!(node.j & LOADING_BIT);
446
+ return !!(node.f & LOADING_BIT);
393
447
  },
394
448
  options
395
449
  );
396
- computation.A = ERROR_BIT | LOADING_BIT;
450
+ computation.C = ERROR_BIT | LOADING_BIT;
397
451
  setOwner(prevOwner);
398
452
  return computation;
399
453
  }
400
454
  function errorState(node) {
401
- const prevOwner = setOwner(node.r);
455
+ const prevOwner = setOwner(node.o);
402
456
  const options = void 0;
403
457
  const computation = new Computation(
404
458
  void 0,
405
459
  () => {
406
460
  track(node);
407
461
  node.s();
408
- return !!(node.j & ERROR_BIT);
462
+ return !!(node.f & ERROR_BIT);
409
463
  },
410
464
  options
411
465
  );
412
- computation.A = ERROR_BIT;
466
+ computation.C = ERROR_BIT;
413
467
  setOwner(prevOwner);
414
468
  return computation;
415
469
  }
416
470
  function track(computation) {
417
471
  if (currentObserver) {
418
- if (!newSources && currentObserver.b && currentObserver.b[newSourcesIndex] === computation) {
472
+ if (!newSources && currentObserver.c && currentObserver.c[newSourcesIndex] === computation) {
419
473
  newSourcesIndex++;
420
474
  } else if (!newSources)
421
475
  newSources = [computation];
@@ -423,7 +477,7 @@ function track(computation) {
423
477
  newSources.push(computation);
424
478
  }
425
479
  if (updateCheck) {
426
- updateCheck.d = computation.H > currentObserver.H;
480
+ updateCheck.d = computation.I > currentObserver.I;
427
481
  }
428
482
  }
429
483
  }
@@ -435,7 +489,7 @@ function update(node) {
435
489
  try {
436
490
  node.dispose(false);
437
491
  node.emptyDisposal();
438
- const result = compute(node, node.D, node);
492
+ const result = compute(node, node.B, node);
439
493
  node.write(result, newFlags, true);
440
494
  } catch (error) {
441
495
  if (error instanceof NotReadyError) {
@@ -445,27 +499,27 @@ function update(node) {
445
499
  }
446
500
  } finally {
447
501
  if (newSources) {
448
- if (node.b)
502
+ if (node.c)
449
503
  removeSourceObservers(node, newSourcesIndex);
450
- if (node.b && newSourcesIndex > 0) {
451
- node.b.length = newSourcesIndex + newSources.length;
504
+ if (node.c && newSourcesIndex > 0) {
505
+ node.c.length = newSourcesIndex + newSources.length;
452
506
  for (let i = 0; i < newSources.length; i++) {
453
- node.b[newSourcesIndex + i] = newSources[i];
507
+ node.c[newSourcesIndex + i] = newSources[i];
454
508
  }
455
509
  } else {
456
- node.b = newSources;
510
+ node.c = newSources;
457
511
  }
458
512
  let source;
459
- for (let i = newSourcesIndex; i < node.b.length; i++) {
460
- source = node.b[i];
461
- if (!source.c)
462
- source.c = [node];
513
+ for (let i = newSourcesIndex; i < node.c.length; i++) {
514
+ source = node.c[i];
515
+ if (!source.b)
516
+ source.b = [node];
463
517
  else
464
- source.c.push(node);
518
+ source.b.push(node);
465
519
  }
466
- } else if (node.b && newSourcesIndex < node.b.length) {
520
+ } else if (node.c && newSourcesIndex < node.c.length) {
467
521
  removeSourceObservers(node, newSourcesIndex);
468
- node.b.length = newSourcesIndex;
522
+ node.c.length = newSourcesIndex;
469
523
  }
470
524
  newSources = prevSources;
471
525
  newSourcesIndex = prevSourcesIndex;
@@ -476,13 +530,13 @@ function update(node) {
476
530
  function removeSourceObservers(node, index) {
477
531
  let source;
478
532
  let swap;
479
- for (let i = index; i < node.b.length; i++) {
480
- source = node.b[i];
481
- if (source.c) {
482
- swap = source.c.indexOf(node);
483
- source.c[swap] = source.c[source.c.length - 1];
484
- source.c.pop();
485
- if (!source.c.length)
533
+ for (let i = index; i < node.c.length; i++) {
534
+ source = node.c[i];
535
+ if (source.b) {
536
+ swap = source.b.indexOf(node);
537
+ source.b[swap] = source.b[source.b.length - 1];
538
+ source.b.pop();
539
+ if (!source.b.length)
486
540
  source.N?.();
487
541
  }
488
542
  }
@@ -527,7 +581,7 @@ function latest(fn) {
527
581
  function compute(owner, compute2, observer) {
528
582
  const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask;
529
583
  currentObserver = observer;
530
- currentMask = observer?.A ?? DEFAULT_FLAGS;
584
+ currentMask = observer?.C ?? DEFAULT_FLAGS;
531
585
  try {
532
586
  return compute2(observer ? observer.d : void 0);
533
587
  } finally {
@@ -543,10 +597,11 @@ function schedule() {
543
597
  if (scheduled)
544
598
  return;
545
599
  scheduled = true;
546
- queueMicrotask(flushSync);
600
+ if (!globalQueue.D)
601
+ queueMicrotask(flushSync);
547
602
  }
548
603
  var Queue = class {
549
- I = false;
604
+ D = false;
550
605
  u = [[], [], []];
551
606
  z = [];
552
607
  enqueue(type, node) {
@@ -571,16 +626,17 @@ var Queue = class {
571
626
  }
572
627
  }
573
628
  flush() {
574
- if (this.I)
629
+ if (this.D)
575
630
  return;
576
- this.I = true;
631
+ this.D = true;
577
632
  try {
578
633
  this.run(EFFECT_PURE);
579
634
  incrementClock();
635
+ scheduled = false;
580
636
  this.run(EFFECT_RENDER);
581
637
  this.run(EFFECT_USER);
582
638
  } finally {
583
- this.I = false;
639
+ this.D = false;
584
640
  }
585
641
  }
586
642
  addChild(child) {
@@ -593,9 +649,18 @@ var Queue = class {
593
649
  }
594
650
  };
595
651
  var globalQueue = new Queue();
652
+ var globalTasks = [];
596
653
  function flushSync() {
597
- globalQueue.flush();
598
- scheduled = false;
654
+ while (scheduled) {
655
+ globalQueue.flush();
656
+ for (let i = 0; i < globalTasks.length; i++)
657
+ globalTasks[i]();
658
+ globalTasks.length = 0;
659
+ }
660
+ }
661
+ function queueTask(fn) {
662
+ globalTasks.push(fn);
663
+ schedule();
599
664
  }
600
665
  function createBoundary(fn, queue) {
601
666
  const owner = new Owner();
@@ -606,7 +671,7 @@ function createBoundary(fn, queue) {
606
671
  }
607
672
  function runTop(node) {
608
673
  const ancestors = [];
609
- for (let current = node; current !== null; current = current.r) {
674
+ for (let current = node; current !== null; current = current.o) {
610
675
  if (current.a !== STATE_CLEAN) {
611
676
  ancestors.push(current);
612
677
  }
@@ -631,23 +696,23 @@ function runEffectQueue(queue) {
631
696
  var Effect = class extends Computation {
632
697
  J;
633
698
  K = false;
634
- B;
635
- C;
699
+ E;
700
+ A;
636
701
  e;
637
702
  constructor(initialValue, compute2, effect, options) {
638
703
  super(initialValue, compute2, options);
639
704
  this.J = effect;
640
- this.B = initialValue;
641
- this.C = options?.render ? EFFECT_RENDER : EFFECT_USER;
705
+ this.E = initialValue;
706
+ this.A = options?.render ? EFFECT_RENDER : EFFECT_USER;
642
707
  this.e = getOwner()?.e || globalQueue;
643
708
  this.s();
644
- this.C === EFFECT_USER ? this.e.enqueue(this.C, this) : this.R();
709
+ this.A === EFFECT_USER ? this.e.enqueue(this.A, this) : this.R();
645
710
  }
646
711
  write(value, flags = 0) {
647
- const currentFlags = this.j;
648
- this.j = flags;
649
- if ((flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
650
- this.e.T?.(this);
712
+ const currentFlags = this.f;
713
+ this.f = flags;
714
+ if (this.A === EFFECT_RENDER && (flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
715
+ this.e.S?.(this);
651
716
  }
652
717
  if (value === UNCHANGED)
653
718
  return this.d;
@@ -659,7 +724,7 @@ var Effect = class extends Computation {
659
724
  if (this.a >= state)
660
725
  return;
661
726
  if (this.a === STATE_CLEAN)
662
- this.e.enqueue(this.C, this);
727
+ this.e.enqueue(this.A, this);
663
728
  this.a = state;
664
729
  }
665
730
  Q(error) {
@@ -667,13 +732,13 @@ var Effect = class extends Computation {
667
732
  }
668
733
  x() {
669
734
  this.J = void 0;
670
- this.B = void 0;
735
+ this.E = void 0;
671
736
  super.x();
672
737
  }
673
738
  R() {
674
739
  if (this.K && this.a !== STATE_DISPOSED) {
675
- this.J(this.d, this.B);
676
- this.B = this.d;
740
+ this.J(this.d, this.E);
741
+ this.E = this.d;
677
742
  this.K = false;
678
743
  }
679
744
  }
@@ -696,35 +761,48 @@ var EagerComputation = class extends Computation {
696
761
 
697
762
  // src/core/suspense.ts
698
763
  var SuspenseQueue = class extends Queue {
699
- f = /* @__PURE__ */ new Set();
700
- p = false;
764
+ g = /* @__PURE__ */ new Set();
765
+ q = false;
701
766
  L = new Computation(false, null);
702
767
  run(type) {
703
- if (type && this.p)
768
+ if (type && this.q)
704
769
  return;
705
770
  super.run(type);
706
771
  }
707
- T(node) {
708
- if (node.j & LOADING_BIT) {
709
- this.f.add(node);
710
- if (!this.p) {
711
- this.p = true;
712
- queueMicrotask(() => this.L.write(true));
772
+ S(node) {
773
+ if (node.f & LOADING_BIT) {
774
+ this.g.add(node);
775
+ if (!this.q) {
776
+ this.q = true;
777
+ queueTask(() => this.L.write(true));
713
778
  }
714
779
  } else {
715
- this.f.delete(node);
716
- if (this.f.size === 0) {
717
- this.p = false;
718
- queueMicrotask(() => this.L.write(false));
780
+ this.g.delete(node);
781
+ if (this.g.size === 0) {
782
+ this.q = false;
783
+ queueTask(() => this.L.write(false));
719
784
  }
720
785
  }
721
786
  }
722
787
  };
788
+ var LiveComputation = class extends EagerComputation {
789
+ write(value, flags = 0) {
790
+ const currentFlags = this.f;
791
+ super.write(value, flags);
792
+ if ((flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
793
+ this.e.S?.(this);
794
+ }
795
+ return this.d;
796
+ }
797
+ };
723
798
  function createSuspense(fn, fallbackFn) {
724
799
  const queue = new SuspenseQueue();
725
- const tree = createBoundary(fn, queue);
726
- const equality = new Computation(null, () => queue.L.read() || queue.p);
727
- const comp = new Computation(null, () => equality.read() ? untrack(fallbackFn) : tree);
800
+ const tree = createBoundary(() => {
801
+ const child = new Computation(null, fn);
802
+ return new LiveComputation(null, () => flatten(child.wait()));
803
+ }, queue);
804
+ const equality = new Computation(null, () => queue.L.read() || queue.q);
805
+ const comp = new Computation(null, () => equality.read() ? untrack(fallbackFn) : tree.read());
728
806
  return comp.read.bind(comp);
729
807
  }
730
808
 
@@ -744,14 +822,35 @@ function createSignal(first, second, third) {
744
822
  const node = new Computation(first, null, second);
745
823
  return [node.read.bind(node), node.write.bind(node)];
746
824
  }
747
- function createAsync(fn, initial, options) {
825
+ function createMemo(compute2, value, options) {
826
+ let node = new Computation(
827
+ value,
828
+ compute2,
829
+ options
830
+ );
831
+ let resolvedValue;
832
+ return () => {
833
+ if (node) {
834
+ resolvedValue = node.wait();
835
+ if (!node.c?.length && node.m?.o !== node) {
836
+ node.dispose();
837
+ node = void 0;
838
+ } else if (!node.o && !node.b?.length) {
839
+ node.dispose();
840
+ node.a = STATE_UNINITIALIZED;
841
+ }
842
+ }
843
+ return resolvedValue;
844
+ };
845
+ }
846
+ function createAsync(compute2, value, options) {
748
847
  const lhs = new EagerComputation(
749
848
  {
750
- d: initial
849
+ d: value
751
850
  },
752
851
  (p) => {
753
- const value = p?.d;
754
- const source = fn(value);
852
+ const value2 = p?.d;
853
+ const source = compute2(value2);
755
854
  const isPromise = source instanceof Promise;
756
855
  const iterator = source[Symbol.asyncIterator];
757
856
  if (!isPromise && !iterator) {
@@ -762,12 +861,12 @@ function createAsync(fn, initial, options) {
762
861
  d: source
763
862
  };
764
863
  }
765
- const signal = new Computation(value, null, options);
864
+ const signal = new Computation(value2, null, options);
766
865
  signal.write(UNCHANGED, LOADING_BIT);
767
866
  if (isPromise) {
768
867
  source.then(
769
- (value2) => {
770
- signal.write(value2, 0);
868
+ (value3) => {
869
+ signal.write(value3, 0, true);
771
870
  },
772
871
  (error) => {
773
872
  signal.write(error, ERROR_BIT);
@@ -778,10 +877,10 @@ function createAsync(fn, initial, options) {
778
877
  onCleanup(() => abort = true);
779
878
  (async () => {
780
879
  try {
781
- for await (let value2 of source) {
880
+ for await (let value3 of source) {
782
881
  if (abort)
783
882
  return;
784
- signal.write(value2, 0);
883
+ signal.write(value3, 0, true);
785
884
  }
786
885
  } catch (error) {
787
886
  signal.write(error, ERROR_BIT);
@@ -793,28 +892,16 @@ function createAsync(fn, initial, options) {
793
892
  );
794
893
  return () => lhs.wait().wait();
795
894
  }
796
- function createMemo(compute2, initialValue, options) {
797
- let node = new Computation(initialValue, compute2, options);
798
- let value;
799
- return () => {
800
- if (node) {
801
- value = node.wait();
802
- if (!node.b?.length)
803
- node = void 0;
804
- }
805
- return value;
806
- };
807
- }
808
- function createEffect(compute2, effect, initialValue, options) {
895
+ function createEffect(compute2, effect, value, options) {
809
896
  void new Effect(
810
- initialValue,
897
+ value,
811
898
  compute2,
812
899
  effect,
813
900
  void 0
814
901
  );
815
902
  }
816
- function createRenderEffect(compute2, effect, initialValue, options) {
817
- void new Effect(initialValue, compute2, effect, {
903
+ function createRenderEffect(compute2, effect, value, options) {
904
+ void new Effect(value, compute2, effect, {
818
905
  render: true,
819
906
  ...void 0
820
907
  });
@@ -833,9 +920,9 @@ function runWithOwner(owner, run) {
833
920
  }
834
921
  function catchError(fn, handler) {
835
922
  const owner = new Owner();
836
- owner.i = owner.i ? [handler, ...owner.i] : [handler];
923
+ owner.j = owner.j ? [handler, ...owner.j] : [handler];
837
924
  try {
838
- compute(owner, fn, null);
925
+ return compute(owner, fn, null);
839
926
  } catch (error) {
840
927
  owner.handleError(error);
841
928
  }
@@ -1300,28 +1387,28 @@ function omit(props, ...keys) {
1300
1387
  function mapArray(list, map, options) {
1301
1388
  const keyFn = typeof options?.keyed === "function" ? options.keyed : void 0;
1302
1389
  return updateKeyedMap.bind({
1303
- S: new Owner(),
1390
+ T: new Owner(),
1304
1391
  w: 0,
1305
1392
  U: list,
1306
- q: [],
1393
+ r: [],
1307
1394
  M: map,
1308
1395
  k: [],
1309
- f: [],
1396
+ g: [],
1310
1397
  y: keyFn,
1311
- g: keyFn || options?.keyed === false ? [] : void 0,
1398
+ h: keyFn || options?.keyed === false ? [] : void 0,
1312
1399
  l: map.length > 1 ? [] : void 0,
1313
- p: options?.fallback
1400
+ q: options?.fallback
1314
1401
  });
1315
1402
  }
1316
1403
  function updateKeyedMap() {
1317
1404
  const newItems = this.U() || [], newLen = newItems.length;
1318
1405
  newItems[$TRACK];
1319
- runWithOwner(this.S, () => {
1320
- let i, j, mapper = this.g ? () => {
1321
- this.g[j] = new Computation(newItems[j], null);
1406
+ runWithOwner(this.T, () => {
1407
+ let i, j, mapper = this.h ? () => {
1408
+ this.h[j] = new Computation(newItems[j], null);
1322
1409
  this.l[j] = new Computation(j, null);
1323
1410
  return this.M(
1324
- Computation.prototype.read.bind(this.g[j]),
1411
+ Computation.prototype.read.bind(this.h[j]),
1325
1412
  Computation.prototype.read.bind(this.l[j])
1326
1413
  );
1327
1414
  } : this.l ? () => {
@@ -1334,36 +1421,40 @@ function updateKeyedMap() {
1334
1421
  };
1335
1422
  if (newLen === 0) {
1336
1423
  if (this.w !== 0) {
1337
- this.S.dispose(false);
1338
- this.f = [];
1339
- this.q = [];
1424
+ this.T.dispose(false);
1425
+ this.g = [];
1426
+ this.r = [];
1340
1427
  this.k = [];
1341
1428
  this.w = 0;
1342
- this.g && (this.g = []);
1429
+ this.h && (this.h = []);
1343
1430
  this.l && (this.l = []);
1344
1431
  }
1345
- if (this.p && !this.k[0]) {
1346
- this.k[0] = compute(this.f[0] = new Owner(), this.p, null);
1432
+ if (this.q && !this.k[0]) {
1433
+ this.k[0] = compute(
1434
+ this.g[0] = new Owner(),
1435
+ this.q,
1436
+ null
1437
+ );
1347
1438
  }
1348
1439
  } else if (this.w === 0) {
1349
- if (this.f[0])
1350
- this.f[0].dispose();
1440
+ if (this.g[0])
1441
+ this.g[0].dispose();
1351
1442
  this.k = new Array(newLen);
1352
1443
  for (j = 0; j < newLen; j++) {
1353
- this.q[j] = newItems[j];
1354
- this.k[j] = compute(this.f[j] = new Owner(), mapper, null);
1444
+ this.r[j] = newItems[j];
1445
+ this.k[j] = compute(this.g[j] = new Owner(), mapper, null);
1355
1446
  }
1356
1447
  this.w = newLen;
1357
1448
  } else {
1358
- let start, end, newEnd, item, key, newIndices, newIndicesNext, temp = new Array(newLen), tempNodes = new Array(newLen), tempRows = this.g ? new Array(newLen) : void 0, tempIndexes = this.l ? new Array(newLen) : void 0;
1359
- for (start = 0, end = Math.min(this.w, newLen); start < end && (this.q[start] === newItems[start] || this.g && compare(this.y, this.q[start], newItems[start])); start++) {
1360
- if (this.g)
1361
- this.g[start].write(newItems[start]);
1449
+ let start, end, newEnd, item, key, newIndices, newIndicesNext, temp = new Array(newLen), tempNodes = new Array(newLen), tempRows = this.h ? new Array(newLen) : void 0, tempIndexes = this.l ? new Array(newLen) : void 0;
1450
+ for (start = 0, end = Math.min(this.w, newLen); start < end && (this.r[start] === newItems[start] || this.h && compare(this.y, this.r[start], newItems[start])); start++) {
1451
+ if (this.h)
1452
+ this.h[start].write(newItems[start]);
1362
1453
  }
1363
- for (end = this.w - 1, newEnd = newLen - 1; end >= start && newEnd >= start && (this.q[end] === newItems[newEnd] || this.g && compare(this.y, this.q[end], newItems[newEnd])); end--, newEnd--) {
1454
+ for (end = this.w - 1, newEnd = newLen - 1; end >= start && newEnd >= start && (this.r[end] === newItems[newEnd] || this.h && compare(this.y, this.r[end], newItems[newEnd])); end--, newEnd--) {
1364
1455
  temp[newEnd] = this.k[end];
1365
- tempNodes[newEnd] = this.f[end];
1366
- tempRows && (tempRows[newEnd] = this.g[end]);
1456
+ tempNodes[newEnd] = this.g[end];
1457
+ tempRows && (tempRows[newEnd] = this.h[end]);
1367
1458
  tempIndexes && (tempIndexes[newEnd] = this.l[end]);
1368
1459
  }
1369
1460
  newIndices = /* @__PURE__ */ new Map();
@@ -1376,37 +1467,37 @@ function updateKeyedMap() {
1376
1467
  newIndices.set(key, j);
1377
1468
  }
1378
1469
  for (i = start; i <= end; i++) {
1379
- item = this.q[i];
1470
+ item = this.r[i];
1380
1471
  key = this.y ? this.y(item) : item;
1381
1472
  j = newIndices.get(key);
1382
1473
  if (j !== void 0 && j !== -1) {
1383
1474
  temp[j] = this.k[i];
1384
- tempNodes[j] = this.f[i];
1385
- tempRows && (tempRows[j] = this.g[i]);
1475
+ tempNodes[j] = this.g[i];
1476
+ tempRows && (tempRows[j] = this.h[i]);
1386
1477
  tempIndexes && (tempIndexes[j] = this.l[i]);
1387
1478
  j = newIndicesNext[j];
1388
1479
  newIndices.set(key, j);
1389
1480
  } else
1390
- this.f[i].dispose();
1481
+ this.g[i].dispose();
1391
1482
  }
1392
1483
  for (j = start; j < newLen; j++) {
1393
1484
  if (j in temp) {
1394
1485
  this.k[j] = temp[j];
1395
- this.f[j] = tempNodes[j];
1486
+ this.g[j] = tempNodes[j];
1396
1487
  if (tempRows) {
1397
- this.g[j] = tempRows[j];
1398
- this.g[j].write(newItems[j]);
1488
+ this.h[j] = tempRows[j];
1489
+ this.h[j].write(newItems[j]);
1399
1490
  }
1400
1491
  if (tempIndexes) {
1401
1492
  this.l[j] = tempIndexes[j];
1402
1493
  this.l[j].write(j);
1403
1494
  }
1404
1495
  } else {
1405
- this.k[j] = compute(this.f[j] = new Owner(), mapper, null);
1496
+ this.k[j] = compute(this.g[j] = new Owner(), mapper, null);
1406
1497
  }
1407
1498
  }
1408
1499
  this.k = this.k.slice(0, this.w = newLen);
1409
- this.q = newItems.slice(0);
1500
+ this.r = newItems.slice(0);
1410
1501
  }
1411
1502
  });
1412
1503
  return this.k;
@@ -1415,4 +1506,4 @@ function compare(key, a, b) {
1415
1506
  return key ? key(a) === key(b) : true;
1416
1507
  }
1417
1508
 
1418
- export { $PROXY, $RAW, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, catchError, createAsync, createBoundary, createContext, createEffect, createMemo, createProjection, createRenderEffect, createRoot, createSignal, createStore, createSuspense, flushSync, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual, isPending, isWrappable, latest, mapArray, merge, omit, onCleanup, reconcile, runWithOwner, setContext, untrack, unwrap };
1509
+ export { $PROXY, $RAW, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, catchError, createAsync, createBoundary, createContext, createEffect, createMemo, createProjection, createRenderEffect, createRoot, createSignal, createStore, createSuspense, flatten, flushSync, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual, isPending, isWrappable, latest, mapArray, merge, omit, onCleanup, reconcile, runWithOwner, setContext, untrack, unwrap };