@solidjs/signals 0.4.1 → 0.4.3

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/dev.js CHANGED
@@ -24,14 +24,22 @@ var EFFECT_RENDER = 1;
24
24
  var EFFECT_USER = 2;
25
25
  var SUPPORTS_PROXY = typeof Proxy === "function";
26
26
 
27
+ // src/core/flags.ts
28
+ var ERROR_OFFSET = 0;
29
+ var ERROR_BIT = 1 << ERROR_OFFSET;
30
+ var LOADING_OFFSET = 1;
31
+ var LOADING_BIT = 1 << LOADING_OFFSET;
32
+ var UNINITIALIZED_OFFSET = 2;
33
+ var UNINITIALIZED_BIT = 1 << UNINITIALIZED_OFFSET;
34
+ var DEFAULT_FLAGS = ERROR_BIT;
35
+
27
36
  // src/core/scheduler.ts
28
37
  var clock = 0;
29
- function getClock() {
30
- return clock;
31
- }
32
38
  function incrementClock() {
33
39
  clock++;
34
40
  }
41
+ var ActiveTransition = null;
42
+ var unobserved = [];
35
43
  var scheduled = false;
36
44
  function schedule() {
37
45
  if (scheduled)
@@ -40,6 +48,14 @@ function schedule() {
40
48
  if (!globalQueue._running)
41
49
  queueMicrotask(flush);
42
50
  }
51
+ function notifyUnobserved() {
52
+ for (let i = 0; i < unobserved.length; i++) {
53
+ const source = unobserved[i];
54
+ if (!source._observers || !source._observers.length)
55
+ unobserved[i]._unobserved?.();
56
+ }
57
+ unobserved = [];
58
+ }
43
59
  var pureQueue = [];
44
60
  var Queue = class {
45
61
  _parent = null;
@@ -48,6 +64,8 @@ var Queue = class {
48
64
  _children = [];
49
65
  created = clock;
50
66
  enqueue(type, fn) {
67
+ if (ActiveTransition)
68
+ return ActiveTransition.enqueue(type, fn);
51
69
  pureQueue.push(fn);
52
70
  if (type)
53
71
  this._queues[type - 1].push(fn);
@@ -79,6 +97,7 @@ var Queue = class {
79
97
  this.run(EFFECT_USER);
80
98
  } finally {
81
99
  this._running = false;
100
+ unobserved.length && notifyUnobserved();
82
101
  }
83
102
  }
84
103
  addChild(child) {
@@ -109,6 +128,200 @@ function runQueue(queue, type) {
109
128
  for (let i = 0; i < queue.length; i++)
110
129
  queue[i](type);
111
130
  }
131
+ var Transition = class _Transition {
132
+ _sources = /* @__PURE__ */ new Map();
133
+ _pendingNodes = /* @__PURE__ */ new Set();
134
+ _promises = /* @__PURE__ */ new Set();
135
+ _optimistic = /* @__PURE__ */ new Set();
136
+ _done = false;
137
+ _queues = [[], []];
138
+ _pureQueue = [];
139
+ _children = [];
140
+ _parent = null;
141
+ _running = false;
142
+ _scheduled = false;
143
+ created = clock;
144
+ enqueue(type, fn) {
145
+ this._pureQueue.push(fn);
146
+ if (type)
147
+ this._queues[type - 1].push(fn);
148
+ this.schedule();
149
+ }
150
+ run(type) {
151
+ if (type === EFFECT_PURE) {
152
+ this._pureQueue.length && runQueue(this._pureQueue, type);
153
+ this._pureQueue = [];
154
+ return;
155
+ } else if (this._queues[type - 1].length) {
156
+ const effects = this._queues[type - 1];
157
+ this._queues[type - 1] = [];
158
+ runQueue(effects, type);
159
+ }
160
+ for (let i = 0; i < this._children.length; i++) {
161
+ this._children[i].run(type);
162
+ }
163
+ }
164
+ flush() {
165
+ if (this._running)
166
+ return;
167
+ this._running = true;
168
+ let currentTransition = ActiveTransition;
169
+ ActiveTransition = this;
170
+ try {
171
+ this.run(EFFECT_PURE);
172
+ incrementClock();
173
+ this._scheduled = false;
174
+ ActiveTransition = currentTransition;
175
+ finishTransition(this);
176
+ } finally {
177
+ this._running = false;
178
+ ActiveTransition = currentTransition;
179
+ }
180
+ }
181
+ addChild(child) {
182
+ this._children.push(child);
183
+ child._parent = this;
184
+ }
185
+ removeChild(child) {
186
+ const index = this._children.indexOf(child);
187
+ if (index >= 0)
188
+ this._children.splice(index, 1);
189
+ }
190
+ notify(node, type, flags) {
191
+ if (!(type & LOADING_BIT))
192
+ return false;
193
+ if (flags & LOADING_BIT) {
194
+ this._pendingNodes.add(node);
195
+ } else {
196
+ this._pendingNodes.delete(node);
197
+ }
198
+ return true;
199
+ }
200
+ schedule() {
201
+ if (this._scheduled)
202
+ return;
203
+ this._scheduled = true;
204
+ if (!this._running)
205
+ queueMicrotask(() => this.flush());
206
+ }
207
+ runTransition(fn, force = false) {
208
+ if (this._done) {
209
+ if (this._done instanceof _Transition)
210
+ return this._done.runTransition(fn, force);
211
+ if (!force)
212
+ throw new Error("Transition already completed");
213
+ fn();
214
+ return;
215
+ }
216
+ ActiveTransition = this;
217
+ try {
218
+ const result = fn();
219
+ const transition2 = ActiveTransition;
220
+ if (result instanceof Promise) {
221
+ transition2._promises.add(result);
222
+ result.finally(() => {
223
+ transition2._promises.delete(result);
224
+ finishTransition(transition2);
225
+ });
226
+ }
227
+ } finally {
228
+ const transition2 = ActiveTransition;
229
+ ActiveTransition = null;
230
+ finishTransition(transition2);
231
+ }
232
+ }
233
+ addOptimistic(fn) {
234
+ if (fn._transition && fn._transition !== this) {
235
+ mergeTransitions(fn._transition, this);
236
+ ActiveTransition = fn._transition;
237
+ return;
238
+ }
239
+ fn._transition = this;
240
+ this._optimistic.add(fn);
241
+ }
242
+ };
243
+ var Transitions = /* @__PURE__ */ new Set();
244
+ function transition(fn) {
245
+ let t = new Transition();
246
+ Transitions.add(t);
247
+ queueMicrotask(() => t.runTransition(() => fn((fn2) => t.runTransition(fn2))));
248
+ }
249
+ function cloneGraph(node) {
250
+ if (node._transition) {
251
+ if (node._transition !== ActiveTransition) {
252
+ mergeTransitions(node._transition, ActiveTransition);
253
+ ActiveTransition = node._transition;
254
+ }
255
+ return node._transition._sources.get(node);
256
+ }
257
+ const clone = Object.create(Object.getPrototypeOf(node));
258
+ Object.assign(clone, node, { _disposal: null, _nextSibling: null, _cloned: node });
259
+ ActiveTransition._sources.set(node, clone);
260
+ node._transition = ActiveTransition;
261
+ if (node._observers) {
262
+ for (let i = 0, length = node._observers.length; i < length; i++) {
263
+ const o = node._observers[i];
264
+ node._observers.push(cloneGraph(o));
265
+ }
266
+ }
267
+ return clone;
268
+ }
269
+ function removeSourceObservers(node, index) {
270
+ let source;
271
+ let swap;
272
+ for (let i = index; i < node._sources.length; i++) {
273
+ source = ActiveTransition && ActiveTransition._sources.get(node._sources[i]) || node._sources[i];
274
+ if (source._observers) {
275
+ if ((swap = source._observers.indexOf(node)) !== -1) {
276
+ source._observers[swap] = source._observers[source._observers.length - 1];
277
+ source._observers.pop();
278
+ }
279
+ if (!source._observers.length)
280
+ unobserved.push(source);
281
+ }
282
+ }
283
+ }
284
+ function mergeTransitions(t1, t2) {
285
+ t2._sources.forEach((value, key) => {
286
+ key._transition = t1;
287
+ t1._sources.set(key, value);
288
+ });
289
+ t2._optimistic.forEach((c) => {
290
+ c._transition = t1;
291
+ t1._optimistic.add(c);
292
+ });
293
+ t2._promises.forEach((p) => t1._promises.add(p));
294
+ t2._pendingNodes.forEach((n) => t1._pendingNodes.add(n));
295
+ t2._queues[0].forEach((f) => t1._queues[0].push(f));
296
+ t2._queues[1].forEach((f) => t1._queues[1].push(f));
297
+ t2._pureQueue.forEach((f) => t1._pureQueue.push(f));
298
+ t2._children.forEach((c) => t1.addChild(c));
299
+ t2._done = t1;
300
+ Transitions.delete(t2);
301
+ }
302
+ function finishTransition(transition2) {
303
+ if (transition2._done || transition2._scheduled || transition2._promises.size || transition2._pendingNodes.size)
304
+ return;
305
+ for (const [source, clone] of transition2._sources) {
306
+ if (source === clone || source._transition !== transition2)
307
+ continue;
308
+ if (clone._sources)
309
+ removeSourceObservers(clone, 0);
310
+ source.dispose(false);
311
+ source.emptyDisposal();
312
+ Object.assign(source, clone);
313
+ delete source._cloned;
314
+ delete source._transition;
315
+ }
316
+ transition2._done = true;
317
+ Transitions.delete(transition2);
318
+ transition2.run(EFFECT_RENDER);
319
+ transition2.run(EFFECT_USER);
320
+ for (const reset of transition2._optimistic) {
321
+ delete reset._transition;
322
+ reset();
323
+ }
324
+ }
112
325
 
113
326
  // src/core/owner.ts
114
327
  var currentOwner = null;
@@ -248,22 +461,12 @@ function isUndefined(value) {
248
461
  return typeof value === "undefined";
249
462
  }
250
463
 
251
- // src/core/flags.ts
252
- var ERROR_OFFSET = 0;
253
- var ERROR_BIT = 1 << ERROR_OFFSET;
254
- var LOADING_OFFSET = 1;
255
- var LOADING_BIT = 1 << LOADING_OFFSET;
256
- var UNINITIALIZED_OFFSET = 2;
257
- var UNINITIALIZED_BIT = 1 << UNINITIALIZED_OFFSET;
258
- var DEFAULT_FLAGS = ERROR_BIT;
259
-
260
464
  // src/core/core.ts
261
465
  var currentObserver = null;
262
466
  var currentMask = DEFAULT_FLAGS;
263
467
  var newSources = null;
264
468
  var newSourcesIndex = 0;
265
469
  var newFlags = 0;
266
- var unobserved = [];
267
470
  var notStale = false;
268
471
  var updateCheck = null;
269
472
  var staleCheck = null;
@@ -290,6 +493,8 @@ var Computation = class extends Owner {
290
493
  _handlerMask = DEFAULT_FLAGS;
291
494
  _time = -1;
292
495
  _forceNotify = false;
496
+ _transition;
497
+ _cloned;
293
498
  constructor(initialValue, compute2, options) {
294
499
  super(options?.id, compute2 === null);
295
500
  this._compute = compute2;
@@ -303,14 +508,12 @@ var Computation = class extends Owner {
303
508
  this._pureWrite = true;
304
509
  if (options?.unobserved)
305
510
  this._unobserved = options?.unobserved;
511
+ if (ActiveTransition) {
512
+ this._transition = ActiveTransition;
513
+ ActiveTransition._sources.set(this, this);
514
+ }
306
515
  }
307
516
  _read() {
308
- if (this._compute) {
309
- if (this._stateFlags & ERROR_BIT && this._time <= getClock())
310
- update(this);
311
- else
312
- this._updateIfNecessary();
313
- }
314
517
  track(this);
315
518
  newFlags |= this._stateFlags & ~currentMask;
316
519
  if (this._stateFlags & ERROR_BIT) {
@@ -324,6 +527,17 @@ var Computation = class extends Owner {
324
527
  * Automatically re-executes the surrounding computation when the value changes
325
528
  */
326
529
  read() {
530
+ if (ActiveTransition && ActiveTransition._sources.has(this)) {
531
+ const clone = ActiveTransition._sources.get(this);
532
+ if (clone !== this)
533
+ return clone.read();
534
+ }
535
+ if (this._compute) {
536
+ if (this._stateFlags & ERROR_BIT && this._time <= clock)
537
+ update(this);
538
+ else
539
+ this._updateIfNecessary();
540
+ }
327
541
  return this._read();
328
542
  }
329
543
  /**
@@ -334,13 +548,19 @@ var Computation = class extends Owner {
334
548
  * before continuing
335
549
  */
336
550
  wait() {
337
- if (this._compute && this._stateFlags & ERROR_BIT && this._time <= getClock()) {
338
- update(this);
339
- } else {
340
- this._updateIfNecessary();
551
+ if (ActiveTransition && ActiveTransition._sources.has(this)) {
552
+ const clone = ActiveTransition._sources.get(this);
553
+ if (clone !== this)
554
+ return clone.wait();
555
+ }
556
+ if (this._compute) {
557
+ if (this._stateFlags & ERROR_BIT && this._time <= clock)
558
+ update(this);
559
+ else
560
+ this._updateIfNecessary();
341
561
  }
342
- track(this);
343
562
  if ((notStale || this._stateFlags & UNINITIALIZED_BIT) && this._stateFlags & LOADING_BIT) {
563
+ track(this);
344
564
  throw new NotReadyError();
345
565
  }
346
566
  if (staleCheck && this._stateFlags & LOADING_BIT) {
@@ -350,6 +570,10 @@ var Computation = class extends Owner {
350
570
  }
351
571
  /** Update the computation with a new value. */
352
572
  write(value, flags = 0, raw = false) {
573
+ if (ActiveTransition && !this._cloned) {
574
+ const clone = cloneGraph(this);
575
+ return clone.write(value, flags, raw);
576
+ }
353
577
  if (!this._compute && !this._pureWrite && getOwner() && !getOwner().firewall)
354
578
  console.warn("A Signal was written to in an owned scope.");
355
579
  const newValue = !raw && typeof value === "function" ? value(this._value) : value;
@@ -361,7 +585,7 @@ var Computation = class extends Owner {
361
585
  }
362
586
  const changedFlagsMask = this._stateFlags ^ flags, changedFlags = changedFlagsMask & flags;
363
587
  this._stateFlags = flags;
364
- this._time = getClock() + 1;
588
+ this._time = clock + 1;
365
589
  if (this._observers) {
366
590
  for (let i = 0; i < this._observers.length; i++) {
367
591
  if (valueChanged) {
@@ -377,6 +601,11 @@ var Computation = class extends Owner {
377
601
  * Set the current node's state, and recursively mark all of this node's observers as STATE_CHECK
378
602
  */
379
603
  _notify(state, skipQueue) {
604
+ if (ActiveTransition && ActiveTransition._sources.has(this)) {
605
+ const clone = ActiveTransition._sources.get(this);
606
+ if (clone !== this)
607
+ return clone._notify(state, skipQueue);
608
+ }
380
609
  if (this._state >= state && !this._forceNotify)
381
610
  return;
382
611
  this._forceNotify = !!skipQueue;
@@ -439,8 +668,9 @@ var Computation = class extends Owner {
439
668
  let observerFlags = 0;
440
669
  if (this._state === STATE_CHECK) {
441
670
  for (let i = 0; i < this._sources.length; i++) {
442
- this._sources[i]._updateIfNecessary();
443
- observerFlags |= this._sources[i]._stateFlags;
671
+ const source = ActiveTransition && ActiveTransition._sources.get(this._sources[i]) || this._sources[i];
672
+ source._updateIfNecessary();
673
+ observerFlags |= source._stateFlags;
444
674
  if (this._state === STATE_DIRTY) {
445
675
  break;
446
676
  }
@@ -465,6 +695,8 @@ var Computation = class extends Owner {
465
695
  }
466
696
  };
467
697
  function track(computation) {
698
+ if (ActiveTransition && computation._cloned)
699
+ computation = computation._cloned;
468
700
  if (currentObserver) {
469
701
  if (!newSources && currentObserver._sources && currentObserver._sources[newSourcesIndex] === computation) {
470
702
  newSourcesIndex++;
@@ -518,36 +750,13 @@ function update(node) {
518
750
  removeSourceObservers(node, newSourcesIndex);
519
751
  node._sources.length = newSourcesIndex;
520
752
  }
521
- unobserved.length && notifyUnobserved();
522
753
  newSources = prevSources;
523
754
  newSourcesIndex = prevSourcesIndex;
524
755
  newFlags = prevFlags;
525
- node._time = getClock() + 1;
756
+ node._time = clock + 1;
526
757
  node._state = STATE_CLEAN;
527
758
  }
528
759
  }
529
- function removeSourceObservers(node, index) {
530
- let source;
531
- let swap;
532
- for (let i = index; i < node._sources.length; i++) {
533
- source = node._sources[i];
534
- if (source._observers) {
535
- swap = source._observers.indexOf(node);
536
- source._observers[swap] = source._observers[source._observers.length - 1];
537
- source._observers.pop();
538
- if (!source._observers.length)
539
- unobserved.push(source);
540
- }
541
- }
542
- }
543
- function notifyUnobserved() {
544
- for (let i = 0; i < unobserved.length; i++) {
545
- const source = unobserved[i];
546
- if (!source._observers || !source._observers.length)
547
- unobserved[i]._unobserved?.();
548
- }
549
- unobserved = [];
550
- }
551
760
  function isEqual(a, b) {
552
761
  return a === b;
553
762
  }
@@ -675,19 +884,18 @@ var Effect = class extends Computation {
675
884
  this._prevValue = initialValue;
676
885
  this._type = options?.render ? EFFECT_RENDER : EFFECT_USER;
677
886
  if (this._type === EFFECT_RENDER) {
678
- this._compute = (p) => getClock() > this._queue.created && !(this._stateFlags & ERROR_BIT) ? latest(() => compute2(p)) : compute2(p);
887
+ this._compute = (p) => !ActiveTransition && clock > this._queue.created && !(this._stateFlags & ERROR_BIT) ? latest(() => compute2(p)) : compute2(p);
679
888
  }
680
889
  this._updateIfNecessary();
681
- !options?.defer && (this._type === EFFECT_USER ? this._queue.enqueue(this._type, this._run.bind(this)) : this._run(this._type));
890
+ !options?.defer && (this._type === EFFECT_USER ? (ActiveTransition || this._queue).enqueue(this._type, this._run.bind(this)) : this._run(this._type));
682
891
  if (!this._parent)
683
892
  console.warn("Effects created outside a reactive context will never be disposed");
684
893
  }
685
894
  write(value, flags = 0) {
686
895
  if (this._state == STATE_DIRTY) {
687
- this._stateFlags;
688
896
  this._stateFlags = flags;
689
897
  if (this._type === EFFECT_RENDER) {
690
- this._queue.notify(this, LOADING_BIT | ERROR_BIT, flags);
898
+ (ActiveTransition || this._queue).notify(this, LOADING_BIT | ERROR_BIT, flags);
691
899
  }
692
900
  }
693
901
  if (value === UNCHANGED)
@@ -697,15 +905,29 @@ var Effect = class extends Computation {
697
905
  return value;
698
906
  }
699
907
  _notify(state, skipQueue) {
908
+ if (ActiveTransition && ActiveTransition._sources.has(this)) {
909
+ return ActiveTransition._sources.get(this)._notify(state, skipQueue);
910
+ }
700
911
  if (this._state >= state || skipQueue)
701
912
  return;
702
913
  if (this._state === STATE_CLEAN)
703
- this._queue.enqueue(this._type, this._run.bind(this));
914
+ (ActiveTransition || this._queue).enqueue(this._type, this._run.bind(this));
704
915
  this._state = state;
705
916
  }
917
+ _notifyFlags(mask, newFlags2) {
918
+ if (ActiveTransition && ActiveTransition._sources.has(this)) {
919
+ if (this._state >= STATE_CHECK)
920
+ return;
921
+ if (mask & 3) {
922
+ this._notify(STATE_DIRTY);
923
+ return;
924
+ }
925
+ }
926
+ super._notifyFlags(mask, newFlags2);
927
+ }
706
928
  _setError(error) {
707
929
  this._error = error;
708
- this._queue.notify(this, LOADING_BIT, 0);
930
+ (ActiveTransition || this._queue).notify(this, LOADING_BIT, 0);
709
931
  this._stateFlags = ERROR_BIT;
710
932
  if (this._type === EFFECT_USER) {
711
933
  try {
@@ -717,7 +939,7 @@ var Effect = class extends Computation {
717
939
  error = e;
718
940
  }
719
941
  }
720
- if (!this._queue.notify(this, ERROR_BIT, ERROR_BIT))
942
+ if (!(ActiveTransition || this._queue).notify(this, ERROR_BIT, ERROR_BIT))
721
943
  throw error;
722
944
  }
723
945
  _disposeNode() {
@@ -737,7 +959,7 @@ var Effect = class extends Computation {
737
959
  try {
738
960
  this._cleanup = this._effect(this._value, this._prevValue);
739
961
  } catch (e) {
740
- if (!this._queue.notify(this, ERROR_BIT, ERROR_BIT))
962
+ if (!(ActiveTransition || this._queue).notify(this, ERROR_BIT, ERROR_BIT))
741
963
  throw e;
742
964
  } finally {
743
965
  this._prevValue = this._value;
@@ -756,10 +978,13 @@ var EagerComputation = class extends Computation {
756
978
  console.warn("Eager Computations created outside a reactive context will never be disposed");
757
979
  }
758
980
  _notify(state, skipQueue) {
981
+ if (ActiveTransition && ActiveTransition._sources.has(this)) {
982
+ return ActiveTransition._sources.get(this)._notify(state, skipQueue);
983
+ }
759
984
  if (this._state >= state && !this._forceNotify)
760
985
  return;
761
986
  if (!skipQueue && (this._state === STATE_CLEAN || this._state === STATE_CHECK && this._forceNotify))
762
- this._queue.enqueue(EFFECT_PURE, this._run.bind(this));
987
+ (ActiveTransition || this._queue).enqueue(EFFECT_PURE, this._run.bind(this));
763
988
  super._notify(state, skipQueue);
764
989
  }
765
990
  _run() {
@@ -774,10 +999,13 @@ var FirewallComputation = class extends Computation {
774
999
  console.warn("Eager Computations created outside a reactive context will never be disposed");
775
1000
  }
776
1001
  _notify(state, skipQueue) {
1002
+ if (ActiveTransition && ActiveTransition._sources.has(this)) {
1003
+ return ActiveTransition._sources.get(this)._notify(state, skipQueue);
1004
+ }
777
1005
  if (this._state >= state && !this._forceNotify)
778
1006
  return;
779
1007
  if (!skipQueue && (this._state === STATE_CLEAN || this._state === STATE_CHECK && this._forceNotify))
780
- this._queue.enqueue(EFFECT_PURE, this._run.bind(this));
1008
+ (ActiveTransition || this._queue).enqueue(EFFECT_PURE, this._run.bind(this));
781
1009
  super._notify(state, true);
782
1010
  this._forceNotify = !!skipQueue;
783
1011
  }
@@ -798,168 +1026,6 @@ function runTop(node) {
798
1026
  }
799
1027
  }
800
1028
 
801
- // src/signals.ts
802
- function createSignal(first, second, third) {
803
- if (typeof first === "function") {
804
- const memo = createMemo((p) => {
805
- const node2 = new Computation(
806
- first(p ? untrack(p[0]) : second),
807
- null,
808
- third
809
- );
810
- return [node2.read.bind(node2), node2.write.bind(node2)];
811
- });
812
- return [() => memo()[0](), (value) => memo()[1](value)];
813
- }
814
- const o = getOwner();
815
- const needsId = o?.id != null;
816
- const node = new Computation(
817
- first,
818
- null,
819
- needsId ? { id: o.getNextChildId(), ...second } : second
820
- );
821
- return [node.read.bind(node), node.write.bind(node)];
822
- }
823
- function createMemo(compute2, value, options) {
824
- let node = new Computation(
825
- value,
826
- compute2,
827
- options
828
- );
829
- let resolvedValue;
830
- return () => {
831
- if (node) {
832
- if (node._state === STATE_DISPOSED) {
833
- node = void 0;
834
- return resolvedValue;
835
- }
836
- resolvedValue = node.wait();
837
- if (!node._sources?.length && node._nextSibling?._parent !== node) {
838
- node.dispose();
839
- node = void 0;
840
- }
841
- }
842
- return resolvedValue;
843
- };
844
- }
845
- function createAsync(compute2, value, options) {
846
- let refreshing = false;
847
- const node = new EagerComputation(
848
- value,
849
- (p) => {
850
- const source = compute2(p, refreshing);
851
- refreshing = false;
852
- const isPromise = source instanceof Promise;
853
- const iterator = source[Symbol.asyncIterator];
854
- if (!isPromise && !iterator) {
855
- return source;
856
- }
857
- let abort = false;
858
- onCleanup(() => abort = true);
859
- if (isPromise) {
860
- source.then(
861
- (value3) => {
862
- if (abort)
863
- return;
864
- node.write(value3, 0, true);
865
- },
866
- (error) => {
867
- if (abort)
868
- return;
869
- node._setError(error);
870
- }
871
- );
872
- } else {
873
- (async () => {
874
- try {
875
- for await (let value3 of source) {
876
- if (abort)
877
- return;
878
- node.write(value3, 0, true);
879
- }
880
- } catch (error) {
881
- if (abort)
882
- return;
883
- node.write(error, ERROR_BIT);
884
- }
885
- })();
886
- }
887
- throw new NotReadyError();
888
- },
889
- options
890
- );
891
- const read = node.wait.bind(node);
892
- read.refresh = () => {
893
- node._state = STATE_DIRTY;
894
- refreshing = true;
895
- node._updateIfNecessary();
896
- };
897
- return read;
898
- }
899
- function createEffect(compute2, effect, value, options) {
900
- void new Effect(
901
- value,
902
- compute2,
903
- effect.effect ? effect.effect : effect,
904
- effect.error,
905
- { ...options, name: options?.name ?? "effect" }
906
- );
907
- }
908
- function createRenderEffect(compute2, effect, value, options) {
909
- void new Effect(value, compute2, effect, void 0, {
910
- render: true,
911
- ...{ ...options, name: options?.name ?? "effect" }
912
- });
913
- }
914
- function createRoot(init, options) {
915
- const owner = new Owner(options?.id);
916
- return compute(owner, !init.length ? init : () => init(() => owner.dispose()), null);
917
- }
918
- function runWithOwner(owner, run) {
919
- return compute(owner, run, null);
920
- }
921
- function resolve(fn) {
922
- return new Promise((res, rej) => {
923
- createRoot((dispose) => {
924
- new EagerComputation(void 0, () => {
925
- try {
926
- res(fn());
927
- } catch (err) {
928
- if (err instanceof NotReadyError)
929
- throw err;
930
- rej(err);
931
- }
932
- dispose();
933
- });
934
- });
935
- });
936
- }
937
- function tryCatch(fn) {
938
- try {
939
- const v = fn();
940
- if (v instanceof Promise) {
941
- return v.then(
942
- (v2) => [void 0, v2],
943
- (e) => {
944
- if (e instanceof NotReadyError)
945
- throw e;
946
- return [e];
947
- }
948
- );
949
- }
950
- return [void 0, v];
951
- } catch (e) {
952
- if (e instanceof NotReadyError)
953
- throw e;
954
- return [e];
955
- }
956
- }
957
- function transition(fn) {
958
- }
959
- function createOptimistic(initial, compute2, options) {
960
- return [];
961
- }
962
-
963
1029
  // src/store/projection.ts
964
1030
  function createProjection(fn, initialValue = {}) {
965
1031
  let wrappedStore;
@@ -1589,6 +1655,213 @@ function omit(props, ...keys) {
1589
1655
  return result;
1590
1656
  }
1591
1657
 
1658
+ // src/signals.ts
1659
+ function createSignal(first, second, third) {
1660
+ if (typeof first === "function") {
1661
+ const memo = createMemo((p) => {
1662
+ const node2 = new Computation(
1663
+ first(p ? untrack(p[0]) : second),
1664
+ null,
1665
+ third
1666
+ );
1667
+ return [node2.read.bind(node2), node2.write.bind(node2)];
1668
+ });
1669
+ return [() => memo()[0](), (value) => memo()[1](value)];
1670
+ }
1671
+ const o = getOwner();
1672
+ const needsId = o?.id != null;
1673
+ const node = new Computation(
1674
+ first,
1675
+ null,
1676
+ needsId ? { id: o.getNextChildId(), ...second } : second
1677
+ );
1678
+ return [node.read.bind(node), node.write.bind(node)];
1679
+ }
1680
+ function createMemo(compute2, value, options) {
1681
+ let node = new Computation(
1682
+ value,
1683
+ compute2,
1684
+ options
1685
+ );
1686
+ let resolvedValue;
1687
+ return () => {
1688
+ if (node) {
1689
+ if (node._state === STATE_DISPOSED) {
1690
+ node = void 0;
1691
+ return resolvedValue;
1692
+ }
1693
+ resolvedValue = node.wait();
1694
+ if (!node._sources?.length && node._nextSibling?._parent !== node) {
1695
+ node.dispose();
1696
+ node = void 0;
1697
+ }
1698
+ }
1699
+ return resolvedValue;
1700
+ };
1701
+ }
1702
+ function createAsync(compute2, value, options) {
1703
+ let refreshing = false;
1704
+ const node = new EagerComputation(
1705
+ value,
1706
+ (p) => {
1707
+ const source = compute2(p, refreshing);
1708
+ refreshing = false;
1709
+ const isPromise = source instanceof Promise;
1710
+ const iterator = source[Symbol.asyncIterator];
1711
+ if (!isPromise && !iterator) {
1712
+ return source;
1713
+ }
1714
+ let abort = false;
1715
+ onCleanup(() => abort = true);
1716
+ const transition2 = ActiveTransition;
1717
+ if (isPromise) {
1718
+ source.then(
1719
+ (value3) => {
1720
+ if (abort)
1721
+ return;
1722
+ if (transition2)
1723
+ return transition2.runTransition(() => node.write(value3, 0, true), true);
1724
+ node.write(value3, 0, true);
1725
+ },
1726
+ (error) => {
1727
+ if (abort)
1728
+ return;
1729
+ if (transition2)
1730
+ return transition2.runTransition(() => node._setError(error), true);
1731
+ node._setError(error);
1732
+ }
1733
+ );
1734
+ } else {
1735
+ (async () => {
1736
+ try {
1737
+ for await (let value3 of source) {
1738
+ if (abort)
1739
+ return;
1740
+ node.write(value3, 0, true);
1741
+ }
1742
+ } catch (error) {
1743
+ if (abort)
1744
+ return;
1745
+ node.write(error, ERROR_BIT);
1746
+ }
1747
+ })();
1748
+ }
1749
+ throw new NotReadyError();
1750
+ },
1751
+ options
1752
+ );
1753
+ const read = node.wait.bind(node);
1754
+ read.refresh = () => {
1755
+ node._state = STATE_DIRTY;
1756
+ refreshing = true;
1757
+ node._updateIfNecessary();
1758
+ };
1759
+ return read;
1760
+ }
1761
+ function createEffect(compute2, effect, value, options) {
1762
+ void new Effect(
1763
+ value,
1764
+ compute2,
1765
+ effect.effect ? effect.effect : effect,
1766
+ effect.error,
1767
+ { ...options, name: options?.name ?? "effect" }
1768
+ );
1769
+ }
1770
+ function createRenderEffect(compute2, effect, value, options) {
1771
+ void new Effect(value, compute2, effect, void 0, {
1772
+ render: true,
1773
+ ...{ ...options, name: options?.name ?? "effect" }
1774
+ });
1775
+ }
1776
+ function createRoot(init, options) {
1777
+ const owner = new Owner(options?.id);
1778
+ return compute(owner, !init.length ? init : () => init(() => owner.dispose()), null);
1779
+ }
1780
+ function runWithOwner(owner, run) {
1781
+ return compute(owner, run, null);
1782
+ }
1783
+ function resolve(fn) {
1784
+ return new Promise((res, rej) => {
1785
+ createRoot((dispose) => {
1786
+ new EagerComputation(void 0, () => {
1787
+ try {
1788
+ res(fn());
1789
+ } catch (err) {
1790
+ if (err instanceof NotReadyError)
1791
+ throw err;
1792
+ rej(err);
1793
+ }
1794
+ dispose();
1795
+ });
1796
+ });
1797
+ });
1798
+ }
1799
+ function tryCatch(fn) {
1800
+ try {
1801
+ const v = fn();
1802
+ if (v instanceof Promise) {
1803
+ return v.then(
1804
+ (v2) => [void 0, v2],
1805
+ (e) => {
1806
+ if (e instanceof NotReadyError)
1807
+ throw e;
1808
+ return [e];
1809
+ }
1810
+ );
1811
+ }
1812
+ return [void 0, v];
1813
+ } catch (e) {
1814
+ if (e instanceof NotReadyError)
1815
+ throw e;
1816
+ return [e];
1817
+ }
1818
+ }
1819
+ function createOptimistic(initial, compute2, key) {
1820
+ if (!compute2) {
1821
+ let write2 = function(v) {
1822
+ if (!ActiveTransition)
1823
+ throw new Error("createOptimistic can only be updated inside a transition");
1824
+ ActiveTransition.addOptimistic(reset2);
1825
+ queueMicrotask(() => node.write(v));
1826
+ };
1827
+ var write = write2;
1828
+ const node = new Computation(initial, null);
1829
+ const reset2 = () => node.write(initial);
1830
+ return [node.read.bind(node), write2];
1831
+ }
1832
+ let store, setStore;
1833
+ if (typeof initial === "function") {
1834
+ [store, setStore] = createStore(
1835
+ (s) => {
1836
+ const value = initial();
1837
+ if (!ActiveTransition)
1838
+ s.value = value;
1839
+ },
1840
+ { value: void 0 }
1841
+ );
1842
+ } else
1843
+ [store, setStore] = createStore({ value: initial });
1844
+ const reset = () => setStore(
1845
+ (s) => reconcile(
1846
+ typeof initial === "function" ? initial() : initial,
1847
+ key
1848
+ )(s.value)
1849
+ );
1850
+ let lastChange = void 0;
1851
+ function write(v) {
1852
+ if (!ActiveTransition)
1853
+ throw new Error("createOptimistic can only be updated inside a transition");
1854
+ ActiveTransition.addOptimistic(reset);
1855
+ queueMicrotask(
1856
+ () => setStore((s) => {
1857
+ lastChange = typeof v === "function" ? v(lastChange) : v;
1858
+ compute2(s.value, lastChange);
1859
+ })
1860
+ );
1861
+ }
1862
+ return [() => store.value, write];
1863
+ }
1864
+
1592
1865
  // src/map.ts
1593
1866
  function mapArray(list, map, options) {
1594
1867
  const keyFn = typeof options?.keyed === "function" ? options.keyed : void 0;