@solidjs/signals 0.4.3 → 0.4.4

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
@@ -64,8 +64,8 @@ var Queue = class {
64
64
  _children = [];
65
65
  created = clock;
66
66
  enqueue(type, fn) {
67
- if (ActiveTransition)
68
- return ActiveTransition.enqueue(type, fn);
67
+ if (ActiveTransition && ActiveTransition._clonedQueues.has(this))
68
+ return ActiveTransition._clonedQueues.get(this).enqueue(type, fn);
69
69
  pureQueue.push(fn);
70
70
  if (type)
71
71
  this._queues[type - 1].push(fn);
@@ -110,10 +110,23 @@ var Queue = class {
110
110
  this._children.splice(index, 1);
111
111
  }
112
112
  notify(...args) {
113
+ if (ActiveTransition && ActiveTransition._clonedQueues.has(this))
114
+ return ActiveTransition._clonedQueues.get(this).notify(...args);
113
115
  if (this._parent)
114
116
  return this._parent.notify(...args);
115
117
  return false;
116
118
  }
119
+ merge(queue) {
120
+ this._queues[0].push.apply(this._queues[0], queue._queues[0]);
121
+ this._queues[1].push.apply(this._queues[1], queue._queues[1]);
122
+ for (let i = 0; i < queue._children.length; i++) {
123
+ const og = this._children.find((c) => c._cloned === queue._children[i]._cloned);
124
+ if (og)
125
+ og.merge(queue._children[i]);
126
+ else
127
+ this.addChild(queue._children[i]);
128
+ }
129
+ }
117
130
  };
118
131
  var globalQueue = new Queue();
119
132
  function flush() {
@@ -135,12 +148,19 @@ var Transition = class _Transition {
135
148
  _optimistic = /* @__PURE__ */ new Set();
136
149
  _done = false;
137
150
  _queues = [[], []];
151
+ _clonedQueues = /* @__PURE__ */ new Map();
138
152
  _pureQueue = [];
139
153
  _children = [];
140
154
  _parent = null;
141
155
  _running = false;
142
156
  _scheduled = false;
143
157
  created = clock;
158
+ constructor() {
159
+ this._clonedQueues.set(globalQueue, this);
160
+ for (const child of globalQueue._children) {
161
+ cloneQueue(child, this, this._clonedQueues);
162
+ }
163
+ }
144
164
  enqueue(type, fn) {
145
165
  this._pureQueue.push(fn);
146
166
  if (type)
@@ -197,6 +217,18 @@ var Transition = class _Transition {
197
217
  }
198
218
  return true;
199
219
  }
220
+ merge(queue) {
221
+ this._queues[0].push.apply(this._queues[0], queue._queues[0]);
222
+ this._queues[1].push.apply(this._queues[1], queue._queues[1]);
223
+ this._pureQueue.push.apply(this._pureQueue, queue._pureQueue);
224
+ for (let i = 0; i < queue._children.length; i++) {
225
+ const og = this._children.find((c) => c._cloned === queue._children[i]._cloned);
226
+ if (og)
227
+ og.merge(queue._children[i]);
228
+ else
229
+ this.addChild(queue._children[i]);
230
+ }
231
+ }
200
232
  schedule() {
201
233
  if (this._scheduled)
202
234
  return;
@@ -240,10 +272,8 @@ var Transition = class _Transition {
240
272
  this._optimistic.add(fn);
241
273
  }
242
274
  };
243
- var Transitions = /* @__PURE__ */ new Set();
244
275
  function transition(fn) {
245
276
  let t = new Transition();
246
- Transitions.add(t);
247
277
  queueMicrotask(() => t.runTransition(() => fn((fn2) => t.runTransition(fn2))));
248
278
  }
249
279
  function cloneGraph(node) {
@@ -281,6 +311,49 @@ function removeSourceObservers(node, index) {
281
311
  }
282
312
  }
283
313
  }
314
+ function cloneQueue(queue, parent, clonedQueues) {
315
+ const clone = Object.create(Object.getPrototypeOf(queue));
316
+ Object.assign(clone, queue, {
317
+ _cloned: queue,
318
+ _parent: parent,
319
+ _children: [],
320
+ enqueue(type, fn) {
321
+ ActiveTransition?.enqueue(type, fn);
322
+ },
323
+ notify(node, type, flags) {
324
+ node = node._cloned || node;
325
+ if (!clone._collectionType || type & LOADING_BIT) {
326
+ type &= ~LOADING_BIT;
327
+ ActiveTransition?.notify(node, LOADING_BIT, flags);
328
+ if (!type)
329
+ return true;
330
+ }
331
+ return queue.notify.call(this, node, type, flags);
332
+ }
333
+ });
334
+ parent._children.push(clone);
335
+ clonedQueues.set(queue, clone);
336
+ for (const child of queue._children) {
337
+ cloneQueue(child, clone, clonedQueues);
338
+ }
339
+ }
340
+ function resolveQueues(children) {
341
+ for (const child of children) {
342
+ const og = child._cloned;
343
+ if (og) {
344
+ const clonedChildren = child._children;
345
+ delete child.enqueue;
346
+ delete child.notify;
347
+ delete child._parent;
348
+ delete child._children;
349
+ Object.assign(og, child);
350
+ delete og._cloned;
351
+ resolveQueues(clonedChildren);
352
+ } else if (child._parent._cloned) {
353
+ child._parent._cloned.addChild(child);
354
+ }
355
+ }
356
+ }
284
357
  function mergeTransitions(t1, t2) {
285
358
  t2._sources.forEach((value, key) => {
286
359
  key._transition = t1;
@@ -292,12 +365,8 @@ function mergeTransitions(t1, t2) {
292
365
  });
293
366
  t2._promises.forEach((p) => t1._promises.add(p));
294
367
  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));
368
+ t1.merge(t2);
299
369
  t2._done = t1;
300
- Transitions.delete(t2);
301
370
  }
302
371
  function finishTransition(transition2) {
303
372
  if (transition2._done || transition2._scheduled || transition2._promises.size || transition2._pendingNodes.size)
@@ -313,10 +382,11 @@ function finishTransition(transition2) {
313
382
  delete source._cloned;
314
383
  delete source._transition;
315
384
  }
385
+ globalQueue._queues[0].push.apply(globalQueue._queues[0], transition2._queues[0]);
386
+ globalQueue._queues[1].push.apply(globalQueue._queues[1], transition2._queues[1]);
387
+ resolveQueues(transition2._children);
316
388
  transition2._done = true;
317
- Transitions.delete(transition2);
318
- transition2.run(EFFECT_RENDER);
319
- transition2.run(EFFECT_USER);
389
+ globalQueue.flush();
320
390
  for (const reset of transition2._optimistic) {
321
391
  delete reset._transition;
322
392
  reset();
@@ -572,7 +642,8 @@ var Computation = class extends Owner {
572
642
  write(value, flags = 0, raw = false) {
573
643
  if (ActiveTransition && !this._cloned) {
574
644
  const clone = cloneGraph(this);
575
- return clone.write(value, flags, raw);
645
+ if (clone !== this)
646
+ return clone.write(value, flags, raw);
576
647
  }
577
648
  if (!this._compute && !this._pureWrite && getOwner() && !getOwner().firewall)
578
649
  console.warn("A Signal was written to in an owned scope.");
@@ -645,6 +716,11 @@ var Computation = class extends Owner {
645
716
  }
646
717
  }
647
718
  _setError(error) {
719
+ if (ActiveTransition && !this._cloned) {
720
+ const clone = cloneGraph(this);
721
+ if (clone !== this)
722
+ return clone._setError(error);
723
+ }
648
724
  this._error = error;
649
725
  this.write(UNCHANGED, this._stateFlags & ~LOADING_BIT | ERROR_BIT | UNINITIALIZED_BIT);
650
726
  }
@@ -887,7 +963,7 @@ var Effect = class extends Computation {
887
963
  this._compute = (p) => !ActiveTransition && clock > this._queue.created && !(this._stateFlags & ERROR_BIT) ? latest(() => compute2(p)) : compute2(p);
888
964
  }
889
965
  this._updateIfNecessary();
890
- !options?.defer && (this._type === EFFECT_USER ? (ActiveTransition || this._queue).enqueue(this._type, this._run.bind(this)) : this._run(this._type));
966
+ !options?.defer && (this._type === EFFECT_USER ? this._queue.enqueue(this._type, this._run.bind(this)) : this._run(this._type));
891
967
  if (!this._parent)
892
968
  console.warn("Effects created outside a reactive context will never be disposed");
893
969
  }
@@ -895,7 +971,7 @@ var Effect = class extends Computation {
895
971
  if (this._state == STATE_DIRTY) {
896
972
  this._stateFlags = flags;
897
973
  if (this._type === EFFECT_RENDER) {
898
- (ActiveTransition || this._queue).notify(this, LOADING_BIT | ERROR_BIT, flags);
974
+ this._queue.notify(this, LOADING_BIT | ERROR_BIT, flags);
899
975
  }
900
976
  }
901
977
  if (value === UNCHANGED)
@@ -911,7 +987,7 @@ var Effect = class extends Computation {
911
987
  if (this._state >= state || skipQueue)
912
988
  return;
913
989
  if (this._state === STATE_CLEAN)
914
- (ActiveTransition || this._queue).enqueue(this._type, this._run.bind(this));
990
+ this._queue.enqueue(this._type, this._run.bind(this));
915
991
  this._state = state;
916
992
  }
917
993
  _notifyFlags(mask, newFlags2) {
@@ -927,7 +1003,7 @@ var Effect = class extends Computation {
927
1003
  }
928
1004
  _setError(error) {
929
1005
  this._error = error;
930
- (ActiveTransition || this._queue).notify(this, LOADING_BIT, 0);
1006
+ this._queue.notify(this, LOADING_BIT, 0);
931
1007
  this._stateFlags = ERROR_BIT;
932
1008
  if (this._type === EFFECT_USER) {
933
1009
  try {
@@ -939,7 +1015,7 @@ var Effect = class extends Computation {
939
1015
  error = e;
940
1016
  }
941
1017
  }
942
- if (!(ActiveTransition || this._queue).notify(this, ERROR_BIT, ERROR_BIT))
1018
+ if (!this._queue.notify(this, ERROR_BIT, ERROR_BIT))
943
1019
  throw error;
944
1020
  }
945
1021
  _disposeNode() {
@@ -954,16 +1030,17 @@ var Effect = class extends Computation {
954
1030
  }
955
1031
  _run(type) {
956
1032
  if (type) {
957
- if (this._modified && this._state !== STATE_DISPOSED) {
958
- this._cleanup?.();
1033
+ const effect = this._cloned || this;
1034
+ if (effect._modified && effect._state !== STATE_DISPOSED) {
1035
+ effect._cleanup?.();
959
1036
  try {
960
- this._cleanup = this._effect(this._value, this._prevValue);
1037
+ effect._cleanup = effect._effect(effect._value, effect._prevValue);
961
1038
  } catch (e) {
962
- if (!(ActiveTransition || this._queue).notify(this, ERROR_BIT, ERROR_BIT))
1039
+ if (!effect._queue.notify(effect, ERROR_BIT, ERROR_BIT))
963
1040
  throw e;
964
1041
  } finally {
965
- this._prevValue = this._value;
966
- this._modified = false;
1042
+ effect._prevValue = effect._value;
1043
+ effect._modified = false;
967
1044
  }
968
1045
  }
969
1046
  } else
@@ -984,7 +1061,7 @@ var EagerComputation = class extends Computation {
984
1061
  if (this._state >= state && !this._forceNotify)
985
1062
  return;
986
1063
  if (!skipQueue && (this._state === STATE_CLEAN || this._state === STATE_CHECK && this._forceNotify))
987
- (ActiveTransition || this._queue).enqueue(EFFECT_PURE, this._run.bind(this));
1064
+ this._queue.enqueue(EFFECT_PURE, this._run.bind(this));
988
1065
  super._notify(state, skipQueue);
989
1066
  }
990
1067
  _run() {
@@ -1005,7 +1082,7 @@ var FirewallComputation = class extends Computation {
1005
1082
  if (this._state >= state && !this._forceNotify)
1006
1083
  return;
1007
1084
  if (!skipQueue && (this._state === STATE_CLEAN || this._state === STATE_CHECK && this._forceNotify))
1008
- (ActiveTransition || this._queue).enqueue(EFFECT_PURE, this._run.bind(this));
1085
+ this._queue.enqueue(EFFECT_PURE, this._run.bind(this));
1009
1086
  super._notify(state, true);
1010
1087
  this._forceNotify = !!skipQueue;
1011
1088
  }
@@ -2104,6 +2181,8 @@ var ConditionalQueue = class extends Queue {
2104
2181
  return super.run(type);
2105
2182
  }
2106
2183
  notify(node, type, flags) {
2184
+ if (ActiveTransition && ActiveTransition._clonedQueues.has(this))
2185
+ return ActiveTransition._clonedQueues.get(this).notify(node, type, flags);
2107
2186
  if (this._disabled.read()) {
2108
2187
  if (type & LOADING_BIT) {
2109
2188
  if (flags & LOADING_BIT) {
@@ -2122,6 +2201,11 @@ var ConditionalQueue = class extends Queue {
2122
2201
  }
2123
2202
  return type ? super.notify(node, type, flags) : true;
2124
2203
  }
2204
+ merge(queue) {
2205
+ queue._pendingNodes.forEach((n) => this.notify(n, LOADING_BIT, LOADING_BIT));
2206
+ queue._errorNodes.forEach((n) => this.notify(n, ERROR_BIT, ERROR_BIT));
2207
+ super.merge(queue);
2208
+ }
2125
2209
  };
2126
2210
  var CollectionQueue = class extends Queue {
2127
2211
  _collectionType;
@@ -2137,6 +2221,8 @@ var CollectionQueue = class extends Queue {
2137
2221
  return super.run(type);
2138
2222
  }
2139
2223
  notify(node, type, flags) {
2224
+ if (ActiveTransition && ActiveTransition._clonedQueues.has(this))
2225
+ return ActiveTransition._clonedQueues.get(this).notify(node, type, flags);
2140
2226
  if (!(type & this._collectionType))
2141
2227
  return super.notify(node, type, flags);
2142
2228
  if (flags & this._collectionType) {
@@ -2151,6 +2237,10 @@ var CollectionQueue = class extends Queue {
2151
2237
  type &= ~this._collectionType;
2152
2238
  return type ? super.notify(node, type, flags) : true;
2153
2239
  }
2240
+ merge(queue) {
2241
+ queue._nodes.forEach((n) => this.notify(n, this._collectionType, this._collectionType));
2242
+ super.merge(queue);
2243
+ }
2154
2244
  };
2155
2245
  function createBoundary(fn, condition) {
2156
2246
  const owner = new Owner();
@@ -2188,17 +2278,17 @@ function createSuspense(fn, fallback) {
2188
2278
  return createCollectionBoundary(LOADING_BIT, fn, () => fallback());
2189
2279
  }
2190
2280
  function createErrorBoundary(fn, fallback) {
2191
- return createCollectionBoundary(
2192
- ERROR_BIT,
2193
- fn,
2194
- (queue) => fallback(queue._nodes.values().next().value._error, () => {
2281
+ return createCollectionBoundary(ERROR_BIT, fn, (queue) => {
2282
+ let node = queue._nodes.values().next().value;
2283
+ ActiveTransition && ActiveTransition._sources.has(node) && (node = ActiveTransition._sources.get(node));
2284
+ return fallback(node._error, () => {
2195
2285
  incrementClock();
2196
- for (let node of queue._nodes) {
2197
- node._state = STATE_DIRTY;
2198
- node._queue?.enqueue(node._type, node._run.bind(node));
2286
+ for (let node2 of queue._nodes) {
2287
+ node2._state = STATE_DIRTY;
2288
+ node2._queue?.enqueue(node2._type, node2._run.bind(node2));
2199
2289
  }
2200
- })
2201
- );
2290
+ });
2291
+ });
2202
2292
  }
2203
2293
  function flatten(children, options) {
2204
2294
  if (typeof children === "function" && !children.length) {