@but212/atom-effect 0.21.3 → 0.22.1

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
+ }, rt = {
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
+ }, L = 1073741823, W = typeof process < "u" && process.env && !1 || !1, ot = 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 ht = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), ct = /* @__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: rt.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?.[ht],
78
+ getDebugType: (i) => i?.[ct]
79
79
  };
80
- let lt = 1;
81
- const _t = () => lt++;
82
- function Z(i, t, e) {
80
+ let ut = 1;
81
+ const _t = () => ut++;
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 = _t() & L;
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 & L || 1, k), M = (i) => i + 1 & L;
199
+ let B = 0, R = !1, tt = 0;
200
+ const et = () => tt;
201
+ function X() {
202
+ return R ? !1 : (R = !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
+ R = !1;
206
+ }, at = () => R ? ++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"), st = /* @__PURE__ */ Symbol.for("atom-effect/computed"), it = /* @__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" && st 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" && it in i;
487
467
  }
488
- function rt(i) {
468
+ function nt(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: N, RESOLVED: E, REJECTED: g, HAS_ERROR: y, RECOMPUTING: C, DISPOSED: U, IS_COMPUTED: dt } = $;
473
+ function pt(i) {
474
+ return i & E ? b.RESOLVED : i & N ? 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._lastDriftEpoch = -1, 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) & L, 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 & N) {
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 | y) ? !0 : this._links.some((t) => t?.node?.flags & y);
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 ot;
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 & y && 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 & N) !== 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 = st, Symbol.dispose)]() {
594
555
  this.dispose();
595
556
  }
596
557
  addDependency(t) {
@@ -604,42 +565,44 @@ 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, nt(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 | N) & -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
- if (this._captureVersionSnapshot() !== this._asyncStartAggregateVersion)
633
- return this._asyncRetryCount++ < this._maxAsyncRetries ? this._markDirty() : this._handleError(
634
- new E(
593
+ if (this._captureVersionSnapshot() !== this._asyncStartAggregateVersion) {
594
+ const n = et();
595
+ return this._lastDriftEpoch !== n && (this._lastDriftEpoch = n, this._asyncRetryCount = 0), this._asyncRetryCount++ < this._maxAsyncRetries ? this._markDirty() : this._handleError(
596
+ new f(
635
597
  `Async drift threshold exceeded after ${this._maxAsyncRetries} retries.`
636
598
  ),
637
- c.COMPUTED_ASYNC_COMPUTATION_FAILED
599
+ h.COMPUTED_ASYNC_COMPUTATION_FAILED
638
600
  );
601
+ }
639
602
  this._finalizeResolution(s), this._notifySubscribers(s, void 0);
640
603
  }
641
604
  },
642
- (s) => e === this._promiseId && this._handleError(s, c.COMPUTED_ASYNC_COMPUTATION_FAILED)
605
+ (s) => e === this._promiseId && this._handleError(s, h.COMPUTED_ASYNC_COMPUTATION_FAILED)
643
606
  );
644
607
  }
645
608
  _captureVersionSnapshot() {
@@ -650,67 +613,65 @@ class gt extends et {
650
613
  return t;
651
614
  }
652
615
  _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)
616
+ const n = S(t, f, e);
617
+ if (!s && !(this.flags & g) && (this.version = M(this.version)), this._error = n, this.flags = this.flags & -121 | g | y, this._onError)
655
618
  try {
656
619
  this._onError(n);
657
620
  } catch (r) {
658
- console.error(c.CALLBACK_ERROR_IN_ERROR_HANDLER, r);
621
+ console.error(h.CALLBACK_ERROR_IN_ERROR_HANDLER, r);
659
622
  }
660
623
  if (s) throw n;
661
624
  this._notifySubscribers(void 0, void 0);
662
625
  }
663
626
  _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;
627
+ (!(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
628
  }
666
629
  execute() {
667
630
  this._markDirty();
668
631
  }
669
632
  /** @internal */
670
633
  _markDirty() {
671
- this.flags & (T | S) || (this.flags |= S, this._notifySubscribers(void 0, void 0));
634
+ this.flags & (C | p) || (this.flags |= p, this._notifySubscribers(void 0, void 0));
672
635
  }
