uneventful 0.0.11 → 0.0.13

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/signals.mjs CHANGED
@@ -1,527 +1,8 @@
1
- import { F as pushCtx, G as popCtx, w as makeJob, c as root, f as isError, e as markHandled, g as getJob, x as detached, k as currentCell, o as reject, b as resolve } from './jobutils-Dvu-e99o.mjs';
2
- import { b as backpressure, I as IsStream, a as callOrWait } from './call-or-wait-EzXJX5Dq.mjs';
3
- import { e as batch, f as arrayEq, d as defer, c as apply, s as setMap, C as CallableObject } from './utils-cyEhnyp7.mjs';
4
-
5
- const ruleQueues = /* @__PURE__ */ new WeakMap();
6
- function ruleQueue(scheduleFn = defer) {
7
- ruleQueues.has(scheduleFn) || ruleQueues.set(scheduleFn, batch(runRules$1, scheduleFn));
8
- return ruleQueues.get(scheduleFn);
9
- }
10
- var currentRule;
11
- const ruleStops = /* @__PURE__ */ new WeakMap();
12
- const defaultQ = /* @__PURE__ */ ruleQueue(defer);
13
- var currentQueue;
14
- function runRules$1(q) {
15
- if (currentQueue)
16
- return;
17
- currentQueue = this;
18
- try {
19
- for (currentRule of q) {
20
- currentRule.catchUp();
21
- q.delete(currentRule);
22
- }
23
- } finally {
24
- currentQueue = currentRule = void 0;
25
- }
26
- }
27
- class WriteConflict extends Error {
28
- }
29
- class CircularDependency extends Error {
30
- }
31
- var timestamp = 1;
32
- const fntrackers = /* @__PURE__ */ new WeakMap();
33
- const obtrackers = /* @__PURE__ */ new WeakMap();
34
- const monitors = /* @__PURE__ */ new WeakMap();
35
- const staleStreams = batch((q) => {
36
- for (const cell of q)
37
- if (!cell.subscribers) {
38
- ++timestamp;
39
- break;
40
- }
41
- q.clear();
42
- });
43
- const demandChanges = batch((q) => {
44
- for (const cell of q)
45
- cell.updateDemand();
46
- });
47
- const dirtyStack = [];
48
- function markDependentsDirty(cell) {
49
- const latestSource = cell.latestSource = timestamp;
50
- for (; cell; cell = dirtyStack.pop()) {
51
- for (let sub = cell.subscribers; sub; sub = sub.nT) {
52
- const tgt = sub.tgt;
53
- if (tgt.latestSource >= latestSource)
54
- continue;
55
- tgt.latestSource = latestSource;
56
- tgt.queue?.add(tgt);
57
- if (tgt.subscribers)
58
- dirtyStack.push(tgt);
59
- }
60
- }
61
- }
62
- function returnValue() {
63
- return this.value;
64
- }
65
- function throwValue() {
66
- throw this.value;
67
- }
68
- const notCaughtUp = [];
69
- var freesubs;
70
- function mksub(source, target) {
71
- let sub = freesubs;
72
- if (sub) {
73
- freesubs = sub.old;
74
- sub.src = source;
75
- sub.nS = void 0;
76
- sub.pS = target.sources;
77
- sub.tgt = target;
78
- sub.nT = sub.pT = void 0;
79
- sub.ts = source.lastChanged;
80
- sub.old = source.adding;
81
- } else
82
- sub = {
83
- src: source,
84
- nS: void 0,
85
- pS: target.sources,
86
- tgt: target,
87
- nT: void 0,
88
- pT: void 0,
89
- ts: source.lastChanged,
90
- old: source.adding
91
- };
92
- if (target.sources)
93
- target.sources.nS = sub;
94
- target.sources = sub;
95
- source.adding = sub;
96
- if (target.flags & 1 /* Observed */)
97
- source.subscribe(sub);
98
- }
99
- function removeConstListener(sub) {
100
- const { tgt, nS, pS } = sub;
101
- if (sub.src.adding === sub)
102
- sub.src.adding = sub.old;
103
- delsub(sub);
104
- if (tgt.sources === sub)
105
- (tgt.sources = nS || pS) || tgt.becomeConstant();
106
- }
107
- function delsub(sub) {
108
- sub.src.unsubscribe(sub);
109
- if (sub.nS)
110
- sub.nS.pS = sub.pS;
111
- if (sub.pS)
112
- sub.pS.nS = sub.nS;
113
- sub.src = sub.tgt = sub.nS = sub.pS = sub.nT = sub.pT = void 0;
114
- sub.old = freesubs;
115
- freesubs = sub;
116
- }
117
- function subscribeAll(c) {
118
- for (let s = firstSource(c); s; s = s.nS)
119
- s.src.subscribe(s);
120
- toggleDemand(c);
121
- }
122
- function unsubscribeAll(c) {
123
- for (let s = firstSource(c); s; s = s.nS)
124
- s.src.unsubscribe(s);
125
- toggleDemand(c);
126
- }
127
- const sentinel = {};
128
- function firstSource(c) {
129
- let s = c.sources;
130
- if (s)
131
- while (s.pS)
132
- s = s.pS;
133
- return s;
134
- }
135
- function toggleDemand(c) {
136
- if (((c.flags ^= 1 /* Observed */) & 64 /* Stateful */) === 64 /* Stateful */) {
137
- demandChanges.has(c) ? demandChanges.delete(c) : demandChanges.add(c);
138
- }
139
- }
140
- let dummySource;
141
- class Cell {
142
- constructor() {
143
- this.value = void 0;
144
- // the value
145
- this.validThrough = 0;
146
- // timestamp of most recent validation or recalculation
147
- this.lastChanged = 0;
148
- // timestamp of last value change
149
- this.latestSource = timestamp;
150
- // max lastChanged of this cell or any ancestor source
151
- this.flags = 0;
152
- this.job = void 0;
153
- /** The subscription being added during the current calculation - used for uniqueness */
154
- this.adding = void 0;
155
- /** Linked list of sources */
156
- this.sources = void 0;
157
- /** Linked list of targets */
158
- this.subscribers = void 0;
159
- this.queue = void 0;
160
- /**
161
- * The computation to be performed (if {@link Is.Compute}) or the
162
- * setup/teardown for {@link Is.Stateful}.
163
- */
164
- this.compute = returnValue;
165
- }
166
- stream(sink, _conn, inlet) {
167
- let lastValue = sentinel;
168
- Cell.mkRule(() => {
169
- const val = this.getValue();
170
- if (val !== lastValue) {
171
- pushCtx();
172
- try {
173
- sink(lastValue = val);
174
- } finally {
175
- popCtx();
176
- }
177
- }
178
- }, inlet ? ruleQueue(backpressure(inlet)) : defaultQ);
179
- return IsStream;
180
- }
181
- getValue() {
182
- if (arguments.length)
183
- return apply(this.stream, this, arguments);
184
- const dep = currentCell;
185
- if (dep && this.flags & 132 /* Variable */ && (dep.flags & 8 /* Peeking */) === 0) {
186
- if (this.flags & 32 /* Running */)
187
- throw new CircularDependency("Cached function dependency cycle");
188
- let s = this.adding;
189
- if (!s || s.tgt !== dep) {
190
- mksub(this, dep);
191
- s = this.adding;
192
- } else {
193
- if (s.ts === -1) {
194
- s.ts = this.lastChanged;
195
- if (s.nS) {
196
- s.nS.pS = s.pS;
197
- if (s.pS)
198
- s.pS.nS = s.nS;
199
- s.nS = void 0;
200
- s.pS = dep.sources;
201
- dep.sources.nS = s;
202
- dep.sources = s;
203
- }
204
- }
205
- }
206
- this.catchUp();
207
- if (this.adding === s)
208
- s.ts = this.lastChanged;
209
- } else
210
- this.catchUp();
211
- if (this.flags & 16 /* Error */)
212
- throw this.value;
213
- return this.value;
214
- }
215
- shouldWrite(changed) {
216
- const cell = currentCell || currentRule;
217
- if (cell) {
218
- if (!cell.queue)
219
- throw new WriteConflict("Side-effects not allowed outside rules");
220
- if (this.adding && this.adding.tgt === cell)
221
- throw new CircularDependency("Can't update direct dependency");
222
- if (this.validThrough === timestamp || this.hasBeenRead())
223
- throw new WriteConflict("Value already used");
224
- } else if (changed) {
225
- if (this.validThrough === timestamp || notCaughtUp.length) {
226
- ++timestamp;
227
- notCaughtUp.length = 0;
228
- }
229
- } else {
230
- return false;
231
- }
232
- this.lastChanged === timestamp || markDependentsDirty(this);
233
- if (this.sources)
234
- this.sources.ts = 0;
235
- return true;
236
- }
237
- setValue(val, isErr) {
238
- if (this.shouldWrite(val !== this.value || isErr !== !!(this.flags & 16 /* Error */))) {
239
- this.value = val;
240
- this.lastChanged = timestamp;
241
- this.flags = isErr ? this.flags | 16 /* Error */ : this.flags & ~16 /* Error */;
242
- }
243
- this.compute = isErr ? throwValue : returnValue;
244
- }
245
- setCalc(compute) {
246
- if (this.shouldWrite(compute !== this.compute)) {
247
- this.flags |= 4 /* Compute */;
248
- this.compute = compute;
249
- }
250
- }
251
- hasBeenRead() {
252
- if (notCaughtUp.length) {
253
- for (const cell of notCaughtUp)
254
- cell.catchUp();
255
- notCaughtUp.length = 0;
256
- }
257
- return this.validThrough === timestamp;
258
- }
259
- catchUp() {
260
- const { validThrough } = this;
261
- if (validThrough === timestamp)
262
- return;
263
- this.validThrough = timestamp;
264
- if (!(this.flags & 4 /* Compute */)) {
265
- if (this.flags & 256 /* Stream */ && !this.subscribers) {
266
- this.lastChanged = timestamp;
267
- staleStreams.add(this);
268
- }
269
- return;
270
- }
271
- if (this.sources) {
272
- for (let sub = this.sources; sub; sub = sub.nS) {
273
- const s = sub.src;
274
- if (sub.ts !== s.lastChanged)
275
- return this.doRecalc();
276
- if (s.subscribers && s.latestSource <= validThrough)
277
- continue;
278
- s.catchUp();
279
- if (s.lastChanged > validThrough) {
280
- return this.doRecalc();
281
- }
282
- }
283
- for (let sub = this.sources; sub; sub = sub.nS) {
284
- if (sub.src.validThrough !== timestamp)
285
- notCaughtUp.push(sub.src);
286
- }
287
- } else
288
- return this.doRecalc();
289
- }
290
- doRecalc() {
291
- pushCtx(this.job, this);
292
- if (this.flags & 64 /* Stateful */) {
293
- this.job?.restart();
294
- }
295
- for (let sub = this.sources; sub; sub = sub.nS) {
296
- sub.ts = -1;
297
- sub.old = sub.src.adding;
298
- sub.src.adding = sub;
299
- this.sources = sub;
300
- }
301
- this.flags |= 32 /* Running */;
302
- try {
303
- try {
304
- const future = this.compute();
305
- if (future !== this.value || !this.lastChanged || this.flags & 16 /* Error */) {
306
- this.value = future;
307
- this.lastChanged = timestamp;
308
- }
309
- this.flags &= ~16 /* Error */;
310
- } catch (e) {
311
- this.flags |= 16 /* Error */;
312
- this.value = e;
313
- this.lastChanged = timestamp;
314
- if (currentRule === this)
315
- throw e;
316
- }
317
- } finally {
318
- this.flags &= ~32 /* Running */;
319
- if (this.flags & 64 /* Stateful */) {
320
- demandChanges.delete(this);
321
- if (this.flags & (16 /* Error */ | 2 /* Stopped */)) {
322
- this.flags &= ~2 /* Stopped */;
323
- this.job?.restart();
324
- } else if (this.flags & 1 /* Observed */) ; else {
325
- this.job?.restart();
326
- }
327
- }
328
- popCtx();
329
- let head;
330
- for (let sub = this.sources; sub; ) {
331
- const pS = sub.pS;
332
- sub.src.adding = sub.old;
333
- sub.old = void 0;
334
- if (sub.ts === -1)
335
- delsub(sub);
336
- else
337
- head = sub;
338
- sub = pS;
339
- }
340
- (this.sources = head) || this.becomeConstant();
341
- }
342
- }
343
- becomeConstant() {
344
- if (this.flags & 32 /* Running */)
345
- return;
346
- if (this.flags & 64 /* Stateful */) {
347
- mksub(dummySource ||= Cell.mkValue(null), this);
348
- return;
349
- }
350
- this.flags &= ~4 /* Compute */;
351
- if (this.flags & 132 /* Variable */)
352
- return;
353
- while (this.adding)
354
- removeConstListener(this.adding);
355
- for (let sub = this.subscribers; sub; ) {
356
- const { nT } = sub;
357
- sub.ts !== this.lastChanged || removeConstListener(sub);
358
- sub = nT;
359
- }
360
- }
361
- stop() {
362
- this.setQ(null);
363
- if (this.flags & 32 /* Running */) {
364
- this.flags |= 2 /* Stopped */;
365
- } else
366
- this.updateDemand();
367
- ruleStops.get(this)?.();
368
- }
369
- setQ(queue = defaultQ) {
370
- if (queue == this.queue)
371
- return;
372
- if (!this.subscribers) {
373
- queue || !this.queue || unsubscribeAll(this);
374
- }
375
- if (this.queue) {
376
- if (this.queue.has(this))
377
- queue?.add(this);
378
- this.queue.delete(this);
379
- } else if (queue) {
380
- queue.add(this);
381
- this.subscribers || subscribeAll(this);
382
- }
383
- this.queue = queue;
384
- }
385
- subscribe(sub) {
386
- if (!this.subscribers && !this.queue) {
387
- subscribeAll(this);
388
- }
389
- if (this.subscribers !== sub && !sub.pT) {
390
- sub.nT = this.subscribers;
391
- if (this.subscribers)
392
- this.subscribers.pT = sub;
393
- this.subscribers = sub;
394
- }
395
- }
396
- unsubscribe(sub) {
397
- if (sub.nT)
398
- sub.nT.pT = sub.pT;
399
- if (sub.pT)
400
- sub.pT.nT = sub.nT;
401
- if (this.subscribers === sub)
402
- this.subscribers = sub.nT;
403
- sub.nT = sub.pT = void 0;
404
- if (!this.subscribers && !this.queue) {
405
- unsubscribeAll(this);
406
- }
407
- }
408
- static mkValue(val) {
409
- const cell = new Cell();
410
- cell.flags = 128 /* Mutable */;
411
- cell.value = val;
412
- cell.lastChanged = timestamp;
413
- return cell;
414
- }
415
- isObserved() {
416
- if (this.flags & 8 /* Peeking */)
417
- return false;
418
- return !!((this.flags |= 64 /* Stateful */) & 1 /* Observed */);
419
- }
420
- getJob() {
421
- if (this.job)
422
- return this.job;
423
- this.flags |= 64 /* Stateful */;
424
- return this.job = makeJob();
425
- }
426
- updateDemand() {
427
- demandChanges.delete(this);
428
- if (monitors.has(this)) {
429
- monitors.get(this)();
430
- } else if (this.flags & 1 /* Observed */) {
431
- this.shouldWrite(true);
432
- } else {
433
- this.job?.restart();
434
- }
435
- }
436
- static mkStream(src, val) {
437
- const cell = this.mkValue(val);
438
- cell.flags |= 64 /* Stateful */ | 256 /* Stream */;
439
- const write = (v) => {
440
- cell.setValue(v, false);
441
- };
442
- let job;
443
- monitors.set(cell, () => {
444
- if (!cell.subscribers) {
445
- write(val);
446
- job?.end();
447
- return;
448
- }
449
- if (job)
450
- return;
451
- job = makeJob(root).do((r) => {
452
- if (isError(r)) {
453
- cell.setValue(markHandled(r), true);
454
- } else {
455
- cell.setValue(val, false);
456
- }
457
- job = void 0;
458
- });
459
- pushCtx(job);
460
- try {
461
- src(write, job);
462
- } catch (e) {
463
- job.end();
464
- root.asyncThrow(e);
465
- } finally {
466
- popCtx();
467
- }
468
- });
469
- return cell;
470
- }
471
- peek(fn, thisArg, args) {
472
- if (this.flags & 8 /* Peeking */)
473
- return apply(fn, thisArg, args);
474
- this.flags |= 8 /* Peeking */;
475
- try {
476
- return apply(fn, thisArg, args);
477
- } finally {
478
- this.flags &= ~8 /* Peeking */;
479
- }
480
- }
481
- recalcWhen(fnOrKey, fn) {
482
- if (this.flags & 8 /* Peeking */)
483
- return;
484
- let trackers = fn ? obtrackers.get(fn) || setMap(obtrackers, fn, /* @__PURE__ */ new WeakMap()) : fntrackers;
485
- let signal = trackers.get(fnOrKey);
486
- if (!signal) {
487
- const src = fn ? fn(fnOrKey) : fnOrKey;
488
- let ct = 0;
489
- const c = Cell.mkStream((s) => (src(() => s(++ct)), IsStream), ct);
490
- trackers.set(fnOrKey, signal = c.getValue.bind(c));
491
- }
492
- signal();
493
- }
494
- static mkCached(compute) {
495
- const cell = new Cell();
496
- cell.compute = compute;
497
- cell.flags = 4 /* Compute */;
498
- return cell;
499
- }
500
- static mkRule(fn, q) {
501
- const outer = getJob(), cell = Cell.mkCached(() => {
502
- try {
503
- const cleanup = fn();
504
- if (cleanup)
505
- (cell.job || cell.getJob()).must(cleanup);
506
- } catch (e) {
507
- cell.stop();
508
- throw e;
509
- }
510
- }), stop = cell.stop.bind(cell);
511
- outer === detached || ruleStops.set(cell, outer.release(stop));
512
- cell.flags |= 64 /* Stateful */;
513
- cell.setQ(q);
514
- return stop;
515
- }
516
- }
517
- function unchangedIf(newVal, equals = arrayEq) {
518
- const cell = currentCell;
519
- if (cell) {
520
- return cell.flags & 16 /* Error */ || !equals(cell.value, newVal) ? newVal : cell.value;
521
- } else {
522
- throw new Error("unchangedIf() must be called from a reactive expression");
523
- }
524
- }
1
+ import { d as root, o as currentCell, g as getJob, m as must, L as currentJob, w as reject, b as resolve } from './jobutils-O9tOTNKC.mjs';
2
+ import { C as Cell, r as ruleQueue, d as defaultQ, c as currentRule, p as perSignal, g as getCell } from './hooks-BnMYHRlM.mjs';
3
+ export { a as CircularDependency, W as WriteConflict } from './hooks-BnMYHRlM.mjs';
4
+ import { d as defer, C as CallableObject, c as apply, s as setMap, b as decorateMethod, i as isFunction, f as isPlainFunction, g as arrayEq } from './utils-BgqyDPjA.mjs';
5
+ import { c as callOrWait } from './call-or-wait-CvOF0pao.mjs';
525
6
 
