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