@but212/atom-effect 0.21.0 → 0.21.2

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 k = {
1
+ const A = {
2
2
  IDLE: "idle",
3
3
  PENDING: "pending",
4
4
  RESOLVED: "resolved",
@@ -6,7 +6,7 @@ const k = {
6
6
  }, l = {
7
7
  DISPOSED: 1,
8
8
  EXECUTING: 8
9
- }, st = {
9
+ }, K = {
10
10
  DISPOSED: 1,
11
11
  /** Marker bit: identifies this node as a computed. */
12
12
  IS_COMPUTED: 2,
@@ -17,15 +17,15 @@ const k = {
17
17
  REJECTED: 128,
18
18
  RECOMPUTING: 256,
19
19
  HAS_ERROR: 512
20
- }, E = {
20
+ }, a = {
21
21
  DISPOSED: 1,
22
22
  SYNC: 8,
23
23
  NOTIFICATION_SCHEDULED: 16
24
- }, kt = {
24
+ }, Ct = {
25
25
  MAX_SIZE: 1e3,
26
26
  WARMUP_SIZE: 100,
27
27
  ENABLE_STATS: !1
28
- }, S = {
28
+ }, p = {
29
29
  // Infinite loop protection
30
30
  MAX_EXECUTIONS_PER_SECOND: 1e3,
31
31
  MAX_EXECUTIONS_PER_EFFECT: 100,
@@ -34,68 +34,52 @@ const k = {
34
34
  MAX_FLUSH_ITERATIONS: 1e3,
35
35
  MIN_FLUSH_ITERATIONS: 10,
36
36
  // Memory management
37
- CLEANUP_THRESHOLD: 1e3,
38
37
  BATCH_QUEUE_SHRINK_THRESHOLD: 1e3
39
- }, _t = {
40
- WARN_INFINITE_LOOP: !0
41
- }, z = 1073741823, u = typeof process < "u" && process.env && process.env.NODE_ENV !== "production" || typeof __DEV__ < "u" && !!__DEV__, at = Object.freeze([]);
42
- class O extends Error {
38
+ }, ot = {
39
+ WARN_INFINITE_LOOP: !0,
40
+ EFFECT_FREQUENCY_WINDOW: 1e3
41
+ }, X = {
42
+ MAX_ASYNC_RETRIES: 3,
43
+ MAX_PROMISE_ID: Number.MAX_SAFE_INTEGER - 1
44
+ }, B = 1073741823, J = typeof process < "u" && process.env && !1 || !1, ht = Object.freeze([]);
45
+ class C extends Error {
43
46
  constructor(t, e = null, s = !0) {
44
47
  super(t), this.cause = e, this.recoverable = s, this.name = "AtomError";
45
48
  }
46
49
  }
47
- class d extends O {
50
+ class E extends C {
48
51
  constructor(t, e = null) {
49
52
  super(t, e, !0), this.name = "ComputedError";
50
53
  }
51
54
  }
52
- class I extends O {
55
+ class g extends C {
53
56
  constructor(t, e = null) {
54
57
  super(t, e, !1), this.name = "EffectError";
55
58
  }
56
59
  }
57
- class m extends O {
60
+ class m extends C {
58
61
  constructor(t, e = null) {
59
62
  super(t, e, !1), this.name = "SchedulerError";
60
63
  }
61
64
  }
62
- const G = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), ft = /* @__PURE__ */ Symbol("AtomEffect.Id"), Q = /* @__PURE__ */ Symbol("AtomEffect.Type"), v = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), Et = (i) => "dependencies" in i && Array.isArray(i.dependencies);
63
- function it(i, t, e) {
64
- if (i === t)
65
- throw new d(
66
- "Circular dependency detected: The computation refers to itself explicitly or implicitly."
67
- );
68
- if (!e.has(i.id) && (e.add(i.id), Et(i))) {
69
- const s = i.dependencies;
70
- for (let n = 0; n < s.length; n++) {
71
- const r = s[n];
72
- r && it(r, t, e);
73
- }
74
- }
75
- }
76
- const D = {
65
+ const ct = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), ut = /* @__PURE__ */ Symbol("AtomEffect.Type"), U = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), L = {
77
66
  // Dev mode flag
78
- enabled: u,
79
- warnInfiniteLoop: _t.WARN_INFINITE_LOOP,
67
+ enabled: J,
68
+ warnInfiniteLoop: ot.WARN_INFINITE_LOOP,
80
69
  warn(i, t) {
81
- u && this.enabled && i && console.warn(`[Atom Effect] ${t}`);
82
70
  },
83
71
  checkCircular(i, t) {
84
72
  if (i === t)
85
- throw new d("Direct circular dependency detected");
86
- u && this.enabled && it(i, t, /* @__PURE__ */ new Set());
73
+ throw new E("Direct circular dependency detected");
87
74
  },
88
75
  attachDebugInfo(i, t, e) {
89
- if (!u || !this.enabled) return;
90
- const s = i;
91
- s[G] = `${t}_${e}`, s[ft] = e, s[Q] = t;
92
76
  },
93
- getDebugName: (i) => i?.[G],
94
- getDebugType: (i) => i?.[Q]
77
+ getDebugName: (i) => i?.[ct],
78
+ getDebugType: (i) => i?.[ut]
95
79
  };
96
- let dt = 1;
97
- const pt = () => dt++;
98
- function nt(i, t, e) {
80
+ let lt = 1;
81
+ const _t = () => lt++;
82
+ function Z(i, t, e) {
99
83
  if (typeof t == "function") {
100
84
  const n = t;
101
85
  for (let r = 0, o = e.length; r < o; r++) {
@@ -116,7 +100,7 @@ function nt(i, t, e) {
116
100
  }
117
101
  e.push(new M(void 0, s));
118
102
  }
119
- function j(i, t, e) {
103
+ function G(i, t, e) {
120
104
  const s = /* @__PURE__ */ new Map();
121
105
  for (let n = 0, r = t.length; n < r; n++) {
122
106
  const o = t[n];
@@ -125,13 +109,13 @@ function j(i, t, e) {
125
109
  for (let n = 0, r = i.length; n < r; n++) {
126
110
  const o = i[n];
127
111
  if (!o) continue;
128
- const h = o.node, f = s.get(h);
129
- f !== void 0 ? (o.unsub = f, s.delete(h)) : (D.checkCircular(h, e), o.unsub = h.subscribe(e));
112
+ const h = o.node, I = s.get(h);
113
+ I !== void 0 ? (o.unsub = I, s.delete(h)) : (L.checkCircular(h, e), o.unsub = h.subscribe(e));
130
114
  }
131
115
  for (const n of s.values())
132
116
  n();
133
117
  }
134
- class R {
118
+ class v {
135
119
  constructor(t, e, s = void 0) {
136
120
  this.node = t, this.version = e, this.unsub = s;
137
121
  }
@@ -162,36 +146,38 @@ const c = {
162
146
  // System / Debug
163
147
  CALLBACK_ERROR_IN_ERROR_HANDLER: "Exception encountered in onError handler",
164
148
  // Effect frequency
165
- EFFECT_FREQUENCY_LIMIT_EXCEEDED: "Effect executed too frequently within 1 second. Suspected infinite loop."
149
+ EFFECT_FREQUENCY_LIMIT_EXCEEDED: "Effect executed too frequently within 1 second. Suspected infinite loop.",
150
+ SCHEDULER_CALLBACK_MUST_BE_FUNCTION: "Scheduler callback must be a function",
151
+ SCHEDULER_END_BATCH_WITHOUT_START: "endBatch() called without matching startBatch(). Ignoring."
166
152
  };
167
- function T(i, t, e) {
168
- if (i instanceof O)
153
+ function D(i, t, e) {
154
+ if (i instanceof C)
169
155
  return i;
170
156
  const s = i instanceof Error, n = s ? i.message : String(i), r = s ? i : void 0, h = `${s ? i.constructor.name : "Unexpected error"} (${e}): ${n}`;
171
157
  return new t(h, r);
172
158
  }
173
- class rt {
159
+ class tt {
174
160
  constructor() {
175
- this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this.id = pt() & z;
161
+ this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this.id = _t() & B;
176
162
  }
177
163
  }
178
- class ot extends rt {
164
+ class et extends tt {
179
165
  /**
180
166
  * Adds subscriber.
181
167
  */
182
168
  subscribe(t) {
183
169
  const e = typeof t == "function";
184
170
  if (!e && (!t || typeof t.execute != "function"))
185
- throw T(
171
+ throw D(
186
172
  new TypeError("Invalid subscriber"),
187
- O,
173
+ C,
188
174
  c.ATOM_SUBSCRIBER_MUST_BE_FUNCTION
189
175
  );
190
176
  const s = this._subscribers;
191
177
  for (let r = 0, o = s.length; r < o; r++) {
192
178
  const h = s[r];
193
179
  if (h && (e ? h.fn === t : h.sub === t))
194
- return u && console.warn("Duplicate subscription ignored."), () => {
180
+ return () => {
195
181
  };
196
182
  }
197
183
  const n = new M(
@@ -223,22 +209,22 @@ class ot extends rt {
223
209
  }
224
210
  }
225
211
  _handleNotifyError(t) {
226
- console.error(T(t, O, c.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
212
+ console.error(D(t, C, c.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
227
213
  }
228
214
  }
229
- let w = 0;
230
- const V = () => (w = w + 1 & z || 1, w), B = (i) => i + 1 & z;
231
- let H = 0, U = !1, ht = 0;
232
- const It = () => ht;
233
- function Y() {
234
- return U ? (u && console.warn("startFlush() called during flush - ignored"), !1) : (U = !0, ht = V(), H = 0, !0);
215
+ let x = 0;
216
+ const z = () => (x = x + 1 & B || 1, x), w = (i) => i + 1 & B;
217
+ let H = 0, k = !1, st = 0;
218
+ const at = () => st;
219
+ function Q() {
220
+ return k ? !1 : (k = !0, st = z(), H = 0, !0);
235
221
  }
236
- const $ = () => {
237
- U = !1;
238
- }, gt = () => U ? ++H : 0;
239
- class St {
222
+ const Y = () => {
223
+ k = !1;
224
+ }, ft = () => k ? ++H : 0;
225
+ class Et {
240
226
  constructor() {
241
- this._queueBuffer = [[], []], this._bufferIndex = 0, this._size = 0, this._epoch = 0, this._isProcessing = !1, this._isBatching = !1, this._isFlushingSync = !1, this._batchDepth = 0, this._batchQueue = [], this._batchQueueSize = 0, this._maxFlushIterations = S.MAX_FLUSH_ITERATIONS, this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
227
+ this._queueBuffer = [[], []], this._bufferIndex = 0, this._size = 0, this._epoch = 0, this._isProcessing = !1, this._isBatching = !1, this._isFlushingSync = !1, this._batchDepth = 0, this._batchQueue = [], this._batchQueueSize = 0, this._maxFlushIterations = p.MAX_FLUSH_ITERATIONS, this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
242
228
  }
243
229
  get phase() {
244
230
  return this._isProcessing || this._isFlushingSync ? 2 : this._isBatching ? 1 : 0;
@@ -253,8 +239,6 @@ class St {
253
239
  * Schedules job.
254
240
  */
255
241
  schedule(t) {
256
- if (u && typeof t != "function")
257
- throw new m("Scheduler callback must be a function");
258
242
  if (t._nextEpoch !== this._epoch) {
259
243
  if (t._nextEpoch = this._epoch, this._isBatching || this._isFlushingSync) {
260
244
  this._batchQueue[this._batchQueueSize++] = t;
@@ -275,19 +259,19 @@ class St {
275
259
  _runLoop() {
276
260
  try {
277
261
  if (this._size === 0) return;
278
- const t = Y();
279
- this._drainQueue(), t && $();
262
+ const t = Q();
263
+ this._drainQueue(), t && Y();
280
264
  } finally {
281
265
  this._isProcessing = !1, this._size > 0 && !this._isBatching && this._flush();
282
266
  }
283
267
  }
284
268
  _flushSync() {
285
269
  this._isFlushingSync = !0;
286
- const t = Y();
270
+ const t = Q();
287
271
  try {
288
272
  this._mergeBatchQueue(), this._drainQueue();
289
273
  } finally {
290
- this._isFlushingSync = !1, t && $();
274
+ this._isFlushingSync = !1, t && Y();
291
275
  }
292
276
  }
293
277
  _mergeBatchQueue() {
@@ -298,7 +282,7 @@ class St {
298
282
  const o = e[r];
299
283
  o._nextEpoch !== t && (o._nextEpoch = t, s[n++] = o);
300
284
  }
301
- this._size = n, this._batchQueueSize = 0, e.length > S.BATCH_QUEUE_SHRINK_THRESHOLD && (e.length = 0);
285
+ this._size = n, this._batchQueueSize = 0, e.length > p.BATCH_QUEUE_SHRINK_THRESHOLD && (e.length = 0);
302
286
  }
303
287
  _drainQueue() {
304
288
  let t = 0;
@@ -337,21 +321,17 @@ class St {
337
321
  this._batchDepth++, this._isBatching = !0;
338
322
  }
339
323
  endBatch() {
340
- if (this._batchDepth === 0) {
341
- u && console.warn("endBatch() called without matching startBatch(). Ignoring.");
342
- return;
343
- }
344
- --this._batchDepth === 0 && (this._flushSync(), this._isBatching = !1);
324
+ this._batchDepth !== 0 && --this._batchDepth === 0 && (this._flushSync(), this._isBatching = !1);
345
325
  }
346
326
  setMaxFlushIterations(t) {
347
- if (t < S.MIN_FLUSH_ITERATIONS)
327
+ if (t < p.MIN_FLUSH_ITERATIONS)
348
328
  throw new m(
349
- `Max flush iterations must be at least ${S.MIN_FLUSH_ITERATIONS}`
329
+ `Max flush iterations must be at least ${p.MIN_FLUSH_ITERATIONS}`
350
330
  );
351
331
  this._maxFlushIterations = t;
352
332
  }
353
333
  }
354
- const N = new St(), X = /* @__PURE__ */ Symbol.for("atom-effect/atom"), ct = /* @__PURE__ */ Symbol.for("atom-effect/computed"), ut = /* @__PURE__ */ Symbol.for("atom-effect/effect"), a = {
334
+ const b = new Et(), V = /* @__PURE__ */ Symbol.for("atom-effect/atom"), it = /* @__PURE__ */ Symbol.for("atom-effect/computed"), nt = /* @__PURE__ */ Symbol.for("atom-effect/effect"), _ = {
355
335
  /** Active listener. */
356
336
  current: null,
357
337
  /**
@@ -371,36 +351,36 @@ const N = new St(), X = /* @__PURE__ */ Symbol.for("atom-effect/atom"), ct = /*
371
351
  }
372
352
  }
373
353
  };
374
- function mt(i) {
375
- const t = a.current;
354
+ function Tt(i) {
355
+ const t = _.current;
376
356
  if (t === null) return i();
377
- a.current = null;
357
+ _.current = null;
378
358
  try {
379
359
  return i();
380
360
  } finally {
381
- a.current = t;
361
+ _.current = t;
382
362
  }
383
363
  }
384
- var q, K;
385
- class Dt extends (K = ot, q = X, K) {
364
+ var j;
365
+ class dt extends et {
386
366
  constructor(t, e) {
387
- super(), this._pendingOldValue = void 0, this._notifyTask = void 0, this._subscribers = [], this[q] = !0, this._value = t, e && (this.flags |= E.SYNC), D.attachDebugInfo(this, "atom", this.id);
367
+ super(), this._pendingOldValue = void 0, this._notifyTask = () => this._flushNotifications(), this._subscribers = [], this[j] = !0, this._value = t, e && (this.flags |= a.SYNC), L.attachDebugInfo(this, "atom", this.id);
388
368
  }
389
369
  get value() {
390
- const t = a.current;
391
- return t && nt(this, t, this._subscribers), this._value;
370
+ const t = _.current;
371
+ return t && Z(this, t, this._subscribers), this._value;
392
372
  }
393
373
  set value(t) {
394
374
  const e = this._value;
395
375
  if (Object.is(e, t)) return;
396
- this._value = t, this.version = B(this.version);
376
+ this._value = t, this.version = w(this.version);
397
377
  const s = this.flags;
398
- if (!(this._subscribers.length === 0 || s & E.NOTIFICATION_SCHEDULED)) {
399
- if (this._pendingOldValue = e, this.flags = s | E.NOTIFICATION_SCHEDULED, s & E.SYNC && !N.isBatching) {
378
+ if (!(this._subscribers.length === 0 || s & a.NOTIFICATION_SCHEDULED)) {
379
+ if (this._pendingOldValue = e, this.flags = s | a.NOTIFICATION_SCHEDULED, s & a.SYNC && !b.isBatching) {
400
380
  this._flushNotifications();
401
381
  return;
402
382
  }
403
- this._notifyTask || (this._notifyTask = () => this._flushNotifications()), N.schedule(this._notifyTask);
383
+ b.schedule(this._notifyTask);
404
384
  }
405
385
  }
406
386
  /**
@@ -408,7 +388,7 @@ class Dt extends (K = ot, q = X, K) {
408
388
  */
409
389
  _flushNotifications() {
410
390
  const t = this.flags;
411
- if (!(t & E.NOTIFICATION_SCHEDULED) || t & E.DISPOSED)
391
+ if (!(t & a.NOTIFICATION_SCHEDULED) || t & a.DISPOSED)
412
392
  return;
413
393
  const e = this._pendingOldValue;
414
394
  this._pendingOldValue = void 0, this.flags &= -17, this._notifySubscribers(this._value, e);
@@ -417,20 +397,23 @@ class Dt extends (K = ot, q = X, K) {
417
397
  return this._value;
418
398
  }
419
399
  dispose() {
420
- this.flags & E.DISPOSED || (this._subscribers.length = 0, this.flags |= E.DISPOSED, this._value = void 0, this._pendingOldValue = void 0, this._notifyTask = void 0);
400
+ this.flags & a.DISPOSED || (this._subscribers.length = 0, this.flags |= a.DISPOSED, this._value = void 0, this._pendingOldValue = void 0);
401
+ }
402
+ [(j = V, Symbol.dispose)]() {
403
+ this.dispose();
421
404
  }
422
405
  }
423
- function Lt(i, t = {}) {
424
- return new Dt(i, t.sync ?? !1);
406
+ function Ot(i, t = {}) {
407
+ return new dt(i, t.sync ?? !1);
425
408
  }
426
- class Ct {
409
+ class pt {
427
410
  /**
428
411
  * @param limit - Max unique arrays to hold (default: 50). Prevents the pool itself from consuming too much memory.
429
412
  * @param capacity - Max length of an array to accept (default: 256).
430
413
  * @param enableStats - Force-enable stats even in production (default: false).
431
414
  */
432
415
  constructor(t = 50, e = 256, s = !1) {
433
- this.limit = t, this.capacity = e, this.pool = [], this.stats = null, this.stats = u || s ? {
416
+ this.limit = t, this.capacity = e, this.pool = [], this.stats = null, this.stats = s ? {
434
417
  acquired: 0,
435
418
  released: 0,
436
419
  rejected: { frozen: 0, tooLarge: 0, poolFull: 0 }
@@ -490,61 +473,61 @@ class Ct {
490
473
  });
491
474
  }
492
475
  }
493
- const _ = Object.freeze(
476
+ const u = Object.freeze(
494
477
  []
495
- ), p = new Ct();
496
- function Ft(i) {
497
- return i !== null && typeof i == "object" && X in i;
478
+ ), f = new pt();
479
+ function bt(i) {
480
+ return i !== null && typeof i == "object" && V in i;
498
481
  }
499
- function Rt(i) {
500
- return i !== null && typeof i == "object" && ct in i;
482
+ function At(i) {
483
+ return i !== null && typeof i == "object" && it in i;
501
484
  }
502
- function xt(i) {
503
- return i !== null && typeof i == "object" && ut in i;
485
+ function Nt(i) {
486
+ return i !== null && typeof i == "object" && nt in i;
504
487
  }
505
- function lt(i) {
488
+ function rt(i) {
506
489
  return i !== null && typeof i == "object" && typeof i.then == "function";
507
490
  }
508
- var W, J, Z;
509
- const bt = 3, Tt = Number.MAX_SAFE_INTEGER - 1, { IDLE: L, DIRTY: b, PENDING: x, RESOLVED: g, REJECTED: A, HAS_ERROR: F, RECOMPUTING: y, DISPOSED: P, IS_COMPUTED: Ot } = st;
510
- function yt(i) {
511
- return i & g ? k.RESOLVED : i & x ? k.PENDING : i & A ? k.REJECTED : k.IDLE;
491
+ var q, $;
492
+ const { IDLE: N, DIRTY: S, PENDING: R, RESOLVED: d, REJECTED: O, HAS_ERROR: y, RECOMPUTING: T, DISPOSED: P, IS_COMPUTED: It } = K;
493
+ function St(i) {
494
+ return i & d ? A.RESOLVED : i & R ? A.PENDING : i & O ? A.REJECTED : A.IDLE;
512
495
  }
513
- class At extends (Z = ot, J = X, W = ct, Z) {
496
+ class gt extends et {
514
497
  constructor(t, e = {}) {
515
- if (typeof t != "function") throw new d(c.COMPUTED_MUST_BE_FUNCTION);
516
- super(), this[J] = !0, this[W] = !0, this._error = null, this._promiseId = 0, this._subscribers = [], this._links = _, this._asyncStartAggregateVersion = 0, this._asyncRetryCount = 0, this._trackEpoch = -1, this._trackLinks = _, this._trackCount = 0, this._value = void 0, this.flags = Ot | b | L, this._equal = e.equal ?? Object.is, this._fn = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : v, this._onError = e.onError ?? null;
498
+ if (typeof t != "function") throw new E(c.COMPUTED_MUST_BE_FUNCTION);
499
+ super(), this[$] = !0, this[q] = !0, this._error = null, this._promiseId = 0, this._subscribers = [], this._links = u, this._asyncStartAggregateVersion = 0, this._asyncRetryCount = 0, this._trackEpoch = -1, this._trackLinks = u, this._trackCount = 0, this._value = void 0, this.flags = It | S | N, this._equal = e.equal ?? Object.is, this._fn = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : U, this._onError = e.onError ?? null;
517
500
  const s = e.maxAsyncRetries;
518
- if (this._maxAsyncRetries = s != null && s >= 0 ? s : bt, D.attachDebugInfo(this, "computed", this.id), e.lazy === !1)
501
+ if (this._maxAsyncRetries = s != null && s >= 0 ? s : X.MAX_ASYNC_RETRIES, L.attachDebugInfo(this, "computed", this.id), e.lazy === !1)
519
502
  try {
520
503
  this._recompute();
521
504
  } catch {
522
505
  }
523
506
  }
524
507
  _track() {
525
- const t = a.current;
526
- t && nt(this, t, this._subscribers);
508
+ const t = _.current;
509
+ t && Z(this, t, this._subscribers);
527
510
  }
528
511
  get value() {
529
512
  this._track();
530
513
  const t = this.flags;
531
- if ((t & (g | b | L)) === g)
514
+ if ((t & (d | S | N)) === d)
532
515
  return this._value;
533
516
  if (t & P)
534
- throw new d(c.COMPUTED_DISPOSED);
535
- if (t & y) {
536
- if (this._defaultValue !== v) return this._defaultValue;
537
- throw new d(c.COMPUTED_CIRCULAR_DEPENDENCY);
517
+ throw new E(c.COMPUTED_DISPOSED);
518
+ if (t & T) {
519
+ if (this._defaultValue !== U) return this._defaultValue;
520
+ throw new E(c.COMPUTED_CIRCULAR_DEPENDENCY);
538
521
  }
539
- if (t & (b | L) && (this._recompute(), this.flags & g))
522
+ if (t & (S | N) && (this._recompute(), this.flags & d))
540
523
  return this._value;
541
- const e = this._defaultValue, s = e !== v;
542
- if (this.flags & x) {
524
+ const e = this._defaultValue, s = e !== U;
525
+ if (this.flags & R) {
543
526
  if (s) return e;
544
- throw new d(c.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
527
+ throw new E(c.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
545
528
  }
546
- if (this.flags & A) {
547
- if (this._error?.recoverable && s) return e;
529
+ if (this.flags & O) {
530
+ if (s) return e;
548
531
  throw this._error;
549
532
  }
550
533
  return this._value;
@@ -553,14 +536,14 @@ class At extends (Z = ot, J = X, W = ct, Z) {
553
536
  return this._value;
554
537
  }
555
538
  get state() {
556
- return this._track(), yt(this.flags);
539
+ return this._track(), St(this.flags);
557
540
  }
558
541
  get hasError() {
559
- if (this._track(), this.flags & (A | F)) return !0;
542
+ if (this._track(), this.flags & (O | y)) return !0;
560
543
  const t = this._links;
561
544
  for (let e = 0, s = t.length; e < s; e++) {
562
545
  const n = t[e]?.node;
563
- if (n && n.flags & F) return !0;
546
+ if (n && n.flags & y) return !0;
564
547
  }
565
548
  return !1;
566
549
  }
@@ -568,21 +551,19 @@ class At extends (Z = ot, J = X, W = ct, Z) {
568
551
  return !this.hasError;
569
552
  }
570
553
  get errors() {
571
- if (this._track(), !this.hasError) return at;
554
+ if (this._track(), !this.hasError) return ht;
572
555
  const t = [];
573
556
  this._error && t.push(this._error);
574
557
  const e = this._links;
575
558
  for (let s = 0, n = e.length; s < n; s++) {
576
559
  const r = e[s].node;
577
- if (r.flags & F) {
578
- const o = r;
579
- if (o.errors) {
580
- const h = o.errors;
581
- for (let f = 0; f < h.length; f++) {
582
- const C = h[f];
583
- C && t.indexOf(C) === -1 && t.push(C);
560
+ if (r.flags & y) {
561
+ const h = r.errors;
562
+ if (h.length > 0)
563
+ for (let I = 0; I < h.length; I++) {
564
+ const F = h[I];
565
+ F && t.indexOf(F) === -1 && t.push(F);
584
566
  }
585
- }
586
567
  }
587
568
  }
588
569
  return Object.freeze(t);
@@ -591,10 +572,10 @@ class At extends (Z = ot, J = X, W = ct, Z) {
591
572
  return this._track(), this._error;
592
573
  }
593
574
  get isPending() {
594
- return this._track(), (this.flags & x) !== 0;
575
+ return this._track(), (this.flags & R) !== 0;
595
576
  }
596
577
  get isResolved() {
597
- return this._track(), (this.flags & g) !== 0;
578
+ return this._track(), (this.flags & d) !== 0;
598
579
  }
599
580
  invalidate() {
600
581
  this._markDirty();
@@ -602,12 +583,15 @@ class At extends (Z = ot, J = X, W = ct, Z) {
602
583
  dispose() {
603
584
  if (this.flags & P) return;
604
585
  const t = this._links;
605
- if (t !== _) {
586
+ if (t !== u) {
606
587
  for (let e = 0, s = t.length; e < s; e++)
607
588
  t[e].unsub?.();
608
- p.release(t), this._links = _;
589
+ f.release(t), this._links = u;
609
590
  }
610
- this._subscribers.length = 0, this.flags = P | b | L, this._error = null, this._value = void 0;
591
+ this._subscribers.length = 0, this.flags = P | S | N, this._error = null, this._value = void 0;
592
+ }
593
+ [($ = V, q = it, Symbol.dispose)]() {
594
+ this.dispose();
611
595
  }
612
596
  addDependency(t) {
613
597
  if (t._lastSeenEpoch !== this._trackEpoch) {
@@ -615,40 +599,39 @@ class At extends (Z = ot, J = X, W = ct, Z) {
615
599
  const e = this._trackLinks[this._trackCount];
616
600
  e.node = t, e.version = t.version;
617
601
  } else
618
- this._trackLinks.push(new R(t, t.version));
602
+ this._trackLinks.push(new v(t, t.version));
619
603
  this._trackCount++;
620
604
  }
621
605
  }
622
606
  _recompute() {
623
- if (this.flags & y) return;
624
- this.flags |= y;
607
+ if (this.flags & T) return;
608
+ this.flags |= T;
625
609
  const t = this._links;
626
- this._trackEpoch = V(), this._trackLinks = p.acquire(), this._trackCount = 0;
610
+ this._trackEpoch = z(), this._trackLinks = f.acquire(), this._trackCount = 0;
627
611
  let e = !1;
628
612
  try {
629
- const s = a.run(this, this._fn);
630
- this._trackLinks.length = this._trackCount, j(this._trackLinks, t, this), this._links = this._trackLinks, e = !0, lt(s) ? this._handleAsyncComputation(s) : this._finalizeResolution(s);
613
+ const s = _.run(this, this._fn);
614
+ this._trackLinks.length = this._trackCount, G(this._trackLinks, t, this), this._links = this._trackLinks, e = !0, rt(s) ? this._handleAsyncComputation(s) : this._finalizeResolution(s);
631
615
  } catch (s) {
632
616
  if (!e)
633
617
  try {
634
- this._trackLinks.length = this._trackCount, j(this._trackLinks, t, this), this._links = this._trackLinks, e = !0;
635
- } catch (n) {
636
- u && console.warn("[atom-effect] _commitDeps failed during error recovery:", n);
618
+ this._trackLinks.length = this._trackCount, G(this._trackLinks, t, this), this._links = this._trackLinks, e = !0;
619
+ } catch {
637
620
  }
638
621
  this._handleError(s, c.COMPUTED_COMPUTATION_FAILED, !0);
639
622
  } finally {
640
- e && t !== _ ? p.release(t) : e || p.release(this._trackLinks), this._trackEpoch = -1, this._trackLinks = _, this._trackCount = 0, this.flags &= ~y;
623
+ e && t !== u ? f.release(t) : e || f.release(this._trackLinks), this._trackEpoch = -1, this._trackLinks = u, this._trackCount = 0, this.flags &= ~T;
641
624
  }
642
625
  }
643
626
  _handleAsyncComputation(t) {
644
- this.flags = (this.flags | x) & -217, this._notifySubscribers(void 0, void 0), this._asyncStartAggregateVersion = this._captureVersionSnapshot(), this._asyncRetryCount = 0, this._promiseId = (this._promiseId + 1) % Tt;
627
+ this.flags = (this.flags | R) & -217, this._notifySubscribers(void 0, void 0), this._asyncStartAggregateVersion = this._captureVersionSnapshot(), this._asyncRetryCount = 0, this._promiseId = (this._promiseId + 1) % X.MAX_PROMISE_ID;
645
628
  const e = this._promiseId;
646
629
  t.then(
647
630
  (s) => {
648
631
  if (e === this._promiseId) {
649
632
  if (this._captureVersionSnapshot() !== this._asyncStartAggregateVersion)
650
633
  return this._asyncRetryCount++ < this._maxAsyncRetries ? this._markDirty() : this._handleError(
651
- new d(
634
+ new E(
652
635
  `Async drift threshold exceeded after ${this._maxAsyncRetries} retries.`
653
636
  ),
654
637
  c.COMPUTED_ASYNC_COMPUTATION_FAILED
@@ -667,8 +650,8 @@ class At extends (Z = ot, J = X, W = ct, Z) {
667
650
  return t;
668
651
  }
669
652
  _handleError(t, e, s = !1) {
670
- const n = T(t, d, e);
671
- if (!s && !(this.flags & A) && (this.version = B(this.version)), this._error = n, this.flags = this.flags & -121 | A | F, this._onError)
653
+ const n = D(t, E, e);
654
+ if (!s && !(this.flags & O) && (this.version = w(this.version)), this._error = n, this.flags = this.flags & -121 | O | y, this._onError)
672
655
  try {
673
656
  this._onError(n);
674
657
  } catch (r) {
@@ -678,58 +661,59 @@ class At extends (Z = ot, J = X, W = ct, Z) {
678
661
  this._notifySubscribers(void 0, void 0);
679
662
  }
680
663
  _finalizeResolution(t) {
681
- (!(this.flags & g) || !this._equal(this._value, t)) && (this.version = B(this.version)), this._value = t, this._error = null, this.flags = (this.flags | g) & -697;
664
+ (!(this.flags & d) || !this._equal(this._value, t)) && (this.version = w(this.version)), this._value = t, this._error = null, this.flags = (this.flags | d) & -697;
682
665
  }
683
666
  execute() {
684
667
  this._markDirty();
685
668
  }
686
669
  /** @internal */
687
670
  _markDirty() {
688
- this.flags & (y | b) || (this.flags |= b, this._notifySubscribers(void 0, void 0));
671
+ this.flags & (T | S) || (this.flags |= S, this._notifySubscribers(void 0, void 0));
689
672
  }
690
673
  }
691
- function Ut(i, t = {}) {
692
- return new At(i, t);
674
+ function yt(i, t = {}) {
675
+ return new gt(i, t);
693
676
  }
694
- var tt, et;
695
- class Nt extends (et = rt, tt = ut, et) {
677
+ var W;
678
+ class Dt extends tt {
696
679
  constructor(t, e = {}) {
697
- super(), this[tt] = !0, this._cleanup = null, this._links = _, this._nextLinks = null, this._prevLinks = null, this._currentEpoch = -1, this._lastFlushEpoch = -1, this._executionsInEpoch = 0, this._executionCount = 0, this._windowStart = 0, this._windowCount = 0, this._execId = 0, this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? S.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? S.MAX_EXECUTIONS_PER_EFFECT, this._trackModifications = e.trackModifications ?? !1, D.attachDebugInfo(this, "effect", this.id);
680
+ super(), this[W] = !0, this._cleanup = null, this._links = u, this._nextLinks = null, this._prevLinks = u, this._currentEpoch = -1, this._lastFlushEpoch = -1, this._executionsInEpoch = 0, this._executionCount = 0, this._windowStart = 0, this._windowCount = 0, this._execId = 0, this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? p.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? p.MAX_EXECUTIONS_PER_EFFECT, this._sync ? (this._executeTask = void 0, this._notifyCallback = () => this.execute()) : (this._executeTask = () => this.execute(), this._notifyCallback = () => b.schedule(this._executeTask)), L.attachDebugInfo(this, "effect", this.id);
698
681
  }
699
682
  run() {
700
683
  if (this.flags & l.DISPOSED)
701
- throw new I(c.EFFECT_DISPOSED);
684
+ throw new g(c.EFFECT_DISPOSED);
702
685
  this.execute(!0);
703
686
  }
704
687
  dispose() {
705
- this.flags & l.DISPOSED || (this.flags |= l.DISPOSED, this._execCleanup(), this._unsubLinks(this._links), this._links !== _ && p.release(this._links), this._links = _, this._executeTask = void 0);
688
+ this.flags & l.DISPOSED || (this.flags |= l.DISPOSED, this._execCleanup(), this._unsubLinks(this._links), this._links !== u && f.release(this._links), this._links = u, this._prevLinks = u);
689
+ }
690
+ [(W = nt, Symbol.dispose)]() {
691
+ this.dispose();
706
692
  }
707
693
  addDependency(t) {
708
- if (!(this.flags & l.EXECUTING) || t._lastSeenEpoch === this._currentEpoch) return;
709
- t._lastSeenEpoch = this._currentEpoch;
710
- const e = this._nextLinks, s = this._prevLinks;
711
- if (s)
712
- for (let n = 0, r = s.length; n < r; n++) {
713
- const o = s[n];
714
- if (o && o.node === t && o.unsub) {
715
- e.push(new R(t, t.version, o.unsub)), o.unsub = void 0;
716
- return;
717
- }
694
+ if (!(this.flags & l.EXECUTING)) return;
695
+ const e = this._currentEpoch;
696
+ if (t._lastSeenEpoch === e) return;
697
+ t._lastSeenEpoch = e;
698
+ const s = this._nextLinks, n = this._prevLinks;
699
+ for (let r = 0, o = n.length; r < o; r++) {
700
+ const h = n[r];
701
+ if (h && h.node === t && h.unsub) {
702
+ h.version = t.version, s.push(h), n[r] = null;
703
+ return;
718
704
  }
705
+ }
719
706
  try {
720
- const n = t.subscribe(() => {
721
- if (this._trackModifications && this.flags & l.EXECUTING && (t._modifiedAtEpoch = this._currentEpoch), this._sync) return this.execute();
722
- this._executeTask ??= () => this.execute(), N.schedule(this._executeTask);
723
- });
724
- e.push(new R(t, t.version, n));
725
- } catch (n) {
726
- const r = T(n, I, c.EFFECT_EXECUTION_FAILED);
727
- if (console.error(r), this._onError)
707
+ const r = t.subscribe(this._notifyCallback);
708
+ s.push(new v(t, t.version, r));
709
+ } catch (r) {
710
+ const o = D(r, g, c.EFFECT_EXECUTION_FAILED);
711
+ if (console.error(o), this._onError)
728
712
  try {
729
- this._onError(r);
713
+ this._onError(o);
730
714
  } catch {
731
715
  }
732
- e.push(new R(t, t.version, void 0));
716
+ s.push(new v(t, t.version, void 0));
733
717
  }
734
718
  }
735
719
  /**
@@ -737,29 +721,17 @@ class Nt extends (et = rt, tt = ut, et) {
737
721
  */
738
722
  execute(t = !1) {
739
723
  if (this.flags & (l.DISPOSED | l.EXECUTING) || !t && this._links.length > 0 && !this._isDirty()) return;
740
- this._checkInfiniteLoops(), this.flags |= l.EXECUTING, this._execCleanup();
741
- const e = this._links;
742
- this._prevLinks = e !== _ ? e : null;
743
- const s = p.acquire();
744
- this._nextLinks = s, this._currentEpoch = V();
745
- let n = !1;
724
+ this._checkInfiniteLoops(), this.flags |= l.EXECUTING, this._execCleanup(), this._prevLinks = this._links;
725
+ const e = f.acquire();
726
+ this._nextLinks = e, this._currentEpoch = z();
727
+ let s = !1;
746
728
  try {
747
- const r = a.run(this, this._fn);
748
- if (this._links = s, n = !0, this._trackModifications && D.enabled) {
749
- const o = this._currentEpoch;
750
- for (let h = 0, f = s.length; h < f; h++) {
751
- const C = s[h].node;
752
- C._modifiedAtEpoch === o && D.warn(
753
- !0,
754
- `Effect is reading a dependency (${D.getDebugName(C) || "unknown"}) that it just modified. Infinite loop may occur`
755
- );
756
- }
757
- }
758
- lt(r) ? this._handleAsyncResult(r) : this._cleanup = typeof r == "function" ? r : null;
759
- } catch (r) {
760
- n = !0, this._handleExecutionError(r), this._cleanup = null;
729
+ const n = _.run(this, this._fn);
730
+ this._links = e, s = !0, rt(n) ? this._handleAsyncResult(n) : this._cleanup = typeof n == "function" ? n : null;
731
+ } catch (n) {
732
+ this._links = e, s = !0, this._handleExecutionError(n), this._cleanup = null;
761
733
  } finally {
762
- this._finalizeDependencies(n, e, s), this.flags &= -9;
734
+ this._finalizeDependencies(s, this._prevLinks, e), this.flags &= -9;
763
735
  }
764
736
  }
765
737
  _handleAsyncResult(t) {
@@ -781,37 +753,36 @@ class Nt extends (et = rt, tt = ut, et) {
781
753
  );
782
754
  }
783
755
  _finalizeDependencies(t, e, s) {
784
- if (this._nextLinks = null, t) {
785
- const n = this._prevLinks;
786
- if (n)
787
- for (let r = 0, o = n.length; r < o; r++)
788
- n[r]?.unsub?.();
789
- e !== _ && p.release(e);
756
+ if (this._nextLinks = null, this._prevLinks = u, t) {
757
+ for (let n = 0, r = e.length; n < r; n++) {
758
+ const o = e[n];
759
+ o && o.unsub?.();
760
+ }
761
+ e !== u && f.release(e);
790
762
  } else
791
- this._unsubLinks(s), p.release(s);
792
- this._prevLinks = null;
763
+ this._unsubLinks(s), f.release(s);
793
764
  }
794
765
  _unsubLinks(t) {
795
766
  for (let e = 0, s = t.length; e < s; e++)
796
767
  t[e]?.unsub?.();
797
768
  }
798
769
  _isDirty() {
799
- const t = this._links, e = a.current;
800
- a.current = null;
770
+ const t = this._links, e = _.current;
771
+ _.current = null;
801
772
  try {
802
773
  for (let s = 0, n = t.length; s < n; s++) {
803
774
  const r = t[s], o = r.node;
804
- if (o.flags & st.IS_COMPUTED)
775
+ if (o.flags & K.IS_COMPUTED)
805
776
  try {
806
777
  o.value;
807
778
  } catch {
808
- return u && console.warn(`[atom-effect] Dependency #${o.id} threw during dirty check`), !0;
779
+ return !0;
809
780
  }
810
781
  if (o.version !== r.version) return !0;
811
782
  }
812
783
  return !1;
813
784
  } finally {
814
- a.current = e;
785
+ _.current = e;
815
786
  }
816
787
  }
817
788
  _execCleanup() {
@@ -825,16 +796,8 @@ class Nt extends (et = rt, tt = ut, et) {
825
796
  }
826
797
  }
827
798
  _checkInfiniteLoops() {
828
- const t = It();
829
- if (this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), gt() > S.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, u && Number.isFinite(this._maxExecutions)) {
830
- const e = Date.now();
831
- if (e - this._windowStart >= 1e3)
832
- this._windowStart = e, this._windowCount = 1;
833
- else if (++this._windowCount > this._maxExecutions) {
834
- const s = new I(c.EFFECT_FREQUENCY_LIMIT_EXCEEDED);
835
- throw this.dispose(), this._handleExecutionError(s), s;
836
- }
837
- }
799
+ const t = at();
800
+ this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), ft() > p.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++;
838
801
  }
839
802
  get isDisposed() {
840
803
  return (this.flags & l.DISPOSED) !== 0;
@@ -846,58 +809,58 @@ class Nt extends (et = rt, tt = ut, et) {
846
809
  return (this.flags & l.EXECUTING) !== 0;
847
810
  }
848
811
  _throwInfiniteLoopError(t) {
849
- const e = new I(
812
+ const e = new g(
850
813
  `Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${H}`
851
814
  );
852
815
  throw this.dispose(), console.error(e), e;
853
816
  }
854
817
  _handleExecutionError(t, e = c.EFFECT_EXECUTION_FAILED) {
855
- const s = T(t, I, e);
818
+ const s = D(t, g, e);
856
819
  if (console.error(s), this._onError)
857
820
  try {
858
821
  this._onError(s);
859
822
  } catch (n) {
860
- console.error(T(n, I, c.CALLBACK_ERROR_IN_ERROR_HANDLER));
823
+ console.error(D(n, g, c.CALLBACK_ERROR_IN_ERROR_HANDLER));
861
824
  }
862
825
  }
863
826
  }
864
- function vt(i, t = {}) {
827
+ function Rt(i, t = {}) {
865
828
  if (typeof i != "function")
866
- throw new I(c.EFFECT_MUST_BE_FUNCTION);
867
- const e = new Nt(i, t);
829
+ throw new g(c.EFFECT_MUST_BE_FUNCTION);
830
+ const e = new Dt(i, t);
868
831
  return e.execute(), e;
869
832
  }
870
- function wt(i) {
833
+ function kt(i) {
871
834
  if (typeof i != "function")
872
835
  throw new TypeError("Batch callback must be a function");
873
- N.startBatch();
836
+ b.startBatch();
874
837
  try {
875
838
  return i();
876
839
  } finally {
877
- N.endBatch();
840
+ b.endBatch();
878
841
  }
879
842
  }
880
843
  export {
881
- X as ATOM_BRAND,
882
- k as AsyncState,
883
- O as AtomError,
884
- ct as COMPUTED_BRAND,
885
- d as ComputedError,
886
- _t as DEBUG_CONFIG,
887
- D as DEBUG_RUNTIME,
888
- ut as EFFECT_BRAND,
889
- I as EffectError,
890
- kt as POOL_CONFIG,
891
- S as SCHEDULER_CONFIG,
844
+ V as ATOM_BRAND,
845
+ A as AsyncState,
846
+ C as AtomError,
847
+ it as COMPUTED_BRAND,
848
+ E as ComputedError,
849
+ ot as DEBUG_CONFIG,
850
+ L as DEBUG_RUNTIME,
851
+ nt as EFFECT_BRAND,
852
+ g as EffectError,
853
+ Ct as POOL_CONFIG,
854
+ p as SCHEDULER_CONFIG,
892
855
  m as SchedulerError,
893
- Lt as atom,
894
- wt as batch,
895
- Ut as computed,
896
- vt as effect,
897
- Ft as isAtom,
898
- Rt as isComputed,
899
- xt as isEffect,
900
- N as scheduler,
901
- mt as untracked
856
+ Ot as atom,
857
+ kt as batch,
858
+ yt as computed,
859
+ Rt as effect,
860
+ bt as isAtom,
861
+ At as isComputed,
862
+ Nt as isEffect,
863
+ b as scheduler,
864
+ Tt as untracked
902
865
  };
903
866
  //# sourceMappingURL=index.mjs.map