673
636
  }
674
- function yt(i, t = {}) {
675
- return new gt(i, t);
637
+ function At(i, t = {}) {
638
+ return new It(i, t);
676
639
  }
677
- var W;
678
- class Dt extends tt {
640
+ var j;
641
+ class St extends J {
679
642
  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);
643
+ 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
644
  }
682
645
  run() {
683
- if (this.flags & l.DISPOSED)
684
- throw new g(c.EFFECT_DISPOSED);
646
+ if (this.flags & u.DISPOSED)
647
+ throw new I(h.EFFECT_DISPOSED);
685
648
  this.execute(!0);
686
649
  }
687
650
  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);
651
+ 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
652
  }
690
- [(W = nt, Symbol.dispose)]() {
653
+ [(j = it, Symbol.dispose)]() {
691
654
  this.dispose();
692
655
  }
693
656
  addDependency(t) {
694
- if (!(this.flags & l.EXECUTING)) return;
657
+ if (!(this.flags & u.EXECUTING)) return;
695
658
  const e = this._currentEpoch;
696
659
  if (t._lastSeenEpoch === e) return;
697
660
  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
- }
661
+ const s = this._nextLinks, n = this._prevLinks, r = n.findIndex((o) => o && o.node === t && o.unsub);
662
+ if (r !== -1) {
663
+ const o = n[r];
664
+ o.version = t.version, s.push(o), n[r] = null;
665
+ return;
705
666
  }
706
667
  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)
668
+ const o = t.subscribe(this._notifyCallback);
669
+ s.push(new v(t, t.version, o));
670
+ } catch (o) {
671
+ const O = S(o, I, h.EFFECT_EXECUTION_FAILED);
672
+ if (console.error(O), this._onError)
712
673
  try {
713
- this._onError(o);
674
+ this._onError(O);
714
675
  } catch {
715
676
  }
716
677
  s.push(new v(t, t.version, void 0));
@@ -720,14 +681,14 @@ class Dt extends tt {
720
681
  * Executes effect with tracking.
721
682
  */
722
683
  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();
684
+ if (this.flags & (u.DISPOSED | u.EXECUTING) || !t && this._links.length > 0 && !this._isDirty()) return;
685
+ this._checkInfiniteLoops(), this.flags |= u.EXECUTING, this._execCleanup(), this._prevLinks = this._links;
686
+ const e = l.acquire();
687
+ this._nextLinks = e, this._currentEpoch = w();
727
688
  let s = !1;
728
689
  try {
729
690
  const n = _.run(this, this._fn);
730
- this._links = e, s = !0, rt(n) ? this._handleAsyncResult(n) : this._cleanup = typeof n == "function" ? n : null;
691
+ this._links = e, s = !0, nt(n) ? this._handleAsyncResult(n) : this._cleanup = typeof n == "function" ? n : null;
731
692
  } catch (n) {
732
693
  this._links = e, s = !0, this._handleExecutionError(n), this._cleanup = null;
733
694
  } finally {
@@ -738,12 +699,12 @@ class Dt extends tt {
738
699
  const e = ++this._execId;
739
700
  t.then(
740
701
  (s) => {
741
- if (e !== this._execId || this.flags & l.DISPOSED) {
702
+ if (e !== this._execId || this.flags & u.DISPOSED) {
742
703
  if (typeof s == "function")
743
704
  try {
744
705
  s();
745
706
  } catch (n) {
746
- this._handleExecutionError(n, c.EFFECT_CLEANUP_FAILED);
707
+ this._handleExecutionError(n, h.EFFECT_CLEANUP_FAILED);
747
708
  }
748
709
  return;
749
710
  }
@@ -753,34 +714,25 @@ class Dt extends tt {
753
714
  );
754
715
  }
755
716
  _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);
717
+ this._nextLinks = null, this._prevLinks = c, t ? (e.forEach((n) => n?.unsub?.()), e !== c && l.release(e)) : (this._unsubLinks(s), l.release(s));
764
718
  }
765
719
  _unsubLinks(t) {
766
- for (let e = 0, s = t.length; e < s; e++)
767
- t[e]?.unsub?.();
720
+ t.forEach((e) => e?.unsub?.());
768
721
  }
769
722
  _isDirty() {
770
723
  const t = this._links, e = _.current;
771
724
  _.current = null;
772
725
  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)
726
+ return t.some((s) => {
727
+ const n = s.node;
728
+ if (n.flags & $.IS_COMPUTED)
776
729
  try {
777
- o.value;
730
+ n.value;
778
731
  } catch {
779
732
  return !0;
780
733
  }
781
- if (o.version !== r.version) return !0;
782
- }
783
- return !1;
734
+ return n.version !== s.version;
735
+ });
784
736
  } finally {
785
737
  _.current = e;
786
738
  }
@@ -790,77 +742,77 @@ class Dt extends tt {
790
742
  try {
791
743
  this._cleanup();
792
744
  } catch (t) {
793
- this._handleExecutionError(t, c.EFFECT_CLEANUP_FAILED);
745
+ this._handleExecutionError(t, h.EFFECT_CLEANUP_FAILED);
794
746
  }
795
747
  this._cleanup = null;
796
748
  }
797
749
  }
798
750
  _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++;
751
+ const t = et();
752
+ 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
753
  }
