rvx
Version:
A signal based rendering library
2 lines (1 loc) • 4.07 kB
JavaScript
import{teardown as e,Context as t,$ as n,nest as r,ENV as s,watch as i,capture as o,captureSelf as c}from"./rvx.min.js";function u(t){const n=new AbortController;return e((()=>n.abort(t))),n}function a(e){return u(e).signal}class l{#e;#t=n(new Set);#n=new Set;constructor(e){this.#e=e}get pending(){return this.#t.value.size>0}track(e){this.#e?.track(e),this.#t.update((t=>{t.add(e)})),e.then((()=>{this.#t.update((t=>{t.delete(e)}))}),(t=>{if(this.#n.size>0)for(const e of this.#n)e.push(t);else Promise.reject(t);this.#t.update((t=>{t.delete(e)}))}))}async complete(){const e=[];for(this.#n.add(e);this.#t.value.size>0;)await Promise.allSettled(this.#t.value);if(this.#n.delete(e),1===e.length)throw e[0];if(e.length>1)throw new d(e)}static fork(){return new l(h.current)}}class d extends Error{errors;constructor(e){super(),this.errors=e}}const h=new t;function g(e,t,s,i){const o=n({type:0,value:void 0});let c;c="function"==typeof e?(async()=>e())():e;const u=h.current;return c.then((e=>{o.value={type:1,value:e}}),(e=>{o.value={type:2,value:e},void 0===u&&void 0===i&&Promise.reject(e)})),u?.track(c),r(o,(e=>{switch(e.type){case 0:return s?.();case 1:return t?.(e.value);case 2:return i?.(e.value)}}))}function f(e){return g(e.source,e.children,e.pending,e.rejected)}class p{#r=[];#s=-1;#i=void 0;#o=void 0;constructor(){e((()=>this.#c()))}#c(){const e=this.#r;for(;e.length>0&&!e[0].blocking;)e.shift(),this.#s--;this.#i?.abort()}#u(){void 0===this.#o&&(this.#o=(async()=>{let e;for(;e=this.#r.shift();)if(this.#s--,e.blocking)try{e.resolve(await e.task())}catch(t){e.reject(t)}else{const t=new AbortController;this.#i=t;try{await e.task(t.signal)}catch(e){Promise.reject(e)}this.#i=void 0}this.#s--,this.#o=void 0})())}sideEffect(e){this.#s>=0||(this.#c(),this.#r.push({blocking:!1,task:e,resolve:void 0,reject:void 0}),this.#u())}block(e){return new Promise(((t,n)=>{this.#c(),this.#s=this.#r.push({blocking:!0,task:e,resolve:t,reject:n}),this.#u()}))}}class v{#a=0;#l=n(!1);#d;#e;constructor(e,t){if(this.#e=e,this.#d=t?.restoreFocus??(!e||e.#d),this.#d){const e=s.current;let t=null;i(this.#l,(n=>{if(n)t=e.document.activeElement;else if(t&&e.document.activeElement===e.document.body){const n=t;queueMicrotask((()=>{t===n&&e.document.activeElement===e.document.body&&n.focus?.()}))}}))}}#h(){this.#a++,this.#l.value=!0}#g(){this.#a--,this.#l.value=this.#a>0}get parent(){return this.#e}get selfPending(){return this.#l.value}get pending(){return(this.#e?.pending??!1)||this.#l.value}setPending(){this.#h();let t=!1;e((()=>{t||(t=!0,this.#g())}))}waitFor(e){"function"==typeof e?(this.#h(),(async()=>{try{return await e()}catch(e){Promise.reject(e)}finally{this.#g()}})()):e instanceof Promise&&(this.#h(),e.then((()=>this.#g()),(()=>this.#g())))}static fork(e){return new v(m.current,e)}}const m=new t;function k(){return m.current?.selfPending??!1}function b(){return m.current?.pending??!1}function w(){m.current?.setPending()}function P(e){m.current?.waitFor(e)}function y(t){let n,r=!0;e((()=>{r=!1,n?.()})),queueMicrotask((()=>{r&&(n=o(t),r||n?.())}))}function F(t,n){let r,s,i=!0;e((()=>{i=!1,clearTimeout(s),r?.()})),s=setTimeout((()=>{r=o(t),i||r()}),n)}function j(t,n){let r,s,i=!0;e((()=>{i=!1,clearInterval(s),r?.()})),s=setInterval((()=>{r?.(),r=void 0,r=o(t),i||r()}),n)}function q(t){let n,r,s=!0;e((()=>{s=!1,cancelAnimationFrame(r),n?.()})),r=requestAnimationFrame((function e(i){r=requestAnimationFrame(e),n?.(),n=void 0,n=o((()=>t(i))),s||(n(),n=void 0)}))}class C extends Error{}function E(t,n,r){return"number"==typeof n?(r=n,n=Boolean):void 0===n&&(n=Boolean),new Promise(((s,o)=>{c((c=>{if(i(t,(e=>{n(e)&&(c(),s(e))})),void 0!==r){const t=setTimeout((()=>{c(),o(new C)}),r);e((()=>clearTimeout(t)))}}))}))}export{h as ASYNC,f as Async,l as AsyncContext,d as AsyncError,p as Queue,m as TASKS,v as Tasks,C as WatchForTimeoutError,b as isPending,k as isSelfPending,g as nestAsync,w as setPending,u as useAbortController,a as useAbortSignal,q as useAnimation,j as useInterval,y as useMicrotask,F as useTimeout,P as waitFor,E as watchFor};