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