UNPKG

@vegajs/vortex

Version:

🌀 A next-gen, lightweight state management library for JavaScript and TypeScript.

1 lines • 6.08 kB
import*as t from"alien-signals";import*as e from"../../utils/index.mjs";class s{fn;stopFn;constructor(t){this.fn=t}run(){let e=(0,t.getCurrentSub)();(0,t.setCurrentSub)(this);try{return this.fn()}finally{(0,t.setCurrentSub)(e)}}start(){return this.stopFn=(0,t.effect)(()=>this.fn()),this.run()}stop(){this.stopFn&&(this.stopFn(),this.stopFn=void 0)}}class i{currentValue;type;signal;setter;constructor(e){this.currentValue=e,this.type="$$reactive";let s=(0,t.signal)(e);this.signal=s,this.setter=s}get value(){return this.signal()}set value(t){this.setter(t),this.currentValue=t}subscribe(e){return(0,t.effect)(()=>e(this.value))}}class r{getter;type;computed;constructor(e){this.getter=e,this.type="$$computed",this.computed=(0,t.computed)(this.getter)}get value(){return this.computed()}subscribe(e){return(0,t.effect)(()=>e(this.value))}}let n=()=>({isLoading:!1,isSuccess:!1,isError:!1,error:null,data:void 0,isIdle:!1});class a{asyncFn;options;type;#t;#e;pollingTimer;unifiedPromise;#s;#i;#r;constructor(t,s){this.asyncFn=t,this.options=s,this.type="$$query",this.#e=!0,this.pollingTimer=null,this.subscribe=t=>this.#r.subscribe(t),this.run=async t=>{if(this.unifiedPromise)return this.unifiedPromise;this.#t=t,this.setLoading();let s=()=>this.asyncFn(t),i=this.options?.retry?()=>(0,e.retry)(s,this.options?.retry):s;return this.unifiedPromise=i().then(t=>(this.setSuccess(t),this.#i?.(t),this.unifiedPromise=void 0,t)).catch(t=>{throw this.setError(t),this.#s?.(t),this.unifiedPromise=void 0,this.options?.stopPollingOnError&&this.stopPolling(),t}),this.unifiedPromise},this.reset=()=>{this.#r.value=n(),this.#t=void 0,this.unifiedPromise=void 0,this.stopPolling(),this.#e=!0},this.update=t=>{this.#r.value={...this.#r.value,data:"function"==typeof t?t(this.#r.value.data):t}},this.refetch=()=>this.run(this.#t),this.setLoading=()=>{this.#r.value={...this.#r.value,isLoading:!0,isSuccess:!1,isError:!1,error:null,isIdle:!0}},this.setSuccess=t=>{this.#r.value={...this.#r.value,isLoading:!1,isSuccess:!0,data:t}},this.setError=t=>{this.#r.value={...this.#r.value,data:this.options?.keepDataOnError?this.#r.value.data:void 0,isLoading:!1,isError:!0,error:t}},this.#r=new i(n()),this.#t=void 0,this.#s=this.options?.onError,this.#i=this.options?.onSuccess}get value(){return this.#e&&this.options?.isAutorun&&("function"!=typeof this.options.enable||this.options?.enable?.())&&(this.#e=!1,this.run(void 0)),this.#r.value}set value(t){this.#r.value=t}subscribe;run;reset;update;refetch;startPolling(t,e=!1){!this.pollingTimer&&(e&&this.refetch(),this.pollingTimer=setInterval(()=>{this.refetch()},t))}stopPolling(){this.pollingTimer&&(clearInterval(this.pollingTimer),this.pollingTimer=null)}setLoading;setSuccess;setError}let u=()=>({isLoading:!1,isSuccess:!1,isError:!1,error:null,isIdle:!1});class o{asyncFn;options;type;#s;#i;#r;constructor(t,e){this.asyncFn=t,this.options=e,this.type="$$mutation",this.subscribe=t=>this.#r.subscribe(t),this.runSync=t=>{this.runAsync(t)},this.runAsync=async t=>{this.setLoading();try{let e=await this.asyncFn(t);return this.setSuccess(),this.#i?.(e,t),e}catch(e){this.setError(e),this.#s?.(e,t)}},this.reset=()=>{this.#r.value=u()},this.setLoading=()=>{this.#r.value={...this.#r.value,isLoading:!0,isSuccess:!1,isError:!1,error:null,isIdle:!0}},this.setSuccess=()=>{this.#r.value={...this.#r.value,isLoading:!1,isSuccess:!0}},this.setError=t=>{this.#r.value={...this.#r.value,isLoading:!1,isError:!0,error:t}},this.#r=new i(u()),this.#s=this.options?.onError,this.#i=this.options?.onSuccess}get state(){return this.#r.value}get value(){return this.#r.value}subscribe;runSync;runAsync;reset;setLoading;setSuccess;setError}let c=["push","pop","shift","unshift","splice","sort","reverse"],h=["push","unshift"];class l{makeReactive;updateCallback;reactiveArray;constructor(t,e,s){this.makeReactive=e,this.updateCallback=s,this.reactiveArray=t.map(e),this.makeReactive=e,this.updateCallback=s}createReactive(){let t=this;return new Proxy(this.reactiveArray,{get(e,s,i){let r=Reflect.get(e,s,i);return"function"==typeof r&&c.includes(s)?(...i)=>{let n=r.apply(e,i);if(h.includes(s))i.forEach((s,r)=>{e[e.length-i.length+r]=t.makeReactive(s)});else if("splice"===s&&i.length>2)for(let e=2;e<i.length;e++)i[e]=t.makeReactive(i[e]);return t.updateCallback(),n}:r},set(e,s,i,r){let n=Number(s),a=i;Number.isNaN(n)||(a=t.makeReactive(i));let u=Reflect.set(e,s,a,r);return t.updateCallback(),u},deleteProperty(e,s){let i=Reflect.deleteProperty(e,s);return t.updateCallback(),i}})}}class p{type="$$reactive";reactive;reactiveCache=new WeakMap;constructor(t){this.reactive=new i({inner:this.makeReactive(t)})}makeReactive(t){if(this.isPrimitive(t))return t;if(this.reactiveCache.has(t))return this.reactiveCache.get(t);let e=Array.isArray(t)?new l(t,this.makeReactive.bind(this),this.update.bind(this)).createReactive():this.defineReactiveObject(t);return this.reactiveCache.set(t,e),e}isPrimitive(t){return"object"!=typeof t||null===t}defineReactiveObject(t){let e=new Proxy(t,{set:(t,e,s)=>{let i=!(e in t);return t[e]=s,i&&this.makeReactiveProperty(t,e),this.update(),!0},deleteProperty:(t,e)=>(e in t&&(delete t[e],this.update()),!0)});return Object.keys(t).forEach(e=>this.makeReactiveProperty(t,e)),e}makeReactiveProperty(t,e){let s=this.makeReactive(t[e]);Object.defineProperty(t,e,{get:()=>s,set:t=>{t!==s&&(s=this.makeReactive(t))},configurable:!0,enumerable:!0})}update(){this.reactive.value={inner:this.reactive.value.inner}}get value(){return this.reactive.value.inner}set value(t){this.reactive.value={inner:this.makeReactive(t)}}subscribe(t){return this.reactive.subscribe(({inner:e})=>t(e))}}function v(t){return new r(t)}function d(e){(0,t.startBatch)();try{e()}finally{(0,t.endBatch)()}}function f(t){return new i(t)}function y(e){return(0,t.effect)(e)}function g(t){return new p(t)}function m(t,e){return new a(t,e)}function b(t,e){return new o(t,e)}export{r as Computed,s as Effect,o as MutationHandler,a as QueryHandler,i as Reactive,d as batch,v as computed,g as deepReactive,y as effect,b as mutation,m as query,f as reactive};