@but212/atom-effect 0.31.0 → 0.32.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.
Files changed (42) hide show
  1. package/README.md +21 -11
  2. package/dist/atom-effect.min.js +1 -1
  3. package/dist/atom-effect.min.js.map +1 -1
  4. package/dist/constants.d.ts +157 -0
  5. package/dist/constants.d.ts.map +1 -0
  6. package/dist/core/atom.d.ts +19 -0
  7. package/dist/core/atom.d.ts.map +1 -0
  8. package/dist/core/base.d.ts +136 -0
  9. package/dist/core/base.d.ts.map +1 -0
  10. package/dist/core/buffers.d.ts +113 -0
  11. package/dist/core/buffers.d.ts.map +1 -0
  12. package/dist/core/computed.d.ts +84 -0
  13. package/dist/core/computed.d.ts.map +1 -0
  14. package/dist/core/effect.d.ts +52 -0
  15. package/dist/core/effect.d.ts.map +1 -0
  16. package/dist/core/index.d.ts +8 -0
  17. package/dist/core/index.d.ts.map +1 -0
  18. package/dist/core/lens.d.ts +102 -0
  19. package/dist/core/lens.d.ts.map +1 -0
  20. package/dist/core/scheduler.d.ts +145 -0
  21. package/dist/core/scheduler.d.ts.map +1 -0
  22. package/dist/core/tracking.d.ts +123 -0
  23. package/dist/core/tracking.d.ts.map +1 -0
  24. package/dist/errors.d.ts +163 -0
  25. package/dist/errors.d.ts.map +1 -0
  26. package/dist/index.cjs +1 -1
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.ts +8 -488
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.mjs +1053 -937
  31. package/dist/index.mjs.map +1 -1
  32. package/dist/symbols.d.ts +30 -0
  33. package/dist/symbols.d.ts.map +1 -0
  34. package/dist/types.d.ts +259 -0
  35. package/dist/types.d.ts.map +1 -0
  36. package/dist/utils/debug.d.ts +20 -0
  37. package/dist/utils/debug.d.ts.map +1 -0
  38. package/dist/utils/index.d.ts +13 -0
  39. package/dist/utils/index.d.ts.map +1 -0
  40. package/dist/utils/type-guards.d.ts +81 -0
  41. package/dist/utils/type-guards.d.ts.map +1 -0
  42. package/package.json +14 -13
