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