UNPKG

rvx

Version:

A signal based rendering library

2 lines (1 loc) 3.9 kB
import{teardown as e,Context as t,$ as n,isolate as r,nest as s,ENV as i,watch as o,capture as c,captureSelf as u}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(f.current)}}class h extends Error{errors;constructor(e){super(),this.errors=e}}const f=new t;function g(e,t,i,o){const c=n({type:0,value:void 0});let u;u="function"==typeof e?r(e):e;const a=f.current;return u.then(e=>{c.value={type:1,value:e}},e=>{c.value={type:2,value:e},void 0===a&&void 0===o&&Promise.reject(e)}),a?.track(u),s(c,e=>{switch(e.type){case 0:return i?.();case 1:return t?.(e.value);case 2:return o?.(e.value)}})}function p(e){return g(e.source,e.children,e.pending,e.rejected)}class v{#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 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}#f(){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.#f())})}waitFor(e){"function"==typeof e?(this.#h(),(async()=>{try{return await r(e)}catch(e){Promise.reject(e)}finally{this.#f()}})()):e instanceof Promise&&(this.#h(),e.finally(()=>this.#f()))}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(t){let n,r=!0;e(()=>{r=!1,n?.()}),queueMicrotask(()=>{r&&(n=c(t),r||n?.())})}function y(t,n){let r,s,i=!0;e(()=>{i=!1,clearTimeout(s),r?.()}),s=setTimeout(()=>{r=c(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=c(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=c(()=>t(i)),s||(n(),n=void 0)})}class F extends Error{}function C(t,n,r){return"number"==typeof n?(r=n,n=Boolean):void 0===n&&(n=Boolean),new Promise((s,i)=>{u(c=>{if(o(t,e=>{n(e)&&(c(),s(e))}),void 0!==r){const t=setTimeout(()=>{c(),i(new F)},r);e(()=>clearTimeout(t))}})})}export{f as ASYNC,p as Async,d as AsyncContext,h as AsyncError,v as Queue,k as TASKS,m as Tasks,F as WatchForTimeoutError,w as isPending,b as isSelfPending,g as nestAsync,a as useAbortController,l as useAbortSignal,q as useAnimation,j as useInterval,P as useMicrotask,y as useTimeout,C as watchFor};