package/dist/index.mjs CHANGED
@@ -1,1250 +1,1366 @@
1
- var h = {
2
- DISPOSED: 1,
3
- IS_COMPUTED: 2,
4
- DIRTY: 256,
5
- RECOMPUTING: 512,
6
- HAS_ERROR: 1024,
7
- FORCE_COMPUTE: 2048,
8
- IDLE: 65536,
9
- PENDING: 131072,
10
- RESOLVED: 262144,
11
- REJECTED: 524288,
12
- ATOM_SYNC: 16777216,
13
- ATOM_NOTIFICATION_SCHEDULED: 33554432,
14
- EFFECT_EXECUTING: 268435456
15
- }, yt = Object.freeze({
16
- ASYNC_STATE: h.IDLE | h.PENDING | h.RESOLVED | h.REJECTED,
17
- COMPUTED_DIRTY_MASK: h.DIRTY | h.RECOMPUTING | h.FORCE_COMPUTE
18
- }), P = Object.freeze({
1
+ var E = {
2
+ CORE: 0,
3
+ COMPUTED: 8,
4
+ ASYNC: 16,
5
+ PRIMITIVE: 24
6
+ }, c = {
7
+ DISPOSED: 1 << E.CORE + 0,
8
+ IS_COMPUTED: 1 << E.CORE + 1,
9
+ DIRTY: 1 << E.COMPUTED + 0,
10
+ RECOMPUTING: 1 << E.COMPUTED + 1,
11
+ HAS_ERROR: 1 << E.COMPUTED + 2,
12
+ FORCE_COMPUTE: 1 << E.COMPUTED + 3,
13
+ IDLE: 1 << E.ASYNC + 0,
14
+ PENDING: 1 << E.ASYNC + 1,
15
+ RESOLVED: 1 << E.ASYNC + 2,
16
+ REJECTED: 1 << E.ASYNC + 3,
17
+ ATOM_SYNC: 1 << E.PRIMITIVE + 0,
18
+ ATOM_NOTIFICATION_SCHEDULED: 1 << E.PRIMITIVE + 1,
19
+ EFFECT_EXECUTING: 1 << E.PRIMITIVE + 4
20
+ }, ke = Object.freeze({
21
+ ASYNC_STATE: c.IDLE | c.PENDING | c.RESOLVED | c.REJECTED,
22
+ COMPUTED_DIRTY_MASK: c.DIRTY | c.RECOMPUTING | c.FORCE_COMPUTE
23
+ }), H = Object.freeze({
19
24
  IDLE: "idle",
20
25
  PENDING: "pending",
21
26
  RESOLVED: "resolved",
22
27
  REJECTED: "rejected"
23
- }), S = Object.freeze({
24
- DISPOSED: h.DISPOSED,
25
- EXECUTING: h.EFFECT_EXECUTING
26
- }), $ = Object.freeze({
27
- DISPOSED: h.DISPOSED,
28
- IS_COMPUTED: h.IS_COMPUTED,
29
- DIRTY: h.DIRTY,
30
- IDLE: h.IDLE,
31
- PENDING: h.PENDING,
32
- RESOLVED: h.RESOLVED,
33
- REJECTED: h.REJECTED,
34
- RECOMPUTING: h.RECOMPUTING,
35
- HAS_ERROR: h.HAS_ERROR,
36
- FORCE_COMPUTE: h.FORCE_COMPUTE
37
- }), C = Object.freeze({
38
- DISPOSED: h.DISPOSED,
39
- SYNC: h.ATOM_SYNC,
40
- NOTIFICATION_SCHEDULED: h.ATOM_NOTIFICATION_SCHEDULED
28
+ }), v = Object.freeze({
29
+ DISPOSED: c.DISPOSED,
30
+ EXECUTING: c.EFFECT_EXECUTING
31
+ }), y = Object.freeze({
32
+ DISPOSED: c.DISPOSED,
33
+ IS_COMPUTED: c.IS_COMPUTED,
34
+ DIRTY: c.DIRTY,
35
+ IDLE: c.IDLE,
36
+ PENDING: c.PENDING,
37
+ RESOLVED: c.RESOLVED,
38
+ REJECTED: c.REJECTED,
39
+ RECOMPUTING: c.RECOMPUTING,
40
+ HAS_ERROR: c.HAS_ERROR,
41
+ FORCE_COMPUTE: c.FORCE_COMPUTE
41
42
  }), g = Object.freeze({
43
+ DISPOSED: c.DISPOSED,
44
+ SYNC: c.ATOM_SYNC,
45
+ NOTIFICATION_SCHEDULED: c.ATOM_NOTIFICATION_SCHEDULED
46
+ }), m = Object.freeze({
42
47
  MAX_EXECUTIONS_PER_SECOND: 1e3,
43
48
  MAX_EXECUTIONS_PER_EFFECT: 100,
44
49
  MAX_EXECUTIONS_PER_FLUSH: 1e4,
45
50
  MAX_FLUSH_ITERATIONS: 1e3,
46
51
  MIN_FLUSH_ITERATIONS: 10,
47
52
  BATCH_QUEUE_SHRINK_THRESHOLD: 1e3
48
- }), H = 1073741823, W = Object.freeze({
53
+ }), Ct = 1073741823, et = Object.freeze({
49
54
  WARN_INFINITE_LOOP: !0,
50
55
  EFFECT_FREQUENCY_WINDOW: 1e3,
51
56
  LOOP_THRESHOLD: 100
52
- }), Et = Object.freeze({ MAX_PROMISE_ID: H }), U = Object.freeze({
57
+ }), L = Object.freeze({
53
58
  UNINITIALIZED: -1,
54
59
  MIN: 1
55
- }), ht = !1;
56
- try {
57
- ht = !!(typeof globalThis < "u" && globalThis.__ATOM_DEBUG__ || typeof sessionStorage < "u" && sessionStorage.getItem("__ATOM_DEBUG__") === "true");
58
- } catch {
59
- }
60
- var f = (typeof process < "u" && process.env, typeof __DEV__ < "u" && !!__DEV__ || ht), it = Object.freeze([]), y = class j extends Error {
61
- constructor(e, s = null, i = !0, n) {
62
- super(e), this.cause = s, this.recoverable = i, this.code = n, this.name = "AtomError", Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
63
- }
64
- getChain() {
65
- const e = this.cause;
66
- if (e == null) return [this];
67
- const s = [this];
68
- let i = e, n = null;
69
- for (; i != null && (s.push(i), !(i === this || n?.has(i))); ) {
70
- if (i instanceof j) i = i.cause;
71
- else if (i instanceof Error) i = i.cause;
72
- else break;
73
- s.length > 3 && (n === null ? n = new Set(s) : n.add(i));
74
- }
75
- return s;
76
- }
77
- toJSON(e) {
78
- const s = e ?? /* @__PURE__ */ new Set();
79
- if (s.has(this)) return {
80
- name: this.name,
81
- message: "[Circular Reference]",
82
- recoverable: this.recoverable,
83
- code: this.code
84
- };
85
- s.add(this);
86
- let i = this.cause;
87
- return i instanceof j ? i = i.toJSON(s) : i instanceof Error && (i = {
88
- name: i.name,
89
- message: i.message,
90
- stack: i.stack,
91
- cause: i.cause
92
- }), {
93
- name: this.name,
94
- message: this.message,
95
- code: this.code,
96
- recoverable: this.recoverable,
97
- stack: this.stack,
98
- cause: i
99
- };
100
- }
101
- static format(e, s, i) {
102
- return `${e} (${s}): ${i}`;
103
- }
104
- }, w = class extends y {
105
- constructor(...t) {
106
- super(...t), this.name = "ComputedError";
107
- }
108
- }, N = class extends y {
109
- constructor(t, e = null, s = !1, i) {
110
- super(t, e, s, i), this.name = "EffectError";
60
+ }), Vt = () => {
61
+ try {
62
+ return !!(typeof globalThis < "u" && globalThis.__ATOM_DEBUG__ || typeof sessionStorage < "u" && sessionStorage.getItem("__ATOM_DEBUG__") === "true");
63
+ } catch {
64
+ return !1;
111
65
  }
112
- }, x = class extends y {
113
- constructor(t, e = null, s = !1, i) {
114
- super(t, e, s, i), this.name = "SchedulerError";
66
+ }, Yt = () => {
67
+ try {
68
+ return !1;
69
+ } catch {
70
+ return !1;
115
71
  }
116
- }, l = {
117
- COMPUTED_MUST_BE_FUNCTION: "Computed target must be a function",
118
- COMPUTED_ASYNC_PENDING_NO_DEFAULT: "Async computation pending with no default value",
119
- COMPUTED_COMPUTATION_FAILED: "Computation execution failed",
120
- COMPUTED_ASYNC_COMPUTATION_FAILED: "Async computation execution failed",
121
- COMPUTED_CIRCULAR_DEPENDENCY: "Circular dependency detected",
122
- COMPUTED_DISPOSED: "Attempted to access disposed computed",
123
- ATOM_SUBSCRIBER_MUST_BE_FUNCTION: "Subscriber must be a function or Subscriber object",
124
- ATOM_INDIVIDUAL_SUBSCRIBER_FAILED: "Subscriber execution failed",
125
- EFFECT_MUST_BE_FUNCTION: "Effect target must be a function",
126
- EFFECT_EXECUTION_FAILED: "Effect execution failed",
127
- EFFECT_CLEANUP_FAILED: "Effect cleanup failed",
128
- EFFECT_DISPOSED: "Attempted to run disposed effect",
129
- SCHEDULER_FLUSH_OVERFLOW: (t, e) => `Maximum flush iterations (${t}) exceeded. ${e} jobs dropped. Possible infinite loop.`,
130
- CALLBACK_ERROR_IN_ERROR_HANDLER: "Exception encountered in onError handler",
131
- EFFECT_FREQUENCY_LIMIT_EXCEEDED: "Effect executed too frequently within 1 second. Suspected infinite loop.",
132
- SCHEDULER_CALLBACK_MUST_BE_FUNCTION: "Scheduler callback must be a function",
133
- SCHEDULER_END_BATCH_WITHOUT_START: "endBatch() called without matching startBatch(). Ignoring.",
134
- BATCH_CALLBACK_MUST_BE_FUNCTION: "Batch callback must be a function"
135
- };
136
- function A(t, e, s) {
137
- return t instanceof y ? new e(`${t.name} (${s}): ${t.message}`, t, t.recoverable, t.code) : t instanceof Error ? new e(`${t.name || "Error"} (${s}): ${t.message}`, t) : new e(`Unexpected error (${s}): ${String(t)}`, t);
138
- }
139
- var nt = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), dt = /* @__PURE__ */ Symbol("AtomEffect.Id"), rt = /* @__PURE__ */ Symbol("AtomEffect.Type"), G = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), pt = "[Atom Effect]", M = () => {
140
- }, It = class {
72
+ }, G = {
73
+ node: typeof process < "u" && process.env && process.env.NODE_ENV !== "production",
74
+ bundler: typeof __DEV__ < "u" && !!__DEV__,
75
+ esm: typeof process > "u" && Yt(),
76
+ runtime: Vt()
77
+ }, d = G.node || G.bundler || G.esm || G.runtime, jt = Object.freeze([]), Xt = /* @__PURE__ */ Symbol.for("atom-effect.Option"), Wt = /* @__PURE__ */ Symbol.for("atom-effect.Result"), Pe = Object.freeze({
78
+ ok: !1,
79
+ value: void 0,
80
+ [Xt]: !0
81
+ }), Le = Object.freeze({
82
+ ok: !0,
83
+ value: void 0,
84
+ error: void 0,
85
+ [Wt]: !0
86
+ }), $t = [
87
+ 0,
88
+ 1,
89
+ 0,
90
+ 2,
91
+ 0,
92
+ 1,
93
+ 0,
94
+ 3,
95
+ 0,
96
+ 1,
97
+ 0,
98
+ 2,
99
+ 0,
100
+ 1,
101
+ 0,
102
+ -1
103
+ ], _ = 4, Kt = 15, St = class {
141
104
  constructor() {
142
- this.enabled = !0, this.warnInfiniteLoop = W.WARN_INFINITE_LOOP, this._updateCounts = /* @__PURE__ */ new Map(), this._nodeRegistry = /* @__PURE__ */ new Map(), this._threshold = W.LOOP_THRESHOLD, this._cleanupScheduled = !1, this.warn = (t, e) => {
143
- this.enabled && t && console.warn(`${pt} ${e}`);
144
- }, this.registerNode = (t) => {
145
- this._nodeRegistry.set(t.id, new WeakRef(t));
146
- }, this.attachDebugInfo = (t, e, s, i) => {
147
- if (!this.enabled) return;
148
- const n = t;
149
- n[nt] = i ?? `${e}_${s}`, n[dt] = s, n[rt] = e, this.registerNode(t);
150
- }, this.trackUpdate = (t, e) => {
151
- if (!this.enabled || !this.warnInfiniteLoop) return;
152
- const s = this._updateCounts, i = (s.get(t) ?? 0) + 1;
153
- i > this._threshold ? this.warn(!0, `Infinite loop detected for ${e ?? `dependency ${t}`}. Over ${this._threshold} updates in a single execution scope.`) : s.set(t, i), this._cleanupScheduled || (this._cleanupScheduled = !0, queueMicrotask(() => {
154
- this._updateCounts.clear(), this._cleanupScheduled = !1;
155
- }));
156
- }, this.dumpGraph = () => {
157
- const t = [];
158
- for (const [e, s] of this._nodeRegistry) {
159
- const i = s.deref();
160
- i ? t.push({
161
- id: e,
162
- name: this.getDebugName(i),
163
- type: this.getDebugType(i),
164
- updateCount: this._updateCounts.get(e) ?? 0
165
- }) : (this._nodeRegistry.delete(e), this._updateCounts.delete(e));
166
- }
167
- return t;
168
- }, this.getDebugName = (t) => {
169
- if (t != null)
170
- return t[nt];
171
- }, this.getDebugType = (t) => {
172
- if (t != null)
173
- return t[rt];
174
- };
175
- }
176
- }, Ct = {
177
- enabled: !1,
178
- warnInfiniteLoop: !1,
179
- warn: M,
180
- registerNode: M,
181
- attachDebugInfo: M,
182
- trackUpdate: M,
183
- dumpGraph: () => [],
184
- getDebugName: () => {
185
- },
186
- getDebugType: () => {
105
+ this._count = 0, this._actualCount = 0, this._mask = 0, this._s0 = null, this._s1 = null, this._s2 = null, this._s3 = null, this._overflow = null, this._freeIndices = null, this._lockCount = 0, this._pendingCompact = !1;
187
106
  }
188
- }, d = f ? new It() : Ct, Dt = 1, gt = () => Dt++ | 0, lt = class {
189
- constructor() {
190
- this._count = 0, this._actualCount = 0, this._s0 = null, this._s1 = null, this._s2 = null, this._s3 = null, this._overflow = null, this._freeIndices = null;
107
+ _firstFreeSlot(t) {
108
+ return $t[t & Kt];
191
109
  }
192
110
  _rawWrite(t, e) {
193
- if (t < 4) t === 0 ? this._s0 = e : t === 1 ? this._s1 = e : t === 2 ? this._s2 = e : this._s3 = e;
194
- else {
195
- this._overflow === null && (this._overflow = []);
196
- const s = this._overflow;
197
- s[t - 4] = e;
198
- }
111
+ if (t < _) {
112
+ const s = 1 << t;
113
+ e === null ? this._mask &= ~s : this._mask |= s, t === 0 ? this._s0 = e : t === 1 ? this._s1 = e : t === 2 ? this._s2 = e : t === 3 && (this._s3 = e);
114
+ } else
115
+ this._overflow || (this._overflow = []), this._overflow[t - _] = e;
199
116
  }
200
117
  _rawAdd(t) {
201
- if (this._s0 === null)
202
- return this._s0 = t, 0;
203
- if (this._s1 === null)
204
- return this._s1 = t, 1;
205
- if (this._s2 === null)
206
- return this._s2 = t, 2;
207
- if (this._s3 === null)
208
- return this._s3 = t, 3;
209
- this._overflow === null && (this._overflow = []);
210
- const e = this._overflow, s = this._freeIndices;
211
- if (s !== null && s.length > 0) {
212
- const i = s.pop();
213
- return e[i] = t, i + 4;
118
+ const e = this._mask, s = this._firstFreeSlot(e);
119
+ if (s !== -1)
120
+ return this._mask = e | 1 << s, s === 0 ? this._s0 = t : s === 1 ? this._s1 = t : s === 2 ? this._s2 = t : this._s3 = t, s;
121
+ this._overflow || (this._overflow = []);
122
+ const r = this._overflow, i = this._freeIndices;
123
+ if (i?.length) {
124
+ const n = i.pop();
125
+ return r[n] = t, n + _;
214
126
  }
215
- return e.push(t), 3 + e.length;
127
+ return r.push(t), _ - 1 + r.length;
216
128
  }
217
129
  _rawSwap(t, e) {
218
130
  if (t === e) return;
219
- const s = this.getAt(t), i = this.getAt(e);
220
- this._rawWrite(t, i), this._rawWrite(e, s);
131
+ const s = this.at(t), r = this.at(e);
132
+ this._rawWrite(t, r), this._rawWrite(e, s);
133
+ }
134
+ get length() {
135
+ return this._count;
221
136
  }
222
137
  get size() {
223
138
  return this._actualCount;
224
139
  }
225
- get physicalSize() {
226
- return this._count;
227
- }
228
- getAt(t) {
229
- if (t < 4)
140
+ at(t) {
141
+ if (t < _)
230
142
  return t === 0 ? this._s0 : t === 1 ? this._s1 : t === 2 ? this._s2 : t === 3 ? this._s3 : null;
231
143
  const e = this._overflow;
232
- return e === null ? null : e[t - 4] ?? null;
144
+ return e ? e[t - _] ?? null : null;
233
145
  }
234
146
  setAt(t, e) {
235
- const s = this.getAt(t);
147
+ const s = this.at(t);
236
148
  s !== e && (this._rawWrite(t, e), s === null ? this._actualCount++ : e === null && this._actualCount--, e !== null ? t >= this._count && (this._count = t + 1) : this._shrinkPhysicalSizeFrom(t));
237
149
  }
238
150
  _shrinkPhysicalSizeFrom(t) {
239
151
  if (t === this._count - 1) {
240
- if (this._count--, this._count > 4) {
152
+ if (this._count--, this._count > _) {
241
153
  const e = this._overflow;
242
- for (; this._count > 4 && e[this._count - 5] === null; ) this._count--;
154
+ for (; this._count > _ && e[this._count - (_ + 1)] == null; ) this._count--;
243
155
  }
244
- this._count === 4 && this._s3 === null && (this._count = 3, this._s2 === null && (this._count = 2, this._s1 === null && (this._count = 1, this._s0 === null && (this._count = 0))));
156
+ this._count <= _ && (this._count = 32 - Math.clz32(this._mask));
245
157
  }
246
158
  }
247
159
  truncateFrom(t) {
248
- t <= 3 && (t <= 3 && this._s3 !== null && (this._onItemRemoved(this._s3), this._s3 = null, this._actualCount--), t <= 2 && this._s2 !== null && (this._onItemRemoved(this._s2), this._s2 = null, this._actualCount--), t <= 1 && this._s1 !== null && (this._onItemRemoved(this._s1), this._s1 = null, this._actualCount--), t <= 0 && this._s0 !== null && (this._onItemRemoved(this._s0), this._s0 = null, this._actualCount--));
249
- const e = this._overflow;
250
- if (e !== null) {
251
- const s = t > 4 ? t - 4 : 0, i = e.length;
252
- for (let n = s; n < i; n++) {
253
- const r = e[n];
254
- r != null && (this._onItemRemoved(r), e[n] = null, this._actualCount--);
255
- }
256
- t <= 4 ? this._overflow = null : e.length = t - 4;
160
+ const e = this._count;
161
+ if (!(t >= e)) {
162
+ for (let s = t; s < e; s++) this.at(s) !== null && this._actualCount--;
163
+ t < _ ? (this._mask &= (1 << t) - 1, t <= 0 && (this._s0 = null), t <= 1 && (this._s1 = null), t <= 2 && (this._s2 = null), t <= 3 && (this._s3 = null), this._overflow = null) : this._overflow && (this._overflow.length = t - _), this._count = t, this._freeIndices = null;
257
164
  }
258
- this._count = t, this._actualCount < 0 && (this._actualCount = 0), this._freeIndices = null;
259
- }
260
- _onItemRemoved(t) {
261
165
  }
262
- add(t) {
166
+ push(t) {
263
167
  const e = this._rawAdd(t);
264
168
  return e >= this._count && (this._count = e + 1), this._actualCount++, e;
265
169
  }
266
170
  remove(t) {
267
- let e = -1;
268
- if (this._s0 === t) e = 0;
269
- else if (this._s1 === t) e = 1;
270
- else if (this._s2 === t) e = 2;
271
- else if (this._s3 === t) e = 3;
272
- else {
273
- const s = this._overflow;
274
- s !== null && (e = s.indexOf(t), e !== -1 && (e += 4));
171
+ if (this._actualCount === 0) return !1;
172
+ const e = this._mask;
173
+ if (e & 1 && this._s0 === t) return this._removeAt(0);
174
+ if (e & 2 && this._s1 === t) return this._removeAt(1);
175
+ if (e & 4 && this._s2 === t) return this._removeAt(2);
176
+ if (e & 8 && this._s3 === t) return this._removeAt(3);
177
+ const s = this._overflow;
178
+ if (s) {
179
+ for (let r = 0, i = s.length; r < i; r++) if (s[r] === t)
180
+ return s[r] = null, this._actualCount--, this._shrinkPhysicalSizeFrom(r + _), this._freeIndices || (this._freeIndices = []), this._freeIndices.push(r), !0;
275
181
  }
276
- return e !== -1 ? (this._rawWrite(e, null), this._shrinkPhysicalSizeFrom(e), this._actualCount--, e >= 4 && (this._freeIndices === null && (this._freeIndices = []), this._freeIndices.push(e - 4)), !0) : !1;
182
+ return !1;
183
+ }
184
+ _removeAt(t) {
185
+ return this._rawWrite(t, null), this._actualCount--, this._shrinkPhysicalSizeFrom(t), !0;
277
186
  }
278
187
  has(t) {
279
188
  if (this._actualCount === 0) return !1;
280
- if (this._s0 === t || this._s1 === t || this._s2 === t || this._s3 === t) return !0;
281
- const e = this._overflow;
282
- return e !== null ? e.indexOf(t) !== -1 : !1;
189
+ const e = this._mask;
190
+ if (e & 1 && this._s0 === t || e & 2 && this._s1 === t || e & 4 && this._s2 === t || e & 8 && this._s3 === t) return !0;
191
+ const s = this._overflow;
192
+ if (s) {
193
+ for (let r = 0, i = s.length; r < i; r++) if (s[r] === t) return !0;
194
+ }
195
+ return !1;
283
196
  }
284
197
  forEach(t) {
285
- const e = this._actualCount;
286
- if (e === 0) return;
287
- if (e === this._count) {
288
- if (t(this._s0), e > 1 && (t(this._s1), e > 2 && (t(this._s2), e > 3 && (t(this._s3), e > 4)))) {
289
- const n = this._overflow;
290
- for (let r = 0, o = n.length; r < o; r++) t(n[r]);
291
- }
292
- return;
198
+ if (this._actualCount === 0) return;
199
+ const e = this._mask;
200
+ e & 1 && t(this._s0), e & 2 && t(this._s1), e & 4 && t(this._s2), e & 8 && t(this._s3);
201
+ const s = this._overflow;
202
+ if (s) for (let r = 0, i = s.length; r < i; r++) {
203
+ const n = s[r];
204
+ n != null && t(n);
293
205
  }
294
- let s = 0;
295
- if (this._s0 !== null && (t(this._s0), ++s >= e) || this._s1 !== null && (t(this._s1), ++s >= e) || this._s2 !== null && (t(this._s2), ++s >= e) || this._s3 !== null && (t(this._s3), ++s >= e))
296
- return;
297
- const i = this._overflow;
298
- if (i !== null) for (let n = 0, r = i.length; n < r; n++) {
299
- const o = i[n];
300
- if (o != null && (t(o), ++s >= e))
301
- return;
206
+ }
207
+ some(t) {
208
+ if (this._actualCount === 0) return !1;
209
+ const e = this._mask;
210
+ if (e & 1 && t(this._s0) || e & 2 && t(this._s1) || e & 4 && t(this._s2) || e & 8 && t(this._s3)) return !0;
211
+ const s = this._overflow;
212
+ if (s) for (let r = 0, i = s.length; r < i; r++) {
213
+ const n = s[r];
214
+ if (n != null && t(n)) return !0;
302
215
  }
216
+ return !1;
303
217
  }
304
218
  compact() {
305
- const t = this._actualCount;
306
- if (t === this._count) return;
219
+ if (this._lockCount > 0) {
220
+ this._pendingCompact = !0;
221
+ return;
222
+ }
223
+ const t = this._actualCount, e = this._count;
224
+ if (t === e) return;
307
225
  if (t === 0) {
308
226
  this.clear();
309
227
  return;
310
228
  }
311
- let e = 0;
312
- const s = this._count;
313
- for (let i = 0; i < s; i++) {
314
- const n = this.getAt(i);
315
- if (n !== null && (i !== e && (this._rawWrite(e, n), this._rawWrite(i, null)), e++, e === t))
229
+ let s = 0;
230
+ const r = this._overflow;
231
+ for (let i = 0; i < e; i++) {
232
+ const n = this.at(i);
233
+ if (n !== null && (i !== s && (this._rawWrite(s, n), this._rawWrite(i, null)), ++s === t))
316
234
  break;
317
235
  }
318
- this._count = t, this._overflow !== null && (e <= 4 ? this._overflow = null : this._overflow.length = e - 4), this._freeIndices = null;
236
+ this._count = t, r !== null && (s <= _ ? this._overflow = null : r.length = s - _), this._freeIndices = null, this._pendingCompact = !1;
237
+ }
238
+ lock() {
239
+ this._lockCount++;
240
+ }
241
+ unlock() {
242
+ --this._lockCount === 0 && this._pendingCompact && this.compact();
319
243
  }
320
244
  clear() {
321
- this._s0 = this._s1 = this._s2 = this._s3 = null, this._count = 0, this._actualCount = 0, this._overflow = null, this._freeIndices = null;
245
+ this._s0 = this._s1 = this._s2 = this._s3 = null, this._count = 0, this._actualCount = 0, this._mask = 0, this._overflow = null, this._freeIndices = null, this._pendingCompact = !1;
322
246
  }
323
247
  dispose() {
324
248
  this.clear();
325
249
  }
326
- }, ct = class extends lt {
327
- constructor(...t) {
328
- super(...t), this._map = null, this._SCAN_THRESHOLD = 32, this.hasComputeds = !1;
329
- }
330
- prepareTracking() {
331
- this.hasComputeds = !1;
332
- }
333
- _onItemRemoved(t) {
334
- t.unsub?.();
250
+ get isLocked() {
251
+ return this._lockCount > 0;
335
252
  }
336
- setAt(t, e) {
337
- const s = this.getAt(t);
338
- super.setAt(t, e), this._map !== null && (s !== null && this._map.delete(s.node), e !== null && this._map.set(e.node, t));
339
- }
340
- claimExisting(t, e) {
341
- const s = this._count;
342
- if (s <= e) return !1;
343
- let i = null;
344
- if (e < 4 ? e === 0 ? i = this._s0 : e === 1 ? i = this._s1 : e === 2 ? i = this._s2 : i = this._s3 : i = this._overflow[e - 4] ?? null, i && i.node === t && i.unsub)
345
- return i.version = t.version, !0;
346
- if (this._map !== null || s - e > this._SCAN_THRESHOLD) return this._claimViaMap(t, e);
347
- let n = -1, r = null, o = e + 1;
348
- for (; o < 4 && o < s; o++) {
349
- const u = o === 1 ? this._s1 : o === 2 ? this._s2 : this._s3;
350
- if (u && u.node === t && u.unsub) {
351
- n = o, r = u;
352
- break;
353
- }
354
- }
355
- if (n === -1 && o < s) {
356
- const u = this._overflow;
357
- for (let c = o - 4, I = s - 4; c < I; c++) {
358
- const E = u[c];
359
- if (E && E.node === t && E.unsub) {
360
- n = c + 4, r = E;
361
- break;
362
- }
363
- }
364
- }
365
- return n !== -1 ? (r.version = t.version, this._rawWrite(e, r), this._rawWrite(n, i), !0) : !1;
366
- }
367
- _claimViaMap(t, e) {
368
- this._map === null && (this._map = this._initMap());
369
- const s = this._map, i = s.get(t);
370
- if (i === void 0 || i < e) return !1;
371
- const n = this.getAt(i);
372
- if (n === null || !n.unsub) return !1;
373
- if (n.version = t.version, i !== e) {
374
- const r = this.getAt(e);
375
- this._rawSwap(i, e), s.set(t, e), r?.unsub && s.set(r.node, i);
376
- }
377
- return !0;
253
+ };
254
+ function gt(t) {
255
+ return t instanceof Promise ? !0 : t === null || typeof t != "object" && typeof t != "function" ? !1 : typeof t.then == "function";
256
+ }
257
+ var Qt = Object.prototype.hasOwnProperty;
258
+ function qt(t, e) {
259
+ if (t === e) return !0;
260
+ if (t === null || e === null || typeof t != "object" || typeof e != "object") return !1;
261
+ const s = t, r = e, i = Object.keys(s);
262
+ if (i.length !== Object.keys(r).length) return !1;
263
+ for (const n of i) if (!Qt.call(r, n) || !Object.is(s[n], r[n])) return !1;
264
+ return !0;
265
+ }
266
+ var b = class extends Error {
267
+ constructor(t, e = null, s = !0, r) {
268
+ super(t), this.cause = e, this.recoverable = s, this.code = r, this.name = "AtomError", Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
378
269
  }
379
- _initMap() {
380
- const t = /* @__PURE__ */ new Map();
381
- this._s0?.unsub && t.set(this._s0.node, 0), this._s1?.unsub && t.set(this._s1.node, 1), this._s2?.unsub && t.set(this._s2.node, 2), this._s3?.unsub && t.set(this._s3.node, 3);
382
- const e = this._overflow;
383
- if (e !== null) for (let s = 0, i = e.length; s < i; s++) {
384
- const n = e[s];
385
- n?.unsub && t.set(n.node, s + 4);
386
- }
270
+ getChain() {
271
+ const t = [], e = /* @__PURE__ */ new Set();
272
+ let s = this;
273
+ for (; s != null && !e.has(s); )
274
+ t.push(s), e.add(s), s = s?.cause;
387
275
  return t;
388
276
  }
389
- insertNew(t, e) {
390
- let s = null;
391
- if (t < 4) t === 0 ? (s = this._s0, this._s0 = e) : t === 1 ? (s = this._s1, this._s1 = e) : t === 2 ? (s = this._s2, this._s2 = e) : (s = this._s3, this._s3 = e);
392
- else {
393
- this._overflow === null && (this._overflow = []);
394
- const i = this._overflow;
395
- s = i[t - 4] ?? null, i[t - 4] = e;
396
- }
397
- if (s !== null) {
398
- const i = this._rawAdd(s);
399
- i >= this._count && (this._count = i + 1), this._map !== null && s.unsub && this._map.set(s.node, i);
400
- }
401
- t >= this._count && (this._count = t + 1), this._actualCount++, this._map !== null && e.unsub && this._map.set(e.node, t);
402
- }
403
- add(t) {
404
- const e = super.add(t);
405
- return this._map !== null && t.unsub && this._map.set(t.node, e), e;
277
+ toJSON(t = /* @__PURE__ */ new Set()) {
278
+ return t.has(this) ? {
279
+ name: this.name,
280
+ message: "[Circular Reference]",
281
+ recoverable: this.recoverable,
282
+ code: this.code
283
+ } : (t.add(this), {
284
+ name: this.name,
285
+ message: this.message,
286
+ code: this.code,
287
+ recoverable: this.recoverable,
288
+ stack: this.stack,
289
+ cause: Dt(this.cause, t)
290
+ });
406
291
  }
407
- remove(t) {
408
- throw new Error("remove() prohibited");
292
+ static format(t, e, s) {
293
+ return `${t} (${e}): ${s}`;
409
294
  }
410
- compact() {
295
+ }, w = class extends b {
296
+ constructor(...t) {
297
+ super(...t), this.name = "ComputedError";
411
298
  }
412
- truncateFrom(t) {
413
- super.truncateFrom(t), this._map !== null && (this._map = null);
299
+ }, R = class extends b {
300
+ constructor(t, e = null, s = !1, r) {
301
+ super(t, e, s, r), this.name = "EffectError";
414
302
  }
415
- disposeAll() {
416
- this.truncateFrom(0), this.hasComputeds = !1;
303
+ }, nt = class extends b {
304
+ constructor(t, e = null, s = !1, r) {
305
+ super(t, e, s, r), this.name = "SchedulerError";
417
306
  }
418
- }, L = /* @__PURE__ */ Symbol.for("atom-effect/brand"), p = {
307
+ }, a = {
308
+ COMPUTED_MUST_BE_FUNCTION: "Computed target must be a function",
309
+ COMPUTED_ASYNC_PENDING_NO_DEFAULT: "Async computation pending with no default value",
310
+ COMPUTED_COMPUTATION_FAILED: "Computation execution failed",
311
+ COMPUTED_ASYNC_COMPUTATION_FAILED: "Async computation execution failed",
312
+ COMPUTED_CIRCULAR_DEPENDENCY: "Circular dependency detected",
313
+ COMPUTED_DISPOSED: "Attempted to access disposed computed",
314
+ ATOM_SUBSCRIBER_MUST_BE_FUNCTION: "Subscriber must be a function or Subscriber object",
315
+ ATOM_INDIVIDUAL_SUBSCRIBER_FAILED: "Subscriber execution failed",
316
+ EFFECT_MUST_BE_FUNCTION: "Effect target must be a function",
317
+ EFFECT_EXECUTION_FAILED: "Effect execution failed",
318
+ EFFECT_CLEANUP_FAILED: "Effect cleanup failed",
319
+ EFFECT_DISPOSED: "Attempted to run disposed effect",
320
+ SCHEDULER_FLUSH_OVERFLOW: (t, e) => `Maximum flush iterations (${t}) exceeded. ${e} jobs dropped. Possible infinite loop.`,
321
+ CALLBACK_ERROR_IN_ERROR_HANDLER: "Exception encountered in onError handler",
322
+ EFFECT_FREQUENCY_LIMIT_EXCEEDED: "Effect executed too frequently within 1 second. Suspected infinite loop.",
323
+ SCHEDULER_CALLBACK_MUST_BE_FUNCTION: "Scheduler callback must be a function",
324
+ SCHEDULER_END_BATCH_WITHOUT_START: "endBatch() called without matching startBatch(). Ignoring.",
325
+ BATCH_CALLBACK_MUST_BE_FUNCTION: "Batch callback must be a function"
326
+ };
327
+ function X(t, e, s) {
328
+ const r = Jt(t);
329
+ return new e(b.format(r.name, s, r.message), t, r.recoverable, r.code);
330
+ }
331
+ function Jt(t) {
332
+ return t instanceof b ? {
333
+ name: t.name,
334
+ message: t.message,
335
+ recoverable: t.recoverable,
336
+ code: t.code
337
+ } : t instanceof Error ? {
338
+ name: t.name,
339
+ message: t.message,
340
+ recoverable: !0,
341
+ code: t?.code
342
+ } : {
343
+ name: "Unexpected error",
344
+ message: String(t),
345
+ recoverable: !0,
346
+ code: void 0
347
+ };
348
+ }
349
+ function Dt(t, e) {
350
+ return t == null || typeof t != "object" ? t : e.has(t) ? "[Circular Reference]" : t instanceof b ? t.toJSON(e) : t instanceof Error ? (e.add(t), {
351
+ name: t.name,
352
+ message: t.message,
353
+ stack: t.stack,
354
+ cause: Dt(t.cause, e)
355
+ }) : t;
356
+ }
357
+ var A = /* @__PURE__ */ Symbol.for("atom-effect/brand"), h = {
419
358
  Atom: 1,
420
359
  Writable: 2,
421
360
  Computed: 4,
422
361
  Effect: 8
362
+ }, st = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), Zt = {
363
+ [h.Atom]: "atom",
364
+ [h.Computed]: "computed",
365
+ [h.Effect]: "effect"
366
+ }, te = h.Atom | h.Computed | h.Effect, J = "[Atom Effect]", U = () => {
367
+ }, ee = class {
368
+ constructor() {
369
+ this.enabled = !0, this.warnInfiniteLoop = et.WARN_INFINITE_LOOP, this._updateCounts = /* @__PURE__ */ new Map(), this._registry = /* @__PURE__ */ new Map(), this.trackGraph = !1, this._finalizer = new FinalizationRegistry((t) => {
370
+ this._registry.delete(t), this._updateCounts.delete(t);
371
+ }), this._threshold = et.LOOP_THRESHOLD, this._cleanupScheduled = !1, this._failedEvaluations = /* @__PURE__ */ new Set(), this._failureCleanupScheduled = !1, this._resetUpdateCounts = () => {
372
+ this._updateCounts.clear(), this._cleanupScheduled = !1;
373
+ }, this._resetFailedEvaluations = () => {
374
+ this._failedEvaluations.clear(), this._failureCleanupScheduled = !1;
375
+ };
376
+ }
377
+ warn(t, e) {
378
+ this.enabled && t && console.warn(`${J} ${e}`);
379
+ }
380
+ registerNode(t) {
381
+ const e = t.id, s = this._getOrCreateMetadata(t, e);
382
+ s.ref = new WeakRef(t), this._finalizer.register(t, e);
383
+ }
384
+ attachDebugInfo(t, e, s, r) {
385
+ if (!this.enabled || r === void 0 && !this.trackGraph) return;
386
+ let i = this._registry.get(s);
387
+ i ? (r !== void 0 && (i.name = r), i.type = e) : (i = {
388
+ name: r ?? `${e}_${s}`,
389
+ type: e
390
+ }, this._registry.set(s, i)), this.registerNode(t);
391
+ }
392
+ trackUpdate(t, e) {
393
+ if (!this.enabled || !this.warnInfiniteLoop) return;
394
+ const s = this._updateCounts, r = (s.get(t) || 0) + 1;
395
+ s.set(t, r), r > this._threshold && r === this._threshold + 1 && console.warn(`${J} Infinite loop detected for ${e ?? `dependency ${t}`}. Detected ${r} updates within a single execution scope, exceeding the threshold of ${this._threshold}.`), this._cleanupScheduled || (this._cleanupScheduled = !0, queueMicrotask(this._resetUpdateCounts));
396
+ }
397
+ trackEvaluationFailure(t) {
398
+ !this.enabled || this._failedEvaluations.has(t) || (this._failedEvaluations.add(t), console.warn(`${J} Dependency #${t} evaluation failed during dirty check.`), this._failureCleanupScheduled || (this._failureCleanupScheduled = !0, queueMicrotask(this._resetFailedEvaluations)));
399
+ }
400
+ dumpGraph() {
401
+ const t = this._registry;
402
+ if (t.size === 0) return [];
403
+ const e = [], s = this._updateCounts;
404
+ for (const [r, i] of t)
405
+ this.trackGraph && i.ref?.deref() === void 0 || e.push({
406
+ id: r,
407
+ name: i.name,
408
+ type: i.type,
409
+ updateCount: s.get(r) ?? 0
410
+ });
411
+ return e;
412
+ }
413
+ getDebugName(t) {
414
+ if (!this.enabled || !t) return;
415
+ const e = t.id;
416
+ if (e === void 0) return;
417
+ const s = this._registry.get(e);
418
+ return s ? s.name : `${this._getTypeFromBrand(t) ?? "unknown"}_${e}`;
419
+ }
420
+ getDebugType(t) {
421
+ if (!this.enabled || !t) return;
422
+ const e = t.id;
423
+ if (e === void 0) return;
424
+ const s = this._registry.get(e);
425
+ return s ? s.type : this._getTypeFromBrand(t);
426
+ }
427
+ _getOrCreateMetadata(t, e) {
428
+ let s = this._registry.get(e);
429
+ if (!s) {
430
+ const r = this._getTypeFromBrand(t) ?? "unknown";
431
+ s = {
432
+ name: `${r}_${e}`,
433
+ type: r
434
+ }, this._registry.set(e, s);
435
+ }
436
+ return s;
437
+ }
438
+ _getTypeFromBrand(t) {
439
+ const e = t[A];
440
+ return e !== void 0 ? Zt[e & te] : void 0;
441
+ }
442
+ }, se = {
443
+ enabled: !1,
444
+ warnInfiniteLoop: !1,
445
+ trackGraph: !1,
446
+ warn: U,
447
+ registerNode: U,
448
+ attachDebugInfo: U,
449
+ trackUpdate: U,
450
+ dumpGraph: () => [],
451
+ getDebugName: () => {
452
+ },
453
+ getDebugType: () => {
454
+ },
455
+ trackEvaluationFailure: U
456
+ }, S = d ? new ee() : se, re = 1, ie = () => re++, K = {
457
+ get: () => {
458
+ },
459
+ set: () => {
460
+ },
461
+ delete: () => {
462
+ }
463
+ }, It = class extends Map {
423
464
  };
424
- function J(t, e) {
425
- return !t || typeof t != "object" && typeof t != "function" ? !1 : !!(t[L] & e);
465
+ function Tt() {
466
+ return {
467
+ slots: new St(),
468
+ map: K,
469
+ hasComputeds: !1
470
+ };
426
471
  }
427
- function wt(t) {
428
- return J(t, p.Atom);
472
+ function vt(t) {
473
+ t.hasComputeds = !1;
429
474
  }
430
- function Rt(t) {
431
- return J(t, p.Computed);
475
+ function Ot(t, e, s) {
476
+ const { slots: r } = t;
477
+ if (r.length <= s) return !1;
478
+ const i = r.at(s);
479
+ if (i?.node === e && i.unsub)
480
+ return i.version = e.version, !0;
481
+ const n = ne(t, e, s);
482
+ if (n === -1) return !1;
483
+ const o = r.at(n);
484
+ o.version = e.version;
485
+ const u = r.at(s);
486
+ return rt(t, s, o), rt(t, n, u), !0;
432
487
  }
433
- function Ft(t) {
434
- return J(t, p.Effect);
488
+ function ne(t, e, s) {
489
+ const r = t.map.get(e);
490
+ if (r !== void 0) return r >= s ? r : -1;
491
+ const i = t.slots;
492
+ for (let n = s + 1, o = i.length; n < o; n++) {
493
+ const u = i.at(n);
494
+ if (u?.node === e && u.unsub) return n;
495
+ }
496
+ return -1;
435
497
  }
436
- function Z(t) {
437
- return t instanceof Promise ? !0 : t === null || typeof t != "object" ? !1 : typeof t.then == "function";
498
+ function mt(t, e, s) {
499
+ const r = t.slots.at(e);
500
+ rt(t, e, s), r !== null && oe(t, r);
438
501
  }
439
- var q = class {
440
- constructor(t, e, s = void 0) {
441
- this.node = t, this.version = e, this.unsub = s;
442
- }
443
- }, St = class {
444
- constructor(t = void 0, e = void 0) {
445
- this.fn = t, this.sub = e;
446
- }
447
- notify(t, e) {
448
- const s = this.fn, i = this.sub;
449
- if (s === void 0 && i === void 0) return;
450
- const n = _, r = n.current;
451
- if (r === null) {
452
- s !== void 0 && s(t, e), i !== void 0 && i.execute();
453
- return;
454
- }
455
- n.current = null;
502
+ function rt(t, e, s) {
503
+ const r = t.slots.at(e);
504
+ t.slots.setAt(e, s), r && t.map.delete(r.node), s?.unsub && (t.map === K && (t.map = new It()), t.map.set(s.node, e));
505
+ }
506
+ function oe(t, e) {
507
+ const s = t.slots.push(e);
508
+ return e.unsub && (t.map === K && (t.map = new It()), t.map.set(e.node, s)), s;
509
+ }
510
+ var ue = {
511
+ 0: (t) => t.node.version !== t.version,
512
+ [y.IS_COMPUTED]: (t) => {
513
+ const e = t.node;
456
514
  try {
457
- s !== void 0 && s(t, e), i !== void 0 && i.execute();
458
- } finally {
459
- n.current = r;
515
+ e.value;
516
+ } catch {
517
+ d && S.trackEvaluationFailure(e.id);
460
518
  }
519
+ return e.version !== t.version;
461
520
  }
462
- }, vt = class {
463
- constructor() {
464
- this.current = null;
521
+ };
522
+ function M(t) {
523
+ const e = t.slots, s = e.length;
524
+ if (e.size === 0) return !1;
525
+ const r = ue;
526
+ for (let i = 0; i < s; i++) {
527
+ const n = e.at(i);
528
+ if (n && r[n.node.flags & y.IS_COMPUTED](n))
529
+ return !0;
530
+ }
531
+ return !1;
532
+ }
533
+ function ce(t) {
534
+ const e = t.slots, s = e.length;
535
+ for (let r = 0; r < s; r++) {
536
+ const i = e.at(r);
537
+ if (!i) continue;
538
+ const n = i.node;
539
+ if (n.version !== i.version || (n.flags & y.DIRTY) !== 0) return !0;
540
+ }
541
+ return !1;
542
+ }
543
+ function ot(t, e) {
544
+ const s = t.slots, r = s.length;
545
+ for (let i = e; i < r; i++) {
546
+ const n = s.at(i);
547
+ if (n) {
548
+ const o = n.unsub;
549
+ if (o) try {
550
+ o();
551
+ } catch (u) {
552
+ d && console.error("[atom-effect] Unsubscribe failed:", u);
553
+ }
554
+ }
465
555
  }
466
- run(t, e) {
467
- if (this.current === t) return e();
468
- const s = this.current;
469
- this.current = t;
556
+ s.truncateFrom(e), t.map = K;
557
+ }
558
+ function Nt(t) {
559
+ ot(t, 0), t.hasComputeds = !1;
560
+ }
561
+ function bt(t, e, s = void 0) {
562
+ return {
563
+ node: t,
564
+ version: e,
565
+ unsub: s
566
+ };
567
+ }
568
+ function ft(t = void 0, e = void 0) {
569
+ return {
570
+ fn: t,
571
+ sub: e
572
+ };
573
+ }
574
+ function le(t, e, s) {
575
+ if (t !== null)
470
576
  try {
471
- if (!f) return e();
472
- const i = e();
473
- return d.warn(Z(i), 'Detected Promise returned within tracking context. Dependencies accessed after "await" will NOT be tracked. Consider using synchronous tracking before the async boundary.'), i;
474
- } finally {
475
- this.current = s;
577
+ const { fn: r, sub: i } = t;
578
+ r !== void 0 && r(e, s), i !== void 0 && i.execute();
579
+ } catch (r) {
580
+ console.error("[atom-effect] Subscriber failed:", r);
476
581
  }
582
+ }
583
+ function ae() {
584
+ return {
585
+ stack: [],
586
+ current: null
587
+ };
588
+ }
589
+ function ut(t, e) {
590
+ t.stack.push(e), t.current = e;
591
+ }
592
+ function At(t) {
593
+ const e = t.stack;
594
+ e.pop();
595
+ const s = e.length;
596
+ t.current = s > 0 ? e[s - 1] : null;
597
+ }
598
+ function ct(t, e) {
599
+ const s = t.stack;
600
+ s.length = e;
601
+ const r = s.length;
602
+ t.current = r > 0 ? s[r - 1] : null;
603
+ }
604
+ function Rt(t, e, s) {
605
+ if (t.current === e) return s();
606
+ ut(t, e);
607
+ try {
608
+ return s();
609
+ } finally {
610
+ At(t);
477
611
  }
478
- }, _ = new vt();
479
- function X(t) {
480
- const e = _, s = e.current;
481
- if (s === null) return t();
482
- e.current = null;
612
+ }
613
+ function he(t) {
614
+ t.stack.length = 0, t.current = null;
615
+ }
616
+ var l = ae();
617
+ function _t(t) {
618
+ if (l.current === null) return t();
619
+ ut(l, null);
483
620
  try {
484
621
  return t();
485
622
  } finally {
486
- e.current = s;
623
+ At(l);
487
624
  }
488
625
  }
489
- var tt = class {
626
+ var lt = class {
490
627
  constructor() {
491
- this.flags = 0, this.version = 0, this._lastSeenEpoch = U.UNINITIALIZED, this._notifying = 0, this._hotIndex = -1, this.id = gt() & H, this._nextEpoch = void 0, this._slots = null, this._deps = null;
628
+ this.flags = 0, this.version = 0, this._lastSeenEpoch = L.UNINITIALIZED, this._nextEpoch = void 0, this.id = ie() & Ct, this._slots = null, this._deps = null;
492
629
  }
493
630
  get isDisposed() {
494
- return (this.flags & $.DISPOSED) !== 0;
631
+ return (this.flags & y.DISPOSED) !== 0;
495
632
  }
496
633
  get isComputed() {
497
- return (this.flags & $.IS_COMPUTED) !== 0;
634
+ return (this.flags & y.IS_COMPUTED) !== 0;
635
+ }
636
+ get isNotifying() {
637
+ return this._slots?.isLocked ?? !1;
638
+ }
639
+ _hasFlag(t) {
640
+ return (this.flags & t) !== 0;
498
641
  }
499
642
  get hasError() {
500
643
  return !1;
501
644
  }
502
645
  subscribe(t) {
503
- const e = typeof t == "function";
504
- if (!e && (t === null || typeof t.execute != "function")) throw A(/* @__PURE__ */ new TypeError("Invalid subscriber"), y, l.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);
646
+ let e;
647
+ if (typeof t == "function" ? e = ft(t, void 0) : t != null && typeof t.execute == "function" && (e = ft(void 0, t)), !e) throw X(/* @__PURE__ */ new TypeError("Invalid subscriber"), b, a.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);
505
648
  let s = this._slots;
506
- if (s === null && (s = new lt(), this._slots = s), s.size > 0) {
507
- let n = !1;
508
- if (s._s0 !== null && (s._s0.fn === t || s._s0.sub === t) || s._s1 !== null && (s._s1.fn === t || s._s1.sub === t) || s._s2 !== null && (s._s2.fn === t || s._s2.sub === t) || s._s3 !== null && (s._s3.fn === t || s._s3.sub === t)) n = !0;
509
- else {
510
- const r = s._overflow;
511
- if (r !== null) {
512
- const o = r.length;
513
- if (e) for (let u = 0; u < o; u++) {
514
- const c = r[u];
515
- if (c !== null && c?.fn === t) {
516
- n = !0;
517
- break;
518
- }
519
- }
520
- else for (let u = 0; u < o; u++) {
521
- const c = r[u];
522
- if (c !== null && c?.sub === t) {
523
- n = !0;
524
- break;
525
- }
526
- }
527
- }
528
- }
529
- if (n)
530
- return f && console.warn(`[atom-effect] Duplicate subscription ignored on node ${this.id}`), () => {
531
- };
532
- }
533
- const i = new St(e ? t : void 0, e ? void 0 : t);
534
- return s.add(i), () => this._unsubscribe(i);
649
+ if (s === null) this._slots = s = new St();
650
+ else if (this._hasSubscription(t))
651
+ return d && console.warn(`[atom-effect] Duplicate subscription ignored on node ${this.id}`), () => {
652
+ };
653
+ return s.push(e), () => this._unsubscribe(e);
654
+ }
655
+ _hasSubscription(t) {
656
+ const e = this._slots;
657
+ return !e || e.size === 0 ? !1 : e.some((s) => s.fn === t || s.sub === t);
535
658
  }
536
659
  _unsubscribe(t) {
537
660
  const e = this._slots;
538
- e !== null && (e.remove(t), this._notifying === 0 && e.compact());
661
+ e !== null && (e.remove(t), e.compact());
539
662
  }
540
663
  subscriberCount() {
541
- const t = this._slots;
542
- return t === null ? 0 : t.size;
664
+ return this._slots?.size ?? 0;
543
665
  }
544
666
  _notifySubscribers(t, e) {
545
667
  const s = this._slots;
546
- if (!(s === null || s.size === 0)) {
547
- this._notifying++;
548
- try {
549
- if (s._s0 !== null) try {
550
- s._s0.notify(t, e);
551
- } catch (n) {
552
- this._logNotifyError(n);
553
- }
554
- if (s._s1 !== null) try {
555
- s._s1.notify(t, e);
556
- } catch (n) {
557
- this._logNotifyError(n);
558
- }
559
- if (s._s2 !== null) try {
560
- s._s2.notify(t, e);
561
- } catch (n) {
562
- this._logNotifyError(n);
563
- }
564
- if (s._s3 !== null) try {
565
- s._s3.notify(t, e);
566
- } catch (n) {
567
- this._logNotifyError(n);
568
- }
569
- const i = s._overflow;
570
- if (i !== null) for (let n = 0, r = i.length; n < r; n++) {
571
- const o = i[n];
572
- if (o !== null) try {
573
- o?.notify(t, e);
574
- } catch (u) {
575
- this._logNotifyError(u);
576
- }
577
- }
578
- } finally {
579
- --this._notifying === 0 && s.compact();
580
- }
668
+ if (!s || s.size === 0) return;
669
+ const r = l.stack.length;
670
+ ut(l, null), s.lock();
671
+ try {
672
+ s.forEach((i) => {
673
+ le(i, t, e);
674
+ });
675
+ } finally {
676
+ ct(l, r), s.unlock();
581
677
  }
582
678
  }
583
- _logNotifyError(t) {
584
- console.error(A(t, y, l.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
585
- }
586
679
  _isDirty() {
587
- const t = this._deps;
588
- if (t === null || t.size === 0) return !1;
589
- const e = this._hotIndex;
590
- if (e !== -1) {
591
- const s = t.getAt(e);
592
- if (s !== null && s.node.version !== s.version) return !0;
593
- }
594
- return this._deepDirtyCheck();
680
+ return this._deps ? M(this._deps) : !1;
595
681
  }
596
- }, Q = 0;
597
- function et() {
598
- const t = Q + 1 & H;
599
- return Q = t === 0 ? 1 : t, Q;
600
- }
601
- function K(t) {
602
- const e = t + 1 & H;
682
+ _isShallowDirty() {
683
+ return this._deps ? ce(this._deps) : !1;
684
+ }
685
+ }, yt = (t) => {
686
+ const e = t + 1 & Ct;
603
687
  return e === 0 ? 1 : e;
688
+ };
689
+ function it(t) {
690
+ return yt(t);
604
691
  }
605
- var st = 0, B = !1, _t = 0;
606
- function Tt() {
607
- return _t;
608
- }
609
- function ot() {
610
- return B ? (f && console.warn("startFlush() called during flush - ignored"), !1) : (B = !0, _t = et(), st = 0, !0);
611
- }
612
- function ut() {
613
- B = !1;
692
+ var fe = 0, Z = 1, at = 2, Q = 4, _e = at | Q;
693
+ function Ee() {
694
+ return {
695
+ size: 0,
696
+ epoch: 0,
697
+ batchQueueSize: 0,
698
+ state: fe,
699
+ batchDepth: 0,
700
+ maxFlushIterations: m.MAX_FLUSH_ITERATIONS,
701
+ sessionActive: !1,
702
+ sessionEpoch: 0,
703
+ sessionExecutionCount: 0,
704
+ activeBuffer: [],
705
+ standbyBuffer: [],
706
+ batchBuffer: [],
707
+ onOverflow: null
708
+ };
614
709
  }
615
- function mt() {
616
- if (!B) return 0;
617
- const t = ++st;
618
- if (t <= g.MAX_EXECUTIONS_PER_FLUSH) return t;
619
- throw new Error(`[atom-effect] Infinite loop detected: flush execution count exceeded ${g.MAX_EXECUTIONS_PER_FLUSH}`);
710
+ function Ut(t, e) {
711
+ const s = t.batchQueueSize;
712
+ if (s === 0) return;
713
+ const r = e(), i = t.batchBuffer, n = t.activeBuffer;
714
+ let o = t.size;
715
+ for (let u = 0; u < s; u++) {
716
+ const T = i[u];
717
+ T._nextEpoch !== r && (T._nextEpoch = r, n[o++] = T), i[u] = void 0;
718
+ }
719
+ t.size = o, t.batchQueueSize = 0, i.length > m.BATCH_QUEUE_SHRINK_THRESHOLD && (i.length = 0);
620
720
  }
621
- var Ot = class {
622
- constructor() {
623
- this._bufferIndex = 0, this._size = 0, this._epoch = 0, this._batchDepth = 0, this._batchQueueSize = 0, this._maxFlushIterations = g.MAX_FLUSH_ITERATIONS, this._isProcessing = !1, this._isFlushingSync = !1, this._buffer0 = [], this._buffer1 = [], this._batchQueue = [], this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
624
- }
625
- get queueSize() {
626
- return this._size + this._batchQueueSize;
627
- }
628
- get isBatching() {
629
- return this._batchDepth > 0;
630
- }
631
- schedule(t) {
632
- if (f && typeof t != "function" && (!t || typeof t.execute != "function"))
633
- throw new x(l.SCHEDULER_CALLBACK_MUST_BE_FUNCTION);
634
- const e = this._epoch;
635
- if (t._nextEpoch === e) return;
636
- if (t._nextEpoch = e, this._batchDepth > 0 || this._isFlushingSync) {
637
- this._batchQueue[this._batchQueueSize++] = t;
721
+ function Ft(t, e, s, r) {
722
+ let i = 0;
723
+ const n = t.maxFlushIterations;
724
+ for (; t.size > 0 || t.batchQueueSize > 0; ) {
725
+ if (++i > n) {
726
+ r(t);
638
727
  return;
639
728
  }
640
- const s = this._bufferIndex === 0 ? this._buffer0 : this._buffer1;
641
- s[this._size++] = t, this._isProcessing || this._flush();
642
- }
643
- _flush() {
644
- this._isProcessing || this._size === 0 && this._batchQueueSize === 0 || (this._isProcessing = !0, queueMicrotask(this._boundRunLoop));
729
+ t.batchQueueSize > 0 && Ut(t, e), t.size > 0 && s(t);
645
730
  }
646
- _runLoop() {
731
+ }
732
+ function wt(t, e) {
733
+ const s = t.activeBuffer, r = t.size;
734
+ t.activeBuffer = t.standbyBuffer, t.standbyBuffer = s, t.size = 0, e();
735
+ for (let i = 0; i < r; i++) {
736
+ const n = s[i];
737
+ s[i] = void 0;
647
738
  try {
648
- if (this._size === 0 && this._batchQueueSize === 0) return;
649
- const t = ot();
650
- this._drainQueue(), t && ut();
651
- } finally {
652
- this._isProcessing = !1;
739
+ typeof n == "function" ? n() : n.execute();
740
+ } catch (o) {
741
+ console.error(new nt("Error occurred during scheduler execution", o));
653
742
  }
654
743
  }
655
- _flushSync() {
656
- if (this._size === 0 && this._batchQueueSize === 0) return;
657
- const t = this._isFlushingSync;
658
- this._isFlushingSync = !0;
659
- const e = ot();
744
+ }
745
+ function kt(t) {
746
+ const e = t.size + t.batchQueueSize;
747
+ if (console.error(new nt(a.SCHEDULER_FLUSH_OVERFLOW(t.maxFlushIterations, e))), t.size = 0, t.activeBuffer.length = 0, t.standbyBuffer.length = 0, t.batchQueueSize = 0, t.batchBuffer.length = 0, t.onOverflow) try {
748
+ t.onOverflow(e);
749
+ } catch {
750
+ }
751
+ }
752
+ function O(t) {
753
+ return t.epoch = yt(t.epoch), t.epoch;
754
+ }
755
+ function Pt(t) {
756
+ return t.sessionActive ? (d && console.warn("startFlush() called during flush - ignored"), !1) : (t.sessionActive = !0, t.sessionEpoch = O(t), t.sessionExecutionCount = 0, !0);
757
+ }
758
+ function Lt(t) {
759
+ t.sessionActive = !1;
760
+ }
761
+ function de(t) {
762
+ if (!t.sessionActive) return 0;
763
+ const e = ++t.sessionExecutionCount;
764
+ if (e <= m.MAX_EXECUTIONS_PER_FLUSH) return e;
765
+ throw new Error(`[atom-effect] Infinite loop detected: flush execution count exceeded ${m.MAX_EXECUTIONS_PER_FLUSH}`);
766
+ }
767
+ function W(t, e) {
768
+ if (d && typeof e != "function" && (!e || typeof e.execute != "function"))
769
+ throw new nt(a.SCHEDULER_CALLBACK_MUST_BE_FUNCTION);
770
+ e._nextEpoch !== t.epoch && (e._nextEpoch = t.epoch, (t.state & _e) === 0 ? t.activeBuffer[t.size++] = e : t.batchBuffer[t.batchQueueSize++] = e, (t.state & Z) === 0 && (t.state |= Z, queueMicrotask(() => {
660
771
  try {
661
- this._mergeBatchQueue(), this._drainQueue();
772
+ if (t.size === 0 && t.batchQueueSize === 0) return;
773
+ const s = Pt(t);
774
+ Ft(t, () => O(t), (r) => wt(r, () => O(r)), (r) => kt(r)), s && Lt(t);
775
+ } catch (s) {
776
+ throw he(l), s;
662
777
  } finally {
663
- this._isFlushingSync = t, e && ut();
664
- }
665
- }
666
- _mergeBatchQueue() {
667
- const t = this._batchQueueSize;
668
- if (t === 0) return;
669
- const e = ++this._epoch | 0, s = this._batchQueue, i = this._bufferIndex === 0 ? this._buffer0 : this._buffer1;
670
- let n = this._size;
671
- for (let r = 0; r < t; r++) {
672
- const o = s[r];
673
- o._nextEpoch !== e && (o._nextEpoch = e, i[n++] = o), s[r] = void 0;
674
- }
675
- this._size = n, this._batchQueueSize = 0, s.length > g.BATCH_QUEUE_SHRINK_THRESHOLD && (s.length = 0);
676
- }
677
- _drainQueue() {
678
- let t = 0;
679
- for (; this._size > 0 || this._batchQueueSize > 0; ) {
680
- if (++t > this._maxFlushIterations) {
681
- this._handleFlushOverflow();
682
- return;
683
- }
684
- this._batchQueueSize > 0 && this._mergeBatchQueue(), this._size > 0 && this._processQueue();
778
+ t.state &= ~Z;
685
779
  }
780
+ })));
781
+ }
782
+ function pe(t) {
783
+ if (t.size === 0 && t.batchQueueSize === 0) return;
784
+ const e = t.state;
785
+ t.state |= at;
786
+ const s = Pt(t);
787
+ try {
788
+ Ut(t, () => O(t)), Ft(t, () => O(t), (r) => wt(r, () => O(r)), (r) => kt(r));
789
+ } finally {
790
+ t.state = e, s && Lt(t);
686
791
  }
687
- _processQueue() {
688
- const t = this._bufferIndex, e = t === 0 ? this._buffer0 : this._buffer1, s = this._size;
689
- this._bufferIndex = t ^ 1, this._size = 0, this._epoch = this._epoch + 1 | 0;
690
- for (let i = 0; i < s; i++) {
691
- const n = e[i];
692
- e[i] = void 0;
693
- try {
694
- typeof n == "function" ? n() : n.execute();
695
- } catch (r) {
696
- console.error(new x("Error occurred during scheduler execution", r));
697
- }
698
- }
699
- }
700
- _handleFlushOverflow() {
701
- const t = this._size + this._batchQueueSize;
702
- console.error(new x(l.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, t))), this._size = 0, this._buffer0.length = 0, this._buffer1.length = 0, this._batchQueueSize = 0, this._batchQueue.length = 0;
703
- const e = this.onOverflow;
704
- if (e) try {
705
- e(t);
706
- } catch {
707
- }
708
- }
709
- startBatch() {
710
- this._batchDepth++;
711
- }
712
- endBatch() {
713
- if (this._batchDepth === 0) {
714
- f && console.warn(l.SCHEDULER_END_BATCH_WITHOUT_START);
715
- return;
716
- }
717
- --this._batchDepth === 0 && (this._isFlushingSync || this._flushSync());
718
- }
719
- setMaxFlushIterations(t) {
720
- if (t < g.MIN_FLUSH_ITERATIONS) throw new x(`Max iterations must be at least ${g.MIN_FLUSH_ITERATIONS}`);
721
- this._maxFlushIterations = t;
792
+ }
793
+ function Ce(t) {
794
+ t.batchDepth++, t.state |= Q;
795
+ }
796
+ function Se(t) {
797
+ if (t.batchDepth === 0) {
798
+ d && console.warn(a.SCHEDULER_END_BATCH_WITHOUT_START);
799
+ return;
722
800
  }
723
- }, T = new Ot();
724
- function Ut(t) {
725
- if (f && typeof t != "function") throw new TypeError(l.BATCH_CALLBACK_MUST_BE_FUNCTION);
726
- T.startBatch();
801
+ --t.batchDepth === 0 && (t.state &= ~Q, (t.state & at) === 0 && pe(t));
802
+ }
803
+ function Et(t) {
804
+ return (t.state & Q) !== 0;
805
+ }
806
+ var p = Ee(), Mt = () => O(p), ge = () => p.sessionEpoch, De = () => de(p);
807
+ function Ie(t) {
808
+ if (d && typeof t != "function") throw new TypeError(a.BATCH_CALLBACK_MUST_BE_FUNCTION);
809
+ Ce(p);
727
810
  try {
728
811
  return t();
729
812
  } finally {
730
- T.endBatch();
813
+ Se(p);
731
814
  }
732
815
  }
733
- var R = null;
734
- function Lt(t) {
816
+ var F = null;
817
+ function Me(t) {
735
818
  return t ? new Promise((e, s) => {
736
- T.schedule(() => {
819
+ W(p, () => {
737
820
  try {
738
821
  t(), e();
739
- } catch (i) {
740
- s(i);
822
+ } catch (r) {
823
+ s(r);
741
824
  }
742
825
  });
743
- }) : R || (R = new Promise((e) => {
744
- T.schedule(() => {
745
- R = null, e();
826
+ }) : F || (F = new Promise((e) => {
827
+ W(p, () => {
828
+ F = null, e();
746
829
  });
747
- }), R);
830
+ }), F);
748
831
  }
749
- var Nt = class extends tt {
832
+ var Te = class extends lt {
750
833
  constructor(t, e) {
751
- super(), this[L] = p.Atom | p.Writable, this._value = t, this._equal = e.equal ?? Object.is, e.sync && (this.flags |= C.SYNC), f && d.attachDebugInfo(this, "atom", this.id, e.name);
834
+ super(), this[A] = h.Atom | h.Writable, this._value = t, this._equal = e.equal ?? Object.is, e.sync && (this.flags |= g.SYNC), S.attachDebugInfo(this, "atom", this.id, e.name);
752
835
  }
753
836
  get isNotificationScheduled() {
754
- return (this.flags & C.NOTIFICATION_SCHEDULED) !== 0;
837
+ return (this.flags & g.NOTIFICATION_SCHEDULED) !== 0;
755
838
  }
756
839
  get isSync() {
757
- return (this.flags & C.SYNC) !== 0;
840
+ return (this.flags & g.SYNC) !== 0;
758
841
  }
759
842
  get value() {
760
- const t = _.current;
761
- return t?.addDependency(this), this._value;
843
+ return l.current?.addDependency(this), this._value;
762
844
  }
763
845
  set value(t) {
846
+ if (this._equal(this._value, t)) return;
764
847
  const e = this._value;
765
- if (this._equal(e, t)) return;
766
- this._value = t, this.version = K(this.version), f && d.trackUpdate(this.id, d.getDebugName(this));
767
- const s = this.flags, i = C.NOTIFICATION_SCHEDULED;
768
- if ((s & i) !== 0) return;
769
- const n = this._slots;
770
- if (n === null || n.size === 0) return;
771
- this._pendingOldValue = e;
772
- const r = s | i;
773
- if (this.flags = r, (r & C.SYNC) !== 0 && !T.isBatching) {
774
- this._notifying === 0 && this._flushNotifications();
775
- return;
776
- }
777
- T.schedule(this);
848
+ this._value = t, this.version = it(this.version), d && S.trackUpdate(this.id, S.getDebugName(this)), this._scheduleNotification(e);
849
+ }
850
+ _scheduleNotification(t) {
851
+ const e = this.flags, s = g.NOTIFICATION_SCHEDULED;
852
+ (e & s) !== 0 || !this._slots?.length || (this._pendingOldValue = t, this.flags |= s, (e & g.SYNC) !== 0 && !Et(p) ? this.isNotifying || this._flushNotifications() : W(p, this));
778
853
  }
779
854
  execute() {
780
855
  this._flushNotifications();
781
856
  }
782
857
  _flushNotifications() {
783
- const t = C.NOTIFICATION_SCHEDULED, e = C.DISPOSED, s = C.SYNC, i = t | e;
784
- let n = this.flags;
785
- for (; (n & i) === t; ) {
786
- const r = this._pendingOldValue;
787
- this._pendingOldValue = void 0, this.flags = n &= ~t;
788
- const o = this._value;
789
- if (this._equal(o, r) || this._notifySubscribers(o, r), n = this.flags, (n & s) === 0 || T.isBatching) break;
858
+ const t = g.NOTIFICATION_SCHEDULED, e = t | g.DISPOSED, s = (this.flags & g.SYNC) !== 0 && !Et(p);
859
+ for (; (this.flags & e) === t; ) {
860
+ const r = this._pendingOldValue, i = this._value;
861
+ if (this._pendingOldValue = void 0, this.flags &= ~t, this._equal(i, r) || this._notifySubscribers(i, r), !s) break;
790
862
  }
791
863
  }
792
864
  peek() {
793
865
  return this._value;
794
866
  }
795
867
  dispose() {
796
- const t = this.flags, e = C.DISPOSED;
797
- (t & e) === 0 && (this.flags = t | e, this._slots?.clear(), this._value = void 0, this._pendingOldValue = void 0, this._equal = Object.is);
868
+ const t = g.DISPOSED;
869
+ (this.flags & t) === 0 && (this.flags |= t, this._slots?.clear(), this._value = void 0, this._pendingOldValue = void 0, this._equal = Object.is);
798
870
  }
799
871
  _deepDirtyCheck() {
800
872
  return !1;
801
873
  }
802
874
  };
803
- function Pt(t, e = {}) {
804
- return new Nt(t, e);
875
+ function Be(t, e = {}) {
876
+ return new Te(t, e);
805
877
  }
806
- var { IDLE: v, DIRTY: a, PENDING: b, RESOLVED: D, REJECTED: m, HAS_ERROR: V, RECOMPUTING: O, DISPOSED: z, IS_COMPUTED: F, FORCE_COMPUTE: Y } = $, bt = class extends tt {
878
+ function q(t, e) {
879
+ return !t || typeof t != "object" && typeof t != "function" ? !1 : !!(t[A] & e);
880
+ }
881
+ function xe(t) {
882
+ return q(t, h.Atom);
883
+ }
884
+ function ze(t) {
885
+ return q(t, h.Writable);
886
+ }
887
+ function He(t) {
888
+ return q(t, h.Computed);
889
+ }
890
+ function Ge(t) {
891
+ return q(t, h.Effect);
892
+ }
893
+ function k(t, e = !1) {
894
+ const s = {};
895
+ for (let r = 0; r < t.length; r++) {
896
+ const i = e ? t[r].peek() : t[r].value;
897
+ i && typeof i == "object" && Object.assign(s, i);
898
+ }
899
+ return s;
900
+ }
901
+ var { IDLE: P, DIRTY: D, PENDING: B, RESOLVED: I, REJECTED: N, HAS_ERROR: ht, RECOMPUTING: C, DISPOSED: V, IS_COMPUTED: dt, FORCE_COMPUTE: $ } = y, Bt = B | N, ve = P | $, xt = N | ht, tt = P | D | B | I | N | ht, Y = {
902
+ TO_RECOMPUTING: {
903
+ clear: $,
904
+ set: C
905
+ },
906
+ TO_RESOLVED: {
907
+ clear: tt | C,
908
+ set: I
909
+ },
910
+ TO_PENDING: {
911
+ clear: tt | C,
912
+ set: B
913
+ },
914
+ TO_REJECTED: {
915
+ clear: tt | C,
916
+ set: N | ht
917
+ }
918
+ }, j = (t, e) => t & ~e.clear | e.set;
919
+ function Oe(t, e, s, r) {
920
+ if ((t & I) !== 0) return e;
921
+ const i = r !== st, n = t & Bt;
922
+ if (n === 0) return e;
923
+ if (i) return r;
924
+ throw n === N ? s ?? /* @__PURE__ */ new Error("REJECTED without error") : new w(a.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
925
+ }
926
+ function me(t, e) {
927
+ const s = (t & Bt) !== 0;
928
+ return (t & ve) !== 0 || M(e) || !s && e.slots.size === 0;
929
+ }
930
+ function pt(t, e) {
931
+ const s = [], r = /* @__PURE__ */ new Set(), i = (n) => {
932
+ if (r.has(n.id)) return !1;
933
+ if (r.add(n.id), (n.flags & xt) !== 0 && (s.push(n.lastError ?? /* @__PURE__ */ new Error("Internal Inconsistency: MASK_ERROR flag set but error is null")), e))
934
+ return !0;
935
+ const o = n._deps;
936
+ if (o?.hasComputeds) for (let u = 0, T = o.slots.length; u < T; u++) {
937
+ const f = o.slots.at(u);
938
+ if (f?.node.isComputed && i(f.node)) return !0;
939
+ }
940
+ return !1;
941
+ };
942
+ return i(t), s;
943
+ }
944
+ var Ne = class extends lt {
807
945
  constructor(t, e = {}) {
808
- if (typeof t != "function") throw new w(l.COMPUTED_MUST_BE_FUNCTION);
809
- if (super(), this[L] = p.Atom | p.Computed, this._promiseId = 0, this._trackEpoch = U.UNINITIALIZED, this._trackCount = 0, this._error = null, this._deps = new ct(), this._value = void 0, this.flags = F | a | v, this._equal = e.equal ?? Object.is, this._computation = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : G, this._onError = e.onError ?? null, d.attachDebugInfo(this, "computed", this.id, e.name), e.lazy === !1) try {
946
+ if (typeof t != "function") throw new w(a.COMPUTED_MUST_BE_FUNCTION);
947
+ if (super(), this[A] = h.Atom | h.Computed, this._activeSessionId = 0, this._sessionCounter = 0, this._trackEpoch = L.UNINITIALIZED, this._trackCount = 0, this._error = null, this._deps = Tt(), this._value = void 0, this.flags = dt | D | P, this._equal = e.equal ?? Object.is, this._computation = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : st, this._onError = e.onError ?? null, S.attachDebugInfo(this, "computed", this.id, e.name), e.lazy === !1) try {
810
948
  this._recompute();
811
949
  } catch {
812
950
  }
813
951
  }
814
952
  get isDirty() {
815
- return (this.flags & a) !== 0;
953
+ return (this.flags & D) !== 0;
816
954
  }
817
955
  get isRejected() {
818
- return (this.flags & m) !== 0;
956
+ return (this.flags & N) !== 0;
819
957
  }
820
958
  get isRecomputing() {
821
- return (this.flags & O) !== 0;
959
+ return (this.flags & C) !== 0;
822
960
  }
823
961
  get value() {
824
- const t = _.current;
825
- t !== null && t.addDependency(this);
826
- const e = this.flags;
827
- if ((e & (D | a | v | z | O)) === D) return this._value;
828
- if ((e & z) !== 0) throw new w(l.COMPUTED_DISPOSED);
829
- if ((e & O) !== 0) {
830
- const n = this._defaultValue;
831
- if (n !== G) return n;
832
- throw new w(l.COMPUTED_CIRCULAR_DEPENDENCY);
833
- }
834
- if ((e & (a | v)) !== 0) {
835
- const n = this._deps;
836
- if ((e & (v | Y)) !== 0 || n.size === 0 || this._isDirty() ? this._recompute() : this.flags &= ~a, (this.flags & D) !== 0) return this._value;
837
- }
838
- const s = this._defaultValue, i = s !== G;
839
- if ((this.flags & b) !== 0) {
840
- if (i) return s;
841
- throw new w(l.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
962
+ if (l.current?.addDependency(this), this._isStable()) return this._value;
963
+ if (this._ensureNotDisposed(), (this.flags & C) !== 0) {
964
+ if (this._defaultValue !== st) return this._defaultValue;
965
+ throw new w(a.COMPUTED_CIRCULAR_DEPENDENCY);
842
966
  }
843
- if ((this.flags & m) !== 0) {
844
- if (i) return s;
845
- throw this._error;
846
- }
847
- return this._value;
967
+ return me(this.flags, this._deps) ? this._recompute() : this.flags &= ~D, Oe(this.flags, this._value, this._error, this._defaultValue);
968
+ }
969
+ _isStable() {
970
+ const t = I | D | P | V | C;
971
+ return (this.flags & t) === I;
972
+ }
973
+ _ensureNotDisposed() {
974
+ if ((this.flags & V) !== 0) throw new w(a.COMPUTED_DISPOSED);
848
975
  }
849
976
  peek() {
850
977
  return this._value;
851
978
  }
852
979
  get state() {
853
- const t = _.current;
854
- t !== null && t.addDependency(this);
855
- const e = this.flags;
856
- return (e & D) !== 0 ? P.RESOLVED : (e & b) !== 0 ? P.PENDING : (e & m) !== 0 ? P.REJECTED : P.IDLE;
980
+ l.current?.addDependency(this);
981
+ const t = this.flags;
982
+ return (t & I) !== 0 ? H.RESOLVED : (t & B) !== 0 ? H.PENDING : (t & N) !== 0 ? H.REJECTED : H.IDLE;
857
983
  }
858
984
  get hasError() {
859
- const t = _.current;
860
- if (t !== null && t.addDependency(this), (this.flags & (m | V)) !== 0) return !0;
861
- const e = this._deps;
862
- return e.hasComputeds ? X(() => {
863
- const s = e.size;
864
- for (let i = 0; i < s; i++) if (e.getAt(i)?.node.hasError) return !0;
865
- return !1;
866
- }) : !1;
985
+ return l.current?.addDependency(this), (this.flags & xt) !== 0 ? !0 : this._deps.hasComputeds ? _t(() => pt(this, !0).length > 0) : !1;
867
986
  }
868
987
  get isValid() {
869
988
  return !this.hasError;
870
989
  }
871
990
  get errors() {
872
- const t = _.current;
873
- t !== null && t.addDependency(this);
874
- const e = this._error, s = this._deps;
875
- if (!s.hasComputeds)
876
- return e === null ? it : Object.freeze([e]);
877
- const i = [];
878
- return e !== null && i.push(e), X(() => {
879
- const n = s.size;
880
- for (let r = 0; r < n; r++) {
881
- const o = s.getAt(r)?.node;
882
- o !== void 0 && (o.flags & F) !== 0 && this._accumulateErrors(o, i);
883
- }
884
- }), i.length === 0 ? it : Object.freeze(i);
885
- }
886
- _accumulateErrors(t, e) {
887
- const s = t._error;
888
- s !== null && !e.includes(s) && e.push(s);
889
- const i = t._deps;
890
- if (!i.hasComputeds) return;
891
- const n = i.size;
892
- for (let r = 0; r < n; r++) {
893
- const o = i.getAt(r)?.node;
894
- o !== void 0 && (o.flags & F) !== 0 && this._accumulateErrors(o, e);
895
- }
991
+ return l.current?.addDependency(this), this._deps.hasComputeds ? _t(() => Object.freeze(pt(this, !1))) : this._error ? Object.freeze([this._error]) : jt;
896
992
  }
897
993
  get lastError() {
898
- const t = _.current;
899
- return t !== null && t.addDependency(this), this._error;
994
+ return l.current?.addDependency(this), this._error;
900
995
  }
901
996
  get isPending() {
902
- const t = _.current;
903
- return t !== null && t.addDependency(this), (this.flags & b) !== 0;
997
+ return l.current?.addDependency(this), (this.flags & B) !== 0;
904
998
  }
905
999
  get isResolved() {
906
- const t = _.current;
907
- return t !== null && t.addDependency(this), (this.flags & D) !== 0;
1000
+ return l.current?.addDependency(this), (this.flags & I) !== 0;
908
1001
  }
909
1002
  invalidate() {
910
- this.flags |= Y, this._markDirty();
1003
+ this.flags |= $, this._markDirty();
911
1004
  }
912
1005
  dispose() {
913
- (this.flags & z) === 0 && (this._deps.disposeAll(), this._slots !== null && this._slots.clear(), this.flags = z | a | v, this._error = null, this._value = void 0, this._hotIndex = -1);
1006
+ (this.flags & V) === 0 && (Nt(this._deps), this._slots?.clear(), this.flags = V | D | P, this._error = null, this._value = void 0);
914
1007
  }
915
1008
  addDependency(t) {
916
1009
  const e = this._trackEpoch;
917
1010
  if (t._lastSeenEpoch === e) return;
918
1011
  t._lastSeenEpoch = e;
919
- const s = this._trackCount++, i = this._deps;
920
- let n = null;
921
- if (s < 4) s === 0 ? n = i._s0 : s === 1 ? n = i._s1 : s === 2 ? n = i._s2 : n = i._s3;
922
- else {
923
- const r = i._overflow;
924
- r !== null && (n = r[s - 4] ?? null);
925
- }
926
- if (n !== null && n.node === t) n.version = t.version;
927
- else if (!i.claimExisting(t, s)) {
928
- const r = new q(t, t.version, t.subscribe(this));
929
- i.insertNew(s, r);
930
- }
931
- (t.flags & F) !== 0 && (i.hasComputeds = !0);
1012
+ const s = this._trackCount++, r = this._deps, i = r.slots.at(s);
1013
+ i?.node === t ? i.version = t.version : Ot(r, t, s) || mt(r, s, bt(t, t.version, t.subscribe(this))), (t.flags & dt) !== 0 && (r.hasComputeds = !0);
932
1014
  }
933
1015
  _recompute() {
934
- if ((this.flags & O) !== 0) return;
935
- this.flags = (this.flags | O) & ~Y, this._trackEpoch = et(), this._trackCount = 0, this._deps.prepareTracking(), this._hotIndex = -1;
936
- let t = !1;
1016
+ if ((this.flags & C) !== 0) return;
1017
+ this.flags = j(this.flags, Y.TO_RECOMPUTING);
1018
+ const t = l.stack.length;
1019
+ this._startTracking();
1020
+ let e, s = !1, r;
937
1021
  try {
938
- const e = _.run(this, this._computation);
939
- this._deps.truncateFrom(this._trackCount), t = !0, Z(e) ? this._handleAsyncComputation(e) : this._finalizeResolution(e);
940
- } catch (e) {
941
- if (!t) try {
942
- this._deps.truncateFrom(this._trackCount);
943
- } catch (s) {
944
- f && console.warn("[atom-effect] _commitDeps failed during error recovery:", s);
1022
+ try {
1023
+ e = Rt(l, this, this._computation);
1024
+ } catch (i) {
1025
+ throw ct(l, t), i;
945
1026
  }
946
- this._handleError(e, l.COMPUTED_COMPUTATION_FAILED, !0);
947
- } finally {
948
- this._trackEpoch = U.UNINITIALIZED, this._trackCount = 0, this.flags &= ~O;
1027
+ } catch (i) {
1028
+ s = !0, r = i;
1029
+ }
1030
+ s ? (this._commitDeps(), this._handleError(r, a.COMPUTED_COMPUTATION_FAILED, !1)) : (this._commitDeps(), gt(e) ? this._handleAsyncComputation(e) : this._finalizeResolution(e)), this._trackEpoch = L.UNINITIALIZED, this._trackCount = 0, this.flags &= ~C;
1031
+ }
1032
+ _startTracking() {
1033
+ this._trackEpoch = Mt(), this._trackCount = 0, vt(this._deps);
1034
+ }
1035
+ _commitDeps() {
1036
+ try {
1037
+ ot(this._deps, this._trackCount);
1038
+ } catch (t) {
1039
+ d && console.warn("[atom-effect] _commitDeps failed during error recovery:", t);
949
1040
  }
950
1041
  }
951
1042
  _handleAsyncComputation(t) {
952
- this.flags = (this.flags | b) & ~(v | a | D | m), this._notifySubscribers(void 0, void 0), this._promiseId = (this._promiseId + 1) % Et.MAX_PROMISE_ID;
953
- const e = this._promiseId;
954
- t.then((s) => {
955
- if (e === this._promiseId) {
1043
+ this.flags = j(this.flags, Y.TO_PENDING), this._notifySubscribers(void 0, void 0);
1044
+ const e = ++this._sessionCounter;
1045
+ this._activeSessionId = e, t.then((s) => {
1046
+ if (this._activeSessionId === e) {
956
1047
  if (this._isDirty()) return this._markDirty();
957
1048
  this._finalizeResolution(s), this._notifySubscribers(s, void 0);
958
1049
  }
959
- }, (s) => e === this._promiseId && this._handleError(s, l.COMPUTED_ASYNC_COMPUTATION_FAILED));
1050
+ }, (s) => {
1051
+ this._activeSessionId === e && this._handleError(s, a.COMPUTED_ASYNC_COMPUTATION_FAILED);
1052
+ });
960
1053
  }
961
1054
  _handleError(t, e, s = !1) {
962
- const i = A(t, w, e);
963
- if ((!this.isRejected || this._error !== i) && (this.version = K(this.version)), this._error = i, this.flags = this.flags & ~(v | a | b | D) | m | V, this._onError) try {
964
- this._onError(i);
1055
+ const r = X(t, w, e), i = this._error;
1056
+ if ((!this.isRejected || i !== r) && (this.version = it(this.version)), this._error = r, this.flags = j(this.flags, Y.TO_REJECTED), this._onError) try {
1057
+ this._onError(r);
965
1058
  } catch (n) {
966
- console.error(l.CALLBACK_ERROR_IN_ERROR_HANDLER, n);
1059
+ console.error(a.CALLBACK_ERROR_IN_ERROR_HANDLER, n);
967
1060
  }
968
- if (this._notifySubscribers(void 0, void 0), s) throw i;
1061
+ if (this._notifySubscribers(void 0, void 0), s) throw r;
969
1062
  }
970
1063
  _finalizeResolution(t) {
971
- const e = this.flags;
972
- ((e & D) === 0 || !this._equal(this._value, t)) && (this.version = K(this.version)), this._value = t, this._error = null, this.flags = (e | D) & ~(v | a | b | m | V);
1064
+ ((this.flags & I) === 0 || !this._equal(this._value, t)) && (this.version = it(this.version)), this._value = t, this._error = null, this.flags = j(this.flags, Y.TO_RESOLVED);
973
1065
  }
974
1066
  execute() {
975
1067
  this._markDirty();
976
1068
  }
977
1069
  _markDirty() {
978
1070
  const t = this.flags;
979
- (t & (O | a)) === 0 && (this.flags = t | a, d.trackUpdate(this.id, d.getDebugName(this)), this._notifySubscribers(void 0, void 0));
1071
+ (t & (C | D)) !== 0 || !(t & $) && !this._isShallowDirty() || (this.flags = t | D, S.trackUpdate(this.id, S.getDebugName(this)), this._notifySubscribers(void 0, void 0));
980
1072
  }
981
1073
  _deepDirtyCheck() {
982
- const t = this._deps, e = t.size, s = this._hotIndex;
983
- return X(() => {
984
- if (s !== -1 && s < e) {
985
- const i = t.getAt(s);
986
- if (i !== null && this._checkLinkDirty(i)) return !0;
987
- }
988
- for (let i = 0; i < e; i++) {
989
- if (i === s) continue;
990
- const n = t.getAt(i);
991
- if (n !== null && this._checkLinkDirty(n))
992
- return this._hotIndex = i, !0;
993
- }
994
- return this._hotIndex = -1, !1;
995
- });
996
- }
997
- _checkLinkDirty(t) {
998
- const e = t.node;
999
- if ((e.flags & F) !== 0) try {
1000
- e.value;
1001
- } catch {
1002
- f && console.warn(`[atom-effect] Dependency #${e.id} threw during check`);
1003
- }
1004
- return e.version !== t.version;
1074
+ return M(this._deps);
1005
1075
  }
1006
1076
  };
1007
- function xt(t, e = {}) {
1008
- return new bt(t, e);
1077
+ function be(t, e = {}) {
1078
+ return new Ne(t, e);
1079
+ }
1080
+ function Ve(...t) {
1081
+ return be(() => k(t));
1082
+ }
1083
+ function Ae() {
1084
+ return {
1085
+ loopCount: 0,
1086
+ lastFlushEpoch: L.UNINITIALIZED,
1087
+ windowCount: 0,
1088
+ windowStart: 0,
1089
+ totalExecutions: 0
1090
+ };
1091
+ }
1092
+ function Re(t, e, s, r, i) {
1093
+ t.lastFlushEpoch !== s && (t.lastFlushEpoch = s, t.loopCount = 0), ++t.loopCount > e && i("per-effect"), r() > m.MAX_EXECUTIONS_PER_FLUSH && i("global"), t.totalExecutions++;
1009
1094
  }
1010
- var At = class extends tt {
1095
+ function ye(t, e, s) {
1096
+ if (!Number.isFinite(e)) return;
1097
+ const r = Date.now();
1098
+ if (r - t.windowStart >= et.EFFECT_FREQUENCY_WINDOW) {
1099
+ t.windowStart = r, t.windowCount = 1;
1100
+ return;
1101
+ }
1102
+ ++t.windowCount > e && s();
1103
+ }
1104
+ var Ue = class extends lt {
1011
1105
  constructor(t, e = {}) {
1012
- super(), this[L] = p.Effect, this._currentEpoch = U.UNINITIALIZED, this._lastFlushEpoch = U.UNINITIALIZED, this._executionsInEpoch = 0, this._executionCount = 0, this._windowStart = 0, this._windowCount = 0, this._execId = 0, this._trackCount = 0, this._cleanup = null, this._deps = new ct(), this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? g.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? g.MAX_EXECUTIONS_PER_EFFECT, this._sync ? this._notifyCallback = () => this.execute() : this._notifyCallback = () => T.schedule(this), d.attachDebugInfo(this, "effect", this.id, e.name);
1106
+ super(), this[A] = h.Effect, this._trackEpoch = L.UNINITIALIZED, this._trackCount = 0, this._trackSessionId = 0, this._budget = Ae(), this._deps = Tt(), this._cleanup = null, this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? m.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? m.MAX_EXECUTIONS_PER_EFFECT, this._notifyCallback = this._sync ? () => this.execute() : () => W(p, this), S.attachDebugInfo(this, "effect", this.id, e.name);
1013
1107
  }
1014
1108
  run() {
1015
- if (this.isDisposed) throw new N(l.EFFECT_DISPOSED);
1109
+ if (this.isDisposed) throw new R(a.EFFECT_DISPOSED);
1016
1110
  this.execute(!0);
1017
1111
  }
1018
1112
  dispose() {
1019
- this.isDisposed || (this.flags |= S.DISPOSED, this._execCleanup(), this._deps?.disposeAll());
1113
+ this.isDisposed || (this.flags |= v.DISPOSED, this._execCleanup(), this._deps && Nt(this._deps));
1020
1114
  }
1021
- addDependency(t) {
1022
- if ((this.flags & S.EXECUTING) === 0 || t._lastSeenEpoch === this._currentEpoch) return;
1023
- t._lastSeenEpoch = this._currentEpoch;
1024
- const e = this._trackCount++, s = this._deps, i = t.version;
1025
- let n = null;
1026
- if (e < 4) e === 0 ? n = s._s0 : e === 1 ? n = s._s1 : e === 2 ? n = s._s2 : n = s._s3;
1027
- else {
1028
- const r = s._overflow;
1029
- r !== null && (n = r[e - 4] ?? null);
1030
- }
1031
- n !== null && n.node === t ? n.version = i : s.claimExisting(t, e) || this._insertNewDependency(t, e, i), t.isComputed && !s.hasComputeds && (s.hasComputeds = !0);
1115
+ get executionCount() {
1116
+ return this._budget.totalExecutions;
1032
1117
  }
1033
- _insertNewDependency(t, e, s) {
1034
- let i;
1118
+ get isExecuting() {
1119
+ return (this.flags & v.EXECUTING) !== 0;
1120
+ }
1121
+ get isDisposed() {
1122
+ return (this.flags & v.DISPOSED) !== 0;
1123
+ }
1124
+ execute(t = !1) {
1125
+ if (!this._prepareExecution(t)) return;
1126
+ this._execCleanup(), this._startTracking();
1127
+ const e = l.stack.length;
1128
+ let s, r = !1, i;
1035
1129
  try {
1036
- i = new q(t, s, t.subscribe(this._notifyCallback));
1037
- } catch (n) {
1038
- const r = A(n, N, l.EFFECT_EXECUTION_FAILED);
1039
- if (console.error(r), this._onError) try {
1040
- this._onError(r);
1041
- } catch {
1130
+ try {
1131
+ s = Rt(l, this, this._fn);
1132
+ } catch (n) {
1133
+ throw ct(l, e), n;
1042
1134
  }
1043
- i = new q(t, s, void 0);
1135
+ } catch (n) {
1136
+ r = !0, i = n;
1044
1137
  }
1045
- this._deps.insertNew(e, i);
1138
+ this._commitDeps(), r ? this._handleExecutionError(i) : this._handleResult(s), this.flags &= ~v.EXECUTING;
1046
1139
  }
1047
- execute(t = !1) {
1048
- const e = this.flags;
1049
- if ((e & (S.DISPOSED | S.EXECUTING)) !== 0) return;
1050
- const s = this._deps;
1051
- if (!t && s.physicalSize > 0 && !this._isDirty()) return;
1052
- this._checkInfiniteLoops(), d.trackUpdate(this.id, d.getDebugName(this)), this.flags = e | S.EXECUTING, this._execCleanup(), this._currentEpoch = et(), this._trackCount = 0, s.prepareTracking(), this._hotIndex = -1;
1053
- let i = !1;
1140
+ _prepareExecution(t) {
1141
+ return (this.flags & (v.DISPOSED | v.EXECUTING)) !== 0 || !(t || this._deps.slots.length === 0 || this._isDirty()) ? !1 : (this._validateBudget(), S.trackUpdate(this.id, S.getDebugName(this)), this.flags |= v.EXECUTING, !0);
1142
+ }
1143
+ addDependency(t) {
1144
+ if (!this.isExecuting || t._lastSeenEpoch === this._trackEpoch) return;
1145
+ t._lastSeenEpoch = this._trackEpoch;
1146
+ const e = this._trackCount++, s = this._deps, r = t.version, i = s.slots.at(e);
1147
+ i?.node === t ? i.version = r : Ot(s, t, e) || this._insertNewDependency(t, e, r), t.isComputed && !s.hasComputeds && (s.hasComputeds = !0);
1148
+ }
1149
+ _insertNewDependency(t, e, s) {
1150
+ const r = bt(t, s, t.subscribe(this._notifyCallback));
1151
+ mt(this._deps, e, r);
1152
+ }
1153
+ _startTracking() {
1154
+ this._trackEpoch = Mt(), this._trackCount = 0, vt(this._deps);
1155
+ }
1156
+ _commitDeps() {
1054
1157
  try {
1055
- const n = _.run(this, this._fn);
1056
- s.truncateFrom(this._trackCount), i = !0, typeof n == "function" ? this._cleanup = n : Z(n) ? this._handleAsyncResult(n) : this._cleanup = null;
1057
- } catch (n) {
1058
- if (!i) try {
1059
- s.truncateFrom(this._trackCount);
1060
- } catch (r) {
1061
- f && console.warn("[atom-effect] _commitDeps failed during error recovery:", r);
1062
- }
1063
- this._handleExecutionError(n), this._cleanup = null;
1064
- } finally {
1065
- this.flags &= ~S.EXECUTING;
1158
+ ot(this._deps, this._trackCount);
1159
+ } catch (t) {
1160
+ d && console.warn("[atom-effect] _commitDeps failed during error recovery:", t);
1066
1161
  }
1067
1162
  }
1163
+ _handleResult(t) {
1164
+ typeof t == "function" ? this._cleanup = t : gt(t) ? this._handleAsyncResult(t) : this._cleanup = null;
1165
+ }
1068
1166
  _handleAsyncResult(t) {
1069
- const e = ++this._execId;
1167
+ const e = ++this._trackSessionId;
1070
1168
  t.then((s) => {
1071
- if (e !== this._execId || (this.flags & S.DISPOSED) !== 0) {
1169
+ if (this._trackSessionId !== e || this.isDisposed) {
1072
1170
  if (typeof s == "function") try {
1073
1171
  s();
1074
- } catch (i) {
1075
- this._handleExecutionError(i, l.EFFECT_CLEANUP_FAILED);
1172
+ } catch (r) {
1173
+ this._handleExecutionError(r, a.EFFECT_CLEANUP_FAILED);
1076
1174
  }
1077
1175
  return;
1078
1176
  }
1079
1177
  typeof s == "function" && (this._cleanup = s);
1080
- }, (s) => e === this._execId && this._handleExecutionError(s));
1081
- }
1082
- _isDirty() {
1083
- const t = this._deps, e = t.size;
1084
- if (e === 0) return !1;
1085
- const s = this._hotIndex;
1086
- if (s !== -1 && s < e) {
1087
- const i = t.getAt(s);
1088
- if (i !== null) {
1089
- const n = i.node;
1090
- if (!n.isComputed && n.version !== i.version) return !0;
1091
- }
1092
- }
1093
- return this._deepDirtyCheck();
1094
- }
1095
- _deepDirtyCheck() {
1096
- const t = this._deps, e = t.size, s = this._hotIndex, i = _.current;
1097
- _.current = null;
1098
- try {
1099
- for (let n = 0; n < e; n++) {
1100
- if (n === s) continue;
1101
- const r = t.getAt(n);
1102
- if (r === null) continue;
1103
- const o = r.node;
1104
- if (o.isComputed) try {
1105
- o.value;
1106
- } catch {
1107
- f && console.warn(`[atom-effect] Dependency #${o.id} error in check`);
1108
- }
1109
- if (o.version !== r.version)
1110
- return this._hotIndex = n, !0;
1111
- }
1112
- return this._hotIndex = -1, !1;
1113
- } finally {
1114
- _.current = i;
1115
- }
1178
+ }, (s) => {
1179
+ this._trackSessionId === e && this._handleExecutionError(s);
1180
+ });
1116
1181
  }
1117
1182
  _execCleanup() {
1118
1183
  const t = this._cleanup;
1119
- if (t != null) {
1184
+ if (t) {
1120
1185
  this._cleanup = null;
1121
1186
  try {
1122
1187
  t();
1123
1188
  } catch (e) {
1124
- this._handleExecutionError(e, l.EFFECT_CLEANUP_FAILED);
1189
+ this._handleExecutionError(e, a.EFFECT_CLEANUP_FAILED);
1125
1190
  }
1126
1191
  }
1127
1192
  }
1128
- _checkInfiniteLoops() {
1129
- const t = Tt();
1130
- this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), mt() > g.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, f && this._checkFrequencyLimit();
1131
- }
1132
- _checkFrequencyLimit() {
1133
- if (!Number.isFinite(this._maxExecutions)) return;
1134
- const t = Date.now();
1135
- if (t - this._windowStart >= W.EFFECT_FREQUENCY_WINDOW) {
1136
- this._windowStart = t, this._windowCount = 1;
1137
- return;
1138
- }
1139
- if (++this._windowCount > this._maxExecutions) {
1140
- const e = new N(l.EFFECT_FREQUENCY_LIMIT_EXCEEDED);
1141
- throw this.dispose(), this._handleExecutionError(e), e;
1142
- }
1143
- }
1144
- get executionCount() {
1145
- return this._executionCount;
1146
- }
1147
- get isExecuting() {
1148
- return (this.flags & S.EXECUTING) !== 0;
1193
+ _validateBudget() {
1194
+ Re(this._budget, this._maxExecutionsPerFlush, ge(), De, (t) => this._abortExecution(t)), d && ye(this._budget, this._maxExecutions, () => {
1195
+ const t = new R(a.EFFECT_FREQUENCY_LIMIT_EXCEEDED);
1196
+ throw this.dispose(), this._handleExecutionError(t), t;
1197
+ });
1149
1198
  }
1150
- _throwInfiniteLoopError(t) {
1151
- const e = new N(`Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${st}`);
1199
+ _abortExecution(t) {
1200
+ const e = new R(t === "per-effect" ? `Infinite loop detected (per-effect): executed ${this._budget.loopCount} times in current flush.` : "Infinite loop detected (global): exceeded total execution limit per flush.");
1152
1201
  throw this.dispose(), console.error(e), e;
1153
1202
  }
1154
- _handleExecutionError(t, e = l.EFFECT_EXECUTION_FAILED) {
1155
- const s = A(t, N, e);
1203
+ _handleExecutionError(t, e = a.EFFECT_EXECUTION_FAILED) {
1204
+ const s = X(t, R, e);
1156
1205
  if (console.error(s), this._onError) try {
1157
1206
  this._onError(s);
1158
- } catch (i) {
1159
- console.error(A(i, N, l.CALLBACK_ERROR_IN_ERROR_HANDLER));
1207
+ } catch (r) {
1208
+ console.error(X(r, R, a.CALLBACK_ERROR_IN_ERROR_HANDLER));
1160
1209
  }
1161
1210
  }
1211
+ _isDirty() {
1212
+ return M(this._deps);
1213
+ }
1214
+ _deepDirtyCheck() {
1215
+ return M(this._deps);
1216
+ }
1162
1217
  };
1163
- function Mt(t, e = {}) {
1164
- if (typeof t != "function") throw new N(l.EFFECT_MUST_BE_FUNCTION);
1165
- const s = new At(t, e);
1218
+ function Ye(t, e = {}) {
1219
+ if (typeof t != "function") throw new R(a.EFFECT_MUST_BE_FUNCTION);
1220
+ const s = new Ue(t, e);
1166
1221
  return s.execute(), s;
1167
1222
  }
1168
- function ft(t, e, s, i) {
1169
- if (s === e.length) return i;
1170
- const n = e[s];
1171
- if (n === "__proto__" || n === "constructor" || n === "prototype") return t;
1172
- const r = t != null && typeof t == "object" ? t : {}, o = r[n], u = ft(o, e, s + 1, i);
1173
- if (Object.is(o, u)) return t;
1174
- if (Array.isArray(r)) {
1175
- const I = r.slice(), E = +n;
1176
- return n.trim() !== "" && E >= 0 && E % 1 === 0 ? I[E] = u : I[n] = u, I;
1177
- }
1178
- const c = { ...r };
1179
- return c[n] = u, c;
1223
+ var zt = /* @__PURE__ */ new Set([
1224
+ "__proto__",
1225
+ "constructor",
1226
+ "prototype"
1227
+ ]);
1228
+ function Fe(t, e, s) {
1229
+ if (Array.isArray(t)) {
1230
+ const n = [...t];
1231
+ return n[e] = s, n;
1232
+ }
1233
+ if (t instanceof Map) {
1234
+ const n = new Map(t);
1235
+ return n.set(e, s), n;
1236
+ }
1237
+ const r = Object.getPrototypeOf(t);
1238
+ if (r === Object.prototype || r === null) return {
1239
+ ...t,
1240
+ [e]: s
1241
+ };
1242
+ const i = Object.create(r);
1243
+ return Object.assign(i, t), i[e] = s, i;
1244
+ }
1245
+ function Ht(t, e, s, r) {
1246
+ if (s === e.length) return r;
1247
+ const i = e[s];
1248
+ if (zt.has(i) || t == null || typeof t != "object") return t;
1249
+ const n = t instanceof Map ? t.get(i) : t[i], o = Ht(n, e, s + 1, r);
1250
+ return Object.is(n, o) ? t : Fe(t, i, o);
1180
1251
  }
1181
- function k(t, e) {
1252
+ function we(t, e) {
1182
1253
  let s = t;
1183
- const i = e.length;
1184
- for (let n = 0; n < i; n++) {
1254
+ const r = e.length;
1255
+ for (let i = 0; i < r; i++) {
1185
1256
  if (s == null) return;
1186
- const r = e[n];
1187
- if (r === "__proto__" || r === "constructor" || r === "prototype") return;
1188
- s = s[r];
1257
+ s instanceof Map ? s = s.get(e[i]) : s = s[e[i]];
1189
1258
  }
1190
1259
  return s;
1191
1260
  }
1192
- function at(t, e) {
1193
- const s = e.includes(".") ? e.split(".") : [e], i = /* @__PURE__ */ new Set();
1261
+ function Gt(t, e) {
1262
+ const s = e.split("."), r = s.some((f) => zt.has(f)), i = /* @__PURE__ */ new Set();
1263
+ let n = null, o;
1264
+ const u = (f) => r ? void 0 : we(f, s), T = () => {
1265
+ const f = u(t.peek());
1266
+ if (!Object.is(f, o)) {
1267
+ const x = o;
1268
+ o = f, i.forEach((z) => z(f, x));
1269
+ }
1270
+ };
1194
1271
  return {
1195
1272
  get value() {
1196
- return k(t.value, s);
1273
+ return u(t.value);
1197
1274
  },
1198
- set value(r) {
1199
- const o = t.peek(), u = ft(o, s, 0, r);
1200
- u !== o && (t.value = u);
1275
+ set value(f) {
1276
+ if (r) return;
1277
+ const x = t.peek(), z = Ht(x, s, 0, f);
1278
+ z !== x && (t.value = z);
1201
1279
  },
1202
- peek: () => k(t.peek(), s),
1203
- subscribe(r) {
1204
- let o = k(t.peek(), s);
1205
- const u = t.subscribe((c) => {
1206
- const I = k(c, s);
1207
- if (!Object.is(I, o)) {
1208
- const E = o;
1209
- o = I, r(I, E);
1210
- }
1211
- });
1212
- return i.add(u), () => {
1213
- u(), i.delete(u);
1280
+ peek: () => u(t.peek()),
1281
+ subscribe(f) {
1282
+ return i.size === 0 && (o = u(t.peek()), n = t.subscribe(T)), i.add(f), () => {
1283
+ i.delete(f), i.size === 0 && n && (n(), n = null);
1214
1284
  };
1215
1285
  },
1216
1286
  subscriberCount: () => i.size,
1217
1287
  dispose: () => {
1218
- i.forEach((r) => r()), i.clear();
1288
+ n?.(), n = null, i.clear();
1289
+ },
1290
+ [A]: h.Atom | h.Writable
1291
+ };
1292
+ }
1293
+ var je = (t, e) => Gt(t, e), Xe = (t) => (e) => Gt(t, e);
1294
+ function We(...t) {
1295
+ let e;
1296
+ const s = /* @__PURE__ */ new Set(), r = [], i = () => {
1297
+ const n = k(t, !0);
1298
+ if (!qt(n, e)) {
1299
+ const o = e;
1300
+ e = n;
1301
+ for (const u of s) u(n, o);
1302
+ }
1303
+ };
1304
+ return {
1305
+ get value() {
1306
+ return k(t);
1307
+ },
1308
+ set value(n) {
1309
+ Ie(() => {
1310
+ for (let o = 0; o < t.length; o++) t[o].value = n;
1311
+ });
1312
+ },
1313
+ peek: () => k(t, !0),
1314
+ subscribe: (n) => {
1315
+ if (s.size === 0) {
1316
+ e = k(t, !0);
1317
+ for (let o = 0; o < t.length; o++) r.push(t[o].subscribe(i));
1318
+ }
1319
+ return s.add(n), () => {
1320
+ if (s.delete(n), s.size === 0) {
1321
+ for (const o of r) o();
1322
+ r.length = 0;
1323
+ }
1324
+ };
1325
+ },
1326
+ subscriberCount: () => s.size,
1327
+ dispose: () => {
1328
+ for (const n of r) n();
1329
+ r.length = 0, s.clear();
1219
1330
  },
1220
- [L]: p.Atom | p.Writable
1331
+ [A]: h.Atom | h.Writable
1221
1332
  };
1222
1333
  }
1223
- var zt = (t, e) => at(t, e), kt = (t) => (e) => at(t, e);
1224
1334
  export {
1225
- P as AsyncState,
1226
- y as AtomError,
1335
+ H as AsyncState,
1336
+ b as AtomError,
1337
+ A as BRAND,
1338
+ h as BrandFlags,
1227
1339
  w as ComputedError,
1228
- W as DEBUG_CONFIG,
1229
- N as EffectError,
1230
- g as SCHEDULER_CONFIG,
1231
- x as SchedulerError,
1232
- Lt as aeNextTick,
1233
- Pt as atom,
1234
- at as atomLens,
1235
- Ut as batch,
1236
- zt as composeLens,
1237
- xt as computed,
1238
- Mt as effect,
1239
- k as getPathValue,
1240
- T as globalScheduler,
1241
- wt as isAtom,
1242
- Rt as isComputed,
1243
- Ft as isEffect,
1244
- kt as lensFor,
1245
- d as runtimeDebug,
1246
- ft as setDeepValue,
1247
- X as untracked
1340
+ R as EffectError,
1341
+ d as IS_DEV,
1342
+ m as SCHEDULER_CONFIG,
1343
+ nt as SchedulerError,
1344
+ Me as aeNextTick,
1345
+ Be as atom,
1346
+ Gt as atomLens,
1347
+ Ie as batch,
1348
+ je as composeLens,
1349
+ be as computed,
1350
+ Ye as effect,
1351
+ we as getPathValue,
1352
+ p as globalScheduler,
1353
+ xe as isAtom,
1354
+ He as isComputed,
1355
+ Ge as isEffect,
1356
+ gt as isPromise,
1357
+ ze as isWritable,
1358
+ Xe as lensFor,
1359
+ Ve as mergeAtoms,
1360
+ We as mergeLenses,
1361
+ S as runtimeDebug,
1362
+ Ht as setDeepValue,
1363
+ _t as untracked
1248
1364
  };
1249
1365
 
1250
1366
  //# sourceMappingURL=index.mjs.map