UNPKG

flexium

Version:

A lightweight, signals-based UI framework with cross-platform renderers

2 lines 3.99 kB
var s=null,c=null,a=0,p=new Set;function k(r){w(()=>E(r));}var v=class{constructor(t,e){this.fn=t;this.onError=e;this.dependencies=new Set;this.cleanups=[];this.isExecuting=false;this.isQueued=false;}execute(){if(this.isExecuting){this.isQueued=true;return}this.isExecuting=true;try{this.run();}finally{this.isExecuting=false,this.isQueued&&(this.isQueued=false,queueMicrotask(()=>this.execute()));}}run(){for(let e of this.cleanups)e();this.cleanups=[];for(let e of this.dependencies)e.subscribers.delete(this);this.dependencies.clear();let t=s;s=this;try{let e=this.fn();typeof e=="function"&&this.cleanups.push(e);}catch(e){this.onError?this.onError(e):console.error("Error in effect:",e);}finally{s=t;}}dispose(){for(let t of this.cleanups)t();this.cleanups=[];for(let t of this.dependencies)t.subscribers.delete(this);this.dependencies.clear();}},T=class{constructor(t){this._value=t;this.subscribers=new Set;}get(){return s&&(this.subscribers.add(s),s.dependencies.add(this)),this._value}set(t){this._value!==t&&(this._value=t,this.notify());}peek(){return this._value}notify(){if(a>0)for(let t of this.subscribers)p.add(t);else {let t=new Set(this.subscribers);for(let e of t)e.execute();}}},g=class{constructor(t){this.computeFn=t;this.subscribers=new Set;this.dependencies=new Set;this._dirty=true;}execute(){this._dirty=true,this.notify();}get(){if(s&&s!==this&&(this.subscribers.add(s),s.dependencies.add(this)),this._dirty){this._dirty=false;for(let e of this.dependencies)e.subscribers.delete(this);this.dependencies.clear();let t=s;s=this;try{this._value=this.computeFn();}finally{s=t;}}return this._value}peek(){if(this._dirty){this._dirty=false;for(let e of this.dependencies)e.subscribers.delete(this);this.dependencies.clear();let t=s;s=this;try{this._value=this.computeFn();}finally{s=t;}}return this._value}notify(){if(a>0)for(let t of this.subscribers)p.add(t);else {let t=new Set(this.subscribers);for(let e of t)e.execute();}}};function f(r){let t=new T(r),e=function(){return t.get()};return Object.defineProperty(e,"value",{get(){return t.get()},set(n){t.set(n);},enumerable:true,configurable:true}),e.set=n=>t.set(n),e.peek=()=>t.peek(),e[b]=true,e}function O(r){let t=new g(r),e=function(){return t.get()};return Object.defineProperty(e,"value",{get(){return t.get()},enumerable:true,configurable:true}),e.peek=()=>t.peek(),e[b]=true,e}function w(r,t){let e=new v(r,t?.onError);e.execute();let n=()=>e.dispose();return c&&c.cleanups.push(n),n}function P(r){a++;try{r();}finally{if(a--,a===0){let t=new Set(p);p.clear();for(let e of t)e.execute();}}}function E(r){let t=s;s=null;try{return r()}finally{s=t;}}function R(r){let t=c,e={cleanups:[]};c=e;let n=()=>{for(let o of e.cleanups)o();e.cleanups=[];};try{return r(n)}finally{c=t;}}var b=Symbol("flexium.signal");function _(r){return r!==null&&typeof r=="function"&&b in r}function C(r){s instanceof v?s.cleanups.push(r):console.warn("onCleanup must be called from within an effect");}function Q(r,t){let e=f(void 0),n=f(void 0),o=f(false),i=f("unresolved"),l=null,S=async(u,m=false)=>{m?(i.value="refreshing",o.value=true):(i.value="pending",o.value=true),n.value=void 0;let d=t(u,{value:e.peek(),refetching:m});l=d;try{let y=await d;l===d&&(e.value=y,i.value="ready",o.value=!1);}catch(y){l===d&&(n.value=y,i.value="errored",o.value=false);}},x=()=>typeof r=="function"?_(r)?r.value:r():r;w(()=>{let u=x();S(u,false);});let h=function(){return e()};return Object.defineProperties(h,{value:{get:()=>e.value},loading:{get:()=>o.value},error:{get:()=>n.value},state:{get:()=>i.value},latest:{get:()=>e.peek()},peek:{value:()=>e.peek()},set:{value:u=>e.set(u)},read:{value:()=>{if(i.value==="pending"||i.value==="refreshing")throw l;if(i.value==="errored")throw n.value;return e.value}}}),h[b]=true,[h,{mutate:u=>e.set(u),refetch:()=>S(x(),true)}]}export{k as a,f as b,O as c,w as d,P as e,E as f,R as g,_ as h,C as i,Q as j};//# sourceMappingURL=chunk-Z7JNAX53.mjs.map //# sourceMappingURL=chunk-Z7JNAX53.mjs.map