802
754
  get isDisposed() {
803
- return (this.flags & l.DISPOSED) !== 0;
755
+ return (this.flags & u.DISPOSED) !== 0;
804
756
  }
805
757
  get executionCount() {
806
758
  return this._executionCount;
807
759
  }
808
760
  get isExecuting() {
809
- return (this.flags & l.EXECUTING) !== 0;
761
+ return (this.flags & u.EXECUTING) !== 0;
810
762
  }
811
763
  _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}`
764
+ const e = new I(
765
+ `Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${B}`
814
766
  );
815
767
  throw this.dispose(), console.error(e), e;
816
768
  }
817
- _handleExecutionError(t, e = c.EFFECT_EXECUTION_FAILED) {
818
- const s = D(t, g, e);
769
+ _handleExecutionError(t, e = h.EFFECT_EXECUTION_FAILED) {
770
+ const s = S(t, I, e);
819
771
  if (console.error(s), this._onError)
820
772
  try {
821
773
  this._onError(s);
822
774
  } catch (n) {
823
- console.error(D(n, g, c.CALLBACK_ERROR_IN_ERROR_HANDLER));
775
+ console.error(S(n, I, h.CALLBACK_ERROR_IN_ERROR_HANDLER));
824
776
  }
825
777
  }
826
778
  }
827
- function Rt(i, t = {}) {
779
+ function yt(i, t = {}) {
828
780
  if (typeof i != "function")
829
- throw new g(c.EFFECT_MUST_BE_FUNCTION);
830
- const e = new Dt(i, t);
781
+ throw new I(h.EFFECT_MUST_BE_FUNCTION);
782
+ const e = new St(i, t);
831
783
  return e.execute(), e;
832
784
  }
833
- function kt(i) {
785
+ function Nt(i) {
834
786
  if (typeof i != "function")
835
787
  throw new TypeError("Batch callback must be a function");
836
- b.startBatch();
788
+ T.startBatch();
837
789
  try {
838
790
  return i();
839
791
  } finally {
840
- b.endBatch();
792
+ T.endBatch();
841
793
  }
842
794
  }
843
795
  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,
796
+ z as ATOM_BRAND,
797
+ b as AsyncState,
798
+ D as AtomError,
799
+ st as COMPUTED_BRAND,
800
+ f as ComputedError,
801
+ rt as DEBUG_CONFIG,
802
+ F as DEBUG_RUNTIME,
803
+ it as EFFECT_BRAND,
804
+ I as EffectError,
805
+ Dt as POOL_CONFIG,
806
+ d as SCHEDULER_CONFIG,
855
807
  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
808
+ gt as atom,
809
+ Nt as batch,
810
+ At as computed,
811
+ yt as effect,
812
+ Tt as isAtom,
813
+ Ot as isComputed,
814
+ bt as isEffect,
815
+ T as scheduler,
816
+ Ct as untracked
865
817
  };
866
818
  //# sourceMappingURL=index.mjs.map