uneventful 0.0.2 → 0.0.4
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 +7 -7
- package/dist/mod.d.ts +922 -608
- package/dist/mod.mjs +1771 -1
- package/dist/mod.mjs.map +1 -1
- package/package.json +2 -10
- package/dist/mod.cjs +0 -2
- package/dist/mod.cjs.map +0 -1
package/dist/mod.mjs
CHANGED
|
@@ -1,2 +1,1772 @@
|
|
|
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 defer = typeof queueMicrotask === "function" ? queueMicrotask : ((p) => (cb) => p.then(cb))(Promise.resolve());
|
|
2
|
+
|
|
3
|
+
function resolve(request, val) {
|
|
4
|
+
request("next", val);
|
|
5
|
+
}
|
|
6
|
+
function reject(request, reason) {
|
|
7
|
+
request("throw", void 0, reason);
|
|
8
|
+
}
|
|
9
|
+
function resolver(request) {
|
|
10
|
+
return request.bind(null, "next");
|
|
11
|
+
}
|
|
12
|
+
function rejecter(request) {
|
|
13
|
+
return request.bind(null, "throw", void 0);
|
|
14
|
+
}
|
|
15
|
+
function noop() {
|
|
16
|
+
}
|
|
17
|
+
function mkResult(op, val, err) {
|
|
18
|
+
return { op, val, err };
|
|
19
|
+
}
|
|
20
|
+
const CancelResult = Object.freeze(mkResult("cancel"));
|
|
21
|
+
function ValueResult(val) {
|
|
22
|
+
return mkResult("next", val);
|
|
23
|
+
}
|
|
24
|
+
function ErrorResult(err) {
|
|
25
|
+
return mkResult("throw", void 0, err);
|
|
26
|
+
}
|
|
27
|
+
function isCancel(res) {
|
|
28
|
+
return res === CancelResult;
|
|
29
|
+
}
|
|
30
|
+
function isValue(res) {
|
|
31
|
+
return res ? res.op === "next" : false;
|
|
32
|
+
}
|
|
33
|
+
function isError(res) {
|
|
34
|
+
return res ? res.op === "throw" : false;
|
|
35
|
+
}
|
|
36
|
+
function isUnhandled(res) {
|
|
37
|
+
return isError(res) && res.val === void 0;
|
|
38
|
+
}
|
|
39
|
+
function isHandled(res) {
|
|
40
|
+
return isError(res) && res.val === null;
|
|
41
|
+
}
|
|
42
|
+
function markHandled(res) {
|
|
43
|
+
res.val = null;
|
|
44
|
+
return res.err;
|
|
45
|
+
}
|
|
46
|
+
function getResult(res) {
|
|
47
|
+
if (isValue(res))
|
|
48
|
+
return res.val;
|
|
49
|
+
res.op;
|
|
50
|
+
fulfillPromise(noop, (e) => {
|
|
51
|
+
throw e;
|
|
52
|
+
}, res);
|
|
53
|
+
}
|
|
54
|
+
function fulfillPromise(resolve2, reject2, res) {
|
|
55
|
+
if (isError(res))
|
|
56
|
+
reject2(markHandled(res));
|
|
57
|
+
else if (isCancel(res))
|
|
58
|
+
reject2(new CancelError("Job canceled"));
|
|
59
|
+
else
|
|
60
|
+
resolve2(res.val);
|
|
61
|
+
}
|
|
62
|
+
function propagateResult(job, res) {
|
|
63
|
+
if (!job.result())
|
|
64
|
+
fulfillPromise(job.return.bind(job), job.throw.bind(job), res);
|
|
65
|
+
}
|
|
66
|
+
class CancelError extends Error {
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var current = makeCtx();
|
|
70
|
+
function swapCtx(future) {
|
|
71
|
+
const now = current;
|
|
72
|
+
current = future;
|
|
73
|
+
return now;
|
|
74
|
+
}
|
|
75
|
+
var freelist = [];
|
|
76
|
+
function makeCtx(job, cell) {
|
|
77
|
+
if (freelist && freelist.length) {
|
|
78
|
+
const s = freelist.pop();
|
|
79
|
+
s.job = job;
|
|
80
|
+
s.cell = cell;
|
|
81
|
+
return s;
|
|
82
|
+
}
|
|
83
|
+
return { job, cell };
|
|
84
|
+
}
|
|
85
|
+
function freeCtx(s) {
|
|
86
|
+
s.job = s.cell = null;
|
|
87
|
+
freelist.push(s);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const catchers = /* @__PURE__ */ new WeakMap(), defaultCatch = (e) => {
|
|
91
|
+
Promise.reject(e);
|
|
92
|
+
};
|
|
93
|
+
const nullCtx = makeCtx();
|
|
94
|
+
const owners = /* @__PURE__ */ new WeakMap();
|
|
95
|
+
|
|
96
|
+
function chain() {
|
|
97
|
+
return link(0, void 0, void 0);
|
|
98
|
+
}
|
|
99
|
+
function recycle(c) {
|
|
100
|
+
while (c.v)
|
|
101
|
+
unlink(c, c.n);
|
|
102
|
+
c.u = void 0;
|
|
103
|
+
unlink(c, c);
|
|
104
|
+
}
|
|
105
|
+
function unshift(c, v) {
|
|
106
|
+
++c.v;
|
|
107
|
+
link(v, c.n, c);
|
|
108
|
+
}
|
|
109
|
+
function push(c, v) {
|
|
110
|
+
++c.v;
|
|
111
|
+
link(v, c, c.p);
|
|
112
|
+
}
|
|
113
|
+
function pushCB(c, v) {
|
|
114
|
+
++c.v;
|
|
115
|
+
return unlinker(c, link(v, c, c.p));
|
|
116
|
+
}
|
|
117
|
+
function isEmpty(c) {
|
|
118
|
+
return !c || c.v === 0;
|
|
119
|
+
}
|
|
120
|
+
function qlen(c) {
|
|
121
|
+
return c ? c.v : 0;
|
|
122
|
+
}
|
|
123
|
+
function pop(c) {
|
|
124
|
+
if (qlen(c))
|
|
125
|
+
return unlink(c, c.p);
|
|
126
|
+
}
|
|
127
|
+
class Node {
|
|
128
|
+
constructor() {
|
|
129
|
+
/** The next node (or the start node if this is a chain head) */
|
|
130
|
+
this.n = this;
|
|
131
|
+
/** The previous node (or the end node if this is a chain head) */
|
|
132
|
+
this.p = this;
|
|
133
|
+
/** The value held by the node (or the chain length if this is a chain head) */
|
|
134
|
+
this.v = 0;
|
|
135
|
+
/** The most recently created undo callback for this node */
|
|
136
|
+
this.u = void 0;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
var free;
|
|
140
|
+
function link(v, n, p) {
|
|
141
|
+
let node = free;
|
|
142
|
+
if (node) {
|
|
143
|
+
free = node.n;
|
|
144
|
+
node.n = n || node;
|
|
145
|
+
node.p = p || node;
|
|
146
|
+
} else {
|
|
147
|
+
node = new Node();
|
|
148
|
+
if (n)
|
|
149
|
+
node.n = n;
|
|
150
|
+
if (p)
|
|
151
|
+
node.p = p;
|
|
152
|
+
}
|
|
153
|
+
node.v = v;
|
|
154
|
+
node.n.p = node;
|
|
155
|
+
node.p.n = node;
|
|
156
|
+
return node;
|
|
157
|
+
}
|
|
158
|
+
function unlink(c, node) {
|
|
159
|
+
--c.v;
|
|
160
|
+
var v = node.v, u = node.u;
|
|
161
|
+
node.n && (node.n.p = node.p);
|
|
162
|
+
node.p && (node.p.n = node.n);
|
|
163
|
+
node.u = node.v = node.p = void 0;
|
|
164
|
+
node.n = free;
|
|
165
|
+
free = node;
|
|
166
|
+
if (u)
|
|
167
|
+
u();
|
|
168
|
+
return v;
|
|
169
|
+
}
|
|
170
|
+
function unlinker(chain2, node) {
|
|
171
|
+
let u = node.u ||= () => {
|
|
172
|
+
if (u) {
|
|
173
|
+
if (u === node.u)
|
|
174
|
+
unlink(chain2, node);
|
|
175
|
+
u = chain2 = node = void 0;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
return u;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function isFunction(f) {
|
|
182
|
+
return typeof f === "function";
|
|
183
|
+
}
|
|
184
|
+
function getJob() {
|
|
185
|
+
const { job } = current;
|
|
186
|
+
if (job)
|
|
187
|
+
return job;
|
|
188
|
+
throw new Error("No job is currently active");
|
|
189
|
+
}
|
|
190
|
+
function recalcJob(job) {
|
|
191
|
+
return (cb) => {
|
|
192
|
+
current.job.must(job.release(cb));
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function runChain(res, cbs) {
|
|
196
|
+
while (qlen(cbs))
|
|
197
|
+
try {
|
|
198
|
+
pop(cbs)(res);
|
|
199
|
+
} catch (e) {
|
|
200
|
+
detached.asyncThrow(e);
|
|
201
|
+
}
|
|
202
|
+
cbs && recycle(cbs);
|
|
203
|
+
return void 0;
|
|
204
|
+
}
|
|
205
|
+
var inProcess = /* @__PURE__ */ new Set();
|
|
206
|
+
class _Job {
|
|
207
|
+
constructor() {
|
|
208
|
+
this.end = () => {
|
|
209
|
+
const res = this._done ||= CancelResult, cbs = this._cbs;
|
|
210
|
+
if (!cbs && !isUnhandled(res))
|
|
211
|
+
return;
|
|
212
|
+
const ct = inProcess.size, old = swapCtx(nullCtx);
|
|
213
|
+
if (!ct)
|
|
214
|
+
inProcess.add(null);
|
|
215
|
+
if (cbs && cbs.u)
|
|
216
|
+
cbs.u = runChain(res, cbs.u);
|
|
217
|
+
inProcess.add(this);
|
|
218
|
+
if (ct) {
|
|
219
|
+
swapCtx(old);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
inProcess.delete(null);
|
|
223
|
+
for (const item of inProcess) {
|
|
224
|
+
if (item._cbs)
|
|
225
|
+
item._cbs = runChain(item._done, item._cbs);
|
|
226
|
+
inProcess.delete(item);
|
|
227
|
+
if (isUnhandled(item._done))
|
|
228
|
+
item.throw(markHandled(item._done));
|
|
229
|
+
}
|
|
230
|
+
swapCtx(old);
|
|
231
|
+
};
|
|
232
|
+
this._done = void 0;
|
|
233
|
+
// Chain whose .u stores a second chain for `release()` callbacks
|
|
234
|
+
this._cbs = void 0;
|
|
235
|
+
}
|
|
236
|
+
/** @internal */
|
|
237
|
+
static create(parent, stop) {
|
|
238
|
+
const job = new _Job();
|
|
239
|
+
if (parent || stop) {
|
|
240
|
+
job.must((parent ||= getJob()).release(stop || job.end));
|
|
241
|
+
owners.set(job, parent);
|
|
242
|
+
}
|
|
243
|
+
return job;
|
|
244
|
+
}
|
|
245
|
+
do(cleanup) {
|
|
246
|
+
unshift(this._chain(), cleanup);
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
onError(cb) {
|
|
250
|
+
return this.do((r) => {
|
|
251
|
+
if (isError(r))
|
|
252
|
+
cb(markHandled(r));
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
onValue(cb) {
|
|
256
|
+
return this.do((r) => {
|
|
257
|
+
if (isValue(r))
|
|
258
|
+
cb(r.val);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
onCancel(cb) {
|
|
262
|
+
return this.do((r) => {
|
|
263
|
+
if (isCancel(r))
|
|
264
|
+
cb();
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
result() {
|
|
268
|
+
return this._done || current.cell?.recalcWhen(this, recalcJob) || void 0;
|
|
269
|
+
}
|
|
270
|
+
get [Symbol.toStringTag]() {
|
|
271
|
+
return "Job";
|
|
272
|
+
}
|
|
273
|
+
restart() {
|
|
274
|
+
if (!this._done && inProcess.size) {
|
|
275
|
+
const old = inProcess;
|
|
276
|
+
inProcess = /* @__PURE__ */ new Set();
|
|
277
|
+
this.end();
|
|
278
|
+
inProcess = old;
|
|
279
|
+
} else {
|
|
280
|
+
this._end(CancelResult);
|
|
281
|
+
}
|
|
282
|
+
this._done = void 0;
|
|
283
|
+
promises.delete(this);
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
_end(res) {
|
|
287
|
+
if (this._done)
|
|
288
|
+
throw new Error("Job already ended");
|
|
289
|
+
if (this !== detached)
|
|
290
|
+
this._done = res;
|
|
291
|
+
this.end();
|
|
292
|
+
return this;
|
|
293
|
+
}
|
|
294
|
+
throw(err) {
|
|
295
|
+
if (this._done) {
|
|
296
|
+
(owners.get(this) || detached).asyncThrow(err);
|
|
297
|
+
return this;
|
|
298
|
+
}
|
|
299
|
+
return this._end(ErrorResult(err));
|
|
300
|
+
}
|
|
301
|
+
return(val) {
|
|
302
|
+
return this._end(ValueResult(val));
|
|
303
|
+
}
|
|
304
|
+
then(onfulfilled, onrejected) {
|
|
305
|
+
return nativePromise(this).then(onfulfilled, onrejected);
|
|
306
|
+
}
|
|
307
|
+
catch(onrejected) {
|
|
308
|
+
return nativePromise(this).catch(onrejected);
|
|
309
|
+
}
|
|
310
|
+
finally(onfinally) {
|
|
311
|
+
return nativePromise(this).finally(onfinally);
|
|
312
|
+
}
|
|
313
|
+
*[Symbol.iterator]() {
|
|
314
|
+
if (this._done) {
|
|
315
|
+
return getResult(this._done);
|
|
316
|
+
} else
|
|
317
|
+
return yield (req) => {
|
|
318
|
+
this.do((res) => fulfillPromise(resolver(req), rejecter(req), res));
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
start(fnOrCtx, fn) {
|
|
322
|
+
if (!fnOrCtx)
|
|
323
|
+
return makeJob(this);
|
|
324
|
+
let init, result;
|
|
325
|
+
if (isFunction(fn)) {
|
|
326
|
+
init = fn.bind(fnOrCtx);
|
|
327
|
+
} else if (isFunction(fnOrCtx)) {
|
|
328
|
+
init = fnOrCtx;
|
|
329
|
+
} else if (fnOrCtx instanceof _Job) {
|
|
330
|
+
return fnOrCtx;
|
|
331
|
+
} else {
|
|
332
|
+
result = fnOrCtx;
|
|
333
|
+
}
|
|
334
|
+
const job = makeJob(this);
|
|
335
|
+
try {
|
|
336
|
+
if (init)
|
|
337
|
+
result = job.run(init, job);
|
|
338
|
+
if (result != null) {
|
|
339
|
+
if (result instanceof _Job) {
|
|
340
|
+
if (result !== job)
|
|
341
|
+
result.do((res) => propagateResult(job, res));
|
|
342
|
+
} else if (isFunction(result.then)) {
|
|
343
|
+
result.then(
|
|
344
|
+
(v) => {
|
|
345
|
+
job.result() || job.return(v);
|
|
346
|
+
},
|
|
347
|
+
(e) => {
|
|
348
|
+
job.result() || job.throw(e);
|
|
349
|
+
}
|
|
350
|
+
);
|
|
351
|
+
return job;
|
|
352
|
+
} else if (isFunction(result[Symbol.iterator]) && typeof result !== "string") {
|
|
353
|
+
job.run(runGen, result, job);
|
|
354
|
+
} else if (isFunction(result)) {
|
|
355
|
+
job.must(result);
|
|
356
|
+
} else {
|
|
357
|
+
throw new TypeError("Invalid value/return for start()");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return job;
|
|
361
|
+
} catch (e) {
|
|
362
|
+
job.end();
|
|
363
|
+
throw e;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
connect(src, sink, inlet) {
|
|
367
|
+
return this.start((job) => void src(sink, job, inlet));
|
|
368
|
+
}
|
|
369
|
+
run(fn, ...args) {
|
|
370
|
+
const old = swapCtx(makeCtx(this));
|
|
371
|
+
try {
|
|
372
|
+
return fn.apply(null, args);
|
|
373
|
+
} finally {
|
|
374
|
+
freeCtx(swapCtx(old));
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
bind(fn) {
|
|
378
|
+
const job = this;
|
|
379
|
+
return function() {
|
|
380
|
+
const old = swapCtx(makeCtx(job));
|
|
381
|
+
try {
|
|
382
|
+
return fn.apply(this, arguments);
|
|
383
|
+
} finally {
|
|
384
|
+
freeCtx(swapCtx(old));
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
must(cleanup) {
|
|
389
|
+
if (isFunction(cleanup))
|
|
390
|
+
push(this._chain(), cleanup);
|
|
391
|
+
return this;
|
|
392
|
+
}
|
|
393
|
+
release(cleanup) {
|
|
394
|
+
if (this === detached)
|
|
395
|
+
return noop;
|
|
396
|
+
let cbs = this._chain();
|
|
397
|
+
if (!this._done || cbs.u)
|
|
398
|
+
cbs = cbs.u ||= chain();
|
|
399
|
+
return pushCB(cbs, cleanup);
|
|
400
|
+
}
|
|
401
|
+
asyncThrow(err) {
|
|
402
|
+
try {
|
|
403
|
+
(catchers.get(this) || this.throw).call(this, err);
|
|
404
|
+
} catch (e) {
|
|
405
|
+
if (this === detached)
|
|
406
|
+
catchers.set(this, defaultCatch);
|
|
407
|
+
else
|
|
408
|
+
catchers.delete(this);
|
|
409
|
+
const catcher = catchers.get(this) || this.throw;
|
|
410
|
+
catcher.call(this, err);
|
|
411
|
+
catcher.call(this, e);
|
|
412
|
+
}
|
|
413
|
+
return this;
|
|
414
|
+
}
|
|
415
|
+
asyncCatch(handler) {
|
|
416
|
+
if (isFunction(handler))
|
|
417
|
+
catchers.set(this, handler);
|
|
418
|
+
else if (handler === null)
|
|
419
|
+
catchers.delete(this);
|
|
420
|
+
return this;
|
|
421
|
+
}
|
|
422
|
+
_chain() {
|
|
423
|
+
if (this === detached)
|
|
424
|
+
this.end();
|
|
425
|
+
if (this._done && isEmpty(this._cbs))
|
|
426
|
+
defer(this.end);
|
|
427
|
+
return this._cbs ||= chain();
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
const promises = /* @__PURE__ */ new WeakMap();
|
|
431
|
+
function nativePromise(job = getJob()) {
|
|
432
|
+
if (!promises.has(job)) {
|
|
433
|
+
promises.set(job, new Promise((res, rej) => {
|
|
434
|
+
const toPromise = fulfillPromise.bind(null, res, rej);
|
|
435
|
+
if (job.result())
|
|
436
|
+
toPromise(job.result());
|
|
437
|
+
else
|
|
438
|
+
job.do(toPromise);
|
|
439
|
+
}));
|
|
440
|
+
}
|
|
441
|
+
return promises.get(job);
|
|
442
|
+
}
|
|
443
|
+
const makeJob = _Job.create;
|
|
444
|
+
const detached = makeJob();
|
|
445
|
+
detached.end = () => {
|
|
446
|
+
throw new Error("Can't do that with the detached job");
|
|
447
|
+
};
|
|
448
|
+
detached.asyncCatch(defaultCatch);
|
|
449
|
+
function runGen(g, job) {
|
|
450
|
+
let it = g[Symbol.iterator](), running = true, ctx = makeCtx(job), ct = 0;
|
|
451
|
+
let done = ctx.job.release(() => {
|
|
452
|
+
job = void 0;
|
|
453
|
+
++ct;
|
|
454
|
+
step("return", void 0);
|
|
455
|
+
});
|
|
456
|
+
defer(() => {
|
|
457
|
+
running = false;
|
|
458
|
+
step("next", void 0);
|
|
459
|
+
});
|
|
460
|
+
function step(method, arg) {
|
|
461
|
+
if (!it)
|
|
462
|
+
return;
|
|
463
|
+
if (running) {
|
|
464
|
+
return defer(step.bind(null, method, arg));
|
|
465
|
+
}
|
|
466
|
+
const old = swapCtx(ctx);
|
|
467
|
+
try {
|
|
468
|
+
running = true;
|
|
469
|
+
try {
|
|
470
|
+
for (; ; ) {
|
|
471
|
+
++ct;
|
|
472
|
+
const { done: done2, value } = it[method](arg);
|
|
473
|
+
if (done2) {
|
|
474
|
+
job && job.return(value);
|
|
475
|
+
job = void 0;
|
|
476
|
+
break;
|
|
477
|
+
} else if (!isFunction(value)) {
|
|
478
|
+
method = "throw";
|
|
479
|
+
arg = new TypeError("Jobs must yield functions (or yield* Yielding<T>s)");
|
|
480
|
+
continue;
|
|
481
|
+
} else {
|
|
482
|
+
let called = false, returned = false, count = ct;
|
|
483
|
+
value((op, val, err) => {
|
|
484
|
+
if (called)
|
|
485
|
+
return;
|
|
486
|
+
else
|
|
487
|
+
called = true;
|
|
488
|
+
method = op;
|
|
489
|
+
arg = op === "next" ? val : err;
|
|
490
|
+
if (returned && count === ct)
|
|
491
|
+
step(op, arg);
|
|
492
|
+
});
|
|
493
|
+
returned = true;
|
|
494
|
+
if (!called)
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
} catch (e) {
|
|
499
|
+
it = job = void 0;
|
|
500
|
+
ctx.job.throw(e);
|
|
501
|
+
}
|
|
502
|
+
it = void 0;
|
|
503
|
+
done?.();
|
|
504
|
+
done = void 0;
|
|
505
|
+
} finally {
|
|
506
|
+
swapCtx(old);
|
|
507
|
+
running = false;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function* to(p) {
|
|
513
|
+
return yield (res) => Promise.resolve(p).then(resolver(res), rejecter(res));
|
|
514
|
+
}
|
|
515
|
+
function* sleep(ms) {
|
|
516
|
+
try {
|
|
517
|
+
var id;
|
|
518
|
+
yield (r) => {
|
|
519
|
+
id = setTimeout(() => {
|
|
520
|
+
id = void 0;
|
|
521
|
+
resolve(r, void 0);
|
|
522
|
+
}, ms);
|
|
523
|
+
};
|
|
524
|
+
} finally {
|
|
525
|
+
if (id)
|
|
526
|
+
clearTimeout(id);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
class RunQueue {
|
|
531
|
+
constructor(sched, reap) {
|
|
532
|
+
this.sched = sched;
|
|
533
|
+
this.reap = reap;
|
|
534
|
+
this._flags = 0 /* Unset */;
|
|
535
|
+
this.q = /* @__PURE__ */ new Set();
|
|
536
|
+
this._run = () => {
|
|
537
|
+
this._flags &= ~2 /* Scheduled */;
|
|
538
|
+
this.flush();
|
|
539
|
+
};
|
|
540
|
+
/** Run all pending items */
|
|
541
|
+
this.flush = () => {
|
|
542
|
+
if (this._flags & 1 /* Running */)
|
|
543
|
+
return;
|
|
544
|
+
const { q: queue } = this;
|
|
545
|
+
if (!queue.size)
|
|
546
|
+
return;
|
|
547
|
+
this._flags |= 1 /* Running */;
|
|
548
|
+
try {
|
|
549
|
+
this.reap(queue);
|
|
550
|
+
} finally {
|
|
551
|
+
this._flags &= ~1 /* Running */;
|
|
552
|
+
!queue.size || this._flags & 2 /* Scheduled */ || this._sched();
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
/** Is this queue currently running? */
|
|
557
|
+
isRunning() {
|
|
558
|
+
return !!(this._flags & 1 /* Running */);
|
|
559
|
+
}
|
|
560
|
+
/** Is this queue currently empty? */
|
|
561
|
+
isEmpty() {
|
|
562
|
+
return !this.q.size;
|
|
563
|
+
}
|
|
564
|
+
add(subject) {
|
|
565
|
+
this.q.size || this._flags & (1 /* Running */ | 2 /* Scheduled */) || this._sched();
|
|
566
|
+
this.q.add(subject);
|
|
567
|
+
}
|
|
568
|
+
delete(subject) {
|
|
569
|
+
this.q.delete(subject);
|
|
570
|
+
}
|
|
571
|
+
_sched() {
|
|
572
|
+
this._flags |= 2 /* Scheduled */;
|
|
573
|
+
this.sched(this._run);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
var currentRule;
|
|
577
|
+
var currentQueue;
|
|
578
|
+
const ruleQueues = /* @__PURE__ */ new WeakMap();
|
|
579
|
+
class RuleQueue extends RunQueue {
|
|
580
|
+
constructor(sched) {
|
|
581
|
+
super(sched, function(q) {
|
|
582
|
+
if (currentQueue)
|
|
583
|
+
return;
|
|
584
|
+
currentQueue = this;
|
|
585
|
+
try {
|
|
586
|
+
for (currentRule of q) {
|
|
587
|
+
currentRule.catchUp();
|
|
588
|
+
q.delete(currentRule);
|
|
589
|
+
}
|
|
590
|
+
} finally {
|
|
591
|
+
currentQueue = currentRule = void 0;
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function ruleQueue(scheduleFn = defer) {
|
|
597
|
+
ruleQueues.has(scheduleFn) || ruleQueues.set(scheduleFn, new RuleQueue(scheduleFn));
|
|
598
|
+
return ruleQueues.get(scheduleFn);
|
|
599
|
+
}
|
|
600
|
+
const defaultQ = /* @__PURE__ */ ruleQueue(defer);
|
|
601
|
+
const pulls = new RunQueue(defer, (pulls2) => {
|
|
602
|
+
for (const conn of pulls2) {
|
|
603
|
+
pulls2.delete(conn);
|
|
604
|
+
conn.doPull();
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
pulls.flush;
|
|
608
|
+
|
|
609
|
+
function backpressure(inlet = defaultInlet) {
|
|
610
|
+
const job = getJob();
|
|
611
|
+
return (cb) => {
|
|
612
|
+
if (!job.result() && inlet.isOpen()) {
|
|
613
|
+
if (cb)
|
|
614
|
+
inlet.onReady(cb, job);
|
|
615
|
+
return inlet.isReady();
|
|
616
|
+
}
|
|
617
|
+
return false;
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
const IsStream = "uneventful/is-stream";
|
|
621
|
+
function connect(src, sink, inlet) {
|
|
622
|
+
return getJob().connect(src, sink, inlet);
|
|
623
|
+
}
|
|
624
|
+
function throttle(job = current.job) {
|
|
625
|
+
return new _Throttle(job);
|
|
626
|
+
}
|
|
627
|
+
class _Throttle {
|
|
628
|
+
/** @internal */
|
|
629
|
+
constructor(_job) {
|
|
630
|
+
this._job = _job;
|
|
631
|
+
/** @internal */
|
|
632
|
+
this._callbacks = void 0;
|
|
633
|
+
this._isReady = true;
|
|
634
|
+
this._isPulling = false;
|
|
635
|
+
}
|
|
636
|
+
isOpen() {
|
|
637
|
+
return !this._job?.result();
|
|
638
|
+
}
|
|
639
|
+
/** Is the conduit currently ready to receive data? */
|
|
640
|
+
isReady() {
|
|
641
|
+
return this.isOpen() && this._isReady;
|
|
642
|
+
}
|
|
643
|
+
onReady(cb, job) {
|
|
644
|
+
if (!this.isOpen())
|
|
645
|
+
return this;
|
|
646
|
+
const _callbacks = this._callbacks ||= /* @__PURE__ */ new Map();
|
|
647
|
+
const unlink = job.release(() => _callbacks.delete(cb));
|
|
648
|
+
if (this.isReady() && this && !_callbacks.size) {
|
|
649
|
+
pulls.add(this);
|
|
650
|
+
}
|
|
651
|
+
_callbacks.set(cb, unlink);
|
|
652
|
+
return this;
|
|
653
|
+
}
|
|
654
|
+
pause() {
|
|
655
|
+
this._isReady = false;
|
|
656
|
+
return this;
|
|
657
|
+
}
|
|
658
|
+
doPull() {
|
|
659
|
+
if (this._isPulling)
|
|
660
|
+
return;
|
|
661
|
+
const { _callbacks } = this;
|
|
662
|
+
if (!_callbacks?.size)
|
|
663
|
+
return;
|
|
664
|
+
this._isPulling = true;
|
|
665
|
+
try {
|
|
666
|
+
for (let [cb, unlink] of _callbacks) {
|
|
667
|
+
if (!this.isReady())
|
|
668
|
+
break;
|
|
669
|
+
unlink();
|
|
670
|
+
_callbacks.delete(cb);
|
|
671
|
+
cb();
|
|
672
|
+
}
|
|
673
|
+
} finally {
|
|
674
|
+
this._isPulling = false;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
resume() {
|
|
678
|
+
if (this.isOpen()) {
|
|
679
|
+
this._isReady = true;
|
|
680
|
+
this.doPull();
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
const defaultInlet = throttle();
|
|
685
|
+
function pipe() {
|
|
686
|
+
var v = arguments[0];
|
|
687
|
+
for (var i = 1; i < arguments.length; i++)
|
|
688
|
+
v = arguments[i](v);
|
|
689
|
+
return v;
|
|
690
|
+
}
|
|
691
|
+
function compose(...fns) {
|
|
692
|
+
return (val) => pipe(val, ...fns);
|
|
693
|
+
}
|
|
694
|
+
function into(...args) {
|
|
695
|
+
return (src) => src(...args);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
function setMap(map, key, val) {
|
|
699
|
+
map.set(key, val);
|
|
700
|
+
return val;
|
|
701
|
+
}
|
|
702
|
+
function CallableObject(fn) {
|
|
703
|
+
return Object.setPrototypeOf(fn, new.target.prototype);
|
|
704
|
+
}
|
|
705
|
+
CallableObject.prototype = Function.prototype;
|
|
706
|
+
|
|
707
|
+
class WriteConflict extends Error {
|
|
708
|
+
}
|
|
709
|
+
class CircularDependency extends Error {
|
|
710
|
+
}
|
|
711
|
+
var timestamp = 1;
|
|
712
|
+
const fntrackers = /* @__PURE__ */ new WeakMap();
|
|
713
|
+
const obtrackers = /* @__PURE__ */ new WeakMap();
|
|
714
|
+
const dirtyStack = [];
|
|
715
|
+
function markDependentsDirty(cell) {
|
|
716
|
+
const latestSource = cell.latestSource = timestamp;
|
|
717
|
+
for (; cell; cell = dirtyStack.pop()) {
|
|
718
|
+
for (let sub = cell.subscribers; sub; sub = sub.nT) {
|
|
719
|
+
const tgt = sub.tgt;
|
|
720
|
+
if (tgt.latestSource >= latestSource)
|
|
721
|
+
continue;
|
|
722
|
+
tgt.latestSource = latestSource;
|
|
723
|
+
if (tgt.flags & 1 /* Rule */)
|
|
724
|
+
tgt.value.q.add(tgt);
|
|
725
|
+
if (tgt.subscribers)
|
|
726
|
+
dirtyStack.push(tgt);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
function returnValue() {
|
|
731
|
+
return this.value;
|
|
732
|
+
}
|
|
733
|
+
function throwValue() {
|
|
734
|
+
throw this.value;
|
|
735
|
+
}
|
|
736
|
+
const notCaughtUp = [];
|
|
737
|
+
var freesubs;
|
|
738
|
+
function mksub(source, target) {
|
|
739
|
+
let sub = freesubs;
|
|
740
|
+
if (sub) {
|
|
741
|
+
freesubs = sub.old;
|
|
742
|
+
sub.src = source;
|
|
743
|
+
sub.nS = void 0;
|
|
744
|
+
sub.pS = target.sources;
|
|
745
|
+
sub.tgt = target;
|
|
746
|
+
sub.nT = sub.pT = void 0;
|
|
747
|
+
sub.ts = source.lastChanged;
|
|
748
|
+
sub.old = source.adding;
|
|
749
|
+
} else
|
|
750
|
+
sub = {
|
|
751
|
+
src: source,
|
|
752
|
+
nS: void 0,
|
|
753
|
+
pS: target.sources,
|
|
754
|
+
tgt: target,
|
|
755
|
+
nT: void 0,
|
|
756
|
+
pT: void 0,
|
|
757
|
+
ts: source.lastChanged,
|
|
758
|
+
old: source.adding
|
|
759
|
+
};
|
|
760
|
+
if (target.sources)
|
|
761
|
+
target.sources.nS = sub;
|
|
762
|
+
target.sources = sub;
|
|
763
|
+
source.adding = sub;
|
|
764
|
+
if (target.subscribers || target.flags & 1 /* Rule */)
|
|
765
|
+
source.subscribe(sub);
|
|
766
|
+
}
|
|
767
|
+
function delsub(sub) {
|
|
768
|
+
sub.src.unsubscribe(sub);
|
|
769
|
+
if (sub.nS)
|
|
770
|
+
sub.nS.pS = sub.pS;
|
|
771
|
+
if (sub.pS)
|
|
772
|
+
sub.pS.nS = sub.nS;
|
|
773
|
+
sub.src = sub.tgt = sub.nS = sub.pS = sub.nT = sub.pT = void 0;
|
|
774
|
+
sub.old = freesubs;
|
|
775
|
+
freesubs = sub;
|
|
776
|
+
}
|
|
777
|
+
const sentinel = {};
|
|
778
|
+
class Cell {
|
|
779
|
+
constructor() {
|
|
780
|
+
this.value = void 0;
|
|
781
|
+
// the value, or, for a rule, the `{q, rm}` struct
|
|
782
|
+
this.validThrough = 0;
|
|
783
|
+
// timestamp of most recent validation or recalculation
|
|
784
|
+
this.lastChanged = 0;
|
|
785
|
+
// timestamp of last value change
|
|
786
|
+
this.latestSource = timestamp;
|
|
787
|
+
// max lastChanged of this cell or any ancestor source
|
|
788
|
+
this.flags = 0;
|
|
789
|
+
this.ctx = void 0;
|
|
790
|
+
/** The subscription being added during the current calculation - used for uniqueness */
|
|
791
|
+
this.adding = void 0;
|
|
792
|
+
/** Linked list of sources */
|
|
793
|
+
this.sources = void 0;
|
|
794
|
+
/** Linked list of targets */
|
|
795
|
+
this.subscribers = void 0;
|
|
796
|
+
/**
|
|
797
|
+
* The computation to be performed (if {@link Is.Calc} or {@link Is.Rule})
|
|
798
|
+
* or the setup/teardown for {@link Is.Stream}.
|
|
799
|
+
*/
|
|
800
|
+
this.compute = returnValue;
|
|
801
|
+
}
|
|
802
|
+
stream(sink, _conn, inlet) {
|
|
803
|
+
let lastValue = sentinel;
|
|
804
|
+
Cell.mkRule(() => {
|
|
805
|
+
const val = this.getValue();
|
|
806
|
+
if (val !== lastValue) {
|
|
807
|
+
const old = swapCtx(nullCtx);
|
|
808
|
+
try {
|
|
809
|
+
sink(lastValue = val);
|
|
810
|
+
} finally {
|
|
811
|
+
swapCtx(old);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
}, inlet ? ruleQueue(backpressure(inlet)) : defaultQ);
|
|
815
|
+
return IsStream;
|
|
816
|
+
}
|
|
817
|
+
getValue() {
|
|
818
|
+
if (arguments.length)
|
|
819
|
+
return this.stream.apply(this, arguments);
|
|
820
|
+
this.catchUp();
|
|
821
|
+
const dep = current.cell;
|
|
822
|
+
if (dep && this.flags & 133 /* Variable */) {
|
|
823
|
+
if (this.flags & 32 /* Running */)
|
|
824
|
+
throw new CircularDependency("Cached function dependency cycle");
|
|
825
|
+
let s = this.adding;
|
|
826
|
+
if (!s || s.tgt !== dep) {
|
|
827
|
+
mksub(this, dep);
|
|
828
|
+
} else {
|
|
829
|
+
if (s.ts === -1) {
|
|
830
|
+
s.ts = this.lastChanged;
|
|
831
|
+
if (s.nS) {
|
|
832
|
+
s.nS.pS = s.pS;
|
|
833
|
+
if (s.pS)
|
|
834
|
+
s.pS.nS = s.nS;
|
|
835
|
+
s.nS = void 0;
|
|
836
|
+
s.pS = dep.sources;
|
|
837
|
+
dep.sources.nS = s;
|
|
838
|
+
dep.sources = s;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
if (this.flags & 16 /* Error */)
|
|
844
|
+
throw this.value;
|
|
845
|
+
return this.value;
|
|
846
|
+
}
|
|
847
|
+
shouldWrite(changed) {
|
|
848
|
+
const cell = current.cell || currentRule;
|
|
849
|
+
if (cell) {
|
|
850
|
+
if (cell.flags & 4 /* Calc */)
|
|
851
|
+
throw new WriteConflict("Side-effects not allowed in cached functions");
|
|
852
|
+
if (this.adding && this.adding.tgt === cell)
|
|
853
|
+
throw new CircularDependency("Can't update direct dependency");
|
|
854
|
+
if (this.validThrough === timestamp || this.hasBeenRead())
|
|
855
|
+
throw new WriteConflict("Value already used");
|
|
856
|
+
} else if (changed) {
|
|
857
|
+
if (this.validThrough === timestamp || notCaughtUp.length) {
|
|
858
|
+
++timestamp;
|
|
859
|
+
notCaughtUp.length = 0;
|
|
860
|
+
}
|
|
861
|
+
} else {
|
|
862
|
+
return false;
|
|
863
|
+
}
|
|
864
|
+
this.lastChanged === timestamp || markDependentsDirty(this);
|
|
865
|
+
if (this.sources)
|
|
866
|
+
this.sources.ts = 0;
|
|
867
|
+
return true;
|
|
868
|
+
}
|
|
869
|
+
setValue(val, isErr) {
|
|
870
|
+
if (this.shouldWrite(val !== this.value || isErr !== !!(this.flags & 16 /* Error */))) {
|
|
871
|
+
this.value = val;
|
|
872
|
+
this.lastChanged = timestamp;
|
|
873
|
+
this.flags = isErr ? this.flags | 16 /* Error */ : this.flags & ~16 /* Error */;
|
|
874
|
+
}
|
|
875
|
+
this.compute = isErr ? throwValue : returnValue;
|
|
876
|
+
}
|
|
877
|
+
setCalc(compute) {
|
|
878
|
+
if (this.shouldWrite(compute !== this.compute)) {
|
|
879
|
+
this.flags |= 4 /* Calc */;
|
|
880
|
+
this.compute = compute;
|
|
881
|
+
this.ctx ||= makeCtx(null, this);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
hasBeenRead() {
|
|
885
|
+
if (notCaughtUp.length) {
|
|
886
|
+
for (const cell of notCaughtUp)
|
|
887
|
+
cell.catchUp();
|
|
888
|
+
notCaughtUp.length = 0;
|
|
889
|
+
}
|
|
890
|
+
return this.validThrough === timestamp;
|
|
891
|
+
}
|
|
892
|
+
catchUp() {
|
|
893
|
+
const { validThrough } = this;
|
|
894
|
+
if (validThrough === timestamp)
|
|
895
|
+
return;
|
|
896
|
+
this.validThrough = timestamp;
|
|
897
|
+
if (!(this.flags & 5 /* Computed */))
|
|
898
|
+
return;
|
|
899
|
+
if (this.sources) {
|
|
900
|
+
for (let sub = this.sources; sub; sub = sub.nS) {
|
|
901
|
+
const s = sub.src;
|
|
902
|
+
if (sub.ts !== s.lastChanged)
|
|
903
|
+
return this.doRecalc();
|
|
904
|
+
if (s.subscribers && s.latestSource <= validThrough)
|
|
905
|
+
continue;
|
|
906
|
+
s.catchUp();
|
|
907
|
+
if (s.lastChanged > validThrough) {
|
|
908
|
+
return this.doRecalc();
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
for (let sub = this.sources; sub; sub = sub.nS) {
|
|
912
|
+
if (sub.src.validThrough !== timestamp)
|
|
913
|
+
notCaughtUp.push(sub.src);
|
|
914
|
+
}
|
|
915
|
+
} else
|
|
916
|
+
return this.doRecalc();
|
|
917
|
+
}
|
|
918
|
+
doRecalc() {
|
|
919
|
+
const oldCtx = swapCtx(this.ctx);
|
|
920
|
+
for (let sub = this.sources; sub; sub = sub.nS) {
|
|
921
|
+
sub.ts = -1;
|
|
922
|
+
sub.old = sub.src.adding;
|
|
923
|
+
sub.src.adding = sub;
|
|
924
|
+
this.sources = sub;
|
|
925
|
+
}
|
|
926
|
+
this.flags |= 32 /* Running */;
|
|
927
|
+
try {
|
|
928
|
+
if (this.flags & 4 /* Calc */) {
|
|
929
|
+
try {
|
|
930
|
+
const future = this.compute();
|
|
931
|
+
if (future !== this.value || !this.lastChanged || this.flags & 16 /* Error */) {
|
|
932
|
+
this.value = future;
|
|
933
|
+
this.lastChanged = timestamp;
|
|
934
|
+
}
|
|
935
|
+
this.flags &= ~16 /* Error */;
|
|
936
|
+
} catch (e) {
|
|
937
|
+
this.flags |= 16 /* Error */;
|
|
938
|
+
this.value = e;
|
|
939
|
+
this.lastChanged = timestamp;
|
|
940
|
+
}
|
|
941
|
+
} else {
|
|
942
|
+
const { job } = this.ctx;
|
|
943
|
+
job.restart();
|
|
944
|
+
try {
|
|
945
|
+
job.must(this.compute());
|
|
946
|
+
this.lastChanged = timestamp;
|
|
947
|
+
} catch (e) {
|
|
948
|
+
this.disposeRule();
|
|
949
|
+
throw e;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
} finally {
|
|
953
|
+
this.flags &= ~32 /* Running */;
|
|
954
|
+
swapCtx(oldCtx);
|
|
955
|
+
let head;
|
|
956
|
+
for (let sub = this.sources; sub; ) {
|
|
957
|
+
const pS = sub.pS;
|
|
958
|
+
sub.src.adding = sub.old;
|
|
959
|
+
sub.old = void 0;
|
|
960
|
+
if (sub.ts === -1)
|
|
961
|
+
delsub(sub);
|
|
962
|
+
else
|
|
963
|
+
head = sub;
|
|
964
|
+
sub = pS;
|
|
965
|
+
}
|
|
966
|
+
this.sources = head;
|
|
967
|
+
if (!head) {
|
|
968
|
+
this.flags &= ~4 /* Calc */;
|
|
969
|
+
}
|
|
970
|
+
if (this.flags & 8 /* Dead */)
|
|
971
|
+
this.disposeRule();
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
disposeRule() {
|
|
975
|
+
this.ctx.job.end();
|
|
976
|
+
this.flags |= 8 /* Dead */;
|
|
977
|
+
this.value.q.delete(this);
|
|
978
|
+
this.value.rm();
|
|
979
|
+
if (current !== this.ctx) {
|
|
980
|
+
for (let s = this.sources; s; ) {
|
|
981
|
+
let nS = s.nS;
|
|
982
|
+
delsub(s);
|
|
983
|
+
s = nS;
|
|
984
|
+
}
|
|
985
|
+
this.sources = void 0;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
subscribe(sub) {
|
|
989
|
+
if (!this.subscribers) {
|
|
990
|
+
if (this.flags & 4 /* Calc */) {
|
|
991
|
+
for (let s = this.sources; s; s = s.nS)
|
|
992
|
+
s.src.subscribe(s);
|
|
993
|
+
}
|
|
994
|
+
if (this.flags & 64 /* Stream */)
|
|
995
|
+
this.compute(true);
|
|
996
|
+
}
|
|
997
|
+
if (this.subscribers !== sub && !sub.pT) {
|
|
998
|
+
sub.nT = this.subscribers;
|
|
999
|
+
if (this.subscribers)
|
|
1000
|
+
this.subscribers.pT = sub;
|
|
1001
|
+
this.subscribers = sub;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
unsubscribe(sub) {
|
|
1005
|
+
if (sub.nT)
|
|
1006
|
+
sub.nT.pT = sub.pT;
|
|
1007
|
+
if (sub.pT)
|
|
1008
|
+
sub.pT.nT = sub.nT;
|
|
1009
|
+
if (this.subscribers === sub)
|
|
1010
|
+
this.subscribers = sub.nT;
|
|
1011
|
+
if (!this.subscribers) {
|
|
1012
|
+
if (this.flags & 4 /* Calc */) {
|
|
1013
|
+
for (let s = this.sources; s; s = s.nS)
|
|
1014
|
+
s.src.unsubscribe(s);
|
|
1015
|
+
}
|
|
1016
|
+
if (this.flags & 64 /* Stream */)
|
|
1017
|
+
this.compute(false);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
static mkValue(val) {
|
|
1021
|
+
const cell = new Cell();
|
|
1022
|
+
cell.flags = 128 /* Mutable */;
|
|
1023
|
+
cell.value = val;
|
|
1024
|
+
cell.lastChanged = timestamp;
|
|
1025
|
+
return cell;
|
|
1026
|
+
}
|
|
1027
|
+
static mkStream(src, val) {
|
|
1028
|
+
const cell = this.mkValue(val);
|
|
1029
|
+
cell.flags |= 64 /* Stream */;
|
|
1030
|
+
cell.ctx = makeCtx();
|
|
1031
|
+
const write = (v) => {
|
|
1032
|
+
cell.setValue(v, false);
|
|
1033
|
+
cell.compute = compute;
|
|
1034
|
+
};
|
|
1035
|
+
let job;
|
|
1036
|
+
const compute = cell.compute = (sub) => {
|
|
1037
|
+
if (!sub) {
|
|
1038
|
+
cell.value = val;
|
|
1039
|
+
cell.flags &= ~16 /* Error */;
|
|
1040
|
+
job?.end();
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
if (job)
|
|
1044
|
+
return;
|
|
1045
|
+
job = cell.ctx.job = makeJob().do((r) => {
|
|
1046
|
+
if (isError(r)) {
|
|
1047
|
+
cell.setValue(markHandled(r), true);
|
|
1048
|
+
} else if (isValue(r)) {
|
|
1049
|
+
cell.setValue(val, false);
|
|
1050
|
+
}
|
|
1051
|
+
cell.compute = compute;
|
|
1052
|
+
cell.ctx.job = job = void 0;
|
|
1053
|
+
});
|
|
1054
|
+
const old = swapCtx(cell.ctx);
|
|
1055
|
+
try {
|
|
1056
|
+
src(write, job);
|
|
1057
|
+
} catch (e) {
|
|
1058
|
+
job.end();
|
|
1059
|
+
throw e;
|
|
1060
|
+
} finally {
|
|
1061
|
+
swapCtx(old);
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
return cell;
|
|
1065
|
+
}
|
|
1066
|
+
recalcWhen(fnOrKey, fn) {
|
|
1067
|
+
let trackers = fn ? obtrackers.get(fn) || setMap(obtrackers, fn, /* @__PURE__ */ new WeakMap()) : fntrackers;
|
|
1068
|
+
let signal = trackers.get(fnOrKey);
|
|
1069
|
+
if (!signal) {
|
|
1070
|
+
const src = fn ? fn(fnOrKey) : fnOrKey;
|
|
1071
|
+
let ct = 0;
|
|
1072
|
+
const c = Cell.mkStream((s) => (src(() => s(++ct)), IsStream), ct);
|
|
1073
|
+
trackers.set(fnOrKey, signal = c.getValue.bind(c));
|
|
1074
|
+
}
|
|
1075
|
+
signal();
|
|
1076
|
+
}
|
|
1077
|
+
static mkCached(compute) {
|
|
1078
|
+
const cell = new Cell();
|
|
1079
|
+
cell.compute = compute;
|
|
1080
|
+
cell.ctx = makeCtx(null, cell);
|
|
1081
|
+
cell.flags = 4 /* Calc */;
|
|
1082
|
+
return cell;
|
|
1083
|
+
}
|
|
1084
|
+
static mkRule(fn, q) {
|
|
1085
|
+
var cell = new Cell(), job = makeJob();
|
|
1086
|
+
const stop = cell.disposeRule.bind(cell);
|
|
1087
|
+
cell.value = { q, rm: getJob().release(stop) };
|
|
1088
|
+
cell.compute = fn.bind(null, stop);
|
|
1089
|
+
cell.ctx = makeCtx(job, cell);
|
|
1090
|
+
cell.flags = 1 /* Rule */;
|
|
1091
|
+
q.add(cell);
|
|
1092
|
+
return stop;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
class RF extends CallableObject {
|
|
1097
|
+
constructor(q) {
|
|
1098
|
+
super((fn) => Cell.mkRule(fn, q));
|
|
1099
|
+
}
|
|
1100
|
+
stop() {
|
|
1101
|
+
if (currentRule)
|
|
1102
|
+
return currentRule.disposeRule();
|
|
1103
|
+
throw new Error("No rule active");
|
|
1104
|
+
}
|
|
1105
|
+
if(condition, action) {
|
|
1106
|
+
const cond = Cell.mkCached(() => !!condition());
|
|
1107
|
+
return this(() => cond.getValue() ? action() : void 0);
|
|
1108
|
+
}
|
|
1109
|
+
get method() {
|
|
1110
|
+
const self = this;
|
|
1111
|
+
return (fn, _ctxOrName, desc) => {
|
|
1112
|
+
if (desc)
|
|
1113
|
+
return void (desc.value = this.method(desc.value));
|
|
1114
|
+
return function(...args) {
|
|
1115
|
+
return self(() => fn.apply(this, args));
|
|
1116
|
+
};
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
factory(scheduleFn) {
|
|
1120
|
+
return factories.get(scheduleFn) || setMap(factories, scheduleFn, new RF(ruleQueue(scheduleFn)));
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
const factories = /* @__PURE__ */ new WeakMap();
|
|
1124
|
+
const rule = RF.prototype.factory(defer);
|
|
1125
|
+
function runRules(scheduleFn) {
|
|
1126
|
+
(scheduleFn ? ruleQueue(scheduleFn) : defaultQ).flush();
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
class SignalImpl extends CallableObject {
|
|
1130
|
+
constructor(_c) {
|
|
1131
|
+
super(_c.getValue.bind(_c));
|
|
1132
|
+
this._c = _c;
|
|
1133
|
+
}
|
|
1134
|
+
get value() {
|
|
1135
|
+
return this();
|
|
1136
|
+
}
|
|
1137
|
+
valueOf() {
|
|
1138
|
+
return this();
|
|
1139
|
+
}
|
|
1140
|
+
toString() {
|
|
1141
|
+
return "" + this();
|
|
1142
|
+
}
|
|
1143
|
+
toJSON() {
|
|
1144
|
+
return this();
|
|
1145
|
+
}
|
|
1146
|
+
peek() {
|
|
1147
|
+
return peek(this);
|
|
1148
|
+
}
|
|
1149
|
+
asReadonly() {
|
|
1150
|
+
return this;
|
|
1151
|
+
}
|
|
1152
|
+
withSet(set) {
|
|
1153
|
+
return Object.assign(new WritableImpl(this._c), { set });
|
|
1154
|
+
}
|
|
1155
|
+
*"uneventful.next"() {
|
|
1156
|
+
return yield (r) => {
|
|
1157
|
+
let seen = false, res;
|
|
1158
|
+
rule((stop) => {
|
|
1159
|
+
try {
|
|
1160
|
+
res = this();
|
|
1161
|
+
} catch (e) {
|
|
1162
|
+
stop();
|
|
1163
|
+
reject(r, e);
|
|
1164
|
+
}
|
|
1165
|
+
if (seen) {
|
|
1166
|
+
stop();
|
|
1167
|
+
resolve(r, res);
|
|
1168
|
+
}
|
|
1169
|
+
seen = true;
|
|
1170
|
+
});
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
*"uneventful.until"() {
|
|
1174
|
+
return yield (r) => {
|
|
1175
|
+
let res;
|
|
1176
|
+
try {
|
|
1177
|
+
res = this();
|
|
1178
|
+
} catch (e) {
|
|
1179
|
+
reject(r, e);
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1182
|
+
if (res)
|
|
1183
|
+
return resolve(r, res);
|
|
1184
|
+
rule((stop) => {
|
|
1185
|
+
try {
|
|
1186
|
+
res = this();
|
|
1187
|
+
} catch (e) {
|
|
1188
|
+
stop();
|
|
1189
|
+
reject(r, e);
|
|
1190
|
+
}
|
|
1191
|
+
if (res) {
|
|
1192
|
+
stop();
|
|
1193
|
+
resolve(r, res);
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
class WritableImpl extends SignalImpl {
|
|
1200
|
+
constructor() {
|
|
1201
|
+
super(...arguments);
|
|
1202
|
+
this.set = (val) => {
|
|
1203
|
+
this._c.setValue(val, false);
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
get value() {
|
|
1207
|
+
return this();
|
|
1208
|
+
}
|
|
1209
|
+
set value(val) {
|
|
1210
|
+
this.set(val);
|
|
1211
|
+
}
|
|
1212
|
+
asReadonly() {
|
|
1213
|
+
return new SignalImpl(this._c);
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
class ConfigurableImpl extends WritableImpl {
|
|
1217
|
+
setf(expr) {
|
|
1218
|
+
this._c.setCalc(expr);
|
|
1219
|
+
return this;
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
function value(val) {
|
|
1223
|
+
const cell = Cell.mkValue(val);
|
|
1224
|
+
return new ConfigurableImpl(cell);
|
|
1225
|
+
}
|
|
1226
|
+
function cached(compute, initVal) {
|
|
1227
|
+
if (compute instanceof SignalImpl)
|
|
1228
|
+
return compute;
|
|
1229
|
+
return new SignalImpl(
|
|
1230
|
+
compute.length ? Cell.mkStream(compute, initVal) : Cell.mkCached(compute)
|
|
1231
|
+
);
|
|
1232
|
+
}
|
|
1233
|
+
function peek(fn, ...args) {
|
|
1234
|
+
if (!current.cell)
|
|
1235
|
+
return fn(...args);
|
|
1236
|
+
const old = swapCtx(makeCtx(current.job));
|
|
1237
|
+
try {
|
|
1238
|
+
return fn.apply(null, args);
|
|
1239
|
+
} finally {
|
|
1240
|
+
freeCtx(swapCtx(old));
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
function recalcWhen(fnOrKey, fn) {
|
|
1244
|
+
current.cell?.recalcWhen(fnOrKey, fn);
|
|
1245
|
+
}
|
|
1246
|
+
function action(fn, _ctx, desc) {
|
|
1247
|
+
if (desc)
|
|
1248
|
+
return { ...desc, value: action(desc.value) };
|
|
1249
|
+
return function() {
|
|
1250
|
+
if (!current.cell)
|
|
1251
|
+
return fn.apply(this, arguments);
|
|
1252
|
+
const old = swapCtx(makeCtx(current.job));
|
|
1253
|
+
try {
|
|
1254
|
+
return fn.apply(this, arguments);
|
|
1255
|
+
} finally {
|
|
1256
|
+
freeCtx(swapCtx(old));
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
function must(cleanup) {
|
|
1262
|
+
return getJob().must(cleanup);
|
|
1263
|
+
}
|
|
1264
|
+
function start(init, fn) {
|
|
1265
|
+
return getJob().start(init, fn);
|
|
1266
|
+
}
|
|
1267
|
+
function isJobActive() {
|
|
1268
|
+
return !!current.job;
|
|
1269
|
+
}
|
|
1270
|
+
const timers = /* @__PURE__ */ new WeakMap();
|
|
1271
|
+
function timeout(ms = 0, job = getJob()) {
|
|
1272
|
+
let timer = timers.get(job);
|
|
1273
|
+
if (timer) {
|
|
1274
|
+
clearTimeout(timer);
|
|
1275
|
+
} else if (timer === void 0 && !job.result()) {
|
|
1276
|
+
job.must(timeout.bind(null, 0, job));
|
|
1277
|
+
}
|
|
1278
|
+
if (job.result()) {
|
|
1279
|
+
timers.delete(job);
|
|
1280
|
+
} else if (ms) {
|
|
1281
|
+
timers.set(job, setTimeout(() => {
|
|
1282
|
+
timers.set(job, null);
|
|
1283
|
+
job.end();
|
|
1284
|
+
}, ms));
|
|
1285
|
+
} else {
|
|
1286
|
+
timers.set(job, null);
|
|
1287
|
+
}
|
|
1288
|
+
return job;
|
|
1289
|
+
}
|
|
1290
|
+
const abortSignals = /* @__PURE__ */ new WeakMap();
|
|
1291
|
+
function abortSignal(job = getJob()) {
|
|
1292
|
+
let signal = abortSignals.get(job);
|
|
1293
|
+
if (!signal) {
|
|
1294
|
+
const ctrl = new AbortController();
|
|
1295
|
+
signal = ctrl.signal;
|
|
1296
|
+
job.do(() => {
|
|
1297
|
+
abortSignals.set(job, null);
|
|
1298
|
+
ctrl.abort();
|
|
1299
|
+
});
|
|
1300
|
+
abortSignals.set(job, signal);
|
|
1301
|
+
if (job.result())
|
|
1302
|
+
ctrl.abort();
|
|
1303
|
+
}
|
|
1304
|
+
return signal;
|
|
1305
|
+
}
|
|
1306
|
+
function restarting(task2) {
|
|
1307
|
+
const outer = getJob(), inner = makeJob(outer), { end } = inner;
|
|
1308
|
+
task2 ||= (f) => {
|
|
1309
|
+
inner.must(f());
|
|
1310
|
+
};
|
|
1311
|
+
inner.asyncCatch((e) => outer.asyncThrow(e));
|
|
1312
|
+
return function() {
|
|
1313
|
+
inner.restart().must(outer.release(end));
|
|
1314
|
+
const old = swapCtx(makeCtx(inner));
|
|
1315
|
+
try {
|
|
1316
|
+
return task2.apply(this, arguments);
|
|
1317
|
+
} catch (e) {
|
|
1318
|
+
inner.restart();
|
|
1319
|
+
throw e;
|
|
1320
|
+
} finally {
|
|
1321
|
+
freeCtx(swapCtx(old));
|
|
1322
|
+
}
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
function task(fn, _ctx, desc) {
|
|
1326
|
+
if (desc)
|
|
1327
|
+
return { ...desc, value: task(desc.value) };
|
|
1328
|
+
return function(...args) {
|
|
1329
|
+
return start(() => fn.apply(this, args));
|
|
1330
|
+
};
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
function emitter() {
|
|
1334
|
+
const emit = mockSource();
|
|
1335
|
+
emit.source = share(emit.source);
|
|
1336
|
+
return emit;
|
|
1337
|
+
}
|
|
1338
|
+
function empty() {
|
|
1339
|
+
return (_, conn) => (conn?.return(), IsStream);
|
|
1340
|
+
}
|
|
1341
|
+
function fromAsyncIterable(iterable) {
|
|
1342
|
+
return (sink, conn = start(), inlet) => {
|
|
1343
|
+
const ready = backpressure(inlet);
|
|
1344
|
+
const iter = iterable[Symbol.asyncIterator]();
|
|
1345
|
+
if (iter.return)
|
|
1346
|
+
must(() => iter.return());
|
|
1347
|
+
return ready(next), IsStream;
|
|
1348
|
+
function next() {
|
|
1349
|
+
iter.next().then(({ value, done }) => {
|
|
1350
|
+
if (done)
|
|
1351
|
+
conn.return();
|
|
1352
|
+
else
|
|
1353
|
+
ready(() => {
|
|
1354
|
+
if (sink(value), ready())
|
|
1355
|
+
next();
|
|
1356
|
+
else
|
|
1357
|
+
ready(next);
|
|
1358
|
+
});
|
|
1359
|
+
}, (e) => conn.throw(e));
|
|
1360
|
+
}
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
function fromDomEvent(target, type, options) {
|
|
1364
|
+
return (sink) => {
|
|
1365
|
+
function push(v) {
|
|
1366
|
+
sink(v);
|
|
1367
|
+
}
|
|
1368
|
+
target.addEventListener(type, push, options);
|
|
1369
|
+
must(() => target.removeEventListener(type, push, options));
|
|
1370
|
+
return IsStream;
|
|
1371
|
+
};
|
|
1372
|
+
}
|
|
1373
|
+
function fromIterable(iterable) {
|
|
1374
|
+
return (sink, conn = start(), inlet) => {
|
|
1375
|
+
const ready = backpressure(inlet);
|
|
1376
|
+
const iter = iterable[Symbol.iterator]();
|
|
1377
|
+
if (iter.return)
|
|
1378
|
+
must(() => iter.return());
|
|
1379
|
+
return ready(loop), IsStream;
|
|
1380
|
+
function loop() {
|
|
1381
|
+
try {
|
|
1382
|
+
for (; ; ) {
|
|
1383
|
+
const { value, done } = iter.next();
|
|
1384
|
+
if (done)
|
|
1385
|
+
return conn.return();
|
|
1386
|
+
if (sink(value), !ready())
|
|
1387
|
+
return ready(loop);
|
|
1388
|
+
}
|
|
1389
|
+
} catch (e) {
|
|
1390
|
+
conn.throw(e);
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1395
|
+
function fromPromise(promise) {
|
|
1396
|
+
return (sink, conn) => {
|
|
1397
|
+
const job = getJob();
|
|
1398
|
+
Promise.resolve(promise).then(
|
|
1399
|
+
(v) => void (job.result() || (sink(v), conn?.return())),
|
|
1400
|
+
(e) => void (job.result() || conn?.throw(e))
|
|
1401
|
+
);
|
|
1402
|
+
return IsStream;
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1405
|
+
function fromSubscribe(subscribe) {
|
|
1406
|
+
return (sink) => {
|
|
1407
|
+
const f = getJob().must(() => sink = noop);
|
|
1408
|
+
return defer(() => f.must(subscribe((v) => {
|
|
1409
|
+
sink(v);
|
|
1410
|
+
}))), IsStream;
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
function fromValue(val) {
|
|
1414
|
+
return (sink, conn) => {
|
|
1415
|
+
must(() => {
|
|
1416
|
+
sink = noop;
|
|
1417
|
+
conn = void 0;
|
|
1418
|
+
});
|
|
1419
|
+
return defer(() => {
|
|
1420
|
+
sink(val);
|
|
1421
|
+
conn?.return();
|
|
1422
|
+
}), IsStream;
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
function interval(ms) {
|
|
1426
|
+
return (sink) => {
|
|
1427
|
+
let idx = 0, id = setInterval(() => sink(idx++), ms);
|
|
1428
|
+
return must(() => clearInterval(id)), IsStream;
|
|
1429
|
+
};
|
|
1430
|
+
}
|
|
1431
|
+
function lazy(factory) {
|
|
1432
|
+
return (sink, conn, inlet) => factory()(sink, conn, inlet);
|
|
1433
|
+
}
|
|
1434
|
+
function mockSource() {
|
|
1435
|
+
let write, outlet, ready;
|
|
1436
|
+
const emit = (val) => {
|
|
1437
|
+
if (write)
|
|
1438
|
+
write(val);
|
|
1439
|
+
};
|
|
1440
|
+
emit.source = (sink, conn, inlet) => {
|
|
1441
|
+
write = sink;
|
|
1442
|
+
outlet = conn;
|
|
1443
|
+
ready = backpressure(inlet);
|
|
1444
|
+
must(() => write = outlet = ready = void 0);
|
|
1445
|
+
return IsStream;
|
|
1446
|
+
};
|
|
1447
|
+
emit.end = () => outlet?.return();
|
|
1448
|
+
emit.throw = (e) => outlet?.throw(e);
|
|
1449
|
+
emit.ready = (cb) => ready(cb);
|
|
1450
|
+
return emit;
|
|
1451
|
+
}
|
|
1452
|
+
function never() {
|
|
1453
|
+
return () => IsStream;
|
|
1454
|
+
}
|
|
1455
|
+
function share(source) {
|
|
1456
|
+
let uplink;
|
|
1457
|
+
const links = /* @__PURE__ */ new Set(), inlets = /* @__PURE__ */ new Map(), t = throttle(), multi = {
|
|
1458
|
+
// A multi-connection inlet that requires all downstreams to be ready
|
|
1459
|
+
isOpen() {
|
|
1460
|
+
return !uplink?.result();
|
|
1461
|
+
},
|
|
1462
|
+
isReady() {
|
|
1463
|
+
if (this.isOpen()) {
|
|
1464
|
+
for (const [i] of inlets)
|
|
1465
|
+
if (!i.isReady())
|
|
1466
|
+
return t.pause(), false;
|
|
1467
|
+
return true;
|
|
1468
|
+
}
|
|
1469
|
+
t.pause();
|
|
1470
|
+
return false;
|
|
1471
|
+
},
|
|
1472
|
+
onReady(cb, job) {
|
|
1473
|
+
if (this.isOpen()) {
|
|
1474
|
+
t.onReady(cb, job);
|
|
1475
|
+
for (const [i] of inlets)
|
|
1476
|
+
i.isReady() || i.onReady(produce, job);
|
|
1477
|
+
}
|
|
1478
|
+
return this;
|
|
1479
|
+
}
|
|
1480
|
+
};
|
|
1481
|
+
function produce() {
|
|
1482
|
+
multi.isReady() && t.resume();
|
|
1483
|
+
}
|
|
1484
|
+
return (sink, conn = start(), inlet) => {
|
|
1485
|
+
const self = [sink, conn];
|
|
1486
|
+
links.add(self);
|
|
1487
|
+
if (inlet)
|
|
1488
|
+
inlets.set(inlet, 1 + (inlets.get(inlet) || 0));
|
|
1489
|
+
conn.must(() => {
|
|
1490
|
+
links.delete(self);
|
|
1491
|
+
if (inlet) {
|
|
1492
|
+
inlets.set(inlet, inlets.get(inlet) - 1);
|
|
1493
|
+
if (!inlets.get(inlet))
|
|
1494
|
+
inlets.delete(inlet);
|
|
1495
|
+
}
|
|
1496
|
+
if (!links.size)
|
|
1497
|
+
uplink?.end();
|
|
1498
|
+
else if (multi.isReady() && !t.isReady())
|
|
1499
|
+
defer(produce);
|
|
1500
|
+
});
|
|
1501
|
+
if (links.size === 1) {
|
|
1502
|
+
uplink = detached.connect(source, (v) => {
|
|
1503
|
+
for (const [s, c] of links)
|
|
1504
|
+
try {
|
|
1505
|
+
s(v);
|
|
1506
|
+
} catch (e) {
|
|
1507
|
+
c.throw(e);
|
|
1508
|
+
}
|
|
1509
|
+
}, multi).do((r) => {
|
|
1510
|
+
uplink = void 0;
|
|
1511
|
+
if (isCancel(r))
|
|
1512
|
+
return;
|
|
1513
|
+
if (isUnhandled(r))
|
|
1514
|
+
markHandled(r);
|
|
1515
|
+
for (const [_, c] of links)
|
|
1516
|
+
isError(r) ? c.throw(r.err) : c.return();
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
return IsStream;
|
|
1520
|
+
};
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
function* each(src) {
|
|
1524
|
+
let yielded = false, waiter;
|
|
1525
|
+
const result = { value: { item: void 0, next: next2 }, done: false };
|
|
1526
|
+
const t = throttle(), conn = getJob().connect(src, (v) => {
|
|
1527
|
+
t.pause();
|
|
1528
|
+
if (!waiter || conn.result())
|
|
1529
|
+
return;
|
|
1530
|
+
result.value.item = v;
|
|
1531
|
+
resolve(waiter, waiter = void 0);
|
|
1532
|
+
}, t).do((r) => {
|
|
1533
|
+
if (isError(r))
|
|
1534
|
+
markHandled(r);
|
|
1535
|
+
if (waiter) {
|
|
1536
|
+
defer(next2.bind(null, waiter));
|
|
1537
|
+
waiter = void 0;
|
|
1538
|
+
}
|
|
1539
|
+
});
|
|
1540
|
+
t.pause();
|
|
1541
|
+
yield next2;
|
|
1542
|
+
return {
|
|
1543
|
+
[Symbol.iterator]() {
|
|
1544
|
+
return this;
|
|
1545
|
+
},
|
|
1546
|
+
next() {
|
|
1547
|
+
if (!yielded)
|
|
1548
|
+
throw new Error("Must `yield next` in loop");
|
|
1549
|
+
yielded = false;
|
|
1550
|
+
return result;
|
|
1551
|
+
},
|
|
1552
|
+
return() {
|
|
1553
|
+
conn.end();
|
|
1554
|
+
return { value: void 0, done: true };
|
|
1555
|
+
}
|
|
1556
|
+
};
|
|
1557
|
+
function next2(r) {
|
|
1558
|
+
if (waiter)
|
|
1559
|
+
throw new Error("Multiple `yield next` in loop");
|
|
1560
|
+
yielded = true;
|
|
1561
|
+
if (conn.result()) {
|
|
1562
|
+
result.value = void 0;
|
|
1563
|
+
result.done = true;
|
|
1564
|
+
fulfillPromise(resolver(r), rejecter(r), conn.result());
|
|
1565
|
+
} else {
|
|
1566
|
+
waiter = r;
|
|
1567
|
+
t.resume();
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
function until(source) {
|
|
1572
|
+
return callOrWait(source, "uneventful.until", waitTruthy, recache);
|
|
1573
|
+
}
|
|
1574
|
+
function recache(s) {
|
|
1575
|
+
return until(cached(s));
|
|
1576
|
+
}
|
|
1577
|
+
function waitTruthy(job, v) {
|
|
1578
|
+
v && job.return(v);
|
|
1579
|
+
}
|
|
1580
|
+
function next(source) {
|
|
1581
|
+
return callOrWait(source, "uneventful.next", waitAny, mustBeSourceOrSignal);
|
|
1582
|
+
}
|
|
1583
|
+
function waitAny(job, v) {
|
|
1584
|
+
job.return(v);
|
|
1585
|
+
}
|
|
1586
|
+
function callOrWait(source, method, handler, noArgs) {
|
|
1587
|
+
if (source && isFunction(source[method]))
|
|
1588
|
+
return source[method]();
|
|
1589
|
+
if (isFunction(source))
|
|
1590
|
+
return (source.length === 0 ? noArgs(source) : false) || start((job) => {
|
|
1591
|
+
connect(source, (v) => handler(job, v)).do((r) => {
|
|
1592
|
+
if (isValue(r))
|
|
1593
|
+
job.throw(new Error("Stream ended"));
|
|
1594
|
+
else if (isError(r))
|
|
1595
|
+
job.throw(markHandled(r));
|
|
1596
|
+
});
|
|
1597
|
+
});
|
|
1598
|
+
mustBeSourceOrSignal();
|
|
1599
|
+
}
|
|
1600
|
+
function mustBeSourceOrSignal() {
|
|
1601
|
+
throw new TypeError("not a source or signal");
|
|
1602
|
+
}
|
|
1603
|
+
function forEach(src, sink, inlet) {
|
|
1604
|
+
if (isFunction(sink))
|
|
1605
|
+
return start((j) => {
|
|
1606
|
+
src(restarting(sink), j, inlet);
|
|
1607
|
+
});
|
|
1608
|
+
inlet = sink;
|
|
1609
|
+
sink = src;
|
|
1610
|
+
return (src2) => forEach(src2, sink, inlet);
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
function concat(sources) {
|
|
1614
|
+
return concatAll(fromIterable(sources));
|
|
1615
|
+
}
|
|
1616
|
+
function concatAll(sources) {
|
|
1617
|
+
return (sink, conn = start(), inlet) => {
|
|
1618
|
+
let inner;
|
|
1619
|
+
const inputs = [], t = throttle();
|
|
1620
|
+
let outer = conn.connect(sources, (s) => {
|
|
1621
|
+
inputs.push(s);
|
|
1622
|
+
startNext();
|
|
1623
|
+
t.pause();
|
|
1624
|
+
}, t).do((r) => {
|
|
1625
|
+
outer = void 0;
|
|
1626
|
+
inputs.length || inner || !isValue(r) || conn.return();
|
|
1627
|
+
});
|
|
1628
|
+
function startNext() {
|
|
1629
|
+
inner ||= conn.connect(inputs.shift(), sink, inlet).do((r) => {
|
|
1630
|
+
inner = void 0;
|
|
1631
|
+
inputs.length ? startNext() : outer ? t.resume() : !isValue(r) || conn.return();
|
|
1632
|
+
});
|
|
1633
|
+
}
|
|
1634
|
+
return IsStream;
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
function concatMap(mapper) {
|
|
1638
|
+
return (src) => concatAll(map(mapper)(src));
|
|
1639
|
+
}
|
|
1640
|
+
function filter(filter2) {
|
|
1641
|
+
return (src) => (sink, conn, inlet) => {
|
|
1642
|
+
let idx = 0;
|
|
1643
|
+
return src((v) => filter2(v, idx++) && sink(v), conn, inlet);
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
function map(mapper) {
|
|
1647
|
+
return (src) => (sink, conn, inlet) => {
|
|
1648
|
+
let idx = 0;
|
|
1649
|
+
return src((v) => sink(mapper(v, idx++)), conn, inlet);
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1652
|
+
function merge(sources) {
|
|
1653
|
+
return mergeAll(fromIterable(sources));
|
|
1654
|
+
}
|
|
1655
|
+
function mergeAll(sources) {
|
|
1656
|
+
return (sink, conn = start(), inlet) => {
|
|
1657
|
+
const uplinks = /* @__PURE__ */ new Set();
|
|
1658
|
+
let outer = conn.connect(sources, (s) => {
|
|
1659
|
+
const c = conn.connect(s, sink, inlet).do((r) => {
|
|
1660
|
+
uplinks.delete(c);
|
|
1661
|
+
uplinks.size || outer || !isValue(r) || conn.return();
|
|
1662
|
+
});
|
|
1663
|
+
uplinks.add(c);
|
|
1664
|
+
}).do((r) => {
|
|
1665
|
+
outer = void 0;
|
|
1666
|
+
uplinks.size || !isValue(r) || conn.return();
|
|
1667
|
+
});
|
|
1668
|
+
return IsStream;
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
function mergeMap(mapper) {
|
|
1672
|
+
return (src) => mergeAll(map(mapper)(src));
|
|
1673
|
+
}
|
|
1674
|
+
function skip(n) {
|
|
1675
|
+
return skipWhile((_, i) => i < n);
|
|
1676
|
+
}
|
|
1677
|
+
function skipUntil(notifier) {
|
|
1678
|
+
return (src) => (sink, conn = start(), inlet) => {
|
|
1679
|
+
let taking = false;
|
|
1680
|
+
const c = conn.connect(notifier, () => {
|
|
1681
|
+
taking = true;
|
|
1682
|
+
c.end();
|
|
1683
|
+
});
|
|
1684
|
+
return src((v) => taking && sink(v), conn, inlet);
|
|
1685
|
+
};
|
|
1686
|
+
}
|
|
1687
|
+
function skipWhile(condition) {
|
|
1688
|
+
return (src) => (sink, conn, inlet) => {
|
|
1689
|
+
let idx = 0, met = false;
|
|
1690
|
+
return src((v) => (met ||= !condition(v, idx++)) && sink(v), conn, inlet);
|
|
1691
|
+
};
|
|
1692
|
+
}
|
|
1693
|
+
function slack(size, dropped = noop) {
|
|
1694
|
+
const max = Math.abs(size);
|
|
1695
|
+
return (src) => (sink, conn = start(), inlet) => {
|
|
1696
|
+
const buffer = [], ready = backpressure(inlet);
|
|
1697
|
+
let paused = false, draining = false;
|
|
1698
|
+
const t = throttle();
|
|
1699
|
+
conn.connect(src, (v) => {
|
|
1700
|
+
buffer.push(v);
|
|
1701
|
+
if (!draining && ready())
|
|
1702
|
+
return drain();
|
|
1703
|
+
while (buffer.length > max) {
|
|
1704
|
+
dropped(size < 0 ? buffer.pop() : buffer.shift());
|
|
1705
|
+
}
|
|
1706
|
+
if (buffer.length === max) {
|
|
1707
|
+
t.pause();
|
|
1708
|
+
paused = true;
|
|
1709
|
+
}
|
|
1710
|
+
if (buffer.length)
|
|
1711
|
+
ready(drain);
|
|
1712
|
+
}, t).do((r) => {
|
|
1713
|
+
if (isValue(r))
|
|
1714
|
+
conn.return();
|
|
1715
|
+
});
|
|
1716
|
+
function drain() {
|
|
1717
|
+
draining = true;
|
|
1718
|
+
try {
|
|
1719
|
+
while (buffer.length) {
|
|
1720
|
+
sink(buffer.shift());
|
|
1721
|
+
if (paused && ready()) {
|
|
1722
|
+
paused = false;
|
|
1723
|
+
t.resume();
|
|
1724
|
+
}
|
|
1725
|
+
if (buffer.length && !ready()) {
|
|
1726
|
+
return ready(drain);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
} finally {
|
|
1730
|
+
draining = false;
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
return IsStream;
|
|
1734
|
+
};
|
|
1735
|
+
}
|
|
1736
|
+
function switchAll(sources) {
|
|
1737
|
+
return (sink, conn = start(), inlet) => {
|
|
1738
|
+
let inner;
|
|
1739
|
+
let outer = conn.connect(sources, (s) => {
|
|
1740
|
+
inner?.end();
|
|
1741
|
+
inner = conn.connect(s, sink, inlet).do((r) => {
|
|
1742
|
+
inner = void 0;
|
|
1743
|
+
outer || !isValue(r) || conn.return();
|
|
1744
|
+
});
|
|
1745
|
+
}).do((r) => {
|
|
1746
|
+
outer = void 0;
|
|
1747
|
+
inner || !isValue(r) || conn.return();
|
|
1748
|
+
});
|
|
1749
|
+
return IsStream;
|
|
1750
|
+
};
|
|
1751
|
+
}
|
|
1752
|
+
function switchMap(mapper) {
|
|
1753
|
+
return (src) => switchAll(map(mapper)(src));
|
|
1754
|
+
}
|
|
1755
|
+
function take(n) {
|
|
1756
|
+
return takeWhile((_, i) => i < n);
|
|
1757
|
+
}
|
|
1758
|
+
function takeUntil(notifier) {
|
|
1759
|
+
return (src) => (sink, conn = start(), inlet) => {
|
|
1760
|
+
conn.connect(notifier, () => conn.return());
|
|
1761
|
+
return src(sink, conn, inlet);
|
|
1762
|
+
};
|
|
1763
|
+
}
|
|
1764
|
+
function takeWhile(condition) {
|
|
1765
|
+
return (src) => (sink, conn, inlet) => {
|
|
1766
|
+
let idx = 0;
|
|
1767
|
+
return src((v) => condition(v, idx++) ? sink(v) : conn?.return(), conn, inlet);
|
|
1768
|
+
};
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
export { CancelError, CancelResult, CircularDependency, ConfigurableImpl, ErrorResult, IsStream, SignalImpl, ValueResult, WritableImpl, WriteConflict, abortSignal, action, 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, next, noop, peek, pipe, propagateResult, recalcWhen, reject, rejecter, resolve, resolver, restarting, rule, runRules, share, skip, skipUntil, skipWhile, slack, sleep, start, switchAll, switchMap, take, takeUntil, takeWhile, task, throttle, timeout, to, until, value };
|
|
2
1772
|
//# sourceMappingURL=mod.mjs.map
|