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