uneventful 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -189,5 +189,5 @@ And at the same time, this has actually been a pretty superficial tour: we haven
189
189
 
190
190
  (Also, at some point you'll be able to use the "Calibre Connect" Obsidian plugin I'm working on as an example of how to use these things to create responsive search and tame Electron WebFrames while keeping track of whether a connection to a remote server is available, handling logins and background processes and integrating a plugin to a larger application, not to mention controlling lots of features via settings.)
191
191
 
192
- In the meantime, this library should now be available for installation and experimentation via [npm](https://www.npmjs.com/package/uneventful). Enjoy!
192
+ In the meantime, this library should now be available for installation and experimentation via [npm](https://www.npmjs.com/package/uneventful), as [an ESM-only package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). Enjoy!
193
193
 
package/dist/mod.d.ts CHANGED
@@ -306,6 +306,38 @@ interface UntilMethod<T> {
306
306
  * @category Scheduling
307
307
  */
308
308
  declare function until<T>(source: Waitable<T>): Yielding<T>;
309
+ /**
310
+ * Run a {@link restarting}() callback for each value produced by a source.
311
+ *
312
+ * With each event that occurs, any previous callback run is cleaned up before
313
+ * the new one begins. (And the last run is cleaned up when the connection or
314
+ * job ends.)
315
+ *
316
+ * This function is almost the exact opposite of {@link each}(), in that the
317
+ * stream is never paused (unless you do so manually via a throttle or inlet),
318
+ * and if the "loop body" (callback job) is still running when a new value
319
+ * arrives, forEach() restarts the job instead of dropping the value.
320
+ *
321
+ * @param src An event source (i.e. a {@link Producer} or {@link Signal})
322
+ * @param sink A callback that receives values from the source
323
+ * @param inlet An optional throttle or inlet that will be used to pause the
324
+ * source (if it's a signal or supports backpressure)
325
+ * @returns a {@link Connection} that can be used to detect the stream
326
+ * end/error, or ended to close it early.
327
+ *
328
+ * @category Stream Consumers
329
+ */
330
+ declare function forEach<T>(src: Source<T>, sink: Sink<T>, inlet?: Inlet): Connection;
331
+ /**
332
+ * When called without a source, return a callback suitable for use w/{@link pipe}().
333
+ * e.g.:
334
+ *
335
+ * ```ts
336
+ * pipe(someSource, ..., forEach(v => { doSomething(v); }), optionalInlet));
337
+ * ```
338
+ *
339
+ */
340
+ declare function forEach<T>(sink: Sink<T>, inlet?: Inlet): (src: Source<T>) => Connection;
309
341
 
310
342
  /**
311
343
  * Error indicating a rule has attempted to write a value it indirectly
@@ -426,7 +458,16 @@ declare const rule: (fn: (stop: DisposeFn) => OptionalCleanup) => DisposeFn;
426
458
  */
427
459
  declare const runRules: () => void;
428
460
 
429
- interface Signal<T> {
461
+ /**
462
+ * A function that can be called to get a value.
463
+ *
464
+ * (This interface is needed because TypeScript won't infer type of
465
+ * {@link Signal} correctly otherwise, specifically it won't see it as a zero-agument function.)
466
+ *
467
+ * @category Types and Interfaces
468
+ */
469
+ type Returns<T> = () => T;
470
+ interface Signal<T> extends Producer<T>, Returns<T> {
430
471
  /**
431
472
  * A signal object implements the {@link Producer} interface, even if it's
432
473
  * not directly recognized as one by TypeScript.
@@ -490,7 +531,7 @@ declare function value<T>(val?: T): Writable<T>;
490
531
  * {@link Signal}.
491
532
  *
492
533
  * Note: If the supplied function has a non-zero `.length` (i.e., it explicitly
493
- * takes arguments), it is assumed to be a {@link Source}, and the second
534
+ * takes arguments), it is assumed to be a {@link Producer}, and the second
494
535
  * calling signature below will apply, even if TypeScript doesn't see it that
495
536
  * way!)
496
537
  *
@@ -499,7 +540,7 @@ declare function value<T>(val?: T): Writable<T>;
499
540
  declare function cached<T>(compute: () => T): Signal<T>;
500
541
  /**
501
542
  * If the supplied function has a non-zero `.length` (i.e., it explicitly takes
502
- * arguments), it is assumed to be a {@link Source}, and the second argument is
543
+ * arguments), it is assumed to be a {@link Producer}, and the second argument is
503
544
  * a default value for the created signal to use as default value until the
504
545
  * source produces a value.
505
546
  *
@@ -510,11 +551,11 @@ declare function cached<T>(compute: () => T): Signal<T>;
510
551
  * the signal is once again unobserved, it will revert to the supplied inital
511
552
  * value.
512
553
  *
513
- * @param source A {@link Source} providing data which will become this signal's value
554
+ * @param source A {@link Producer} providing data which will become this signal's value
514
555
  * @param initVal The value to use when the signal is unobserved or waiting for the
515
556
  * first item from the source.
516
557
  */
517
- declare function cached<T>(source: Source<T>, initVal?: T): Signal<T>;
558
+ declare function cached<T>(source: Producer<T>, initVal?: T): Signal<T>;
518
559
  declare function cached<T extends Signal<any>>(signal: T): T;
519
560
  /**
520
561
  * Call a function without creating a dependency on any signals it reads. (Like
@@ -535,7 +576,7 @@ declare function noDeps<F extends PlainFunction>(fn: F, ...args: Parameters<F>):
535
576
  * subscribe to changes to it, but not directly produce a signal. (Such as
536
577
  * querying the DOM state and using a MutationObserver.)
537
578
  *
538
- * By calling this with a {@link Source} or {@link RecalcSource}, you arrange
579
+ * By calling this with a {@link Producer} or {@link RecalcSource}, you arrange
539
580
  * for it to be subscribed, if and when the call occurs in a rule or a cached
540
581
  * function that's in use by a rule (directly or indirectly). When the source
541
582
  * emits a value, the signal machinery will invalidate the caching of the
@@ -665,7 +706,7 @@ type Producer<T> = (sink: Sink<T>, conn?: Connection, inlet?: Throttle | Inlet)
665
706
  *
666
707
  * @category Types and Interfaces
667
708
  */
668
- type Source<T> = Producer<T> | Signal<T> | Writable<T>;
709
+ type Source<T> = Producer<T> | Signal<T>;
669
710
  /**
670
711
  * A specially-typed string used to verify that a function supports uneventful's
671
712
  * streaming protocol. Return it from a function to implement the
@@ -875,8 +916,8 @@ type OptionalCleanup<T = any> = CleanupFn<T> | Nothing;
875
916
  */
876
917
  type AsyncStart<T, This = void> = (this: This, job: Job<T>) => StartObj<T>;
877
918
  /**
878
- * A synchronous start function returns void. It runs immediately and gets
879
- * passed the newly created job as its first argument.
919
+ * A synchronous start function returns void or a {@link CleanupFn}. It runs
920
+ * immediately and gets passed the newly created job as its first argument.
880
921
  *
881
922
  * @template T The type the job will end up returning
882
923
  * @template This The type of `this` the function accepts, if using two-argument
@@ -884,7 +925,7 @@ type AsyncStart<T, This = void> = (this: This, job: Job<T>) => StartObj<T>;
884
925
  *
885
926
  * @category Types and Interfaces
886
927
  */
887
- type SyncStart<T, This = void> = (this: This, job: Job<T>) => void;
928
+ type SyncStart<T, This = void> = (this: This, job: Job<T>) => OptionalCleanup;
888
929
  /**
889
930
  * A synchronous or asynchronous initializing function for use with the
890
931
  * {@link start}() function or a job's {@link Job.start .start}() method.
@@ -1261,10 +1302,10 @@ type JobIterator<T> = Generator<Suspend<any>, T, any>;
1261
1302
  * explicit throw()/return() calls on the job instance.)
1262
1303
  *
1263
1304
  * Also note that any subjobs the Suspend function creates (or cleanup callbacks
1264
- * it registers) **will not be called until the *calling* job ends**. So any
1265
- * resources that won't be needed once the job is resumed should be explicitly
1266
- * disposed of -- in which case you should probably just `yield *` to a
1267
- * {@link start}(), instead of yielding a Suspend!
1305
+ * it registers) **will not be cleaned up until the *calling* job ends**. So
1306
+ * any resources that won't be needed once the job is resumed should be
1307
+ * explicitly disposed of -- in which case you should probably just `yield *` to
1308
+ * a {@link start}(), instead of yielding a Suspend!
1268
1309
  *
1269
1310
  * @category Types and Interfaces
1270
1311
  */
@@ -1790,7 +1831,7 @@ declare function must<T>(cleanup?: OptionalCleanup<T>): Job<T>;
1790
1831
  * generator or job), a promise, or void. A returned iterator or promise will
1791
1832
  * be treated as if the method was called with that to begin with; a returned
1792
1833
  * job will be awaited and its result transferred to the new job
1793
- * asynchronously.
1834
+ * asynchronously. A returned function will be added to the job via `must()`.
1794
1835
  *
1795
1836
  * - When called with one argument that's a {@link Yielding} iterator (such as a
1796
1837
  * generator or an existing job): it's attached to the new job and executed
@@ -1812,7 +1853,7 @@ declare function must<T>(cleanup?: OptionalCleanup<T>): Job<T>;
1812
1853
  * *(this) {...}));`) in order to correctly infer types inside a generator
1813
1854
  * function.)
1814
1855
  *
1815
- * In any of the above cases, if a supplied function throws an error when
1856
+ * In any of the above cases, if a supplied function throws an error while
1816
1857
  * starting, the new job will be ended, and the error synchronously re-thrown.
1817
1858
  *
1818
1859
  * @returns the created {@link Job}
@@ -1892,4 +1933,4 @@ declare function abortSignal(job?: Job): AbortSignal;
1892
1933
  declare function restarting<F extends AnyFunction>(task: F): F;
1893
1934
  declare function restarting(): (task: () => OptionalCleanup<never>) => void;
1894
1935
 
1895
- export { type AnyFunction, type AsyncStart, type Backpressure, CancelError, CancelResult, CircularDependency, type CleanupFn, type Connection, type DisposeFn, type Each, type EachResult, type Emitter, ErrorResult, type HandledError, type Inlet, IsStream, type Job, type JobIterator, type JobResult, type MockSource, type Nothing, type OptionalCleanup, type PlainFunction, type Producer, type RecalcSource, type Request, RuleScheduler, Signal, type Sink, type Source, type StartFn, type StartObj, type Suspend, type SyncStart, type Throttle, type Transformer, type UnhandledError, type UntilMethod, ValueResult, type Waitable, Writable, WriteConflict, type Yielding, abortSignal, backpressure, cached, compose, concat, concatAll, concatMap, connect, defer, detached, each, emitter, empty, filter, fromAsyncIterable, fromDomEvent, fromIterable, fromPromise, fromSubscribe, fromValue, fulfillPromise, getJob, getResult, interval, into, isCancel, isError, isFunction, isHandled, isJobActive, isUnhandled, isValue, lazy, makeJob, map, markHandled, merge, mergeAll, mergeMap, mockSource, must, nativePromise, never, noDeps, noop, pipe, propagateResult, recalcWhen, reject, rejecter, resolve, resolver, restarting, rule, runRules, share, skip, skipUntil, skipWhile, slack, sleep, start, switchAll, switchMap, take, takeUntil, takeWhile, throttle, timeout, to, until, value };
1936
+ export { type AnyFunction, type AsyncStart, type Backpressure, CancelError, CancelResult, CircularDependency, type CleanupFn, type Connection, type DisposeFn, type Each, type EachResult, type Emitter, ErrorResult, type HandledError, type Inlet, IsStream, type Job, type JobIterator, type JobResult, type MockSource, type Nothing, type OptionalCleanup, type PlainFunction, type Producer, type RecalcSource, type Request, type Returns, RuleScheduler, Signal, type Sink, type Source, type StartFn, type StartObj, type Suspend, type SyncStart, type Throttle, type Transformer, type UnhandledError, type UntilMethod, ValueResult, type Waitable, Writable, WriteConflict, type Yielding, abortSignal, backpressure, cached, compose, concat, concatAll, concatMap, connect, defer, detached, each, emitter, empty, filter, forEach, fromAsyncIterable, fromDomEvent, fromIterable, fromPromise, fromSubscribe, fromValue, fulfillPromise, getJob, getResult, interval, into, isCancel, isError, isFunction, isHandled, isJobActive, isUnhandled, isValue, lazy, makeJob, map, markHandled, merge, mergeAll, mergeMap, mockSource, must, nativePromise, never, noDeps, noop, pipe, propagateResult, recalcWhen, reject, rejecter, resolve, resolver, restarting, rule, runRules, share, skip, skipUntil, skipWhile, slack, sleep, start, switchAll, switchMap, take, takeUntil, takeWhile, throttle, timeout, to, until, value };
package/dist/mod.mjs CHANGED
@@ -1,2 +1,2 @@
1
- let y=typeof queueMicrotask=="function"?queueMicrotask:(n=>t=>n.then(t))(Promise.resolve());function P(n,t){n("next",t)}function Q(n,t){n("throw",void 0,t)}function it(n){return n.bind(null,"next")}function st(n){return n.bind(null,"throw",void 0)}function V(){}function ut(n,t,e){return{op:n,val:t,err:e}}const H=Object.freeze(ut("cancel"));function vt(n){return ut("next",n)}function gt(n){return ut("throw",void 0,n)}function J(n){return n===H}function _(n){return n?n.op==="next":!1}function C(n){return n?n.op==="throw":!1}function L(n){return C(n)&&n.val===void 0}function Gt(n){return C(n)&&n.val===null}function W(n){return n.val=null,n.err}function yt(n){if(_(n))return n.val;n.op,U(V,t=>{throw t},n)}function U(n,t,e){C(e)?t(W(e)):J(e)?t(new wt("Job canceled")):n(e.val)}function mt(n,t){n.result()||U(n.return.bind(n),n.throw.bind(n),t)}class wt extends Error{}var p=k();function a(n){const t=p;return p=n,t}var B=[];function k(n,t){if(B&&B.length){const e=B.pop();return e.job=n,e.cell=t,e}return{job:n,cell:t}}function G(n){n.job=n.cell=null,B.push(n)}const z=new WeakMap,St=n=>{Promise.reject(n)},bt=k(),_t=new WeakMap;function kt(){return X(0,void 0,void 0)}function Yt(n){for(;n.v;)Z(n,n.n);n.u=void 0,Z(n,n)}function Xt(n,t){++n.v,X(t,n.n,n)}function Zt(n,t){++n.v,X(t,n,n.p)}function $t(n,t){return++n.v,te(n,X(t,n,n.p))}function Ft(n){return!n||n.v===0}function xt(n){return n?n.v:0}function jt(n){if(xt(n))return Z(n,n.p)}class Kt{constructor(){this.n=this,this.p=this,this.v=0,this.u=void 0}}var Y;function X(n,t,e){let r=Y;return r?(Y=r.n,r.n=t||r,r.p=e||r):(r=new Kt,t&&(r.n=t),e&&(r.p=e)),r.v=n,r.n.p=r,r.p.n=r,r}function Z(n,t){--n.v;var e=t.v,r=t.u;return t.n&&(t.n.p=t.p),t.p&&(t.p.n=t.n),t.u=t.v=t.p=void 0,t.n=Y,Y=t,r&&r(),e}function te(n,t){let e=t.u||(t.u=()=>{e&&(e===t.u&&Z(n,t),e=n=t=void 0)});return e}function w(n){return typeof n=="function"}function d(){const{job:n}=p;if(n)return n;throw new Error("No job is currently active")}function ee(n){return t=>{p.job.must(n.release(t))}}function Rt(n,t){for(;xt(t);)try{jt(t)(n)}catch(e){m.asyncThrow(e)}t&&Yt(t)}var x=new Set;class N{constructor(){this.end=()=>{const t=this._done||(this._done=H),e=this._cbs;if(!e&&!L(t))return;const r=x.size,i=a(bt);if(r||x.add(null),e&&e.u&&(e.u=Rt(t,e.u)),x.add(this),r){a(i);return}x.delete(null);for(const s of x)s._cbs&&(s._cbs=Rt(s._done,s._cbs)),x.delete(s),L(s._done)&&s.throw(W(s._done));a(i)},this._done=void 0,this._cbs=void 0}static create(t,e){const r=new N;return(t||e)&&(r.must((t||(t=d())).release(e||r.end)),_t.set(r,t)),r}do(t){return Xt(this._chain(),t),this}onError(t){return this.do(e=>{C(e)&&t(W(e))})}onValue(t){return this.do(e=>{_(e)&&t(e.val)})}onCancel(t){return this.do(e=>{J(e)&&t()})}result(){return this._done||p.cell?.recalcWhen(this,ee)||void 0}get[Symbol.toStringTag](){return"Job"}restart(){if(!this._done&&x.size){const t=x;x=new Set,this.end(),x=t}else this._end(H);return this._done=void 0,$.delete(this),this}_end(t){if(this._done)throw new Error("Job already ended");return this!==m&&(this._done=t),this.end(),this}throw(t){return this._done?((_t.get(this)||m).asyncThrow(t),this):this._end(gt(t))}return(t){return this._end(vt(t))}then(t,e){return F(this).then(t,e)}catch(t){return F(this).catch(t)}finally(t){return F(this).finally(t)}*[Symbol.iterator](){return this._done?yt(this._done):yield t=>{this.do(e=>U(it(t),st(t),e))}}start(t,e){if(!t)return E(this);let r,i;if(w(e))r=e.bind(t);else if(w(t))r=t;else{if(t instanceof N)return t;i=t}const s=E(this);try{if(r&&(i=s.run(r,s)),i!=null)if(i instanceof N)i!==s&&i.do(u=>mt(s,u));else{if(w(i.then))return i.then(u=>{s.result()||s.return(u)},u=>{s.result()||s.throw(u)}),s;if(w(i[Symbol.iterator])&&typeof i!="string")s.run(ne,i,s);else throw new TypeError("Invalid value/return for start()")}return s}catch(u){throw s.end(),u}}connect(t,e,r){return this.start(i=>void t(e,i,r))}run(t,...e){const r=a(k(this));try{return t.apply(null,e)}finally{G(a(r))}}bind(t){const e=this;return function(){const r=a(k(e));try{return t.apply(this,arguments)}finally{G(a(r))}}}must(t){return w(t)&&Zt(this._chain(),t),this}release(t){if(this===m)return V;let e=this._chain();return(!this._done||e.u)&&(e=e.u||(e.u=kt())),$t(e,t)}asyncThrow(t){try{(z.get(this)||this.throw).call(this,t)}catch(e){this===m?z.set(this,St):z.delete(this);const r=z.get(this)||this.throw;r.call(this,t),r.call(this,e)}return this}asyncCatch(t){return w(t)?z.set(this,t):t===null&&z.delete(this),this}_chain(){return this===m&&this.end(),this._done&&Ft(this._cbs)&&y(this.end),this._cbs||(this._cbs=kt())}}const $=new WeakMap;function F(n=d()){return $.has(n)||$.set(n,new Promise((t,e)=>{const r=U.bind(null,t,e);n.result()?r(n.result()):n.do(r)})),$.get(n)}const E=N.create,m=E();m.end=()=>{throw new Error("Can't do that with the detached job")},m.asyncCatch(St);function ne(n,t){let e=n[Symbol.iterator](),r=!0,i=k(t),s=0,u=i.job.release(()=>{t=void 0,++s,o("return",void 0)});y(()=>{r=!1,o("next",void 0)});function o(c,l){if(!e)return;if(r)return y(o.bind(null,c,l));const T=a(i);try{r=!0;try{for(;;){++s;const{done:f,value:b}=e[c](l);if(f){t&&t.return(b),t=void 0;break}else if(w(b)){let g=!1,O=!1,Ht=s;if(b((rt,Lt,Bt)=>{g||(g=!0,c=rt,l=rt==="next"?Lt:Bt,O&&Ht===s&&o(rt,l))}),O=!0,!g)return}else{c="throw",l=new TypeError("Jobs must yield functions (or yield* Yielding<T>s)");continue}}}catch(f){e=t=void 0,i.job.throw(f)}e=void 0,u?.(),u=void 0}finally{a(T),r=!1}}}function*Tt(n){return yield t=>Promise.resolve(n).then(it(t),st(t))}function*re(n){try{var t;yield e=>{t=setTimeout(()=>{t=void 0,P(e,void 0)},n)}}finally{t&&clearTimeout(t)}}class Ct{constructor(t,e){this.sched=t,this.reap=e,this._flags=0,this.q=new Set,this._run=()=>{this._flags&=-3,this.flush()},this.flush=()=>{if(this._flags&1)return;const{q:r}=this;if(r.size){this._flags|=1;try{this.reap(r)}finally{this._flags&=-2,!r.size||this._flags&2||this._sched()}}}}isRunning(){return!!(this._flags&1)}isEmpty(){return!this.q.size}add(t){this.q.size||this._flags&3||this._sched(),this.q.add(t)}delete(t){this.q.delete(t)}_sched(){this._flags|=2,this.sched(this._run)}}const Et=new Ct(y,n=>{for(const t of n)n.delete(t),t.doPull()});Et.flush;function q(n=se){const t=d();return e=>!t.result()&&n.isOpen()?(e&&n.onReady(e,t),n.isReady()):!1}const h="uneventful/is-stream";function Mt(n,t,e){return d().connect(n,t,e)}function I(n=p.job){return new ie(n)}class ie{constructor(t){this._job=t,this._callbacks=void 0,this._isReady=!0,this._isPulling=!1}isOpen(){return!this._job?.result()}isReady(){return this.isOpen()&&this._isReady}onReady(t,e){if(!this.isOpen())return this;const r=this._callbacks||(this._callbacks=new Map),i=e.release(()=>r.delete(t));return this.isReady()&&this&&!r.size&&Et.add(this),r.set(t,i),this}pause(){return this._isReady=!1,this}doPull(){if(this._isPulling)return;const{_callbacks:t}=this;if(t?.size){this._isPulling=!0;try{for(let[e,r]of t){if(!this.isReady())break;r(),t.delete(e),e()}}finally{this._isPulling=!1}}}resume(){this.isOpen()&&(this._isReady=!0,this.doPull())}}const se=I();function Pt(){for(var n=arguments[0],t=1;t<arguments.length;t++)n=arguments[t](n);return n}function ue(...n){return t=>Pt(t,...n)}function oe(...n){return t=>t(...n)}function le(n,t,e){return n.set(t,e),e}class ot extends Error{}class lt extends Error{}var v=1,A,ct;class j{constructor(t=y){this.rule=e=>R.mkRule(e,this.q),this.q=new Ct(t,e=>{if(!ct){ct=this;try{for(A of e)A.catchUp(),e.delete(A)}finally{ct=A=void 0}}}),this.flush=this.q.flush}static for(t=y){return this.cache.has(t)||this.cache.set(t,new this(t)),this.cache.get(t)}}j.cache=new WeakMap;const Vt=j.for(y),at=Vt.rule,ce=Vt.flush,ae=new WeakMap,Wt=new WeakMap,zt=[];function he(n){const t=n.lastChanged=n.latestSource=v;for(;n;n=zt.pop())for(let e=n.subscribers;e;e=e.nT){const r=e.tgt;r.latestSource>=t||r.latestSource===0||(r.latestSource=t,r.flags&1&&r.value.add(r),r.subscribers&&zt.push(r))}}var K;function fe(n,t){let e=K;e?(K=e.old,e.src=n,e.nS=void 0,e.pS=t.sources,e.tgt=t,e.nT=e.pT=void 0,e.ts=n.lastChanged,e.old=n.adding):e={src:n,nS:void 0,pS:t.sources,tgt:t,nT:void 0,pT:void 0,ts:n.lastChanged,old:n.adding},t.sources&&(t.sources.nS=e),t.sources=e,n.adding=e,t.latestSource===0||n.subscribe(e)}function qt(n){n.src.unsubscribe(n),n.nS&&(n.nS.pS=n.pS),n.pS&&(n.pS.nS=n.nS),n.src=n.tgt=n.nS=n.pS=n.nT=n.pT=void 0,n.old=K,K=n}const de={};class R{constructor(){this.value=void 0,this.validThrough=0,this.lastChanged=0,this.latestSource=v,this.flags=0,this.ctx=void 0,this.adding=void 0,this.sources=void 0,this.subscribers=void 0,this.compute=void 0}stream(t,e,r){let i=de;return(r?j.for(q(r)).rule:at)(()=>{const s=this.getValue();if(s!==i){const u=a(bt);try{t(i=s)}finally{a(u)}}}),h}getValue(){if(arguments.length)return this.stream.apply(this,arguments);this.catchUp();const t=p.cell;if(t){if(this.flags&32)throw new lt("Cached function dependency cycle");let e=this.adding;!e||e.tgt!==t?fe(this,t):e.ts===-1&&(e.ts=this.lastChanged,e.nS&&(e.nS.pS=e.pS,e.pS&&(e.pS.nS=e.nS),e.nS=void 0,e.pS=t.sources,t.sources.nS=e,t.sources=e))}if(this.flags&16)throw this.value;return this.value}setValue(t){const e=p.cell||A;if(e){if(e.flags&4)throw new ot("Side-effects not allowed in cached functions");if(this.adding&&this.adding.tgt===e)throw new lt("Can't update direct dependency");if(this.validThrough===v)throw new ot("Value already used")}else{if(t===this.value)return;this.validThrough===v&&++v}this.lastChanged===v||he(this),this.value=t}catchUp(){const{validThrough:t}=this;if(t!==v&&(this.validThrough=v,!(this.latestSource!==0&&this.latestSource<=t||!(this.flags&5))))if(this.sources)for(let e=this.sources;e;e=e.nS){const r=e.src;if(!(r.latestSource!==0&&r.latestSource<=t)){if(e.ts!==r.lastChanged)return this.doRecalc();if(r.catchUp(),r.lastChanged>t)return this.doRecalc()}}else return this.doRecalc()}doRecalc(){const t=a(this.ctx);for(let e=this.sources;e;e=e.nS)e.ts=-1,e.old=e.src.adding,e.src.adding=e,this.sources=e;this.flags|=32;try{if(this.flags&4){this.flags&=-17;try{const e=this.compute();(e!==this.value||!this.lastChanged)&&(this.value=e,this.lastChanged=v)}catch(e){this.flags|=16,this.value=e,this.lastChanged=v}}else{const{job:e}=this.ctx;e.restart();try{e.must(this.compute()),this.lastChanged=v}catch(r){throw this.disposeRule(),r}}}finally{this.flags&=-33,a(t);let e;for(let r=this.sources;r;){const i=r.pS;r.src.adding=r.old,r.old=void 0,r.ts===-1?qt(r):e=r,r=i}this.sources=e,this.flags&8&&this.disposeRule()}}disposeRule(){if(this.ctx.job.end(),this.flags|=8,this.value.delete(this),p!==this.ctx){for(let t=this.sources;t;){let e=t.nS;qt(t),t=e}this.sources=void 0}}subscribe(t){if(!this.subscribers){if(this.flags&4){this.latestSource=v;for(let e=this.sources;e;e=e.nS)e.src.subscribe(e)}this.flags&64&&this.compute()}this.subscribers!==t&&!t.pT&&(t.nT=this.subscribers,this.subscribers&&(this.subscribers.pT=t),this.subscribers=t)}unsubscribe(t){if(t.nT&&(t.nT.pT=t.pT),t.pT&&(t.pT.nT=t.nT),this.subscribers===t&&(this.subscribers=t.nT),!this.subscribers){if(this.flags&4){this.latestSource=0;for(let e=this.sources;e;e=e.nS)e.src.unsubscribe(e)}this.flags&64&&this.ctx.job?.restart()}}static mkValue(t){const e=new R;return e.value=t,e.lastChanged=v,e}static mkStream(t,e){const r=this.mkValue(e);r.flags|=64,r.ctx=k();const i=r.setValue.bind(r);return r.compute=()=>{var s;(s=r.ctx).job||(s.job=E().asyncCatch(o=>m.asyncThrow(o)).must(o=>{r.value=e,J(o)||(r.ctx.job=void 0)}));const u=a(r.ctx);try{t(i)}catch(o){m.asyncThrow(o),r.ctx.job.end(),r.ctx.job=void 0}finally{a(u)}},r.getValue.bind(r)}recalcWhen(t,e){let r=e?Wt.get(e)||le(Wt,e,new WeakMap):ae,i=r.get(t);if(!i){const s=e?e(t):t;let u=0;i=R.mkStream(o=>(s(()=>o(++u)),h),u),r.set(t,i)}i()}static mkCached(t){const e=new R;return e.compute=t,e.ctx=k(null,e),e.flags=4,e.latestSource=0,e.getValue.bind(e)}static mkRule(t,e){var r=d().release(u),i=new R,s=E();return i.value=e,i.compute=t.bind(null,u),i.ctx=k(s,i),i.flags=1,e.add(i),u;function u(){r(),i&&(i.disposeRule(),i=void 0)}}}class tt extends Function{get value(){return this()}valueOf(){return this()}toString(){return""+this()}toJSON(){return this()}peek(){return Jt(this)}readonly(){return this}withSet(t){const e=this;return et(function(){return e.apply(null,arguments)},t)}*"uneventful.until"(){return yield t=>{try{let e=this.peek();if(e)return P(t,e);at(r=>{try{(e=this())&&(r(),P(t,e))}catch(i){r(),Q(t,i)}})}catch(e){Q(t,e)}}}constructor(){super()}}class It extends tt{get value(){return this()}set value(t){this.set(t)}readonly(){const t=this;return et(function(){return t.apply(null,arguments)})}}function pe(n){const t=R.mkValue(n);return et(t.getValue.bind(t),t.setValue.bind(t))}function ve(n,t){return n instanceof tt?n:et(n.length?R.mkStream(n,t):R.mkCached(n))}function Jt(n,...t){if(!p.cell)return n(...t);const e=a(k(p.job));try{return n.apply(null,t)}finally{G(a(e))}}function ge(n,t){p.cell?.recalcWhen(n,t)}function et(n,t){return t&&(n.set=t),Object.setPrototypeOf(n,(t?It:tt).prototype)}function M(n){return d().must(n)}function S(n,t){return d().start(n,t)}function ye(){return!!p.job}const D=new WeakMap;function Ut(n=0,t=d()){let e=D.get(t);return e?clearTimeout(e):e===void 0&&!t.result()&&t.must(Ut.bind(null,0,t)),t.result()?D.delete(t):n?D.set(t,setTimeout(()=>{D.set(t,null),t.end()},n)):D.set(t,null),t}const ht=new WeakMap;function me(n=d()){let t=ht.get(n);if(!t){const e=new AbortController;t=e.signal,n.do(()=>{ht.set(n,null),e.abort()}),ht.set(n,t),n.result()&&e.abort()}return t}function we(n){const t=d(),e=E(t),{end:r}=e;return n||(n=i=>{e.must(i())}),e.asyncCatch(i=>t.asyncThrow(i)),function(){e.restart().must(t.release(r));const i=a(k(e));try{return n.apply(this,arguments)}catch(s){throw e.restart(),s}finally{G(a(i))}}}function Se(){const n=At();return n.source=Dt(n.source),n}function be(){return(n,t)=>(t?.return(),h)}function _e(n){return(t,e=S(),r)=>{const i=q(r),s=n[Symbol.asyncIterator]();return s.return&&M(()=>s.return()),i(u),h;function u(){s.next().then(({value:o,done:c})=>{c?e.return():i(()=>{t(o),i()?u():i(u)})},o=>e.throw(o))}}}function ke(n,t,e){return r=>{function i(s){r(s)}return n.addEventListener(t,i,e),M(()=>n.removeEventListener(t,i,e)),h}}function ft(n){return(t,e=S(),r)=>{const i=q(r),s=n[Symbol.iterator]();return s.return&&M(()=>s.return()),i(u),h;function u(){try{for(;;){const{value:o,done:c}=s.next();if(c)return e.return();if(t(o),!i())return i(u)}}catch(o){e.throw(o)}}}}function xe(n){return(t,e)=>{const r=d();return Promise.resolve(n).then(i=>void(r.result()||(t(i),e?.return())),i=>void(r.result()||e?.throw(i))),h}}function Re(n){return t=>{const e=d().must(()=>t=V);return y(()=>e.must(n(r=>{t(r)}))),h}}function Te(n){return(t,e)=>(M(()=>{t=V,e=void 0}),y(()=>{t(n),e?.return()}),h)}function Ce(n){return t=>{let e=0,r=setInterval(()=>t(e++),n);return M(()=>clearInterval(r)),h}}function Ee(n){return(t,e)=>n()(t,e)}function At(){let n,t,e;const r=i=>{n&&n(i)};return r.source=(i,s,u)=>(n=i,t=s,e=q(u),M(()=>n=t=e=void 0),h),r.end=()=>t?.return(),r.throw=i=>t?.throw(i),r.ready=i=>e(i),r}function Me(){return()=>h}function Dt(n){let t;const e=new Set,r=new Map,i=I(),s={isOpen(){return!t?.result()},isReady(){if(this.isOpen()){for(const[o]of r)if(!o.isReady())return i.pause(),!1;return!0}return i.pause(),!1},onReady(o,c){if(this.isOpen()){i.onReady(o,c);for(const[l]of r)l.isReady()||l.onReady(u,c)}return this}};function u(){s.isReady()&&i.resume()}return(o,c=S(),l)=>{const T=[o,c];return e.add(T),l&&r.set(l,1+(r.get(l)||0)),c.must(()=>{e.delete(T),l&&(r.set(l,r.get(l)-1),r.get(l)||r.delete(l)),e.size?s.isReady()&&!i.isReady()&&y(u):t?.end()}),e.size===1&&(t=m.connect(n,f=>{for(const[b,g]of e)try{b(f)}catch(O){g.throw(O)}},s).do(f=>{if(t=void 0,!J(f)){L(f)&&W(f);for(const[b,g]of e)C(f)?g.throw(f.err):g.return()}})),h}}function*Pe(n){let t=!1,e;const r={value:{item:void 0,next:u},done:!1},i=I(),s=d().connect(n,o=>{i.pause(),!(!e||s.result())&&(r.value.item=o,P(e,e=void 0))},i).do(o=>{C(o)&&W(o),e&&(y(u.bind(null,e)),e=void 0)});return i.pause(),yield u,{[Symbol.iterator](){return this},next(){if(!t)throw new Error("Must `yield next` in loop");return t=!1,r},return(){return s.end(),{value:void 0,done:!0}}};function u(o){if(e)throw new Error("Multiple `yield next` in loop");t=!0,s.result()?(r.value=void 0,r.done=!0,C(s.result())?Q(o,s.result().err):P(o,void 0)):(e=o,i.resume())}}function Ve(n){if(w(n["uneventful.until"]))return n["uneventful.until"]();if(w(n.then))return Tt(n);if(w(n))return S(t=>{Mt(n,e=>t.return(e)).onError(e=>t.throw(e)).onValue(()=>t.throw(new Error("Stream ended")))});throw new TypeError("until(): must be signal, source, or then-able")}function We(n){return dt(ft(n))}function dt(n){return(t,e=S(),r)=>{let i;const s=[],u=I();let o=e.connect(n,l=>{s.push(l),c(),u.pause()},u).do(l=>{o=void 0,s.length||i||!_(l)||e.return()});function c(){i||(i=e.connect(s.shift(),t,r).do(l=>{i=void 0,s.length?c():o?u.resume():!_(l)||e.return()}))}return h}}function ze(n){return t=>dt(nt(n)(t))}function qe(n){return t=>(e,r,i)=>{let s=0;return t(u=>n(u,s++)&&e(u),r,i)}}function nt(n){return t=>(e,r,i)=>{let s=0;return t(u=>e(n(u,s++)),r,i)}}function Ie(n){return pt(ft(n))}function pt(n){return(t,e=S(),r)=>{const i=new Set;let s=e.connect(n,u=>{const o=e.connect(u,t,r).do(c=>{i.delete(o),i.size||s||!_(c)||e.return()});i.add(o)}).do(u=>{s=void 0,i.size||!_(u)||e.return()});return h}}function Je(n){return t=>pt(nt(n)(t))}function Ue(n){return Nt((t,e)=>e<n)}function Ae(n){return t=>(e,r=S(),i)=>{let s=!1;const u=r.connect(n,()=>{s=!0,u.end()});return t(o=>s&&e(o),r,i)}}function Nt(n){return t=>(e,r,i)=>{let s=0,u=!1;return t(o=>(u||(u=!n(o,s++)))&&e(o),r,i)}}function De(n,t=V){const e=Math.abs(n);return r=>(i,s=S(),u)=>{const o=[],c=q(u);let l=!1,T=!1;const f=I();s.connect(r,g=>{if(o.push(g),!T&&c())return b();for(;o.length>e;)t(n<0?o.pop():o.shift());o.length===e&&(f.pause(),l=!0),o.length&&c(b)},f).do(g=>{_(g)&&s.return()});function b(){T=!0;try{for(;o.length;)if(i(o.shift()),l&&c()&&(l=!1,f.resume()),o.length&&!c())return c(b)}finally{T=!1}}return h}}function Ot(n){return(t,e=S(),r)=>{let i,s=e.connect(n,u=>{i?.end(),i=e.connect(u,t,r).do(o=>{i=void 0,s||!_(o)||e.return()})}).do(u=>{s=void 0,i||!_(u)||e.return()});return h}}function Ne(n){return t=>Ot(nt(n)(t))}function Oe(n){return Qt((t,e)=>e<n)}function Qe(n){return t=>(e,r=S(),i)=>(r.connect(n,()=>r.return()),t(e,r,i))}function Qt(n){return t=>(e,r,i)=>{let s=0;return t(u=>n(u,s++)?e(u):r?.return(),r,i)}}export{wt as CancelError,H as CancelResult,lt as CircularDependency,gt as ErrorResult,h as IsStream,j as RuleScheduler,tt as Signal,vt as ValueResult,It as Writable,ot as WriteConflict,me as abortSignal,q as backpressure,ve as cached,ue as compose,We as concat,dt as concatAll,ze as concatMap,Mt as connect,y as defer,m as detached,Pe as each,Se as emitter,be as empty,qe as filter,_e as fromAsyncIterable,ke as fromDomEvent,ft as fromIterable,xe as fromPromise,Re as fromSubscribe,Te as fromValue,U as fulfillPromise,d as getJob,yt as getResult,Ce as interval,oe as into,J as isCancel,C as isError,w as isFunction,Gt as isHandled,ye as isJobActive,L as isUnhandled,_ as isValue,Ee as lazy,E as makeJob,nt as map,W as markHandled,Ie as merge,pt as mergeAll,Je as mergeMap,At as mockSource,M as must,F as nativePromise,Me as never,Jt as noDeps,V as noop,Pt as pipe,mt as propagateResult,ge as recalcWhen,Q as reject,st as rejecter,P as resolve,it as resolver,we as restarting,at as rule,ce as runRules,Dt as share,Ue as skip,Ae as skipUntil,Nt as skipWhile,De as slack,re as sleep,S as start,Ot as switchAll,Ne as switchMap,Oe as take,Qe as takeUntil,Qt as takeWhile,I as throttle,Ut as timeout,Tt as to,Ve as until,pe as value};
1
+ let m=typeof queueMicrotask=="function"?queueMicrotask:(n=>t=>n.then(t))(Promise.resolve());function P(n,t){n("next",t)}function Q(n,t){n("throw",void 0,t)}function it(n){return n.bind(null,"next")}function st(n){return n.bind(null,"throw",void 0)}function V(){}function ut(n,t,e){return{op:n,val:t,err:e}}const H=Object.freeze(ut("cancel"));function vt(n){return ut("next",n)}function gt(n){return ut("throw",void 0,n)}function J(n){return n===H}function _(n){return n?n.op==="next":!1}function C(n){return n?n.op==="throw":!1}function L(n){return C(n)&&n.val===void 0}function Xt(n){return C(n)&&n.val===null}function W(n){return n.val=null,n.err}function yt(n){if(_(n))return n.val;n.op,U(V,t=>{throw t},n)}function U(n,t,e){C(e)?t(W(e)):J(e)?t(new wt("Job canceled")):n(e.val)}function mt(n,t){n.result()||U(n.return.bind(n),n.throw.bind(n),t)}class wt extends Error{}var p=k();function a(n){const t=p;return p=n,t}var B=[];function k(n,t){if(B&&B.length){const e=B.pop();return e.job=n,e.cell=t,e}return{job:n,cell:t}}function G(n){n.job=n.cell=null,B.push(n)}const z=new WeakMap,St=n=>{Promise.reject(n)},bt=k(),_t=new WeakMap;function kt(){return X(0,void 0,void 0)}function Zt(n){for(;n.v;)Z(n,n.n);n.u=void 0,Z(n,n)}function $t(n,t){++n.v,X(t,n.n,n)}function Ft(n,t){++n.v,X(t,n,n.p)}function jt(n,t){return++n.v,ne(n,X(t,n,n.p))}function Kt(n){return!n||n.v===0}function xt(n){return n?n.v:0}function te(n){if(xt(n))return Z(n,n.p)}class ee{constructor(){this.n=this,this.p=this,this.v=0,this.u=void 0}}var Y;function X(n,t,e){let r=Y;return r?(Y=r.n,r.n=t||r,r.p=e||r):(r=new ee,t&&(r.n=t),e&&(r.p=e)),r.v=n,r.n.p=r,r.p.n=r,r}function Z(n,t){--n.v;var e=t.v,r=t.u;return t.n&&(t.n.p=t.p),t.p&&(t.p.n=t.n),t.u=t.v=t.p=void 0,t.n=Y,Y=t,r&&r(),e}function ne(n,t){let e=t.u||(t.u=()=>{e&&(e===t.u&&Z(n,t),e=n=t=void 0)});return e}function v(n){return typeof n=="function"}function d(){const{job:n}=p;if(n)return n;throw new Error("No job is currently active")}function re(n){return t=>{p.job.must(n.release(t))}}function Rt(n,t){for(;xt(t);)try{te(t)(n)}catch(e){w.asyncThrow(e)}t&&Zt(t)}var x=new Set;class N{constructor(){this.end=()=>{const t=this._done||(this._done=H),e=this._cbs;if(!e&&!L(t))return;const r=x.size,i=a(bt);if(r||x.add(null),e&&e.u&&(e.u=Rt(t,e.u)),x.add(this),r){a(i);return}x.delete(null);for(const s of x)s._cbs&&(s._cbs=Rt(s._done,s._cbs)),x.delete(s),L(s._done)&&s.throw(W(s._done));a(i)},this._done=void 0,this._cbs=void 0}static create(t,e){const r=new N;return(t||e)&&(r.must((t||(t=d())).release(e||r.end)),_t.set(r,t)),r}do(t){return $t(this._chain(),t),this}onError(t){return this.do(e=>{C(e)&&t(W(e))})}onValue(t){return this.do(e=>{_(e)&&t(e.val)})}onCancel(t){return this.do(e=>{J(e)&&t()})}result(){return this._done||p.cell?.recalcWhen(this,re)||void 0}get[Symbol.toStringTag](){return"Job"}restart(){if(!this._done&&x.size){const t=x;x=new Set,this.end(),x=t}else this._end(H);return this._done=void 0,$.delete(this),this}_end(t){if(this._done)throw new Error("Job already ended");return this!==w&&(this._done=t),this.end(),this}throw(t){return this._done?((_t.get(this)||w).asyncThrow(t),this):this._end(gt(t))}return(t){return this._end(vt(t))}then(t,e){return F(this).then(t,e)}catch(t){return F(this).catch(t)}finally(t){return F(this).finally(t)}*[Symbol.iterator](){return this._done?yt(this._done):yield t=>{this.do(e=>U(it(t),st(t),e))}}start(t,e){if(!t)return E(this);let r,i;if(v(e))r=e.bind(t);else if(v(t))r=t;else{if(t instanceof N)return t;i=t}const s=E(this);try{if(r&&(i=s.run(r,s)),i!=null)if(i instanceof N)i!==s&&i.do(u=>mt(s,u));else{if(v(i.then))return i.then(u=>{s.result()||s.return(u)},u=>{s.result()||s.throw(u)}),s;if(v(i[Symbol.iterator])&&typeof i!="string")s.run(ie,i,s);else if(v(i))s.must(i);else throw new TypeError("Invalid value/return for start()")}return s}catch(u){throw s.end(),u}}connect(t,e,r){return this.start(i=>void t(e,i,r))}run(t,...e){const r=a(k(this));try{return t.apply(null,e)}finally{G(a(r))}}bind(t){const e=this;return function(){const r=a(k(e));try{return t.apply(this,arguments)}finally{G(a(r))}}}must(t){return v(t)&&Ft(this._chain(),t),this}release(t){if(this===w)return V;let e=this._chain();return(!this._done||e.u)&&(e=e.u||(e.u=kt())),jt(e,t)}asyncThrow(t){try{(z.get(this)||this.throw).call(this,t)}catch(e){this===w?z.set(this,St):z.delete(this);const r=z.get(this)||this.throw;r.call(this,t),r.call(this,e)}return this}asyncCatch(t){return v(t)?z.set(this,t):t===null&&z.delete(this),this}_chain(){return this===w&&this.end(),this._done&&Kt(this._cbs)&&m(this.end),this._cbs||(this._cbs=kt())}}const $=new WeakMap;function F(n=d()){return $.has(n)||$.set(n,new Promise((t,e)=>{const r=U.bind(null,t,e);n.result()?r(n.result()):n.do(r)})),$.get(n)}const E=N.create,w=E();w.end=()=>{throw new Error("Can't do that with the detached job")},w.asyncCatch(St);function ie(n,t){let e=n[Symbol.iterator](),r=!0,i=k(t),s=0,u=i.job.release(()=>{t=void 0,++s,o("return",void 0)});m(()=>{r=!1,o("next",void 0)});function o(c,l){if(!e)return;if(r)return m(o.bind(null,c,l));const T=a(i);try{r=!0;try{for(;;){++s;const{done:f,value:b}=e[c](l);if(f){t&&t.return(b),t=void 0;break}else if(v(b)){let y=!1,O=!1,Bt=s;if(b((rt,Gt,Yt)=>{y||(y=!0,c=rt,l=rt==="next"?Gt:Yt,O&&Bt===s&&o(rt,l))}),O=!0,!y)return}else{c="throw",l=new TypeError("Jobs must yield functions (or yield* Yielding<T>s)");continue}}}catch(f){e=t=void 0,i.job.throw(f)}e=void 0,u?.(),u=void 0}finally{a(T),r=!1}}}function*Tt(n){return yield t=>Promise.resolve(n).then(it(t),st(t))}function*se(n){try{var t;yield e=>{t=setTimeout(()=>{t=void 0,P(e,void 0)},n)}}finally{t&&clearTimeout(t)}}class Ct{constructor(t,e){this.sched=t,this.reap=e,this._flags=0,this.q=new Set,this._run=()=>{this._flags&=-3,this.flush()},this.flush=()=>{if(this._flags&1)return;const{q:r}=this;if(r.size){this._flags|=1;try{this.reap(r)}finally{this._flags&=-2,!r.size||this._flags&2||this._sched()}}}}isRunning(){return!!(this._flags&1)}isEmpty(){return!this.q.size}add(t){this.q.size||this._flags&3||this._sched(),this.q.add(t)}delete(t){this.q.delete(t)}_sched(){this._flags|=2,this.sched(this._run)}}const Et=new Ct(m,n=>{for(const t of n)n.delete(t),t.doPull()});Et.flush;function q(n=oe){const t=d();return e=>!t.result()&&n.isOpen()?(e&&n.onReady(e,t),n.isReady()):!1}const h="uneventful/is-stream";function Mt(n,t,e){return d().connect(n,t,e)}function I(n=p.job){return new ue(n)}class ue{constructor(t){this._job=t,this._callbacks=void 0,this._isReady=!0,this._isPulling=!1}isOpen(){return!this._job?.result()}isReady(){return this.isOpen()&&this._isReady}onReady(t,e){if(!this.isOpen())return this;const r=this._callbacks||(this._callbacks=new Map),i=e.release(()=>r.delete(t));return this.isReady()&&this&&!r.size&&Et.add(this),r.set(t,i),this}pause(){return this._isReady=!1,this}doPull(){if(this._isPulling)return;const{_callbacks:t}=this;if(t?.size){this._isPulling=!0;try{for(let[e,r]of t){if(!this.isReady())break;r(),t.delete(e),e()}}finally{this._isPulling=!1}}}resume(){this.isOpen()&&(this._isReady=!0,this.doPull())}}const oe=I();function Pt(){for(var n=arguments[0],t=1;t<arguments.length;t++)n=arguments[t](n);return n}function le(...n){return t=>Pt(t,...n)}function ce(...n){return t=>t(...n)}function ae(n,t,e){return n.set(t,e),e}class ot extends Error{}class lt extends Error{}var g=1,A,ct;class j{constructor(t=m){this.rule=e=>R.mkRule(e,this.q),this.q=new Ct(t,e=>{if(!ct){ct=this;try{for(A of e)A.catchUp(),e.delete(A)}finally{ct=A=void 0}}}),this.flush=this.q.flush}static for(t=m){return this.cache.has(t)||this.cache.set(t,new this(t)),this.cache.get(t)}}j.cache=new WeakMap;const Vt=j.for(m),at=Vt.rule,he=Vt.flush,fe=new WeakMap,Wt=new WeakMap,zt=[];function de(n){const t=n.lastChanged=n.latestSource=g;for(;n;n=zt.pop())for(let e=n.subscribers;e;e=e.nT){const r=e.tgt;r.latestSource>=t||r.latestSource===0||(r.latestSource=t,r.flags&1&&r.value.add(r),r.subscribers&&zt.push(r))}}var K;function pe(n,t){let e=K;e?(K=e.old,e.src=n,e.nS=void 0,e.pS=t.sources,e.tgt=t,e.nT=e.pT=void 0,e.ts=n.lastChanged,e.old=n.adding):e={src:n,nS:void 0,pS:t.sources,tgt:t,nT:void 0,pT:void 0,ts:n.lastChanged,old:n.adding},t.sources&&(t.sources.nS=e),t.sources=e,n.adding=e,t.latestSource===0||n.subscribe(e)}function qt(n){n.src.unsubscribe(n),n.nS&&(n.nS.pS=n.pS),n.pS&&(n.pS.nS=n.nS),n.src=n.tgt=n.nS=n.pS=n.nT=n.pT=void 0,n.old=K,K=n}const ve={};class R{constructor(){this.value=void 0,this.validThrough=0,this.lastChanged=0,this.latestSource=g,this.flags=0,this.ctx=void 0,this.adding=void 0,this.sources=void 0,this.subscribers=void 0,this.compute=void 0}stream(t,e,r){let i=ve;return(r?j.for(q(r)).rule:at)(()=>{const s=this.getValue();if(s!==i){const u=a(bt);try{t(i=s)}finally{a(u)}}}),h}getValue(){if(arguments.length)return this.stream.apply(this,arguments);this.catchUp();const t=p.cell;if(t){if(this.flags&32)throw new lt("Cached function dependency cycle");let e=this.adding;!e||e.tgt!==t?pe(this,t):e.ts===-1&&(e.ts=this.lastChanged,e.nS&&(e.nS.pS=e.pS,e.pS&&(e.pS.nS=e.nS),e.nS=void 0,e.pS=t.sources,t.sources.nS=e,t.sources=e))}if(this.flags&16)throw this.value;return this.value}setValue(t){const e=p.cell||A;if(e){if(e.flags&4)throw new ot("Side-effects not allowed in cached functions");if(this.adding&&this.adding.tgt===e)throw new lt("Can't update direct dependency");if(this.validThrough===g)throw new ot("Value already used")}else{if(t===this.value)return;this.validThrough===g&&++g}this.lastChanged===g||de(this),this.value=t}catchUp(){const{validThrough:t}=this;if(t!==g&&(this.validThrough=g,!(this.latestSource!==0&&this.latestSource<=t||!(this.flags&5))))if(this.sources)for(let e=this.sources;e;e=e.nS){const r=e.src;if(!(r.latestSource!==0&&r.latestSource<=t)){if(e.ts!==r.lastChanged)return this.doRecalc();if(r.catchUp(),r.lastChanged>t)return this.doRecalc()}}else return this.doRecalc()}doRecalc(){const t=a(this.ctx);for(let e=this.sources;e;e=e.nS)e.ts=-1,e.old=e.src.adding,e.src.adding=e,this.sources=e;this.flags|=32;try{if(this.flags&4){this.flags&=-17;try{const e=this.compute();(e!==this.value||!this.lastChanged)&&(this.value=e,this.lastChanged=g)}catch(e){this.flags|=16,this.value=e,this.lastChanged=g}}else{const{job:e}=this.ctx;e.restart();try{e.must(this.compute()),this.lastChanged=g}catch(r){throw this.disposeRule(),r}}}finally{this.flags&=-33,a(t);let e;for(let r=this.sources;r;){const i=r.pS;r.src.adding=r.old,r.old=void 0,r.ts===-1?qt(r):e=r,r=i}this.sources=e,this.flags&8&&this.disposeRule()}}disposeRule(){if(this.ctx.job.end(),this.flags|=8,this.value.delete(this),p!==this.ctx){for(let t=this.sources;t;){let e=t.nS;qt(t),t=e}this.sources=void 0}}subscribe(t){if(!this.subscribers){if(this.flags&4){this.latestSource=g;for(let e=this.sources;e;e=e.nS)e.src.subscribe(e)}this.flags&64&&this.compute()}this.subscribers!==t&&!t.pT&&(t.nT=this.subscribers,this.subscribers&&(this.subscribers.pT=t),this.subscribers=t)}unsubscribe(t){if(t.nT&&(t.nT.pT=t.pT),t.pT&&(t.pT.nT=t.nT),this.subscribers===t&&(this.subscribers=t.nT),!this.subscribers){if(this.flags&4){this.latestSource=0;for(let e=this.sources;e;e=e.nS)e.src.unsubscribe(e)}this.flags&64&&this.ctx.job?.restart()}}static mkValue(t){const e=new R;return e.value=t,e.lastChanged=g,e}static mkStream(t,e){const r=this.mkValue(e);r.flags|=64,r.ctx=k();const i=r.setValue.bind(r);return r.compute=()=>{var s;(s=r.ctx).job||(s.job=E().asyncCatch(o=>w.asyncThrow(o)).must(o=>{r.value=e,J(o)||(r.ctx.job=void 0)}));const u=a(r.ctx);try{t(i)}catch(o){w.asyncThrow(o),r.ctx.job.end(),r.ctx.job=void 0}finally{a(u)}},r.getValue.bind(r)}recalcWhen(t,e){let r=e?Wt.get(e)||ae(Wt,e,new WeakMap):fe,i=r.get(t);if(!i){const s=e?e(t):t;let u=0;i=R.mkStream(o=>(s(()=>o(++u)),h),u),r.set(t,i)}i()}static mkCached(t){const e=new R;return e.compute=t,e.ctx=k(null,e),e.flags=4,e.latestSource=0,e.getValue.bind(e)}static mkRule(t,e){var r=d().release(u),i=new R,s=E();return i.value=e,i.compute=t.bind(null,u),i.ctx=k(s,i),i.flags=1,e.add(i),u;function u(){r(),i&&(i.disposeRule(),i=void 0)}}}class tt extends Function{get value(){return this()}valueOf(){return this()}toString(){return""+this()}toJSON(){return this()}peek(){return Jt(this)}readonly(){return this}withSet(t){const e=this;return et(function(){return e.apply(null,arguments)},t)}*"uneventful.until"(){return yield t=>{try{let e=this.peek();if(e)return P(t,e);at(r=>{try{(e=this())&&(r(),P(t,e))}catch(i){r(),Q(t,i)}})}catch(e){Q(t,e)}}}constructor(){super()}}class It extends tt{get value(){return this()}set value(t){this.set(t)}readonly(){const t=this;return et(function(){return t.apply(null,arguments)})}}function ge(n){const t=R.mkValue(n);return et(t.getValue.bind(t),t.setValue.bind(t))}function ye(n,t){return n instanceof tt?n:et(n.length?R.mkStream(n,t):R.mkCached(n))}function Jt(n,...t){if(!p.cell)return n(...t);const e=a(k(p.job));try{return n.apply(null,t)}finally{G(a(e))}}function me(n,t){p.cell?.recalcWhen(n,t)}function et(n,t){return t&&(n.set=t),Object.setPrototypeOf(n,(t?It:tt).prototype)}function M(n){return d().must(n)}function S(n,t){return d().start(n,t)}function we(){return!!p.job}const D=new WeakMap;function Ut(n=0,t=d()){let e=D.get(t);return e?clearTimeout(e):e===void 0&&!t.result()&&t.must(Ut.bind(null,0,t)),t.result()?D.delete(t):n?D.set(t,setTimeout(()=>{D.set(t,null),t.end()},n)):D.set(t,null),t}const ht=new WeakMap;function Se(n=d()){let t=ht.get(n);if(!t){const e=new AbortController;t=e.signal,n.do(()=>{ht.set(n,null),e.abort()}),ht.set(n,t),n.result()&&e.abort()}return t}function At(n){const t=d(),e=E(t),{end:r}=e;return n||(n=i=>{e.must(i())}),e.asyncCatch(i=>t.asyncThrow(i)),function(){e.restart().must(t.release(r));const i=a(k(e));try{return n.apply(this,arguments)}catch(s){throw e.restart(),s}finally{G(a(i))}}}function be(){const n=Dt();return n.source=Nt(n.source),n}function _e(){return(n,t)=>(t?.return(),h)}function ke(n){return(t,e=S(),r)=>{const i=q(r),s=n[Symbol.asyncIterator]();return s.return&&M(()=>s.return()),i(u),h;function u(){s.next().then(({value:o,done:c})=>{c?e.return():i(()=>{t(o),i()?u():i(u)})},o=>e.throw(o))}}}function xe(n,t,e){return r=>{function i(s){r(s)}return n.addEventListener(t,i,e),M(()=>n.removeEventListener(t,i,e)),h}}function ft(n){return(t,e=S(),r)=>{const i=q(r),s=n[Symbol.iterator]();return s.return&&M(()=>s.return()),i(u),h;function u(){try{for(;;){const{value:o,done:c}=s.next();if(c)return e.return();if(t(o),!i())return i(u)}}catch(o){e.throw(o)}}}}function Re(n){return(t,e)=>{const r=d();return Promise.resolve(n).then(i=>void(r.result()||(t(i),e?.return())),i=>void(r.result()||e?.throw(i))),h}}function Te(n){return t=>{const e=d().must(()=>t=V);return m(()=>e.must(n(r=>{t(r)}))),h}}function Ce(n){return(t,e)=>(M(()=>{t=V,e=void 0}),m(()=>{t(n),e?.return()}),h)}function Ee(n){return t=>{let e=0,r=setInterval(()=>t(e++),n);return M(()=>clearInterval(r)),h}}function Me(n){return(t,e)=>n()(t,e)}function Dt(){let n,t,e;const r=i=>{n&&n(i)};return r.source=(i,s,u)=>(n=i,t=s,e=q(u),M(()=>n=t=e=void 0),h),r.end=()=>t?.return(),r.throw=i=>t?.throw(i),r.ready=i=>e(i),r}function Pe(){return()=>h}function Nt(n){let t;const e=new Set,r=new Map,i=I(),s={isOpen(){return!t?.result()},isReady(){if(this.isOpen()){for(const[o]of r)if(!o.isReady())return i.pause(),!1;return!0}return i.pause(),!1},onReady(o,c){if(this.isOpen()){i.onReady(o,c);for(const[l]of r)l.isReady()||l.onReady(u,c)}return this}};function u(){s.isReady()&&i.resume()}return(o,c=S(),l)=>{const T=[o,c];return e.add(T),l&&r.set(l,1+(r.get(l)||0)),c.must(()=>{e.delete(T),l&&(r.set(l,r.get(l)-1),r.get(l)||r.delete(l)),e.size?s.isReady()&&!i.isReady()&&m(u):t?.end()}),e.size===1&&(t=w.connect(n,f=>{for(const[b,y]of e)try{b(f)}catch(O){y.throw(O)}},s).do(f=>{if(t=void 0,!J(f)){L(f)&&W(f);for(const[b,y]of e)C(f)?y.throw(f.err):y.return()}})),h}}function*Ve(n){let t=!1,e;const r={value:{item:void 0,next:u},done:!1},i=I(),s=d().connect(n,o=>{i.pause(),!(!e||s.result())&&(r.value.item=o,P(e,e=void 0))},i).do(o=>{C(o)&&W(o),e&&(m(u.bind(null,e)),e=void 0)});return i.pause(),yield u,{[Symbol.iterator](){return this},next(){if(!t)throw new Error("Must `yield next` in loop");return t=!1,r},return(){return s.end(),{value:void 0,done:!0}}};function u(o){if(e)throw new Error("Multiple `yield next` in loop");t=!0,s.result()?(r.value=void 0,r.done=!0,C(s.result())?Q(o,s.result().err):P(o,void 0)):(e=o,i.resume())}}function We(n){if(v(n["uneventful.until"]))return n["uneventful.until"]();if(v(n.then))return Tt(n);if(v(n))return S(t=>{Mt(n,e=>t.return(e)).onError(e=>t.throw(e)).onValue(()=>t.throw(new Error("Stream ended")))});throw new TypeError("until(): must be signal, source, or then-able")}function Ot(n,t,e){return v(t)?S(r=>{n(At(t),r,e)}):(e=t,t=n,r=>Ot(r,t,e))}function ze(n){return dt(ft(n))}function dt(n){return(t,e=S(),r)=>{let i;const s=[],u=I();let o=e.connect(n,l=>{s.push(l),c(),u.pause()},u).do(l=>{o=void 0,s.length||i||!_(l)||e.return()});function c(){i||(i=e.connect(s.shift(),t,r).do(l=>{i=void 0,s.length?c():o?u.resume():!_(l)||e.return()}))}return h}}function qe(n){return t=>dt(nt(n)(t))}function Ie(n){return t=>(e,r,i)=>{let s=0;return t(u=>n(u,s++)&&e(u),r,i)}}function nt(n){return t=>(e,r,i)=>{let s=0;return t(u=>e(n(u,s++)),r,i)}}function Je(n){return pt(ft(n))}function pt(n){return(t,e=S(),r)=>{const i=new Set;let s=e.connect(n,u=>{const o=e.connect(u,t,r).do(c=>{i.delete(o),i.size||s||!_(c)||e.return()});i.add(o)}).do(u=>{s=void 0,i.size||!_(u)||e.return()});return h}}function Ue(n){return t=>pt(nt(n)(t))}function Ae(n){return Qt((t,e)=>e<n)}function De(n){return t=>(e,r=S(),i)=>{let s=!1;const u=r.connect(n,()=>{s=!0,u.end()});return t(o=>s&&e(o),r,i)}}function Qt(n){return t=>(e,r,i)=>{let s=0,u=!1;return t(o=>(u||(u=!n(o,s++)))&&e(o),r,i)}}function Ne(n,t=V){const e=Math.abs(n);return r=>(i,s=S(),u)=>{const o=[],c=q(u);let l=!1,T=!1;const f=I();s.connect(r,y=>{if(o.push(y),!T&&c())return b();for(;o.length>e;)t(n<0?o.pop():o.shift());o.length===e&&(f.pause(),l=!0),o.length&&c(b)},f).do(y=>{_(y)&&s.return()});function b(){T=!0;try{for(;o.length;)if(i(o.shift()),l&&c()&&(l=!1,f.resume()),o.length&&!c())return c(b)}finally{T=!1}}return h}}function Ht(n){return(t,e=S(),r)=>{let i,s=e.connect(n,u=>{i?.end(),i=e.connect(u,t,r).do(o=>{i=void 0,s||!_(o)||e.return()})}).do(u=>{s=void 0,i||!_(u)||e.return()});return h}}function Oe(n){return t=>Ht(nt(n)(t))}function Qe(n){return Lt((t,e)=>e<n)}function He(n){return t=>(e,r=S(),i)=>(r.connect(n,()=>r.return()),t(e,r,i))}function Lt(n){return t=>(e,r,i)=>{let s=0;return t(u=>n(u,s++)?e(u):r?.return(),r,i)}}export{wt as CancelError,H as CancelResult,lt as CircularDependency,gt as ErrorResult,h as IsStream,j as RuleScheduler,tt as Signal,vt as ValueResult,It as Writable,ot as WriteConflict,Se as abortSignal,q as backpressure,ye as cached,le as compose,ze as concat,dt as concatAll,qe as concatMap,Mt as connect,m as defer,w as detached,Ve as each,be as emitter,_e as empty,Ie as filter,Ot as forEach,ke as fromAsyncIterable,xe as fromDomEvent,ft as fromIterable,Re as fromPromise,Te as fromSubscribe,Ce as fromValue,U as fulfillPromise,d as getJob,yt as getResult,Ee as interval,ce as into,J as isCancel,C as isError,v as isFunction,Xt as isHandled,we as isJobActive,L as isUnhandled,_ as isValue,Me as lazy,E as makeJob,nt as map,W as markHandled,Je as merge,pt as mergeAll,Ue as mergeMap,Dt as mockSource,M as must,F as nativePromise,Pe as never,Jt as noDeps,V as noop,Pt as pipe,mt as propagateResult,me as recalcWhen,Q as reject,st as rejecter,P as resolve,it as resolver,At as restarting,at as rule,he as runRules,Nt as share,Ae as skip,De as skipUntil,Qt as skipWhile,Ne as slack,se as sleep,S as start,Ht as switchAll,Oe as switchMap,Qe as take,He as takeUntil,Lt as takeWhile,I as throttle,Ut as timeout,Tt as to,We as until,ge as value};
2
2
  //# sourceMappingURL=mod.mjs.map