@but212/atom-effect 0.21.3 → 0.22.0

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,12 +1,12 @@
1
- const A = {
1
+ const b = {
2
2
  IDLE: "idle",
3
3
  PENDING: "pending",
4
4
  RESOLVED: "resolved",
5
5
  REJECTED: "rejected"
6
- }, l = {
6
+ }, u = {
7
7
  DISPOSED: 1,
8
8
  EXECUTING: 8
9
- }, K = {
9
+ }, $ = {
10
10
  DISPOSED: 1,
11
11
  /** Marker bit: identifies this node as a computed. */
12
12
  IS_COMPUTED: 2,
@@ -21,11 +21,11 @@ const A = {
21
21
  DISPOSED: 1,
22
22
  SYNC: 8,
23
23
  NOTIFICATION_SCHEDULED: 16
24
- }, Ct = {
24
+ }, Dt = {
25
25
  MAX_SIZE: 1e3,
26
26
  WARMUP_SIZE: 100,
27
27
  ENABLE_STATS: !1
28
- }, p = {
28
+ }, d = {
29
29
  // Infinite loop protection
30
30
  MAX_EXECUTIONS_PER_SECOND: 1e3,
31
31
  MAX_EXECUTIONS_PER_EFFECT: 100,
@@ -35,58 +35,55 @@ const A = {
35
35
  MIN_FLUSH_ITERATIONS: 10,
36
36
  // Memory management
37
37
  BATCH_QUEUE_SHRINK_THRESHOLD: 1e3
38
- }, ot = {
38
+ }, nt = {
39
39
  WARN_INFINITE_LOOP: !0,
40
40
  EFFECT_FREQUENCY_WINDOW: 1e3
41
- }, X = {
41
+ }, H = {
42
42
  MAX_ASYNC_RETRIES: 3,
43
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 {
44
+ }, R = 1073741823, W = typeof process < "u" && process.env && !1 || !1, rt = Object.freeze([]);
45
+ class D extends Error {
46
46
  constructor(t, e = null, s = !0) {
47
47
  super(t), this.cause = e, this.recoverable = s, this.name = "AtomError";
48
48
  }
49
49
  }
50
- class E extends C {
50
+ class f extends D {
51
51
  constructor(t, e = null) {
52
52
  super(t, e, !0), this.name = "ComputedError";
53
53
  }
54
54
  }
55
- class g extends C {
55
+ class I extends D {
56
56
  constructor(t, e = null) {
57
57
  super(t, e, !1), this.name = "EffectError";
58
58
  }
59
59
  }
60
- class m extends C {
60
+ class m extends D {
61
61
  constructor(t, e = null) {
62
62
  super(t, e, !1), this.name = "SchedulerError";
63
63
  }
64
64
  }
65
- const ct = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), ut = /* @__PURE__ */ Symbol("AtomEffect.Type"), U = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), L = {
65
+ const ot = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), ht = /* @__PURE__ */ Symbol("AtomEffect.Type"), x = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), F = {
66
66
  // Dev mode flag
67
- enabled: J,
68
- warnInfiniteLoop: ot.WARN_INFINITE_LOOP,
67
+ enabled: W,
68
+ warnInfiniteLoop: nt.WARN_INFINITE_LOOP,
69
69
  warn(i, t) {
70
70
  },
71
71
  checkCircular(i, t) {
72
72
  if (i === t)
73
- throw new E("Direct circular dependency detected");
73
+ throw new f("Direct circular dependency detected");
74
74
  },
75
75
  attachDebugInfo(i, t, e) {
76
76
  },
77
- getDebugName: (i) => i?.[ct],
78
- getDebugType: (i) => i?.[ut]
77
+ getDebugName: (i) => i?.[ot],
78
+ getDebugType: (i) => i?.[ht]
79
79
  };
80
- let lt = 1;
81
- const _t = () => lt++;
82
- function Z(i, t, e) {
80
+ let ct = 1;
81
+ const ut = () => ct++;
82
+ function K(i, t, e) {
83
83
  if (typeof t == "function") {
84
84
  const n = t;
85
- for (let r = 0, o = e.length; r < o; r++) {
86
- const h = e[r];
87
- if (h && h.fn === n) return;
88
- }
89
- e.push(new M(n, void 0));
85
+ if (e.some((r) => r && r.fn === n)) return;
86
+ e.push(new P(n, void 0));
90
87
  return;
91
88
  }
92
89
  if ("addDependency" in t) {
@@ -94,38 +91,29 @@ function Z(i, t, e) {
94
91
  return;
95
92
  }
96
93
  const s = t;
97
- for (let n = 0, r = e.length; n < r; n++) {
98
- const o = e[n];
99
- if (o && o.sub === s) return;
100
- }
101
- e.push(new M(void 0, s));
94
+ e.some((n) => n && n.sub === s) || e.push(new P(void 0, s));
102
95
  }
103
- function G(i, t, e) {
96
+ function V(i, t, e) {
104
97
  const s = /* @__PURE__ */ new Map();
105
- for (let n = 0, r = t.length; n < r; n++) {
106
- const o = t[n];
107
- o?.unsub && s.set(o.node, o.unsub);
108
- }
109
- for (let n = 0, r = i.length; n < r; n++) {
110
- const o = i[n];
111
- if (!o) continue;
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));
114
- }
115
- for (const n of s.values())
116
- n();
98
+ t.forEach((n) => {
99
+ n?.unsub && s.set(n.node, n.unsub);
100
+ }), i.forEach((n) => {
101
+ if (!n) return;
102
+ const r = n.node, o = s.get(r);
103
+ o !== void 0 ? (n.unsub = o, s.delete(r)) : (F.checkCircular(r, e), n.unsub = r.subscribe(e));
104
+ }), s.forEach((n) => n());
117
105
  }
118
106
  class v {
119
107
  constructor(t, e, s = void 0) {
120
108
  this.node = t, this.version = e, this.unsub = s;
121
109
  }
122
110
  }
123
- class M {
111
+ class P {
124
112
  constructor(t, e) {
125
113
  this.fn = t, this.sub = e;
126
114
  }
127
115
  }
128
- const c = {
116
+ const h = {
129
117
  // Computed Errors
130
118
  COMPUTED_MUST_BE_FUNCTION: "Computed target must be a function",
131
119
  COMPUTED_ASYNC_PENDING_NO_DEFAULT: "Async computation pending with no default value",
@@ -150,41 +138,37 @@ const c = {
150
138
  SCHEDULER_CALLBACK_MUST_BE_FUNCTION: "Scheduler callback must be a function",
151
139
  SCHEDULER_END_BATCH_WITHOUT_START: "endBatch() called without matching startBatch(). Ignoring."
152
140
  };
153
- function D(i, t, e) {
154
- if (i instanceof C)
141
+ function S(i, t, e) {
142
+ if (i instanceof D)
155
143
  return i;
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}`;
157
- return new t(h, r);
144
+ const s = i instanceof Error, n = s ? i.message : String(i), r = s ? i : void 0, O = `${s ? i.constructor.name : "Unexpected error"} (${e}): ${n}`;
145
+ return new t(O, r);
158
146
  }
159
- class tt {
147
+ class J {
160
148
  constructor() {
161
- this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this.id = _t() & B;
149
+ this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this.id = ut() & R;
162
150
  }
163
151
  }
164
- class et extends tt {
152
+ class Z extends J {
165
153
  /**
166
154
  * Adds subscriber.
167
155
  */
168
156
  subscribe(t) {
169
157
  const e = typeof t == "function";
170
158
  if (!e && (!t || typeof t.execute != "function"))
171
- throw D(
159
+ throw S(
172
160
  new TypeError("Invalid subscriber"),
173
- C,
174
- c.ATOM_SUBSCRIBER_MUST_BE_FUNCTION
161
+ D,
162
+ h.ATOM_SUBSCRIBER_MUST_BE_FUNCTION
175
163
  );
176
- const s = this._subscribers;
177
- for (let r = 0, o = s.length; r < o; r++) {
178
- const h = s[r];
179
- if (h && (e ? h.fn === t : h.sub === t))
180
- return () => {
181
- };
182
- }
183
- const n = new M(
164
+ if (this._subscribers.some((r) => r ? e ? r.fn === t : r.sub === t : !1))
165
+ return () => {
166
+ };
167
+ const s = new P(
184
168
  e ? t : void 0,
185
169
  e ? void 0 : t
186
170
  );
187
- return s.push(n), () => this._unsubscribe(n);
171
+ return this._subscribers.push(s), () => this._unsubscribe(s);
188
172
  }
189
173
  _unsubscribe(t) {
190
174
  const e = this._subscribers, s = e.indexOf(t);
@@ -197,34 +181,32 @@ class et extends tt {
197
181
  }
198
182
  _notifySubscribers(t, e) {
199
183
  if (this._subscribers.length === 0) return;
200
- const s = this._subscribers.slice(0), n = s.length;
201
- for (let r = 0; r < n; r++) {
202
- const o = s[r];
203
- if (o)
184
+ this._subscribers.slice(0).forEach((n) => {
185
+ if (n)
204
186
  try {
205
- o.fn ? o.fn(t, e) : o.sub && o.sub.execute();
206
- } catch (h) {
207
- this._handleNotifyError(h);
187
+ n.fn ? n.fn(t, e) : n.sub && n.sub.execute();
188
+ } catch (r) {
189
+ this._handleNotifyError(r);
208
190
  }
209
- }
191
+ });
210
192
  }
211
193
  _handleNotifyError(t) {
212
- console.error(D(t, C, c.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
194
+ console.error(S(t, D, h.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
213
195
  }
214
196
  }
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);
197
+ let k = 0;
198
+ const w = () => (k = k + 1 & R || 1, k), M = (i) => i + 1 & R;
199
+ let B = 0, L = !1, tt = 0;
200
+ const _t = () => tt;
201
+ function X() {
202
+ return L ? !1 : (L = !0, tt = w(), B = 0, !0);
221
203
  }
222
- const Y = () => {
223
- k = !1;
224
- }, ft = () => k ? ++H : 0;
225
- class Et {
204
+ const G = () => {
205
+ L = !1;
206
+ }, at = () => L ? ++B : 0;
207
+ class lt {
226
208
  constructor() {
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);
209
+ 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 = d.MAX_FLUSH_ITERATIONS, this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
228
210
  }
229
211
  get phase() {
230
212
  return this._isProcessing || this._isFlushingSync ? 2 : this._isBatching ? 1 : 0;
@@ -259,30 +241,28 @@ class Et {
259
241
  _runLoop() {
260
242
  try {
261
243
  if (this._size === 0) return;
262
- const t = Q();
263
- this._drainQueue(), t && Y();
244
+ const t = X();
245
+ this._drainQueue(), t && G();
264
246
  } finally {
265
247
  this._isProcessing = !1, this._size > 0 && !this._isBatching && this._flush();
266
248
  }
267
249
  }
268
250
  _flushSync() {
269
251
  this._isFlushingSync = !0;
270
- const t = Q();
252
+ const t = X();
271
253
  try {
272
254
  this._mergeBatchQueue(), this._drainQueue();
273
255
  } finally {
274
- this._isFlushingSync = !1, t && Y();
256
+ this._isFlushingSync = !1, t && G();
275
257
  }
276
258
  }
277
259
  _mergeBatchQueue() {
278
260
  if (this._batchQueueSize === 0) return;
279
261
  const t = ++this._epoch, e = this._batchQueue, s = this._queueBuffer[this._bufferIndex];
280
262
  let n = this._size;
281
- for (let r = 0; r < this._batchQueueSize; r++) {
282
- const o = e[r];
283
- o._nextEpoch !== t && (o._nextEpoch = t, s[n++] = o);
284
- }
285
- this._size = n, this._batchQueueSize = 0, e.length > p.BATCH_QUEUE_SHRINK_THRESHOLD && (e.length = 0);
263
+ e.slice(0, this._batchQueueSize).forEach((r) => {
264
+ r._nextEpoch !== t && (r._nextEpoch = t, s[n++] = r);
265
+ }), this._size = n, this._batchQueueSize = 0, e.length > d.BATCH_QUEUE_SHRINK_THRESHOLD && (e.length = 0);
286
266
  }
287
267
  _drainQueue() {
288
268
  let t = 0;
@@ -309,7 +289,7 @@ class Et {
309
289
  const t = this._size + this._batchQueueSize;
310
290
  if (console.error(
311
291
  new m(
312
- c.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, t)
292
+ h.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, t)
313
293
  )
314
294
  ), this._size = 0, this._queueBuffer[this._bufferIndex].length = 0, this._batchQueueSize = 0, this.onOverflow)
315
295
  try {
@@ -324,14 +304,14 @@ class Et {
324
304
  this._batchDepth !== 0 && --this._batchDepth === 0 && (this._flushSync(), this._isBatching = !1);
325
305
  }
326
306
  setMaxFlushIterations(t) {
327
- if (t < p.MIN_FLUSH_ITERATIONS)
307
+ if (t < d.MIN_FLUSH_ITERATIONS)
328
308
  throw new m(
329
- `Max flush iterations must be at least ${p.MIN_FLUSH_ITERATIONS}`
309
+ `Max flush iterations must be at least ${d.MIN_FLUSH_ITERATIONS}`
330
310
  );
331
311
  this._maxFlushIterations = t;
332
312
  }
333
313
  }
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"), _ = {
314
+ const T = new lt(), z = /* @__PURE__ */ Symbol.for("atom-effect/atom"), et = /* @__PURE__ */ Symbol.for("atom-effect/computed"), st = /* @__PURE__ */ Symbol.for("atom-effect/effect"), _ = {
335
315
  /** Active listener. */
336
316
  current: null,
337
317
  /**
@@ -351,7 +331,7 @@ const b = new Et(), V = /* @__PURE__ */ Symbol.for("atom-effect/atom"), it = /*
351
331
  }
352
332
  }
353
333
  };
354
- function Tt(i) {
334
+ function Ct(i) {
355
335
  const t = _.current;
356
336
  if (t === null) return i();
357
337
  _.current = null;
@@ -361,26 +341,26 @@ function Tt(i) {
361
341
  _.current = t;
362
342
  }
363
343
  }
364
- var j;
365
- class dt extends et {
344
+ var Q;
345
+ class ft extends Z {
366
346
  constructor(t, e) {
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);
347
+ super(), this._pendingOldValue = void 0, this._notifyTask = () => this._flushNotifications(), this._subscribers = [], this[Q] = !0, this._value = t, e && (this.flags |= a.SYNC), F.attachDebugInfo(this, "atom", this.id);
368
348
  }
369
349
  get value() {
370
350
  const t = _.current;
371
- return t && Z(this, t, this._subscribers), this._value;
351
+ return t && K(this, t, this._subscribers), this._value;
372
352
  }
373
353
  set value(t) {
374
354
  const e = this._value;
375
355
  if (Object.is(e, t)) return;
376
- this._value = t, this.version = w(this.version);
356
+ this._value = t, this.version = M(this.version);
377
357
  const s = this.flags;
378
358
  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) {
359
+ if (this._pendingOldValue = e, this.flags = s | a.NOTIFICATION_SCHEDULED, s & a.SYNC && !T.isBatching) {
380
360
  this._flushNotifications();
381
361
  return;
382
362
  }
383
- b.schedule(this._notifyTask);
363
+ T.schedule(this._notifyTask);
384
364
  }
385
365
  }
386
366
  /**
@@ -399,14 +379,14 @@ class dt extends et {
399
379
  dispose() {
400
380
  this.flags & a.DISPOSED || (this._subscribers.length = 0, this.flags |= a.DISPOSED, this._value = void 0, this._pendingOldValue = void 0);
401
381
  }
402
- [(j = V, Symbol.dispose)]() {
382
+ [(Q = z, Symbol.dispose)]() {
403
383
  this.dispose();
404
384
  }
405
385
  }
406
- function Ot(i, t = {}) {
407
- return new dt(i, t.sync ?? !1);
386
+ function gt(i, t = {}) {
387
+ return new ft(i, t.sync ?? !1);
408
388
  }
409
- class pt {
389
+ class Et {
410
390
  /**
411
391
  * @param limit - Max unique arrays to hold (default: 50). Prevents the pool itself from consuming too much memory.
412
392
  * @param capacity - Max length of an array to accept (default: 256).
@@ -473,32 +453,32 @@ class pt {
473
453
  });
474
454
  }
475
455
  }
476
- const u = Object.freeze(
456
+ const c = Object.freeze(
477
457
  []
478
- ), f = new pt();
479
- function bt(i) {
480
- return i !== null && typeof i == "object" && V in i;
458
+ ), l = new Et();
459
+ function Tt(i) {
460
+ return i !== null && typeof i == "object" && z in i;
481
461
  }
482
- function At(i) {
483
- return i !== null && typeof i == "object" && it in i;
462
+ function Ot(i) {
463
+ return i !== null && typeof i == "object" && et in i;
484
464
  }
485
- function Nt(i) {
486
- return i !== null && typeof i == "object" && nt in i;
465
+ function bt(i) {
466
+ return i !== null && typeof i == "object" && st in i;
487
467
  }
488
- function rt(i) {
468
+ function it(i) {
489
469
  return i !== null && typeof i == "object" && typeof i.then == "function";
490
470
  }
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;
471
+ var Y, q;
472
+ const { IDLE: A, DIRTY: p, PENDING: y, RESOLVED: E, REJECTED: g, HAS_ERROR: N, RECOMPUTING: C, DISPOSED: U, IS_COMPUTED: dt } = $;
473
+ function pt(i) {
474
+ return i & E ? b.RESOLVED : i & y ? b.PENDING : i & g ? b.REJECTED : b.IDLE;
495
475
  }
496
- class gt extends et {
476
+ class It extends Z {
497
477
  constructor(t, e = {}) {
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;
478
+ if (typeof t != "function") throw new f(h.COMPUTED_MUST_BE_FUNCTION);
479
+ super(), this[q] = !0, this[Y] = !0, this._error = null, this._promiseId = 0, this._subscribers = [], this._links = c, this._asyncStartAggregateVersion = 0, this._asyncRetryCount = 0, this._trackEpoch = -1, this._trackLinks = c, this._trackCount = 0, this._value = void 0, this.flags = dt | p | A, this._equal = e.equal ?? Object.is, this._fn = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : x, this._onError = e.onError ?? null;
500
480
  const s = e.maxAsyncRetries;
501
- if (this._maxAsyncRetries = s != null && s >= 0 ? s : X.MAX_ASYNC_RETRIES, L.attachDebugInfo(this, "computed", this.id), e.lazy === !1)
481
+ if (this._maxAsyncRetries = (s ?? H.MAX_ASYNC_RETRIES) & R, F.attachDebugInfo(this, "computed", this.id), e.lazy === !1)
502
482
  try {
503
483
  this._recompute();
504
484
  } catch {
@@ -506,27 +486,27 @@ class gt extends et {
506
486
  }
507
487
  _track() {
508
488
  const t = _.current;
509
- t && Z(this, t, this._subscribers);
489
+ t && K(this, t, this._subscribers);
510
490
  }
511
491
  get value() {
512
492
  this._track();
513
493
  const t = this.flags;
514
- if ((t & (d | S | N)) === d)
494
+ if ((t & (E | p | A)) === E)
515
495
  return this._value;
516
- if (t & P)
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);
496
+ if (t & U)
497
+ throw new f(h.COMPUTED_DISPOSED);
498
+ if (t & C) {
499
+ if (this._defaultValue !== x) return this._defaultValue;
500
+ throw new f(h.COMPUTED_CIRCULAR_DEPENDENCY);
521
501
  }
522
- if (t & (S | N) && (this._recompute(), this.flags & d))
502
+ if (t & (p | A) && (this._recompute(), this.flags & E))
523
503
  return this._value;
524
- const e = this._defaultValue, s = e !== U;
525
- if (this.flags & R) {
504
+ const e = this._defaultValue, s = e !== x;
505
+ if (this.flags & y) {
526
506
  if (s) return e;
527
- throw new E(c.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
507
+ throw new f(h.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
528
508
  }
529
- if (this.flags & O) {
509
+ if (this.flags & g) {
530
510
  if (s) return e;
531
511
  throw this._error;
532
512
  }
@@ -536,61 +516,42 @@ class gt extends et {
536
516
  return this._value;
537
517
  }
538
518
  get state() {
539
- return this._track(), St(this.flags);
519
+ return this._track(), pt(this.flags);
540
520
  }
541
521
  get hasError() {
542
- if (this._track(), this.flags & (O | y)) return !0;
543
- const t = this._links;
544
- for (let e = 0, s = t.length; e < s; e++) {
545
- const n = t[e]?.node;
546
- if (n && n.flags & y) return !0;
547
- }
548
- return !1;
522
+ return this._track(), this.flags & (g | N) ? !0 : this._links.some((t) => t?.node?.flags & N);
549
523
  }
550
524
  get isValid() {
551
525
  return !this.hasError;
552
526
  }
553
527
  get errors() {
554
- if (this._track(), !this.hasError) return ht;
528
+ if (this._track(), !this.hasError) return rt;
555
529
  const t = [];
556
- this._error && t.push(this._error);
557
- const e = this._links;
558
- for (let s = 0, n = e.length; s < n; s++) {
559
- const r = e[s].node;
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);
566
- }
567
- }
568
- }
569
- return Object.freeze(t);
530
+ return this._error && t.push(this._error), this._links.forEach((s) => {
531
+ const n = s.node;
532
+ n.flags & N && n.errors.forEach((o) => {
533
+ o && !t.includes(o) && t.push(o);
534
+ });
535
+ }), Object.freeze(t);
570
536
  }
571
537
  get lastError() {
572
538
  return this._track(), this._error;
573
539
  }
574
540
  get isPending() {
575
- return this._track(), (this.flags & R) !== 0;
541
+ return this._track(), (this.flags & y) !== 0;
576
542
  }
577
543
  get isResolved() {
578
- return this._track(), (this.flags & d) !== 0;
544
+ return this._track(), (this.flags & E) !== 0;
579
545
  }
580
546
  invalidate() {
581
547
  this._markDirty();
582
548
  }
583
549
  dispose() {
584
- if (this.flags & P) return;
550
+ if (this.flags & U) return;
585
551
  const t = this._links;
586
- if (t !== u) {
587
- for (let e = 0, s = t.length; e < s; e++)
588
- t[e].unsub?.();
589
- f.release(t), this._links = u;
590
- }
591
- this._subscribers.length = 0, this.flags = P | S | N, this._error = null, this._value = void 0;
552
+ t !== c && (t.forEach((e) => e.unsub?.()), l.release(t), this._links = c), this._subscribers.length = 0, this.flags = U | p | A, this._error = null, this._value = void 0;
592
553
  }
593
- [($ = V, q = it, Symbol.dispose)]() {
554
+ [(q = z, Y = et, Symbol.dispose)]() {
594
555
  this.dispose();
595
556
  }
596
557
  addDependency(t) {
@@ -604,42 +565,42 @@ class gt extends et {
604
565
  }
605
566
  }
606
567
  _recompute() {
607
- if (this.flags & T) return;
608
- this.flags |= T;
568
+ if (this.flags & C) return;
569
+ this.flags |= C;
609
570
  const t = this._links;
610
- this._trackEpoch = z(), this._trackLinks = f.acquire(), this._trackCount = 0;
571
+ this._trackEpoch = w(), this._trackLinks = l.acquire(), this._trackCount = 0;
611
572
  let e = !1;
612
573
  try {
613
574
  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);
575
+ this._trackLinks.length = this._trackCount, V(this._trackLinks, t, this), this._links = this._trackLinks, e = !0, it(s) ? this._handleAsyncComputation(s) : this._finalizeResolution(s);
615
576
  } catch (s) {
616
577
  if (!e)
617
578
  try {
618
- this._trackLinks.length = this._trackCount, G(this._trackLinks, t, this), this._links = this._trackLinks, e = !0;
579
+ this._trackLinks.length = this._trackCount, V(this._trackLinks, t, this), this._links = this._trackLinks, e = !0;
619
580
  } catch {
620
581
  }
621
- this._handleError(s, c.COMPUTED_COMPUTATION_FAILED, !0);
582
+ this._handleError(s, h.COMPUTED_COMPUTATION_FAILED, !0);
622
583
  } finally {
623
- e && t !== u ? f.release(t) : e || f.release(this._trackLinks), this._trackEpoch = -1, this._trackLinks = u, this._trackCount = 0, this.flags &= ~T;
584
+ e && t !== c ? l.release(t) : e || l.release(this._trackLinks), this._trackEpoch = -1, this._trackLinks = c, this._trackCount = 0, this.flags &= ~C;
624
585
  }
625
586
  }
626
587
  _handleAsyncComputation(t) {
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;
588
+ this.flags = (this.flags | y) & -217, this._notifySubscribers(void 0, void 0), this._asyncStartAggregateVersion = this._captureVersionSnapshot(), this._asyncRetryCount = 0, this._promiseId = (this._promiseId + 1) % H.MAX_PROMISE_ID;
628
589
  const e = this._promiseId;
629
590
  t.then(
630
591
  (s) => {
631
592
  if (e === this._promiseId) {
632
593
  if (this._captureVersionSnapshot() !== this._asyncStartAggregateVersion)
633
594
  return this._asyncRetryCount++ < this._maxAsyncRetries ? this._markDirty() : this._handleError(
634
- new E(
595
+ new f(
635
596
  `Async drift threshold exceeded after ${this._maxAsyncRetries} retries.`
636
597
  ),
637
- c.COMPUTED_ASYNC_COMPUTATION_FAILED
598
+ h.COMPUTED_ASYNC_COMPUTATION_FAILED
638
599
  );
639
600
  this._finalizeResolution(s), this._notifySubscribers(s, void 0);
640
601
  }
641
602
  },
642
- (s) => e === this._promiseId && this._handleError(s, c.COMPUTED_ASYNC_COMPUTATION_FAILED)
603
+ (s) => e === this._promiseId && this._handleError(s, h.COMPUTED_ASYNC_COMPUTATION_FAILED)
643
604
  );
644
605
  }
645
606
  _captureVersionSnapshot() {
@@ -650,67 +611,65 @@ class gt extends et {
650
611
  return t;
651
612
  }
652
613
  _handleError(t, e, s = !1) {
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)
614
+ const n = S(t, f, e);
615
+ if (!s && !(this.flags & g) && (this.version = M(this.version)), this._error = n, this.flags = this.flags & -121 | g | N, this._onError)
655
616
  try {
656
617
  this._onError(n);
657
618
  } catch (r) {
658
- console.error(c.CALLBACK_ERROR_IN_ERROR_HANDLER, r);
619
+ console.error(h.CALLBACK_ERROR_IN_ERROR_HANDLER, r);
659
620
  }
660
621
  if (s) throw n;
661
622
  this._notifySubscribers(void 0, void 0);
662
623
  }
663
624
  _finalizeResolution(t) {
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;
625
+ (!(this.flags & E) || !this._equal(this._value, t)) && (this.version = M(this.version)), this._value = t, this._error = null, this.flags = (this.flags | E) & -697;
665
626
  }
666
627
  execute() {
667
628
  this._markDirty();
668
629
  }
669
630
  /** @internal */
670
631
  _markDirty() {
671
- this.flags & (T | S) || (this.flags |= S, this._notifySubscribers(void 0, void 0));
632
+ this.flags & (C | p) || (this.flags |= p, this._notifySubscribers(void 0, void 0));
672
633
  }
673
634
  }
674
- function yt(i, t = {}) {
675
- return new gt(i, t);
635
+ function At(i, t = {}) {
636
+ return new It(i, t);
676
637
  }
677
- var W;
678
- class Dt extends tt {
638
+ var j;
639
+ class St extends J {
679
640
  constructor(t, e = {}) {
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);
641
+ super(), this[j] = !0, this._cleanup = null, this._links = c, this._nextLinks = null, this._prevLinks = c, 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 ?? d.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? d.MAX_EXECUTIONS_PER_EFFECT, this._sync ? (this._executeTask = void 0, this._notifyCallback = () => this.execute()) : (this._executeTask = () => this.execute(), this._notifyCallback = () => T.schedule(this._executeTask)), F.attachDebugInfo(this, "effect", this.id);
681
642
  }
682
643
  run() {
683
- if (this.flags & l.DISPOSED)
684
- throw new g(c.EFFECT_DISPOSED);
644
+ if (this.flags & u.DISPOSED)
645
+ throw new I(h.EFFECT_DISPOSED);
685
646
  this.execute(!0);
686
647
  }
687
648
  dispose() {
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);
649
+ this.flags & u.DISPOSED || (this.flags |= u.DISPOSED, this._execCleanup(), this._unsubLinks(this._links), this._links !== c && l.release(this._links), this._links = c, this._prevLinks = c);
689
650
  }
690
- [(W = nt, Symbol.dispose)]() {
651
+ [(j = st, Symbol.dispose)]() {
691
652
  this.dispose();
692
653
  }
693
654
  addDependency(t) {
694
- if (!(this.flags & l.EXECUTING)) return;
655
+ if (!(this.flags & u.EXECUTING)) return;
695
656
  const e = this._currentEpoch;
696
657
  if (t._lastSeenEpoch === e) return;
697
658
  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;
704
- }
659
+ const s = this._nextLinks, n = this._prevLinks, r = n.findIndex((o) => o && o.node === t && o.unsub);
660
+ if (r !== -1) {
661
+ const o = n[r];
662
+ o.version = t.version, s.push(o), n[r] = null;
663
+ return;
705
664
  }
706
665
  try {
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)
666
+ const o = t.subscribe(this._notifyCallback);
667
+ s.push(new v(t, t.version, o));
668
+ } catch (o) {
669
+ const O = S(o, I, h.EFFECT_EXECUTION_FAILED);
670
+ if (console.error(O), this._onError)
712
671
  try {
713
- this._onError(o);
672
+ this._onError(O);
714
673
  } catch {
715
674
  }
716
675
  s.push(new v(t, t.version, void 0));
@@ -720,14 +679,14 @@ class Dt extends tt {
720
679
  * Executes effect with tracking.
721
680
  */
722
681
  execute(t = !1) {
723
- if (this.flags & (l.DISPOSED | l.EXECUTING) || !t && this._links.length > 0 && !this._isDirty()) return;
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();
682
+ if (this.flags & (u.DISPOSED | u.EXECUTING) || !t && this._links.length > 0 && !this._isDirty()) return;
683
+ this._checkInfiniteLoops(), this.flags |= u.EXECUTING, this._execCleanup(), this._prevLinks = this._links;
684
+ const e = l.acquire();
685
+ this._nextLinks = e, this._currentEpoch = w();
727
686
  let s = !1;
728
687
  try {
729
688
  const n = _.run(this, this._fn);
730
- this._links = e, s = !0, rt(n) ? this._handleAsyncResult(n) : this._cleanup = typeof n == "function" ? n : null;
689
+ this._links = e, s = !0, it(n) ? this._handleAsyncResult(n) : this._cleanup = typeof n == "function" ? n : null;
731
690
  } catch (n) {
732
691
  this._links = e, s = !0, this._handleExecutionError(n), this._cleanup = null;
733
692
  } finally {
@@ -738,12 +697,12 @@ class Dt extends tt {
738
697
  const e = ++this._execId;
739
698
  t.then(
740
699
  (s) => {
741
- if (e !== this._execId || this.flags & l.DISPOSED) {
700
+ if (e !== this._execId || this.flags & u.DISPOSED) {
742
701
  if (typeof s == "function")
743
702
  try {
744
703
  s();
745
704
  } catch (n) {
746
- this._handleExecutionError(n, c.EFFECT_CLEANUP_FAILED);
705
+ this._handleExecutionError(n, h.EFFECT_CLEANUP_FAILED);
747
706
  }
748
707
  return;
749
708
  }
@@ -753,34 +712,25 @@ class Dt extends tt {
753
712
  );
754
713
  }
755
714
  _finalizeDependencies(t, e, s) {
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);
762
- } else
763
- this._unsubLinks(s), f.release(s);
715
+ this._nextLinks = null, this._prevLinks = c, t ? (e.forEach((n) => n?.unsub?.()), e !== c && l.release(e)) : (this._unsubLinks(s), l.release(s));
764
716
  }
765
717
  _unsubLinks(t) {
766
- for (let e = 0, s = t.length; e < s; e++)
767
- t[e]?.unsub?.();
718
+ t.forEach((e) => e?.unsub?.());
768
719
  }
769
720
  _isDirty() {
770
721
  const t = this._links, e = _.current;
771
722
  _.current = null;
772
723
  try {
773
- for (let s = 0, n = t.length; s < n; s++) {
774
- const r = t[s], o = r.node;
775
- if (o.flags & K.IS_COMPUTED)
724
+ return t.some((s) => {
725
+ const n = s.node;
726
+ if (n.flags & $.IS_COMPUTED)
776
727
  try {
777
- o.value;
728
+ n.value;
778
729
  } catch {
779
730
  return !0;
780
731
  }
781
- if (o.version !== r.version) return !0;
782
- }
783
- return !1;
732
+ return n.version !== s.version;
733
+ });
784
734
  } finally {
785
735
  _.current = e;
786
736
  }
@@ -790,77 +740,77 @@ class Dt extends tt {
790
740
  try {
791
741
  this._cleanup();
792
742
  } catch (t) {
793
- this._handleExecutionError(t, c.EFFECT_CLEANUP_FAILED);
743
+ this._handleExecutionError(t, h.EFFECT_CLEANUP_FAILED);
794
744
  }
795
745
  this._cleanup = null;
796
746
  }
797
747
  }
798
748
  _checkInfiniteLoops() {
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++;
749
+ const t = _t();
750
+ this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), at() > d.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++;
801
751
  }
802
752
  get isDisposed() {
803
- return (this.flags & l.DISPOSED) !== 0;
753
+ return (this.flags & u.DISPOSED) !== 0;
804
754
  }
805
755
  get executionCount() {
806
756
  return this._executionCount;
807
757
  }
808
758
  get isExecuting() {
809
- return (this.flags & l.EXECUTING) !== 0;
759
+ return (this.flags & u.EXECUTING) !== 0;
810
760
  }
811
761
  _throwInfiniteLoopError(t) {
812
- const e = new g(
813
- `Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${H}`
762
+ const e = new I(
763
+ `Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${B}`
814
764
  );
815
765
  throw this.dispose(), console.error(e), e;
816
766
  }
817
- _handleExecutionError(t, e = c.EFFECT_EXECUTION_FAILED) {
818
- const s = D(t, g, e);
767
+ _handleExecutionError(t, e = h.EFFECT_EXECUTION_FAILED) {
768
+ const s = S(t, I, e);
819
769
  if (console.error(s), this._onError)
820
770
  try {
821
771
  this._onError(s);
822
772
  } catch (n) {
823
- console.error(D(n, g, c.CALLBACK_ERROR_IN_ERROR_HANDLER));
773
+ console.error(S(n, I, h.CALLBACK_ERROR_IN_ERROR_HANDLER));
824
774
  }
825
775
  }
826
776
  }
827
- function Rt(i, t = {}) {
777
+ function Nt(i, t = {}) {
828
778
  if (typeof i != "function")
829
- throw new g(c.EFFECT_MUST_BE_FUNCTION);
830
- const e = new Dt(i, t);
779
+ throw new I(h.EFFECT_MUST_BE_FUNCTION);
780
+ const e = new St(i, t);
831
781
  return e.execute(), e;
832
782
  }
833
- function kt(i) {
783
+ function yt(i) {
834
784
  if (typeof i != "function")
835
785
  throw new TypeError("Batch callback must be a function");
836
- b.startBatch();
786
+ T.startBatch();
837
787
  try {
838
788
  return i();
839
789
  } finally {
840
- b.endBatch();
790
+ T.endBatch();
841
791
  }
842
792
  }
843
793
  export {
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,
794
+ z as ATOM_BRAND,
795
+ b as AsyncState,
796
+ D as AtomError,
797
+ et as COMPUTED_BRAND,
798
+ f as ComputedError,
799
+ nt as DEBUG_CONFIG,
800
+ F as DEBUG_RUNTIME,
801
+ st as EFFECT_BRAND,
802
+ I as EffectError,
803
+ Dt as POOL_CONFIG,
804
+ d as SCHEDULER_CONFIG,
855
805
  m as SchedulerError,
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
806
+ gt as atom,
807
+ yt as batch,
808
+ At as computed,
809
+ Nt as effect,
810
+ Tt as isAtom,
811
+ Ot as isComputed,
812
+ bt as isEffect,
813
+ T as scheduler,
814
+ Ct as untracked
865
815
  };
866
816
  //# sourceMappingURL=index.mjs.map