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