@but212/atom-effect 0.8.1 → 0.8.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/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const te = {
1
+ const Z = {
2
2
  /** One second in milliseconds */
3
3
  ONE_SECOND_MS: 1e3
4
4
  }, v = {
@@ -26,7 +26,7 @@ const te = {
26
26
  // 100000 - Currently recomputing
27
27
  HAS_ERROR: 64
28
28
  // 1000000 - Has error state
29
- }, pe = {
29
+ }, be = {
30
30
  /** Maximum number of pooled objects to prevent memory bloat */
31
31
  MAX_SIZE: 1e3,
32
32
  /** Number of objects to pre-allocate for performance-critical paths */
@@ -55,7 +55,7 @@ const te = {
55
55
  MAX_DEPENDENCIES: 1e3,
56
56
  /** Enable infinite loop detection warnings */
57
57
  WARN_INFINITE_LOOP: !0
58
- }, I = 1073741823, a = typeof process < "u" && process.env && process.env.NODE_ENV !== "production", se = Object.freeze([]);
58
+ }, I = 1073741823, a = typeof process < "u" && process.env && process.env.NODE_ENV !== "production", ee = Object.freeze([]);
59
59
  class p extends Error {
60
60
  /**
61
61
  * Creates a new AtomError
@@ -181,8 +181,8 @@ const h = {
181
181
  * @remarks This prevents cascading failures from masking the original error.
182
182
  */
183
183
  CALLBACK_ERROR_IN_ERROR_HANDLER: "Error occurred during onError callback execution"
184
- }, L = /* @__PURE__ */ Symbol("debugName"), ie = /* @__PURE__ */ Symbol("id"), V = /* @__PURE__ */ Symbol("type"), q = /* @__PURE__ */ Symbol("noDefaultValue");
185
- function ne(i) {
184
+ }, L = /* @__PURE__ */ Symbol("debugName"), te = /* @__PURE__ */ Symbol("id"), V = /* @__PURE__ */ Symbol("type"), q = /* @__PURE__ */ Symbol("noDefaultValue");
185
+ function se(i) {
186
186
  return "dependencies" in i && Array.isArray(i.dependencies);
187
187
  }
188
188
  let j = 0;
@@ -190,7 +190,7 @@ function G(i, e, t) {
190
190
  if (i._visitedEpoch !== t) {
191
191
  if (i._visitedEpoch = t, i === e)
192
192
  throw new N("Indirect circular dependency detected");
193
- if (ne(i)) {
193
+ if (se(i)) {
194
194
  const s = i.dependencies;
195
195
  for (let n = 0; n < s.length; n++) {
196
196
  const r = s[n];
@@ -220,7 +220,7 @@ const d = {
220
220
  if (!this.enabled)
221
221
  return;
222
222
  const s = i;
223
- s[L] = `${e}_${t}`, s[ie] = t, s[V] = e;
223
+ s[L] = `${e}_${t}`, s[te] = t, s[V] = e;
224
224
  },
225
225
  getDebugName(i) {
226
226
  if (i != null && L in i)
@@ -231,11 +231,11 @@ const d = {
231
231
  return i[V];
232
232
  }
233
233
  };
234
- let re = 1;
235
- const oe = () => re++;
234
+ let ie = 1;
235
+ const ne = () => ie++;
236
236
  class Y {
237
237
  constructor() {
238
- this.id = oe() & I, this.flags = 0;
238
+ this.id = ne() & I, this.flags = 0;
239
239
  }
240
240
  }
241
241
  class $ extends Y {
@@ -282,7 +282,7 @@ let U = 0;
282
282
  function H() {
283
283
  return U = (U + 1 | 0) & I, U;
284
284
  }
285
- function ce() {
285
+ function re() {
286
286
  return U;
287
287
  }
288
288
  let x = 0, w = 0, F = !1;
@@ -294,10 +294,10 @@ function z() {
294
294
  function X() {
295
295
  F = !1;
296
296
  }
297
- function he() {
297
+ function oe() {
298
298
  return F ? ++w : 0;
299
299
  }
300
- class ue {
300
+ class ce {
301
301
  constructor() {
302
302
  this.queueA = [], this.queueB = [], this.queue = this.queueA, this.queueSize = 0, this._epoch = 0, this.isProcessing = !1, this.isBatching = !1, this.batchDepth = 0, this.batchQueue = [], this.batchQueueSize = 0, this.isFlushingSync = !1, this.maxFlushIterations = g.MAX_FLUSH_ITERATIONS;
303
303
  }
@@ -397,8 +397,8 @@ class ue {
397
397
  this.maxFlushIterations = e;
398
398
  }
399
399
  }
400
- const T = new ue();
401
- function be(i) {
400
+ const T = new ce();
401
+ function Se(i) {
402
402
  if (typeof i != "function")
403
403
  throw new p("Batch callback must be a function");
404
404
  T.startBatch();
@@ -423,7 +423,7 @@ const y = {
423
423
  return this.current;
424
424
  }
425
425
  };
426
- function _e(i) {
426
+ function he(i) {
427
427
  if (typeof i != "function")
428
428
  throw new p("Untracked callback must be a function");
429
429
  const e = y.current;
@@ -490,98 +490,6 @@ class P {
490
490
  return this.subscribers ? [...this.subscribers] : [];
491
491
  }
492
492
  }
493
- function ae(i) {
494
- return i !== null && typeof i == "object" && "value" in i && "subscribe" in i && typeof i.subscribe == "function";
495
- }
496
- function Se(i) {
497
- if (d.enabled && (i == null || typeof i == "object")) {
498
- const e = d.getDebugType(i);
499
- if (e)
500
- return e === "computed";
501
- }
502
- return ae(i) && "invalidate" in i && typeof i.invalidate == "function";
503
- }
504
- function ge(i) {
505
- return i !== null && typeof i == "object" && "dispose" in i && "run" in i && typeof i.dispose == "function" && typeof i.run == "function";
506
- }
507
- function J(i) {
508
- return i != null && typeof i.then == "function";
509
- }
510
- function le(i) {
511
- return typeof i == "object" && i !== null;
512
- }
513
- function Q(i) {
514
- return (typeof i == "object" || typeof i == "function") && i !== null && typeof i.addDependency == "function";
515
- }
516
- function W(i) {
517
- return typeof i == "function" && typeof i.addDependency != "function";
518
- }
519
- function K(i) {
520
- return le(i) && typeof i.execute == "function";
521
- }
522
- class fe extends $ {
523
- constructor(e, t) {
524
- super(), this._isNotificationScheduled = !1, this._value = e, this._functionSubscribersStore = new P(), this._objectSubscribersStore = new P(), this._sync = t, this._notifyTask = this._flushNotifications.bind(this), d.attachDebugInfo(this, "atom", this.id);
525
- }
526
- /** Gets the manager for function-based subscribers. */
527
- get _functionSubscribers() {
528
- return this._functionSubscribersStore;
529
- }
530
- /** Gets the manager for object-based subscribers. */
531
- get _objectSubscribers() {
532
- return this._objectSubscribersStore;
533
- }
534
- /**
535
- * Returns the current value and registers the atom as a dependency in the current tracking context.
536
- */
537
- get value() {
538
- const e = y.getCurrent();
539
- return e && this._track(e), this._value;
540
- }
541
- /**
542
- * Sets a new value and schedules notifications if the value has changed.
543
- * Uses `Object.is` for comparison.
544
- */
545
- set value(e) {
546
- if (Object.is(this._value, e)) return;
547
- const t = this._value;
548
- this.version = this.version + 1 & I, this._value = e, !(!this._functionSubscribersStore.hasSubscribers && !this._objectSubscribersStore.hasSubscribers) && this._scheduleNotification(t);
549
- }
550
- _track(e) {
551
- if (Q(e)) {
552
- e.addDependency(this);
553
- return;
554
- }
555
- if (W(e)) {
556
- this._functionSubscribersStore.add(e);
557
- return;
558
- }
559
- K(e) && this._objectSubscribersStore.add(e);
560
- }
561
- _scheduleNotification(e) {
562
- this._isNotificationScheduled || (this._pendingOldValue = e, this._isNotificationScheduled = !0), this._sync && !T.isBatching ? this._flushNotifications() : T.schedule(this._notifyTask);
563
- }
564
- _flushNotifications() {
565
- if (!this._isNotificationScheduled) return;
566
- const e = this._pendingOldValue, t = this._value;
567
- this._pendingOldValue = void 0, this._isNotificationScheduled = !1, this._notifySubscribers(t, e);
568
- }
569
- /**
570
- * Returns the current value without registering as a dependency in the tracking context.
571
- */
572
- peek() {
573
- return this._value;
574
- }
575
- /**
576
- * Disposes of the atom, clearing all subscribers and resetting the value.
577
- */
578
- dispose() {
579
- this._functionSubscribersStore.clear(), this._objectSubscribersStore.clear(), this._value = void 0;
580
- }
581
- }
582
- function De(i, e = {}) {
583
- return new fe(i, e.sync ?? !1);
584
- }
585
493
  class B {
586
494
  constructor() {
587
495
  this.pool = [], this.maxPoolSize = 50, this.maxReusableCapacity = 256, this.stats = a ? {
@@ -635,6 +543,48 @@ class B {
635
543
  }
636
544
  }
637
545
  const u = Object.freeze([]), l = Object.freeze([]), _ = Object.freeze([]), S = new B(), D = new B(), f = new B();
546
+ function ue(i) {
547
+ return i !== null && typeof i == "object" && "value" in i && "subscribe" in i && typeof i.subscribe == "function";
548
+ }
549
+ function ge(i) {
550
+ if (d.enabled && (i == null || typeof i == "object")) {
551
+ const e = d.getDebugType(i);
552
+ if (e)
553
+ return e === "computed";
554
+ }
555
+ return ue(i) && "invalidate" in i && typeof i.invalidate == "function";
556
+ }
557
+ function De(i) {
558
+ return i !== null && typeof i == "object" && "dispose" in i && "run" in i && typeof i.dispose == "function" && typeof i.run == "function";
559
+ }
560
+ function J(i) {
561
+ return i != null && typeof i.then == "function";
562
+ }
563
+ function _e(i) {
564
+ return typeof i == "object" && i !== null;
565
+ }
566
+ function ae(i) {
567
+ return (typeof i == "object" || typeof i == "function") && i !== null && typeof i.addDependency == "function";
568
+ }
569
+ function le(i) {
570
+ return typeof i == "function" && typeof i.addDependency != "function";
571
+ }
572
+ function fe(i) {
573
+ return _e(i) && typeof i.execute == "function";
574
+ }
575
+ function Q(i, e, t, s) {
576
+ if (e) {
577
+ if (ae(e)) {
578
+ e.addDependency(i);
579
+ return;
580
+ }
581
+ if (le(e)) {
582
+ t.add(e);
583
+ return;
584
+ }
585
+ fe(e) && s.add(e);
586
+ }
587
+ }
638
588
  function de(i, e, t, s) {
639
589
  if (e !== u && t !== l)
640
590
  for (let r = 0; r < e.length; r++) {
@@ -654,6 +604,61 @@ function de(i, e, t, s) {
654
604
  }
655
605
  return t !== l && D.release(t), n;
656
606
  }
607
+ class Ee extends $ {
608
+ constructor(e, t) {
609
+ super(), this._isNotificationScheduled = !1, this._value = e, this._functionSubscribersStore = new P(), this._objectSubscribersStore = new P(), this._sync = t, this._notifyTask = this._flushNotifications.bind(this), d.attachDebugInfo(this, "atom", this.id);
610
+ }
611
+ /** Gets the manager for function-based subscribers. */
612
+ get _functionSubscribers() {
613
+ return this._functionSubscribersStore;
614
+ }
615
+ /** Gets the manager for object-based subscribers. */
616
+ get _objectSubscribers() {
617
+ return this._objectSubscribersStore;
618
+ }
619
+ /**
620
+ * Returns the current value and registers the atom as a dependency in the current tracking context.
621
+ */
622
+ get value() {
623
+ const e = y.getCurrent();
624
+ return e && this._track(e), this._value;
625
+ }
626
+ /**
627
+ * Sets a new value and schedules notifications if the value has changed.
628
+ * Uses `Object.is` for comparison.
629
+ */
630
+ set value(e) {
631
+ if (Object.is(this._value, e)) return;
632
+ const t = this._value;
633
+ this.version = this.version + 1 & I, this._value = e, !(!this._functionSubscribersStore.hasSubscribers && !this._objectSubscribersStore.hasSubscribers) && this._scheduleNotification(t);
634
+ }
635
+ _track(e) {
636
+ Q(this, e, this._functionSubscribersStore, this._objectSubscribersStore);
637
+ }
638
+ _scheduleNotification(e) {
639
+ this._isNotificationScheduled || (this._pendingOldValue = e, this._isNotificationScheduled = !0), this._sync && !T.isBatching ? this._flushNotifications() : T.schedule(this._notifyTask);
640
+ }
641
+ _flushNotifications() {
642
+ if (!this._isNotificationScheduled) return;
643
+ const e = this._pendingOldValue, t = this._value;
644
+ this._pendingOldValue = void 0, this._isNotificationScheduled = !1, this._notifySubscribers(t, e);
645
+ }
646
+ /**
647
+ * Returns the current value without registering as a dependency in the tracking context.
648
+ */
649
+ peek() {
650
+ return this._value;
651
+ }
652
+ /**
653
+ * Disposes of the atom, clearing all subscribers and resetting the value.
654
+ */
655
+ dispose() {
656
+ this._functionSubscribersStore.clear(), this._objectSubscribersStore.clear(), this._value = void 0;
657
+ }
658
+ }
659
+ function Ie(i, e = {}) {
660
+ return new Ee(i, e.sync ?? !1);
661
+ }
657
662
  function R(i, e, t) {
658
663
  if (i instanceof TypeError)
659
664
  return new e(`Type error (${t}): ${i.message}`, i);
@@ -664,11 +669,11 @@ function R(i, e, t) {
664
669
  const s = i instanceof Error ? i.message : String(i), n = i instanceof Error ? i : null;
665
670
  return new e(`Unexpected error (${t}): ${s}`, n);
666
671
  }
667
- const Z = c.RESOLVED | c.PENDING | c.REJECTED, M = Array(Z + 1).fill(v.IDLE);
672
+ const W = c.RESOLVED | c.PENDING | c.REJECTED, M = Array(W + 1).fill(v.IDLE);
668
673
  M[c.RESOLVED] = v.RESOLVED;
669
674
  M[c.PENDING] = v.PENDING;
670
675
  M[c.REJECTED] = v.REJECTED;
671
- class ee extends $ {
676
+ class K extends $ {
672
677
  constructor(e, t = {}) {
673
678
  if (typeof e != "function")
674
679
  throw new N(h.COMPUTED_MUST_BE_FUNCTION);
@@ -723,8 +728,8 @@ class ee extends $ {
723
728
  }
724
729
  get errors() {
725
730
  if (this._registerTracking(), !this.hasError)
726
- return se;
727
- const e = ce();
731
+ return ee;
732
+ const e = re();
728
733
  if (this._errorCacheEpoch === e && this._cachedErrors !== null)
729
734
  return this._cachedErrors;
730
735
  const t = /* @__PURE__ */ new Set();
@@ -805,7 +810,7 @@ class ee extends $ {
805
810
  this.flags = this.flags & ~t | -Number(e) & t;
806
811
  }
807
812
  _getAsyncState() {
808
- return M[this.flags & Z];
813
+ return M[this.flags & W];
809
814
  }
810
815
  _getFlagsAsString() {
811
816
  const e = [];
@@ -902,25 +907,19 @@ class ee extends $ {
902
907
  this._isRecomputing() || this._isDirty() || (this._setDirty(), this._notifyJob());
903
908
  }
904
909
  _registerTracking() {
905
- const e = y.getCurrent();
906
- if (e) {
907
- if (Q(e)) {
908
- e.addDependency(this);
909
- return;
910
- }
911
- if (W(e)) {
912
- this._functionSubscribersStore.add(e);
913
- return;
914
- }
915
- K(e) && this._objectSubscribersStore.add(e);
916
- }
910
+ Q(
911
+ this,
912
+ y.getCurrent(),
913
+ this._functionSubscribersStore,
914
+ this._objectSubscribersStore
915
+ );
917
916
  }
918
917
  }
919
- Object.freeze(ee.prototype);
920
- function Ie(i, e = {}) {
921
- return new ee(i, e);
918
+ Object.freeze(K.prototype);
919
+ function ye(i, e = {}) {
920
+ return new K(i, e);
922
921
  }
923
- class Ee extends Y {
922
+ class pe extends Y {
924
923
  /**
925
924
  * Creates a new EffectImpl instance.
926
925
  * @param fn - The effect function to run.
@@ -1072,7 +1071,7 @@ class Ee extends Y {
1072
1071
  * @throws {EffectError} If an infinite loop is detected.
1073
1072
  */
1074
1073
  _checkInfiniteLoop() {
1075
- if (this._lastFlushEpoch !== x && (this._lastFlushEpoch = x, this._executionsInEpoch = 0), this._executionsInEpoch++, this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), he() > g.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, this._history) {
1074
+ if (this._lastFlushEpoch !== x && (this._lastFlushEpoch = x, this._executionsInEpoch = 0), this._executionsInEpoch++, this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), oe() > g.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, this._history) {
1076
1075
  const e = Date.now();
1077
1076
  this._history.push(e), this._history.length > g.MAX_EXECUTIONS_PER_SECOND + 10 && this._history.shift(), this._checkTimestampLoop(e);
1078
1077
  }
@@ -1080,7 +1079,7 @@ class Ee extends Y {
1080
1079
  _checkTimestampLoop(e) {
1081
1080
  const t = this._history;
1082
1081
  if (!t || this._maxExecutions <= 0) return;
1083
- const s = e - te.ONE_SECOND_MS;
1082
+ const s = e - Z.ONE_SECOND_MS;
1084
1083
  let n = 0;
1085
1084
  for (let r = t.length - 1; r >= 0 && !(t[r] < s); r--)
1086
1085
  n++;
@@ -1110,7 +1109,7 @@ class Ee extends Y {
1110
1109
  if (t) {
1111
1110
  if ("value" in t)
1112
1111
  try {
1113
- _e(() => t.value);
1112
+ he(() => t.value);
1114
1113
  } catch {
1115
1114
  return !0;
1116
1115
  }
@@ -1137,10 +1136,10 @@ class Ee extends Y {
1137
1136
  }
1138
1137
  }
1139
1138
  }
1140
- function ye(i, e = {}) {
1139
+ function me(i, e = {}) {
1141
1140
  if (typeof i != "function")
1142
1141
  throw new b(h.EFFECT_MUST_BE_FUNCTION);
1143
- const t = new Ee(i, e);
1142
+ const t = new pe(i, e);
1144
1143
  return t.execute(), t;
1145
1144
  }
1146
1145
  export {
@@ -1150,17 +1149,17 @@ export {
1150
1149
  k as DEBUG_CONFIG,
1151
1150
  d as DEBUG_RUNTIME,
1152
1151
  b as EffectError,
1153
- pe as POOL_CONFIG,
1152
+ be as POOL_CONFIG,
1154
1153
  g as SCHEDULER_CONFIG,
1155
1154
  O as SchedulerError,
1156
- De as atom,
1157
- be as batch,
1158
- Ie as computed,
1159
- ye as effect,
1160
- ae as isAtom,
1161
- Se as isComputed,
1162
- ge as isEffect,
1155
+ Ie as atom,
1156
+ Se as batch,
1157
+ ye as computed,
1158
+ me as effect,
1159
+ ue as isAtom,
1160
+ ge as isComputed,
1161
+ De as isEffect,
1163
1162
  T as scheduler,
1164
- _e as untracked
1163
+ he as untracked
1165
1164
  };
1166
1165
  //# sourceMappingURL=index.mjs.map