526
7
  class RF extends CallableObject {
527
8
  constructor(q) {
@@ -549,9 +30,6 @@ class RF extends CallableObject {
549
30
  factory(scheduleFn) {
550
31
  return factories.get(scheduleFn) || setMap(factories, scheduleFn, new RF(ruleQueue(scheduleFn)));
551
32
  }
552
- detached(fn) {
553
- return detached.run(this, fn);
554
- }
555
33
  root(fn) {
556
34
  return root.run(this, fn);
557
35
  }
@@ -568,6 +46,75 @@ function runRules(scheduleFn) {
568
46
  (scheduleFn ? ruleQueue(scheduleFn) : defaultQ).flush();
569
47
  }
570
48
 
49
+ function sosSize(set) {
50
+ return set ? set instanceof Set ? set.size : 1 : 0;
51
+ }
52
+ function sosAdd(set, item) {
53
+ return set ? set instanceof Set ? (set.add(item), set) : /* @__PURE__ */ new Set([set, item]) : item;
54
+ }
55
+ function sosDel(set, item) {
56
+ if (set !== item)
57
+ return set instanceof Set ? (set.delete(item), set) : set;
58
+ return void 0;
59
+ }
60
+ function sosHas(set, item) {
61
+ return set === item || set instanceof Set && set.has(item);
62
+ }
63
+
64
+ function fn(f, ...args) {
65
+ if (args.length) {
66
+ return decorateMethod((f2) => {
67
+ const map = /* @__PURE__ */ new WeakMap();
68
+ return function() {
69
+ return (map.get(this) || setMap(map, this, Cell.mkCached(f2.bind(this)))).getValue();
70
+ };
71
+ }, f, ...args);
72
+ } else if (!isFunction(f)) {
73
+ return perSignal(fn, f, "fn``() ");
74
+ } else if (f.length) {
75
+ const map = /* @__PURE__ */ new WeakMap();
76
+ return (ob) => (map.get(ob) || setMap(map, ob, Cell.mkCached(f(ob)))).getValue();
77
+ }
78
+ return cached(f);
79
+ }
80
+ function fx(f, ...args) {
81
+ if (args.length) {
82
+ return decorateMethod((method) => {
83
+ const m = fx((ob) => method.bind(ob));
84
+ return function() {
85
+ m(this);
86
+ };
87
+ }, f, ...args);
88
+ } else if (!isFunction(f)) {
89
+ return perSignal(fx, f, "fx``() ");
90
+ } else if (f.length) {
91
+ const map = /* @__PURE__ */ new WeakMap();
92
+ return (ob) => (map.get(ob) || setMap(map, ob, fx(f(ob))))();
93
+ } else if (!isPlainFunction(f)) {
94
+ throw new Error("fx() bodies must be plain functions, not signals, generators, or async");
95
+ } else {
96
+ let jobs, cell = Cell.mkCached(f);
97
+ return () => {
98
+ if (currentCell) {
99
+ if (currentCell.isObserved())
100
+ cell.getValue();
101
+ } else {
102
+ if (!sosHas(jobs, getJob())) {
103
+ const job = currentJob;
104
+ jobs = sosAdd(jobs, job);
105
+ sosSize(jobs) > 1 || (cell.setQ(defaultQ), defaultQ.delete(cell));
106
+ must(() => {
107
+ jobs = sosDel(jobs, job);
108
+ if (!sosSize(jobs))
109
+ cell.setQ();
110
+ });
111
+ }
112
+ cell.catchUp();
113
+ }
114
+ };
115
+ }
116
+ }
117
+
571
118
  class SignalImpl extends CallableObject {
572
119
  constructor(_c) {
573
120
  super(_c.getValue.bind(_c));
@@ -685,6 +232,17 @@ function action(fn, _ctx, desc) {
685
232
  return currentCell ? currentCell.peek(fn, this, args) : apply(fn, this, args);
686
233
  };
687
234
  }
235
+ function stable(equals, newVal) {
236
+ return getCell("stabilizers ").unchangedIf(newVal, equals);
237
+ }
238
+ function unchangedIf(newVal, equals = arrayEq) {
239
+ return stable(equals, newVal);
240
+ }
241
+ function stabilizer(equals) {
242
+ return stable.bind(null, equals);
243
+ }
244
+ const stableArray = /* @__PURE__ */ stabilizer(arrayEq);
245
+ const stableJSON = /* @__PURE__ */ stabilizer((a, b) => JSON.stringify(a) === JSON.stringify(b));
688
246
  function until(source) {
689
247
  return callOrWait(source, "uneventful.until", waitTruthy, recache);
690
248
  }
@@ -695,4 +253,4 @@ function waitTruthy(job, v) {
695
253
  v && job.return(v);
696
254
  }
697
255
 
698
- export { CircularDependency, ConfigurableImpl, SignalImpl, WritableImpl, WriteConflict, action, cached, peek, rule, runRules, unchangedIf, until, value };
256
+ export { ConfigurableImpl, SignalImpl, WritableImpl, action, cached, fn, fx, peek, rule, runRules, stabilizer, stable, stableArray, stableJSON, unchangedIf, until, value };
@@ -1,4 +1,4 @@
1
- import { h as Suspend, a as Stream, Y as Yielding, b as Sink, I as Inlet, K as Connection, i as RecalcSource } from './types-N2ua11te.js';
1
+ import { h as Suspend, a as Stream, Y as Yielding, b as Sink, I as Inlet, K as Connection, i as RecalcSource } from './types-pElgImr7.js';
2
2
 
3
3
  /**
4
4
  * The result type returned from calls to {@link Each}.next()