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