reflex-quant 1.2.0 → 1.3.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.
@@ -1,4 +1,4 @@
1
- /* reflex-quant 1.2.0 | MIT */
1
+ /* reflex-quant 1.3.1 | MIT */
2
2
  "use strict";
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -19,11 +19,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0
19
19
  var index_exports = {};
20
20
  __export(index_exports, {
21
21
  batch: () => batch,
22
+ compute: () => compute,
22
23
  computed: () => computed,
23
24
  dispose: () => dispose2,
24
25
  effect: () => effect,
25
26
  get: () => get,
26
27
  peek: () => peek2,
28
+ read: () => read2,
29
+ readSignal: () => readSignal,
27
30
  set: () => set,
28
31
  signal: () => signal,
29
32
  typeOf: () => typeOf,
@@ -33,7 +36,7 @@ __export(index_exports, {
33
36
  module.exports = __toCommonJS(index_exports);
34
37
 
35
38
  // src/graph.ts
36
- var UNREAD = -1, SKIPPED = -2, SETTLE = 536870911, SCAN = 8, TAIL = 2, KEEP = 256, TRIM = 512;
39
+ var UNREAD = -1, SKIPPED = -2, EXPIRED = -3, SETTLE = 536870911, SCAN = 8, TAIL = 2, KEEP = 256, TRIM = 512;
37
40
  function createNode(fn, value, flags) {
38
41
  return {
39
42
  flags,
@@ -80,28 +83,39 @@ function trim() {
80
83
  stack.length > TRIM && top === 0 && (stack.length = KEEP);
81
84
  }
82
85
  function write(s, value) {
83
- if (!compare(s.value, value)) {
84
- s.value = value, s.ver = s.ver + 1 & 1073741823 /* Mask */;
85
- for (let e = s.firstSub; e !== null; e = e.nextSub) {
86
- let n = e.to, f = n.flags;
87
- if ((f & 4 /* Busy */) !== 0) {
88
- if ((f & 1 /* Stale */) === 0)
89
- if (n.flags = f | 1 /* Stale */, (f & 16 /* Watch */) !== 0) wake(n);
90
- else {
91
- let subs = n.firstSub;
92
- subs !== null && mark(subs);
93
- }
94
- } else if ((f & 16 /* Watch */) !== 0)
95
- n.flags = f | 2 /* Dirty */ | 32 /* Woken */, wake(n);
96
- else if ((f & 1 /* Stale */) !== 0)
97
- n.flags = f | 2 /* Dirty */;
86
+ let old = s.value;
87
+ if (old === value ? value !== 0 || 1 / old === 1 / value : old !== old && value !== value)
88
+ return !1;
89
+ s.value = value;
90
+ let e = s.firstSub;
91
+ if (e === null) return !1;
92
+ for (bump(s); e !== null; e = e.nextSub) {
93
+ let n = e.to, f = n.flags;
94
+ if ((f & 5) === 4 /* Busy */)
95
+ if (n.flags = f | 1 /* Stale */, (f & 16 /* Watch */) !== 0) wake(n);
98
96
  else {
99
- n.flags = f | 2 /* Dirty */ | 1 /* Stale */;
100
97
  let subs = n.firstSub;
101
98
  subs !== null && mark(subs);
102
99
  }
100
+ else if ((f & 16 /* Watch */) !== 0)
101
+ n.flags = f | 2 /* Dirty */ | 32 /* Woken */, wake(n);
102
+ else if ((f & 1 /* Stale */) !== 0)
103
+ n.flags = f | 2 /* Dirty */;
104
+ else {
105
+ n.flags = f | 2 /* Dirty */ | 1 /* Stale */;
106
+ let subs = n.firstSub;
107
+ subs !== null && mark(subs);
103
108
  }
104
109
  }
110
+ return !0;
111
+ }
112
+ function bump(n) {
113
+ let version = n.ver + 1 & 1073741823 /* Mask */;
114
+ version === 0 && expire(n), n.ver = version;
115
+ }
116
+ function expire(n) {
117
+ for (let e = n.firstSub; e !== null; e = e.nextSub)
118
+ e.ver >= 0 && (e.ver = EXPIRED);
105
119
  }
106
120
  function mark(first) {
107
121
  let base = top, e = first;
@@ -129,15 +143,34 @@ function mark(first) {
129
143
  }
130
144
  }
131
145
  function read(n) {
132
- let value = peek(n), c = active;
133
- if (c !== null) {
134
- let last = cursor, next = last === null ? c.firstDep : last.nextDep;
135
- next !== null && next.from === n ? (cursor = next, next.ver = n.ver) : last !== null ? last.from !== n && miss(c, n, last, next) : next !== null ? miss(c, n, last, next) : link(c, n, null, null);
136
- }
137
- return value;
146
+ (n.flags & 47 /* Pull */) !== 0 && pull(n);
147
+ let c = active;
148
+ if (c === null) return n.value;
149
+ let last = cursor, next = last === null ? c.firstDep : last.nextDep;
150
+ if (next !== null && next.from === n)
151
+ return cursor = next, next.ver = n.ver, n.value;
152
+ if (last !== null) {
153
+ if (last.from === n)
154
+ return n.value;
155
+ miss(c, n, last, next);
156
+ } else next !== null ? miss(c, n, last, next) : attach(c, n, null, null);
157
+ return n.value;
158
+ }
159
+ function track(n) {
160
+ let c = active;
161
+ if (c === null) return n.value;
162
+ let last = cursor, next = last === null ? c.firstDep : last.nextDep;
163
+ if (next !== null && next.from === n)
164
+ return cursor = next, next.ver = n.ver, n.value;
165
+ if (last !== null) {
166
+ if (last.from === n)
167
+ return n.value;
168
+ miss(c, n, last, next);
169
+ } else next !== null ? miss(c, n, last, next) : attach(c, n, null, null);
170
+ return n.value;
138
171
  }
139
172
  function peek(n) {
140
- return (n.flags & 39 /* Pull */) !== 0 && pull(n), n.value;
173
+ return (n.flags & 47 /* Pull */) !== 0 && pull(n), n.value;
141
174
  }
142
175
  function pull(root) {
143
176
  let f = root.flags;
@@ -146,26 +179,32 @@ function pull(root) {
146
179
  verify(root, f);
147
180
  }
148
181
  function verify(root, f) {
149
- root.flags = f & -2 | 32 /* Checking */;
182
+ root.flags = f & -10 | 32 /* Checking */;
150
183
  let base = top, n = root, e = root.firstDep;
151
184
  try {
152
185
  for (; ; ) {
153
186
  settled: {
154
187
  for (; e !== null; ) {
155
188
  let s = e.from, sf = s.flags;
156
- if ((sf & 39 /* Pull */) !== 0) {
157
- if ((sf & 36 /* Active */) !== 0) throw new Error("Cycle detected");
189
+ if ((sf & 47 /* Pull */) === 0) {
190
+ if (s.ver === e.ver) {
191
+ e = e.nextDep;
192
+ continue;
193
+ }
194
+ } else {
195
+ if ((sf & 36 /* Active */) !== 0)
196
+ throw new Error("Cycle detected");
158
197
  if ((sf & 2 /* Dirty */) === 0) {
159
- s.flags = sf & -2 | 32 /* Checking */, stack[top++] = e, n = s, e = s.firstDep;
198
+ s.flags = sf & -10 | 32 /* Checking */, stack[top++] = e, n = s, e = s.firstDep;
199
+ continue;
200
+ }
201
+ if (exec(s), s.ver === e.ver) {
202
+ e = e.nextDep;
160
203
  continue;
161
204
  }
162
- exec(s);
163
- }
164
- if (s.ver !== e.ver) {
165
- n.fn !== null && exec(n);
166
- break settled;
167
205
  }
168
- e = e.nextDep;
206
+ n.fn !== null && exec(n);
207
+ break settled;
169
208
  }
170
209
  n.flags &= -33;
171
210
  }
@@ -183,26 +222,47 @@ function verify(root, f) {
183
222
  }
184
223
  }
185
224
  } catch (error) {
186
- for (n.fn !== null && (n.flags = (n.flags | 2 /* Dirty */) & -33); top > base; ) {
187
- let p = stack[--top].to;
188
- p.fn !== null && (p.flags = (p.flags | 2 /* Dirty */) & -33), stack[top] = null;
225
+ for (; ; ) {
226
+ if (n.fn !== null) {
227
+ let flags = n.flags & -42;
228
+ n.flags = (flags & 2 /* Dirty */) === 0 ? flags | 8 /* Unverified */ : flags;
229
+ }
230
+ if (top === base) break;
231
+ n = stack[--top].to, stack[top] = null;
189
232
  }
190
233
  throw trim(), error;
191
234
  }
192
235
  }
193
- function check(w) {
194
- let tentative = w.gen + 1 & 1073741823 /* Mask */, e = w.firstDep;
195
- if ((w.flags & 2 /* Dirty */) === 0)
196
- for (; e !== null; e = e.nextDep) {
197
- let s = e.from;
198
- if ((s.flags & 39 /* Pull */) !== 0 && e.gen !== tentative && pull(s), s.ver !== e.ver) {
236
+ function check(w, flags) {
237
+ let tentative = w.gen + 1 & 1073741823 /* Mask */;
238
+ for (; ; ) {
239
+ let e = w.firstDep;
240
+ if ((flags & 2 /* Dirty */) === 0)
241
+ for (; e !== null; ) {
242
+ let s = e.from;
243
+ if ((s.flags & 47 /* Pull */) === 0) {
244
+ if (s.ver === e.ver) {
245
+ e = e.nextDep;
246
+ continue;
247
+ }
248
+ } else if (pull(s), s.ver === e.ver) {
249
+ e = e.nextDep;
250
+ continue;
251
+ }
199
252
  w.flags |= 2 /* Dirty */, e = e.nextDep;
200
253
  break;
201
254
  }
255
+ for (; e !== null; ) {
256
+ let s = e.from;
257
+ if ((s.flags & 47 /* Pull */) === 0 || e.gen === tentative) {
258
+ e = e.nextDep;
259
+ continue;
260
+ }
261
+ pull(s), e = e.nextDep;
202
262
  }
203
- for (; e !== null; e = e.nextDep) {
204
- let s = e.from;
205
- (s.flags & 39 /* Pull */) !== 0 && e.gen !== tentative && pull(s);
263
+ if (w.fn === null) return 0;
264
+ if (flags = w.flags, (flags & 1 /* Stale */) === 0) return flags;
265
+ w.flags = flags & -2;
206
266
  }
207
267
  }
208
268
  function untracked(fn) {
@@ -216,23 +276,25 @@ function untracked(fn) {
216
276
  }
217
277
  function exec(n) {
218
278
  let prev = active, prevCursor = cursor, base = top;
219
- n.flags = n.flags & -36 | 4 /* Busy */, active = n, cursor = null;
279
+ n.flags = n.flags & -44 | 4 /* Busy */, active = n, cursor = null;
220
280
  let value;
221
281
  try {
222
282
  value = n.fn(n.value);
223
283
  } catch (error) {
224
284
  let last2 = cursor;
225
- throw active = prev, cursor = prevCursor, fail(n, base, last2), error;
285
+ throw active = prev, cursor = prevCursor, n.flags = n.flags & -5 | (n.fn !== null ? 2 /* Dirty */ : 0), fail(n, base, last2), error;
226
286
  }
227
287
  let last = cursor;
228
288
  active = prev, cursor = prevCursor;
229
289
  let f = n.flags;
230
- if (last !== null && last.from.slot === last && unindex(n, base), n.flags = f & -5, n.fn === null) return detach(n);
231
- (last === null ? n.firstDep : last.nextDep) !== null && cutDeps(n, last), n.gen = n.gen + 1 & 1073741823 /* Mask */, (n.gen & SETTLE) === 0 && settleDeps(n), compare(value, n.value) === !1 && (n.value = value, n.ver = n.ver + 1 & 1073741823 /* Mask */);
290
+ if (last !== null && last.from.slot === last && unindex(n, base), n.flags = f & -5, n.fn === null) return disposed(n, value);
291
+ (last === null ? n.firstDep : last.nextDep) !== null && cutDeps(n, last), n.gen = n.gen + 1 & 1073741823 /* Mask */, (n.gen & SETTLE) === 0 && settleDeps(n), compare(value, n.value) === !1 && (n.value = value, bump(n));
292
+ }
293
+ function disposed(n, value) {
294
+ detach(n), (n.flags & 16 /* Watch */) === 0 && compare(value, n.value) === !1 && (n.value = value, bump(n));
232
295
  }
233
296
  function fail(n, base, last) {
234
- if (last !== null && last.from.slot === last && unindex(n, base), n.flags &= -5, n.fn === null) return detach(n);
235
- n.flags |= 2 /* Dirty */;
297
+ if (last !== null && last.from.slot === last && unindex(n, base), n.fn === null) return detach(n);
236
298
  let tentative = n.gen + 1 & 1073741823 /* Mask */;
237
299
  for (let e = last === null ? n.firstDep : last.nextDep; e !== null; ) {
238
300
  let after = e.nextDep;
@@ -247,7 +309,15 @@ function dispose(n) {
247
309
  n.fn !== null && (n.fn = null, (n.flags & 4 /* Busy */) === 0 && detach(n));
248
310
  }
249
311
  function detach(n) {
250
- cutDeps(n, null), n.flags &= -36;
312
+ if ((n.flags & 16 /* Watch */) === 0) cutDeps(n, null);
313
+ else {
314
+ let e = n.firstDep;
315
+ for (n.firstDep = n.lastDep = null; e !== null; ) {
316
+ let next = e.nextDep;
317
+ unlinkSub(e), e.prevDep = e.nextDep = null, e = next;
318
+ }
319
+ }
320
+ n.flags &= -44;
251
321
  }
252
322
  function index(c, next) {
253
323
  let unread = !1;
@@ -266,23 +336,23 @@ function unindex(n, base) {
266
336
  }
267
337
  trim();
268
338
  }
269
- function link(c, s, last, next) {
339
+ function attach(c, s, last, next) {
270
340
  let first = s.firstSub, tail = first !== null ? first.prevSub : null, e = {
271
341
  from: s,
272
- to: c,
273
342
  ver: s.ver,
343
+ nextDep: next,
344
+ to: c,
345
+ nextSub: null,
274
346
  gen: c.gen + 1 & 1073741823 /* Mask */,
275
347
  prevDep: last,
276
- nextDep: next,
277
- prevSub: tail,
278
- nextSub: null
348
+ prevSub: tail
279
349
  };
280
- last !== null ? last.nextDep = e : c.firstDep = e, next !== null ? next.prevDep = e : c.lastDep = e, first !== null ? (tail.nextSub = e, first.prevSub = e) : (s.firstSub = e, e.prevSub = e), cursor = e;
350
+ last !== null ? last.nextDep = e : c.firstDep = e, next !== null ? next.prevDep = e : c.lastDep = e, first !== null ? (tail.nextSub = e, first.prevSub = e) : (s.firstSub = e, e.prevSub = e), cursor = e, (s.flags & 1 /* Stale */) !== 0 && mark(e);
281
351
  }
282
352
  function miss(c, s, last, next) {
283
353
  if (last !== null && last.from.slot === last) return slot(c, s, last, next);
284
354
  let first = s.firstSub;
285
- if (first === null || alien(first, c)) return link(c, s, last, next);
355
+ if (first === null || alien(first, c)) return attach(c, s, last, next);
286
356
  if (next !== null && next.ver === UNREAD)
287
357
  return settle(c, s, last, next, pair(first, c));
288
358
  if (next !== null) {
@@ -307,7 +377,7 @@ function near(c, s, last, next, first) {
307
377
  let p = last !== null ? last.prevDep : null, budget = SCAN;
308
378
  for (; p !== null && p.from !== s && --budget !== 0; ) p = p.prevDep;
309
379
  if (!(p !== null && p.from === s)) {
310
- if (e === null && p === null) return link(c, s, last, next);
380
+ if (e === null && p === null) return attach(c, s, last, next);
311
381
  lookup(c, s, last, next, first);
312
382
  }
313
383
  }
@@ -317,17 +387,22 @@ function skip(c, s, last, next, e) {
317
387
  }
318
388
  function lookup(c, s, last, next, first) {
319
389
  let found = pair(first, c);
320
- found != null && begin(c, found, first, next), settle(c, s, last, next, found);
390
+ if (found != null) {
391
+ if (found.ver === SKIPPED)
392
+ return found.ver = s.ver, cursor = found, moveDep(c, found, last, next);
393
+ begin(c, found, first, next);
394
+ }
395
+ settle(c, s, last, next, found);
321
396
  }
322
397
  function settle(c, s, last, next, found) {
323
- if (found === null) return link(c, s, last, next);
398
+ if (found === null) return attach(c, s, last, next);
324
399
  if (found !== void 0) return take(c, found, s, last, next);
325
400
  index(c, next), slot(c, s, last, next);
326
401
  }
327
402
  function slot(c, s, last, next) {
328
403
  let cell = s.slot;
329
404
  if (cell !== null && cell.to === c) return take(c, cell, s, last, next);
330
- link(c, s, last, next), claim(s, cursor);
405
+ attach(c, s, last, next), claim(s, cursor);
331
406
  }
332
407
  function alien(first, c) {
333
408
  let tail = first.prevSub;
@@ -356,7 +431,7 @@ function take(c, e, s, last, next) {
356
431
  var queue = new Array(KEEP).fill(null), mask = KEEP - 1, head = 0, spare = 0, calm = 0, size = 0, hold = 0;
357
432
  function wake2(w) {
358
433
  let flags = w.flags;
359
- (flags & 192 /* AutoQueued */) === 64 /* Auto */ && (w.flags = flags | 128 /* Queued */, size > mask && grow(), queue[head + size++ & mask] = w);
434
+ (flags & 192 /* AutoQueued */) === 64 /* Auto */ && (size > mask && grow(), queue[head + size++ & mask] = w, w.flags = flags | 128 /* Queued */);
360
435
  }
361
436
  onWake(wake2);
362
437
  function grow() {
@@ -376,14 +451,12 @@ function deliver(w, flags) {
376
451
  w.flags = flags;
377
452
  return;
378
453
  }
379
- if (w.flags = flags & -34, (flags & 1 /* Stale */) !== 0) {
454
+ if (w.flags = flags & -34, (flags & 1 /* Stale */) !== 0)
380
455
  try {
381
- check(w);
456
+ if ((check(w, flags) & 2 /* Dirty */) === 0) return;
382
457
  } catch (error) {
383
458
  throw w.flags |= 1 /* Stale */, error;
384
459
  }
385
- if ((w.flags & 2 /* Dirty */) === 0 || w.fn === null) return;
386
- }
387
460
  let cleanup = w.value;
388
461
  typeof cleanup == "function" && (w.value = void 0, untracked(cleanup), w.fn === null) || exec(w);
389
462
  }
@@ -414,8 +487,10 @@ function drain(failed, first) {
414
487
  for (; size !== 0; taken++) {
415
488
  let queuedWatcher = queue[head];
416
489
  queue[head] = null, head = head + 1 & mask, size--;
490
+ let flags = queuedWatcher.flags & -129;
491
+ queuedWatcher.flags = flags;
417
492
  try {
418
- deliver(queuedWatcher, queuedWatcher.flags & -129);
493
+ deliver(queuedWatcher, flags);
419
494
  } catch (error) {
420
495
  failed || (first = error), failed = !0;
421
496
  }
@@ -471,9 +546,17 @@ function effect(fn) {
471
546
  }
472
547
  return effectNode;
473
548
  }
474
- var get = read, peek2 = peek;
549
+ var read2 = read, get = read2, readSignal = checkedReadSignal;
550
+ function checkedReadSignal(n) {
551
+ if (n.fn !== null)
552
+ throw new TypeError(
553
+ "readSignal() takes a signal; read a computed with read() or compute()"
554
+ );
555
+ return track(n);
556
+ }
557
+ var compute = read2, peek2 = peek;
475
558
  function set(source, value) {
476
- write(source, value), size !== 0 && hold === 0 && drainNow();
559
+ write(source, value) && size !== 0 && hold === 0 && drainNow();
477
560
  }
478
561
  function update(source, fn) {
479
562
  set(