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