@fluixi/reactive 1.0.0-alpha.83 → 1.0.0-alpha.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cdn/reactive.cjs +2722 -1
- package/dist/cdn/reactive.global.js +2 -1
- package/dist/cdn/reactive.mjs +2706 -1
- package/dist/cdn/signal.cjs +299 -1
- package/dist/cdn/signal.d.ts +1 -1
- package/dist/cdn/signal.global.js +2 -1
- package/dist/cdn/signal.js +1 -1
- package/dist/cdn/signal.mjs +276 -1
- package/dist/index.cjs +2722 -1
- package/dist/index.mjs +2706 -1
- package/dist/lib/index.cjs +2723 -1
- package/dist/lib/index.js +1 -1
- package/dist/lib/index.mjs +2708 -1
- package/dist/lib/observable/index.cjs +386 -1
- package/dist/lib/observable/index.mjs +364 -1
- package/dist/lib/observable/observable.cjs +384 -1
- package/dist/lib/observable/observable.d.ts +0 -137
- package/dist/lib/observable/observable.d.ts.map +1 -1
- package/dist/lib/observable/observable.js +0 -71
- package/dist/lib/observable/observable.mjs +361 -1
- package/dist/lib/observable/subscription.cjs +180 -1
- package/dist/lib/observable/subscription.d.ts +0 -12
- package/dist/lib/observable/subscription.d.ts.map +1 -1
- package/dist/lib/observable/subscription.js +0 -12
- package/dist/lib/observable/subscription.mjs +157 -1
- package/dist/lib/observable/types.cjs +18 -1
- package/dist/lib/observable/types.d.ts.map +1 -1
- package/dist/lib/predicates.cjs +44 -1
- package/dist/lib/predicates.d.ts +2 -2
- package/dist/lib/predicates.js +2 -2
- package/dist/lib/predicates.mjs +23 -1
- package/dist/lib/signal/api.cjs +571 -1
- package/dist/lib/signal/api.d.ts +211 -1
- package/dist/lib/signal/api.d.ts.map +1 -1
- package/dist/lib/signal/api.js +207 -12
- package/dist/lib/signal/api.mjs +548 -1
- package/dist/lib/signal/cached-resource.cjs +672 -1
- package/dist/lib/signal/cached-resource.d.ts +28 -16
- package/dist/lib/signal/cached-resource.d.ts.map +1 -1
- package/dist/lib/signal/cached-resource.js +35 -29
- package/dist/lib/signal/cached-resource.mjs +650 -1
- package/dist/lib/signal/graph/children.cjs +325 -1
- package/dist/lib/signal/graph/children.js +1 -1
- package/dist/lib/signal/graph/children.mjs +302 -1
- package/dist/lib/signal/graph/compat.cjs +578 -1
- package/dist/lib/signal/graph/compat.d.ts +13 -13
- package/dist/lib/signal/graph/compat.d.ts.map +1 -1
- package/dist/lib/signal/graph/compat.js +33 -31
- package/dist/lib/signal/graph/compat.mjs +556 -1
- package/dist/lib/signal/graph/core.cjs +295 -1
- package/dist/lib/signal/graph/core.d.ts +28 -24
- package/dist/lib/signal/graph/core.d.ts.map +1 -1
- package/dist/lib/signal/graph/core.js +38 -31
- package/dist/lib/signal/graph/core.mjs +273 -1
- package/dist/lib/signal/graph/debounced.cjs +668 -1
- package/dist/lib/signal/graph/debounced.d.ts +5 -5
- package/dist/lib/signal/graph/debounced.d.ts.map +1 -1
- package/dist/lib/signal/graph/debounced.js +5 -5
- package/dist/lib/signal/graph/debounced.mjs +645 -1
- package/dist/lib/signal/graph/derived.cjs +496 -1
- package/dist/lib/signal/graph/derived.d.ts +1 -1
- package/dist/lib/signal/graph/derived.js +1 -1
- package/dist/lib/signal/graph/derived.mjs +473 -1
- package/dist/lib/signal/graph/detect.cjs +66 -1
- package/dist/lib/signal/graph/detect.mjs +45 -1
- package/dist/lib/signal/graph/error-boundary.cjs +301 -1
- package/dist/lib/signal/graph/error-boundary.js +3 -3
- package/dist/lib/signal/graph/error-boundary.mjs +278 -1
- package/dist/lib/signal/graph/index.cjs +305 -1
- package/dist/lib/signal/graph/index.d.ts +7 -6
- package/dist/lib/signal/graph/index.d.ts.map +1 -1
- package/dist/lib/signal/graph/index.js +7 -6
- package/dist/lib/signal/graph/index.mjs +283 -1
- package/dist/lib/signal/graph/observe.cjs +163 -1
- package/dist/lib/signal/graph/observe.d.ts +8 -8
- package/dist/lib/signal/graph/observe.js +5 -5
- package/dist/lib/signal/graph/observe.mjs +140 -1
- package/dist/lib/signal/graph/resource.cjs +620 -1
- package/dist/lib/signal/graph/resource.d.ts +1 -1
- package/dist/lib/signal/graph/resource.js +7 -8
- package/dist/lib/signal/graph/resource.mjs +597 -1
- package/dist/lib/signal/graph/runtime.cjs +501 -1
- package/dist/lib/signal/graph/runtime.d.ts +7 -7
- package/dist/lib/signal/graph/runtime.d.ts.map +1 -1
- package/dist/lib/signal/graph/runtime.js +34 -30
- package/dist/lib/signal/graph/runtime.mjs +478 -1
- package/dist/lib/signal/graph/signal-next.cjs +1208 -1
- package/dist/lib/signal/graph/signal-next.d.ts +5 -5
- package/dist/lib/signal/graph/signal-next.d.ts.map +1 -1
- package/dist/lib/signal/graph/signal-next.js +14 -12
- package/dist/lib/signal/graph/signal-next.mjs +1186 -1
- package/dist/lib/signal/graph/ssr-hooks.cjs +60 -1
- package/dist/lib/signal/graph/ssr-hooks.d.ts +6 -7
- package/dist/lib/signal/graph/ssr-hooks.d.ts.map +1 -1
- package/dist/lib/signal/graph/ssr-hooks.js +6 -7
- package/dist/lib/signal/graph/ssr-hooks.mjs +39 -1
- package/dist/lib/signal/graph/state.cjs +72 -1
- package/dist/lib/signal/graph/state.d.ts +41 -10
- package/dist/lib/signal/graph/state.d.ts.map +1 -1
- package/dist/lib/signal/graph/state.js +83 -22
- package/dist/lib/signal/graph/state.mjs +49 -1
- package/dist/lib/signal/graph/suspense.cjs +301 -1
- package/dist/lib/signal/graph/suspense.js +4 -4
- package/dist/lib/signal/graph/suspense.mjs +278 -1
- package/dist/lib/signal/index-legacy.cjs +2540 -1
- package/dist/lib/signal/index-legacy.d.ts +1 -1
- package/dist/lib/signal/index-legacy.js +1 -1
- package/dist/lib/signal/index-legacy.mjs +2518 -1
- package/dist/lib/signal/index.cjs +1275 -1
- package/dist/lib/signal/index.d.ts +3 -2
- package/dist/lib/signal/index.d.ts.map +1 -1
- package/dist/lib/signal/index.js +7 -4
- package/dist/lib/signal/index.mjs +1253 -1
- package/dist/lib/signal/list.cjs +549 -1
- package/dist/lib/signal/list.d.ts +5 -5
- package/dist/lib/signal/list.js +5 -5
- package/dist/lib/signal/list.mjs +526 -1
- package/dist/lib/signal/resource-api.cjs +626 -1
- package/dist/lib/signal/resource-api.d.ts +5 -5
- package/dist/lib/signal/resource-api.js +5 -5
- package/dist/lib/signal/resource-api.mjs +603 -1
- package/dist/lib/signal/signal.cjs +2023 -1
- package/dist/lib/signal/signal.d.ts +4 -4
- package/dist/lib/signal/signal.d.ts.map +1 -1
- package/dist/lib/signal/signal.js +22 -23
- package/dist/lib/signal/signal.mjs +2000 -1
- package/dist/lib/signal/types.cjs +38 -1
- package/dist/lib/signal/types.d.ts.map +1 -1
- package/dist/lib/signal/types.js +2 -2
- package/dist/lib/signal/types.mjs +17 -1
- package/dist/lib/signal/utilities.cjs +1012 -1
- package/dist/lib/signal/utilities.d.ts +0 -108
- package/dist/lib/signal/utilities.d.ts.map +1 -1
- package/dist/lib/signal/utilities.js +0 -158
- package/dist/lib/signal/utilities.mjs +990 -1
- package/dist/lib/store/api.cjs +661 -1
- package/dist/lib/store/api.d.ts +33 -0
- package/dist/lib/store/api.d.ts.map +1 -1
- package/dist/lib/store/api.js +41 -8
- package/dist/lib/store/api.mjs +645 -1
- package/dist/lib/store/index.cjs +740 -1
- package/dist/lib/store/index.js +1 -1
- package/dist/lib/store/index.mjs +725 -1
- package/dist/lib/store/setter.types.cjs +18 -1
- package/dist/lib/store/setter.types.d.ts +43 -3
- package/dist/lib/store/setter.types.d.ts.map +1 -1
- package/dist/lib/store/store.cjs +707 -1
- package/dist/lib/store/store.d.ts +72 -0
- package/dist/lib/store/store.d.ts.map +1 -1
- package/dist/lib/store/store.js +238 -8
- package/dist/lib/store/store.mjs +693 -1
- package/dist/lib/store/types.cjs +18 -1
- package/dist/lib/store/types.d.ts +1 -1
- package/dist/lib/store/types.d.ts.map +1 -1
- package/dist/lib/store/utils.cjs +43 -1
- package/dist/lib/store/utils.d.ts +1 -1
- package/dist/lib/store/utils.js +1 -1
- package/dist/lib/store/utils.mjs +22 -1
- package/dist/lib/subject.cjs +450 -1
- package/dist/lib/subject.js +3 -91
- package/dist/lib/subject.mjs +427 -1
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/version.generated.cjs +27 -1
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +2 -2
- package/dist/version.generated.mjs +7 -1
- package/package.json +5 -4
|
@@ -1 +1,650 @@
|
|
|
1
|
-
function ce(e){return typeof e=="function"&&!/^class\s/.test(Function.prototype.toString.call(e))}function Ce(e){return e==null||Array.isArray(e)||typeof e=="function"||e instanceof Date||e instanceof RegExp||e instanceof Map||e instanceof Set||e instanceof Error?!1:typeof e=="object"}function fe(e){return e instanceof Promise||!!e&&(typeof e=="object"||typeof e=="function")&&"then"in e}var j=class e{constructor(n){this._closed=!1;this._parentOrParents=null;this._subscriptions=null;n&&(this._subscriptions=[n])}get closed(){return this._closed}unsubscribe(){if(this._closed)return;this._closed=!0;let{_parentOrParents:n,_subscriptions:t}=this;if(n instanceof e)n.remove(this);else if(n!==null)for(let o of n)o.remove(this);if(t){for(let o of t)if(ce(o)&&!(o instanceof e))try{o()}catch(r){console.error("Error in unsubscribe function:",r)}else if(Ce(o)&&typeof o.unsubscribe=="function")try{o.unsubscribe()}catch(r){console.error("Error unsubscribing child:",r)}this._subscriptions=null}}add(n){if(!n||n===this)return this;if(n.closed)return this;let{_closed:t,_subscriptions:o}=this;if(t){if(ce(n))try{n()}catch(r){console.error("Error in unsubscribe function:",r)}else if(n.unsubscribe)try{n.unsubscribe()}catch(r){console.error("Error unsubscribing:",r)}return this}return o||(this._subscriptions=[]),this._subscriptions.push(n),n instanceof e&&n._addParent(this),this}remove(n){let{_subscriptions:t}=this;if(!t)return;let o=t.indexOf(n);o!==-1&&(t.splice(o,1),n._removeParent(this))}_addParent(n){let{_parentOrParents:t}=this;t?Array.isArray(t)?t.push(n):this._parentOrParents=[t,n]:this._parentOrParents=n}_removeParent(n){let{_parentOrParents:t}=this;if(t===n)this._parentOrParents=null;else if(Array.isArray(t)){let o=t.indexOf(n);o!==-1&&(t.splice(o,1),t.length===1&&(this._parentOrParents=t[0]))}}};var K=class extends j{constructor(t,o,r){super();this.isStopped=!1;let s;t?typeof t=="object"?s=t:s={next:t,error:o,complete:r}:s={},this.destination=s}next(t){if(!this.isStopped&&this.destination.next)try{this.destination.next(t)}catch(o){this.error(o)}}error(t){if(!this.isStopped){if(this.isStopped=!0,this.destination.error)try{this.destination.error(t)}catch(o){throw o}else throw t;this.unsubscribe()}}complete(){if(!this.isStopped){if(this.isStopped=!0,this.destination.complete)try{this.destination.complete()}catch(t){this.error(t);return}this.unsubscribe()}}unsubscribe(){this.closed||(this.isStopped=!0,super.unsubscribe())}},de=class extends K{constructor(t,o,r,s){super(t,o,r);this._isUnsubscribing=!1;this._parentSubscriber=null;this._parentSubscriber=s||null}next(t){if(!this.isStopped&&!this._isUnsubscribing)try{super.next(t)}catch(o){this.error(o)}}error(t){if(!this.isStopped&&!this._isUnsubscribing){this._isUnsubscribing=!0;try{super.error(t)}finally{this._isUnsubscribing=!1}}}complete(){if(!this.isStopped&&!this._isUnsubscribing){this._isUnsubscribing=!0;try{super.complete()}finally{this._isUnsubscribing=!1}}}unsubscribe(){this.closed||(this._isUnsubscribing=!0,super.unsubscribe(),this._isUnsubscribing=!1)}};var Y=class extends j{constructor(n){if(super(),n)this._subscribe=n;else throw new TypeError("Observable constructor requires a subscribe function")}subscribe(n,t,o){let r;n instanceof K?r=n:r=new de(n,t,o);let{_subscribe:s}=this;try{let l=s.call(this,r);l&&(typeof l=="function"||typeof l.unsubscribe=="function")&&r.add(l)}catch(l){r.error(l)}return r}pipe(...n){return n.length===0?this:n.reduce((t,o)=>o(t),this)}toPromise(){return new Promise((n,t)=>{let o;this.subscribe({next:r=>o=r,error:t,complete:()=>n(o)})})}};var en=typeof window>"u"?!1:window.__FLUIXI_PROD__===!0,X=typeof process>"u"?en:!1,nn=Symbol.for("signal");function pe(e){return typeof e=="function"&&e[nn]===!0}var tn="__fluixi_signal_next_state__",rn=e=>{typeof queueMicrotask=="function"?queueMicrotask(e):Promise.resolve().then(e)},a=globalThis[tn]??={consumer:null,untracked:!1,owner:null,queue:[],scheduled:!1,flushing:!1,batchDepth:0,hostSchedule:rn,onSuspend:null,onError:null,observer:null};var ke="1.0.0-alpha.83";function on(e){return a.observer=e,()=>{a.observer===e&&(a.observer=null)}}var ve=[],sn;function Ae(e){let n=a.observer;n&&n.created(e.parents===void 0?{...e,parents:ve,origin:e.origin??sn}:e)}function Ee(e){return a.observer?(ve.push(e),!0):!1}function Pe(){ve.pop()}function Fe(e){a.observer?.ran?.(e)}function De(e){a.observer?.disposed?.(e)}var un="__FLUIXI_DEVTOOLS_HOOK__",_e=globalThis[un];if(typeof _e?.inject=="function")try{_e.inject({observeReactiveNodes:on,version:ke})}catch{}function qe(){return a.observer!=null}var Ne=0,Ie=1,J=2,he=class{constructor(){this.observers=null;this.watchers=null;this.version=0}updateIfNecessary(){}track(){if(a.untracked)return;let n=a.consumer;n&&((this.observers??=new Set).add(n),(n.sources??=new Set).add(this))}};var Q=class extends he{constructor(t,o){super();this._threw=!1;this._initialized=!1;this.state=J;this.sources=null;this._fn=t,this._equals=o?.equals===!1?()=>!1:o?.equals??Object.is}get(){if(a.consumer===this)throw new Error("Signal.Computed cannot read itself");if(this.track(),this.updateIfNecessary(),this._threw)throw this._error;return this._value}updateIfNecessary(){if(this.state===Ie&&this.sources){for(let t of this.sources)if(t.updateIfNecessary(),this.state===J)break}(this.state===J||!this._initialized)&&this.recompute(),this.state=Ne}recompute(){if(this.sources){for(let v of this.sources)v.observers?.delete(this);this.sources.clear()}let t=a.consumer,o=a.untracked,r=Ee(this);a.consumer=this,a.untracked=!1;let s,l=!1,d;try{s=this._fn()}catch(v){l=!0,d=v,s=this._value}finally{a.consumer=t,a.untracked=o,r&&Pe()}let b=!this._initialized||this._threw!==l||(l?d!==this._error:!this._equals(this._value,s));if(this._initialized=!0,this._threw=l,this._error=d,this._value=s,Fe(this),b){if(this.version++,this.observers)for(let v of[...this.observers])v.markDirty(J);Ue(this)}}markDirty(t){if(this.state>=t)return;let o=this.state===Ne;if(this.state=t,o){if(this.observers)for(let r of[...this.observers])r.markDirty(Ie);Ue(this)}}peek(){if(this.updateIfNecessary(),this._threw)throw this._error;return this._value}};function Ue(e){if(e.watchers)for(let n of[...e.watchers])n._notify(e)}function Le(e){return a.onSuspend?(a.onSuspend(e,a.owner),!0):!1}function Me(){return a.owner}function Be(e,n){let t=a.owner;a.owner=e;try{return n()}finally{a.owner=t}}function je(e){if(a.owner){if(a.owner.disposed){e();return}(a.owner.cleanups??=[]).push(e)}}function Ve(e){a.hostSchedule=e??(n=>{Promise.resolve().then(n)})}function an(e){return!!e&&typeof e.then=="function"}Ve(()=>{});var ln=Symbol.for("signal"),cn=Symbol.for("memo");function Z(e,n,t){let o=n,r=Me(),s=new Q(()=>Be(r,()=>{try{return o=e(o)}catch(d){if(an(d))return Le(d),o;throw d}}),{equals:t?.equals}),l=(()=>s.get());return Object.defineProperty(l,ln,{value:!0,enumerable:!0}),Object.defineProperty(l,cn,{value:!0,enumerable:!0}),Ae({kind:"memo",node:s,handle:l,name:t?.name}),qe()&&je(()=>De(s)),l}function ee(e){if(typeof e=="function"&&!e.length){let n=e();return pe(n)?n:ee(n)}if(Array.isArray(e)){let n=[];for(let t=0;t<e.length;t++){let o=ee(e[t]);Array.isArray(o)?n.push.apply(n,o):n.push(o)}return n}return e}function Ge(e){let n=Z(e);return X?Z(()=>ee(n()),void 0,{name:"children"}):Z(()=>ee(n()))}var $e=null;function be(e){$e&&$e(e)}var fn=Symbol.for("signal");var We=Symbol("fluixi-proxy"),dn=Symbol.for("signal-node"),V=0,ye=1,oe=2,I=3,f=null,h=null,i=null,te=null,re=0,pn=!1,Te=!1,N=0,vn=1e6,O=new WeakMap,ne=null;var hn=new Map,bn=0;function Tn(){return`node_${++bn}_${Date.now()}`}function yn(e){X&&hn.set(e.id,new WeakRef(e))}function mn(e,n,t){let o={id:Tn(),type:e,value:n,version:0,equals:t?.equals,name:t?.name,created:Date.now(),updated:Date.now()};return yn(o),o}function wn(e,n){return e===n}function ze(e){throw typeof console<"u"&&console.error("Reactive error:",e),e}function xn(e){if(i)i.indexOf(e)===-1&&i.push(e);else if(ne&&ne.running&&!e.pure)ne.effects.indexOf(e)===-1&&ne.effects.push(e);else if(!e.pure||e.computed)we(e);else if(e.pure&&!e.computed&&e.accessor){let n=h,t=i;h=null,i=[],e.accessor(),h=n;let o=i;if(i=t,o.length>0){let r=i;se(o),i=r}}}function He(e,n){for(let t=0;t<e.length;t++)n(e[t])}function C(e,n){let t=e;typeof t=="object"&&t&&We in t&&delete t[We];let o=mn("signal",t,{equals:n?.equals===!1?void 0:n?.equals,name:n?.name}),r=[l,d];O.set(r,new Set);let s;function l(){let b=h;if(b){b.sources?b.sources.indexOf(r)===-1&&b.sources.push(r):b.sources=[r];let v=O.get(r);v&&!v.has(b)&&v.add(b)}return t}function d(b){let v=typeof b=="function"?b(t):b,B=o.equals||wn;if(n?.equals===!1||!B(t,v)){t=v,o.value=v,o.version++,o.updated=Date.now();let R=O.get(r);if(R&&R.size>0){let P=function(c,ie=!1){let m=c.state;if(!(m===I&&c===h)){if(ie&&!c.pure){m===V&&(c.state=ye,pn&&(i!==null&&i.indexOf(c)===-1?i.push(c):x.push(c)));return}if(!g.has(c)&&(g.add(c),c.state=oe,m===V||m===ye?x.push(c):!c.pure&&i!==null&&i.indexOf(c)===-1&&i.push(c),c.pure&&c.accessor)){let $=O.get(c.accessor);$&&He(Array.from($),F=>P(F,!0))}}};var G=P;let E=Array.from(R),x=[],g=new Set;He(E,P);for(let c of x)xn(c)}}}return l.toObservable=()=>new Y(b=>{let v=!0;return me(()=>{let G=l();v?v=!1:b.next(G)})}),Object.defineProperty(l,fn,{value:!0,enumerable:!0}),Object.defineProperty(l,dn,{value:o,enumerable:!1}),r}function me(e,n,t){let o={fn:e,state:oe,sources:null,sourceSlots:null,owned:null,cleanups:null,owner:f,contexts:null,pure:!1,updatedAt:null};f&&(f.owned?f.owned.push(o):f.owned=[o]);let r=w&&M(w);return r&&(o.suspense=r),Ke(o,n),()=>U(o)}function Ke(e,n){if(e.sources){for(let r of e.sources){let s=O.get(r);s&&s.delete(e)}e.sources=null,e.sourceSlots=null}if(e.owned){for(let r=0;r<e.owned.length;r++)U(e.owned[r]);e.owned=null}let t=h,o=f;if(h=e,f=e,re++,re>vn)throw re=0,h=t,f=o,new Error("Potential infinite loop detected. Exceeded maximum iterations.");try{e.state=I;let r=e.fn(n);typeof r=="function"&&(e.cleanups?e.cleanups.push(r):e.cleanups=[r]),e.state===I&&(e.state=V)}catch(r){if(fe(r)){let s=w&&M(w);s&&(s.increment(),r.finally(()=>s.decrement()),e.suspense&&s.effects.push(e));return}ze(r)}finally{h=t,f=o}}function we(e){if(!(e.disposed||!e.fn||e.state===V)&&(e.state===oe||e.state===ye||e.state===I)){if(e.cleanups){for(let n=0;n<e.cleanups.length;n++)e.cleanups[n]();e.cleanups=null}if(e.owned){for(let n=0;n<e.owned.length;n++)U(e.owned[n]);e.owned=null}e.renderEffect?Xe(e):Ke(e,e.value)}}function U(e){if(e.disposed=!0,e.sources)for(let n of e.sources){let t=O.get(n);t&&t.delete(e)}if(e.cleanups)for(let n=0;n<e.cleanups.length;n++)e.cleanups[n]();if(e.owned)for(let n=0;n<e.owned.length;n++)U(e.owned[n]);if(e.owner&&e.owner.owned){let n=e.owner.owned.indexOf(e);n!==-1&&e.owner.owned.splice(n,1)}}function se(e){let n=[],t=[];for(let r of e)r.pure&&!r.computed&&r.accessor?n.indexOf(r)===-1&&n.push(r):(r.computed||!r.pure)&&t.indexOf(r)===-1&&t.push(r);i=[];let o=0;for(;o<n.length;){let r=n[o++],s=O.get(r.accessor);if(s&&s.size>0){let d=h;h=null,r.accessor(),h=d}let l=i.splice(0);for(let d of l)d.pure&&!d.computed&&d.accessor?n.indexOf(d)===-1&&n.push(d):(d.computed||!d.pure)&&t.indexOf(d)===-1&&t.push(d)}i=null;for(let r of t)(r.computed||!r.pure)&&we(r)}function k(e){if(i&&N>0)return e();if(i&&N===0){let n=i,t=[];i=t,N++;let o;try{o=e()}finally{N--,i=n}let r=i;return se(t),i=r,o}N++,i=[],te=[];try{return e()}finally{N--,gn()}}function gn(){if(!Te){Te=!0,re=0;try{for(;i&&i.length>0;){let e=i;i=null,se(e)}if(i=null,te){let e=te;te=null;for(let n=0;n<e.length;n++)we(e[n])}}finally{Te=!1}}}function L(e){let n=h;h=null;try{return e()}finally{h=n}}function Ye(e){f&&(f.cleanups?f.cleanups.push(e):f.cleanups=[e])}function Sn(e){let n=Symbol("context"),t=On(n);return{id:n,Provider:t,defaultValue:e}}function On(e){return n=>{let t=f;for(;t;)t.contexts||(t.contexts=new Map),t.contexts.has(e)||t.contexts.set(e,n.value),t=t.owner;let o;return Rn(()=>{o=L(()=>{let r=f;return r&&(r.contexts||(r.contexts=new Map),r.contexts.set(e,n.value)),Ge(()=>n.children)})}),o}}function M(e){let n=f;for(;n;){if(n.contexts&&n.contexts.has(e.id))return n.contexts.get(e.id);n=n.owner}if(e)return e.defaultValue}function Rn(e,n,t){let o={fn:e,state:oe,sources:null,sourceSlots:null,value:n,owned:null,cleanups:null,owner:f,contexts:null,pure:!1,updatedAt:null,renderEffect:!0};f&&(f.owned?f.owned.push(o):f.owned=[o]);let r=w&&M(w);return r&&(o.suspense=r),Xe(o),()=>U(o)}function Xe(e){if(e.sources){for(let r of e.sources){let s=O.get(r);s&&s.delete(e)}e.sources=null,e.sourceSlots=null}if(e.cleanups){for(let r=0;r<e.cleanups.length;r++)e.cleanups[r]();e.cleanups=null}if(e.owned){for(let r=0;r<e.owned.length;r++)U(e.owned[r]);e.owned=null}let n=h,t=f,o=i;h=e,f=e,i===null&&(i=[]),e.state=I;try{let r=e.fn(e.value);typeof r=="function"&&(e.cleanups||(e.cleanups=[]),e.cleanups.push(r)),e.value=r,e.state===I&&(e.state=V)}catch(r){if(fe(r)){let s=w&&M(w);s&&(s.increment(),r.finally(()=>s.decrement()),e.suspense&&s.effects.push(e));return}ze(r)}finally{h=n,f=t;let r=i;if(i=o,r!==o&&r&&r.length>0){let s=i;se(r),i=s}}}var w;function Je(){return w||(w=Sn())}var A=new Map,_=new Map,Cn=0,kn=Symbol.for("signal"),_n=Symbol.for("resource");function Et(e,n,t={}){let o,r,s;typeof n=="function"?(o=e,r=n,s=t):(o=()=>!0,r=e,s=n??t);let{ttl:l=1/0,name:d=`_auto_${Cn++}`,staleWhileRevalidate:b=!0}=s,v=d,B=u=>`${v}:${JSON.stringify(u)}`,G=u=>l===1/0||Date.now()-u.timestamp<=l,[R,E]=C(void 0,{equals:!1}),[x,g]=C("unresolved",{equals:!1}),[P,c]=C(void 0,{equals:!1}),[ie,m]=C(void 0,{equals:!1}),[$,F]=C(!1),[Qe,xe]=C(0,{equals:!1}),ue=Symbol("unset"),W=ue,ae=!1,le=!1;function ge(u,T,y,S){if(_.has(T)){_.get(T).then(p=>{S()||Se(p,y)},p=>{!S()&&!y&&Oe(p)}),y||m(_.get(T));return}let D=Promise.resolve(r(u)).then(p=>(A.set(T,{value:p,timestamp:Date.now()}),_.delete(T),p),p=>{throw _.delete(T),p});if(_.set(T,D),!y){be(D),m(D);let p=H&&M(H);p&&(p.increment(),D.finally(()=>p.decrement()))}D.then(p=>{S()||Se(p,y)},p=>{!S()&&!y&&Oe(p)})}function Se(u,T){k(()=>{E(()=>u),g("ready"),F(!1),m(void 0),c(void 0)})}function Oe(u){k(()=>{c(u),g("errored"),m(void 0)})}let H;me(()=>{H===void 0&&(H=Je());let u=o();if(Qe(),u===!1||u===null||u===void 0){W=ue,k(()=>{g("unresolved"),m(void 0),c(void 0)});return}let T=ae,y=le;ae=!1,le=!1;let S=B(u);y&&A.delete(S);let D=W===ue||u!==W;W=u;let p=!1;Ye(()=>{p=!0});let q=A.get(S);if(q&&G(q)&&!T&&!y){(L(x)!=="ready"||L(R)!==q.value)&&k(()=>{E(()=>q.value),g("ready"),F(!1),c(void 0)});return}if(q&&b&&!y){k(()=>{E(()=>q.value),g("ready"),F(!0),c(void 0),m(void 0)}),ge(u,S,!0,()=>p);return}let Re=L(x),Ze=Re==="ready"||Re==="refreshing";k(()=>{g(Ze?"refreshing":"pending"),c(void 0),F(!1)}),ge(u,S,!1,()=>p)});let z=(()=>{let u=P();if(u)throw u;if(x()==="pending"){let y=ie();if(y)throw y}return R()});return Object.defineProperty(z,_n,{value:!0,enumerable:!0}),Object.defineProperty(z,kn,{value:!0,enumerable:!0}),Object.defineProperties(z,{state:{get:x},loading:{get:()=>{let u=x();return u==="pending"||u==="refreshing"}},error:{get:P},latest:{get:R},stale:{get:$}}),[z,{mutate:E,refetch:()=>{ae=!0,xe(u=>u+1)},invalidate:()=>{let u=L(o);u!=null&&u!==!1&&A.delete(B(u)),le=!0,xe(T=>T+1)}}]}function Pt(e){let n=`${e}:`;for(let t of A.keys())t.startsWith(n)&&A.delete(t)}function Ft(){A.clear(),_.clear()}export{Ft as clearAllCachedResources,Pt as clearCachedResourceNamespace,Et as createCachedResource};
|
|
1
|
+
/*! @fluixi/reactive v1.0.0-alpha.85 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
|
|
3
|
+
// src/version.generated.ts
|
|
4
|
+
var VERSION = "1.0.0-alpha.85";
|
|
5
|
+
|
|
6
|
+
// src/lib/signal/graph/state.ts
|
|
7
|
+
var MAJOR = VERSION.split(".")[0];
|
|
8
|
+
var KEY = `__fluixi_signal_state_v${MAJOR}__`;
|
|
9
|
+
var VERSIONS_KEY = `__fluixi_signal_builds_v${MAJOR}__`;
|
|
10
|
+
var defaultSchedule = (run) => {
|
|
11
|
+
if (typeof queueMicrotask === "function") queueMicrotask(run);
|
|
12
|
+
else void Promise.resolve().then(run);
|
|
13
|
+
};
|
|
14
|
+
function defaults() {
|
|
15
|
+
return {
|
|
16
|
+
consumer: null,
|
|
17
|
+
untracked: false,
|
|
18
|
+
owner: null,
|
|
19
|
+
queue: [],
|
|
20
|
+
scheduled: false,
|
|
21
|
+
flushing: false,
|
|
22
|
+
batchDepth: 0,
|
|
23
|
+
hostSchedule: defaultSchedule,
|
|
24
|
+
onSuspend: null,
|
|
25
|
+
onError: null,
|
|
26
|
+
observer: null
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
var realm = globalThis;
|
|
30
|
+
function backfillState(existing) {
|
|
31
|
+
const fresh = defaults();
|
|
32
|
+
const target = existing;
|
|
33
|
+
for (const field of Object.keys(fresh)) {
|
|
34
|
+
if (!(field in existing)) target[field] = fresh[field];
|
|
35
|
+
}
|
|
36
|
+
return existing;
|
|
37
|
+
}
|
|
38
|
+
function claim() {
|
|
39
|
+
const existing = realm[KEY];
|
|
40
|
+
return existing ? backfillState(existing) : realm[KEY] = defaults();
|
|
41
|
+
}
|
|
42
|
+
var G = claim();
|
|
43
|
+
(realm[VERSIONS_KEY] ??= []).push(VERSION);
|
|
44
|
+
|
|
45
|
+
// src/lib/signal/graph/observe.ts
|
|
46
|
+
function observeReactiveNodes(next) {
|
|
47
|
+
G.observer = next;
|
|
48
|
+
return () => {
|
|
49
|
+
if (G.observer === next) G.observer = null;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
var running = [];
|
|
53
|
+
var origin;
|
|
54
|
+
function reportReactiveNode(report) {
|
|
55
|
+
const observer = G.observer;
|
|
56
|
+
if (observer) {
|
|
57
|
+
observer.created(
|
|
58
|
+
report.parents === void 0 ? { ...report, parents: running, origin: report.origin ?? origin } : report
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function enterReactiveRun(node) {
|
|
63
|
+
if (!G.observer) return false;
|
|
64
|
+
running.push(node);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
function exitReactiveRun() {
|
|
68
|
+
running.pop();
|
|
69
|
+
}
|
|
70
|
+
function reportReactiveRun(node) {
|
|
71
|
+
G.observer?.ran?.(node);
|
|
72
|
+
}
|
|
73
|
+
function reportReactiveWrite(node, by) {
|
|
74
|
+
G.observer?.wrote?.(node, by);
|
|
75
|
+
}
|
|
76
|
+
function reportReactiveDispose(node) {
|
|
77
|
+
G.observer?.disposed?.(node);
|
|
78
|
+
}
|
|
79
|
+
var HOOK = "__FLUIXI_DEVTOOLS_HOOK__";
|
|
80
|
+
var hook = globalThis[HOOK];
|
|
81
|
+
if (typeof hook?.inject === "function") {
|
|
82
|
+
try {
|
|
83
|
+
hook.inject({ observeReactiveNodes, version: VERSION });
|
|
84
|
+
} catch {
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function isObserved() {
|
|
88
|
+
return G.observer != null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/lib/signal/graph/core.ts
|
|
92
|
+
var CLEAN = 0;
|
|
93
|
+
var CHECK = 1;
|
|
94
|
+
var DIRTY = 2;
|
|
95
|
+
var Source = class {
|
|
96
|
+
constructor() {
|
|
97
|
+
/** Computeds, and watched leaves, that read this node. */
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
99
|
+
this.observers = null;
|
|
100
|
+
/** Watchers eagerly observing this node, which is how `runtime.ts` schedules effects. */
|
|
101
|
+
this.watchers = null;
|
|
102
|
+
/** Bumped whenever this node's value actually changes (equality-gated). */
|
|
103
|
+
this.version = 0;
|
|
104
|
+
}
|
|
105
|
+
/** Settle this node so a reader sees a current value/version. Overridden by Computed. */
|
|
106
|
+
updateIfNecessary() {
|
|
107
|
+
}
|
|
108
|
+
/** Link the current consumer (if any) as an observer of this node. */
|
|
109
|
+
track() {
|
|
110
|
+
if (G.untracked) return;
|
|
111
|
+
const consumer = G.consumer;
|
|
112
|
+
if (!consumer) return;
|
|
113
|
+
(this.observers ??= /* @__PURE__ */ new Set()).add(consumer);
|
|
114
|
+
(consumer.sources ??= /* @__PURE__ */ new Set()).add(this);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var State = class extends Source {
|
|
118
|
+
constructor(value, options) {
|
|
119
|
+
super();
|
|
120
|
+
this._value = value;
|
|
121
|
+
this._equals = options?.equals === false ? () => false : options?.equals ?? Object.is;
|
|
122
|
+
}
|
|
123
|
+
get() {
|
|
124
|
+
this.track();
|
|
125
|
+
return this._value;
|
|
126
|
+
}
|
|
127
|
+
set(next) {
|
|
128
|
+
if (this._equals(this._value, next)) return;
|
|
129
|
+
this._value = next;
|
|
130
|
+
this.version++;
|
|
131
|
+
reportReactiveWrite(this, G.consumer);
|
|
132
|
+
if (this.observers) for (const o of [...this.observers]) o.markDirty(DIRTY);
|
|
133
|
+
notifyWatchers(this);
|
|
134
|
+
}
|
|
135
|
+
/** Read without subscribing (peek). */
|
|
136
|
+
peek() {
|
|
137
|
+
return this._value;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
var Computed = class extends Source {
|
|
141
|
+
constructor(fn, options) {
|
|
142
|
+
super();
|
|
143
|
+
this._threw = false;
|
|
144
|
+
this._initialized = false;
|
|
145
|
+
this.state = DIRTY;
|
|
146
|
+
/** Nodes this computation read on its last run. Rebuilt on every run. */
|
|
147
|
+
this.sources = null;
|
|
148
|
+
this._fn = fn;
|
|
149
|
+
this._equals = options?.equals === false ? () => false : options?.equals ?? Object.is;
|
|
150
|
+
}
|
|
151
|
+
get() {
|
|
152
|
+
if (G.consumer === this) {
|
|
153
|
+
throw new Error("Signal.Computed cannot read itself");
|
|
154
|
+
}
|
|
155
|
+
this.track();
|
|
156
|
+
this.updateIfNecessary();
|
|
157
|
+
if (this._threw) throw this._error;
|
|
158
|
+
return this._value;
|
|
159
|
+
}
|
|
160
|
+
/** Settle, if CHECK, verify sources; if DIRTY (or never run), recompute. */
|
|
161
|
+
updateIfNecessary() {
|
|
162
|
+
if (this.state === CHECK) {
|
|
163
|
+
if (this.sources) {
|
|
164
|
+
for (const s of this.sources) {
|
|
165
|
+
s.updateIfNecessary();
|
|
166
|
+
if (this.state === DIRTY) break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (this.state === DIRTY || !this._initialized) this.recompute();
|
|
171
|
+
this.state = CLEAN;
|
|
172
|
+
}
|
|
173
|
+
recompute() {
|
|
174
|
+
if (this.sources) {
|
|
175
|
+
for (const s of this.sources) s.observers?.delete(this);
|
|
176
|
+
this.sources.clear();
|
|
177
|
+
}
|
|
178
|
+
const prevConsumer = G.consumer;
|
|
179
|
+
const prevUntracked = G.untracked;
|
|
180
|
+
const watched = enterReactiveRun(this);
|
|
181
|
+
G.consumer = this;
|
|
182
|
+
G.untracked = false;
|
|
183
|
+
let next;
|
|
184
|
+
let threw = false;
|
|
185
|
+
let err;
|
|
186
|
+
try {
|
|
187
|
+
next = this._fn();
|
|
188
|
+
} catch (e) {
|
|
189
|
+
threw = true;
|
|
190
|
+
err = e;
|
|
191
|
+
next = this._value;
|
|
192
|
+
} finally {
|
|
193
|
+
G.consumer = prevConsumer;
|
|
194
|
+
G.untracked = prevUntracked;
|
|
195
|
+
if (watched) exitReactiveRun();
|
|
196
|
+
}
|
|
197
|
+
const changed = !this._initialized || this._threw !== threw || (threw ? err !== this._error : !this._equals(this._value, next));
|
|
198
|
+
this._initialized = true;
|
|
199
|
+
this._threw = threw;
|
|
200
|
+
this._error = err;
|
|
201
|
+
this._value = next;
|
|
202
|
+
reportReactiveRun(this);
|
|
203
|
+
if (changed) {
|
|
204
|
+
this.version++;
|
|
205
|
+
if (this.observers) for (const o of [...this.observers]) o.markDirty(DIRTY);
|
|
206
|
+
notifyWatchers(this);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Push a maybe-dirty mark. A direct observer of a changed node gets DIRTY; its
|
|
211
|
+
* own observers get CHECK (recurse), stopping where already marked, so the
|
|
212
|
+
* push is O(newly-dirtied) and never recomputes.
|
|
213
|
+
*/
|
|
214
|
+
markDirty(level) {
|
|
215
|
+
if (this.state >= level) return;
|
|
216
|
+
const wasClean = this.state === CLEAN;
|
|
217
|
+
this.state = level;
|
|
218
|
+
if (wasClean) {
|
|
219
|
+
if (this.observers) for (const o of [...this.observers]) o.markDirty(CHECK);
|
|
220
|
+
notifyWatchers(this);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
peek() {
|
|
224
|
+
this.updateIfNecessary();
|
|
225
|
+
if (this._threw) throw this._error;
|
|
226
|
+
return this._value;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
function notifyWatchers(node) {
|
|
230
|
+
if (!node.watchers) return;
|
|
231
|
+
for (const w of [...node.watchers]) w._notify(node);
|
|
232
|
+
}
|
|
233
|
+
var Watcher = class {
|
|
234
|
+
constructor(notify) {
|
|
235
|
+
this._watched = /* @__PURE__ */ new Set();
|
|
236
|
+
this._pending = /* @__PURE__ */ new Set();
|
|
237
|
+
this._notifyFn = notify;
|
|
238
|
+
}
|
|
239
|
+
watch(...nodes) {
|
|
240
|
+
for (const n of nodes) {
|
|
241
|
+
this._watched.add(n);
|
|
242
|
+
(n.watchers ??= /* @__PURE__ */ new Set()).add(this);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
unwatch(...nodes) {
|
|
246
|
+
for (const n of nodes) {
|
|
247
|
+
this._watched.delete(n);
|
|
248
|
+
n.watchers?.delete(this);
|
|
249
|
+
this._pending.delete(n);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** Watched nodes that may have changed since the last getPending(). */
|
|
253
|
+
getPending() {
|
|
254
|
+
const out = [...this._pending];
|
|
255
|
+
this._pending.clear();
|
|
256
|
+
return out;
|
|
257
|
+
}
|
|
258
|
+
/** @internal, a watched node became non-clean. Edge-triggered: notify once. */
|
|
259
|
+
_notify(node) {
|
|
260
|
+
if (!this._watched.has(node) || this._pending.has(node)) return;
|
|
261
|
+
const wasEmpty = this._pending.size === 0;
|
|
262
|
+
this._pending.add(node);
|
|
263
|
+
if (wasEmpty) this._notifyFn();
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
function untrack(fn) {
|
|
267
|
+
if (G.untracked) return fn();
|
|
268
|
+
G.untracked = true;
|
|
269
|
+
try {
|
|
270
|
+
return fn();
|
|
271
|
+
} finally {
|
|
272
|
+
G.untracked = false;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/lib/signal/graph/runtime.ts
|
|
277
|
+
function makeOwner(parent) {
|
|
278
|
+
return { cleanups: null, owned: null, parent, contexts: null };
|
|
279
|
+
}
|
|
280
|
+
function runWithOwner(owner, fn) {
|
|
281
|
+
const prev = G.owner;
|
|
282
|
+
G.owner = owner;
|
|
283
|
+
try {
|
|
284
|
+
return fn();
|
|
285
|
+
} finally {
|
|
286
|
+
G.owner = prev;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function onCleanup(fn) {
|
|
290
|
+
if (!G.owner) return;
|
|
291
|
+
if (G.owner.disposed) {
|
|
292
|
+
fn();
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
(G.owner.cleanups ??= []).push(fn);
|
|
296
|
+
}
|
|
297
|
+
function disposeOwner(owner, final = true) {
|
|
298
|
+
if (owner.disposed) return;
|
|
299
|
+
if (owner.owned) {
|
|
300
|
+
for (let i = owner.owned.length - 1; i >= 0; i--) disposeOwner(owner.owned[i]);
|
|
301
|
+
owner.owned = null;
|
|
302
|
+
}
|
|
303
|
+
if (owner.cleanups) {
|
|
304
|
+
for (let i = owner.cleanups.length - 1; i >= 0; i--) owner.cleanups[i]();
|
|
305
|
+
owner.cleanups = null;
|
|
306
|
+
}
|
|
307
|
+
if (final) owner.disposed = true;
|
|
308
|
+
}
|
|
309
|
+
function setScheduler(fn) {
|
|
310
|
+
G.hostSchedule = fn ?? ((run) => void Promise.resolve().then(run));
|
|
311
|
+
}
|
|
312
|
+
function enqueue(e) {
|
|
313
|
+
if (e.queued) return;
|
|
314
|
+
e.queued = true;
|
|
315
|
+
G.queue.push(e);
|
|
316
|
+
if (!G.scheduled && G.batchDepth === 0) {
|
|
317
|
+
G.scheduled = true;
|
|
318
|
+
G.hostSchedule(flushSync);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function flushSync() {
|
|
322
|
+
G.scheduled = false;
|
|
323
|
+
if (G.flushing) return;
|
|
324
|
+
G.flushing = true;
|
|
325
|
+
let guard = 0;
|
|
326
|
+
try {
|
|
327
|
+
while (G.queue.length) {
|
|
328
|
+
if (++guard > 1e5) throw new Error("[fluixi] effect flush did not settle (cycle?)");
|
|
329
|
+
const batchOf = G.queue;
|
|
330
|
+
G.queue = [];
|
|
331
|
+
for (const e of batchOf) {
|
|
332
|
+
e.queued = false;
|
|
333
|
+
if (!e.disposed) e.run();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
} finally {
|
|
337
|
+
G.flushing = false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function afterWrite() {
|
|
341
|
+
if (G.batchDepth === 0) flushSync();
|
|
342
|
+
}
|
|
343
|
+
function batch(fn) {
|
|
344
|
+
G.batchDepth++;
|
|
345
|
+
try {
|
|
346
|
+
return fn();
|
|
347
|
+
} finally {
|
|
348
|
+
if (--G.batchDepth === 0) flushSync();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
var EffectNode = class {
|
|
352
|
+
constructor(fn, report = false) {
|
|
353
|
+
this.queued = false;
|
|
354
|
+
this.disposed = false;
|
|
355
|
+
this.owner = makeOwner(G.owner);
|
|
356
|
+
if (G.owner) (G.owner.cleanups ??= []).push(() => this.dispose());
|
|
357
|
+
this.computed = new Computed(() => {
|
|
358
|
+
disposeOwner(this.owner, false);
|
|
359
|
+
runWithOwner(this.owner, () => {
|
|
360
|
+
const r = fn();
|
|
361
|
+
if (typeof r === "function") (this.owner.cleanups ??= []).push(r);
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
reportReactiveNode({ kind: "effect", node: this.computed, handle: this, internal: !report });
|
|
365
|
+
this.watcher = new Watcher(() => enqueue(this));
|
|
366
|
+
this.run();
|
|
367
|
+
this.watcher.watch(this.computed);
|
|
368
|
+
}
|
|
369
|
+
run() {
|
|
370
|
+
if (this.disposed) return;
|
|
371
|
+
this.watcher.getPending();
|
|
372
|
+
try {
|
|
373
|
+
this.computed.get();
|
|
374
|
+
} catch (e) {
|
|
375
|
+
if (e && typeof e.then === "function" && G.onSuspend) {
|
|
376
|
+
G.onSuspend(e, this.owner);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (G.onError && G.onError(e, this.owner)) return;
|
|
380
|
+
throw e;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
dispose() {
|
|
384
|
+
if (this.disposed) return;
|
|
385
|
+
this.disposed = true;
|
|
386
|
+
reportReactiveDispose(this.computed);
|
|
387
|
+
this.watcher.unwatch(this.computed);
|
|
388
|
+
disposeOwner(this.owner);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
function effect(fn, report = false) {
|
|
392
|
+
const node = new EffectNode(fn, report);
|
|
393
|
+
return () => node.dispose();
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// src/lib/signal/graph/compat.ts
|
|
397
|
+
setScheduler(() => {
|
|
398
|
+
});
|
|
399
|
+
var $SIGNAL = /* @__PURE__ */ Symbol.for("signal");
|
|
400
|
+
var $NODE = /* @__PURE__ */ Symbol.for("signal-node");
|
|
401
|
+
function createSignal(value, options) {
|
|
402
|
+
const state = new State(value, { equals: options?.equals });
|
|
403
|
+
const read = (() => state.get());
|
|
404
|
+
const write = ((next) => {
|
|
405
|
+
const v = typeof next === "function" ? next(state.peek()) : next;
|
|
406
|
+
state.set(v);
|
|
407
|
+
afterWrite();
|
|
408
|
+
return v;
|
|
409
|
+
});
|
|
410
|
+
Object.defineProperty(read, $SIGNAL, { value: true, enumerable: true });
|
|
411
|
+
Object.defineProperty(read, $NODE, {
|
|
412
|
+
value: {
|
|
413
|
+
get value() {
|
|
414
|
+
return state.peek();
|
|
415
|
+
},
|
|
416
|
+
get version() {
|
|
417
|
+
return state.version;
|
|
418
|
+
}
|
|
419
|
+
},
|
|
420
|
+
enumerable: false
|
|
421
|
+
});
|
|
422
|
+
reportReactiveNode({
|
|
423
|
+
kind: "signal",
|
|
424
|
+
node: state,
|
|
425
|
+
handle: read,
|
|
426
|
+
name: options?.name,
|
|
427
|
+
set: write
|
|
428
|
+
});
|
|
429
|
+
if (isObserved()) onCleanup(() => reportReactiveDispose(state));
|
|
430
|
+
return [read, write];
|
|
431
|
+
}
|
|
432
|
+
function createEffect(fn, value) {
|
|
433
|
+
let prev = value;
|
|
434
|
+
return effect(() => {
|
|
435
|
+
const r = fn(prev);
|
|
436
|
+
prev = r;
|
|
437
|
+
}, true);
|
|
438
|
+
}
|
|
439
|
+
function batch2(fn) {
|
|
440
|
+
return batch(fn);
|
|
441
|
+
}
|
|
442
|
+
function untrack2(fn) {
|
|
443
|
+
return untrack(fn);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// src/lib/signal/graph/ssr-hooks.ts
|
|
447
|
+
var _resourceTracker = null;
|
|
448
|
+
function trackResourcePromise(p) {
|
|
449
|
+
if (_resourceTracker) _resourceTracker(p);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// src/lib/signal/cached-resource.ts
|
|
453
|
+
var ttlCache = /* @__PURE__ */ new Map();
|
|
454
|
+
var inflight = /* @__PURE__ */ new Map();
|
|
455
|
+
var _autoId = 0;
|
|
456
|
+
var $SIGNAL2 = /* @__PURE__ */ Symbol.for("signal");
|
|
457
|
+
var $RESOURCE = /* @__PURE__ */ Symbol.for("resource");
|
|
458
|
+
function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
459
|
+
let sourceFn;
|
|
460
|
+
let fetchFn;
|
|
461
|
+
let options;
|
|
462
|
+
if (typeof fetcherOrOptions === "function") {
|
|
463
|
+
sourceFn = sourceOrFetcher;
|
|
464
|
+
fetchFn = fetcherOrOptions;
|
|
465
|
+
options = opts;
|
|
466
|
+
} else {
|
|
467
|
+
sourceFn = () => true;
|
|
468
|
+
fetchFn = sourceOrFetcher;
|
|
469
|
+
options = fetcherOrOptions ?? opts;
|
|
470
|
+
}
|
|
471
|
+
const {
|
|
472
|
+
ttl = Infinity,
|
|
473
|
+
name = `_auto_${_autoId++}`,
|
|
474
|
+
staleWhileRevalidate = true
|
|
475
|
+
} = options;
|
|
476
|
+
const ns = name;
|
|
477
|
+
const buildKey = (key) => `${ns}:${JSON.stringify(key)}`;
|
|
478
|
+
const isFresh = (entry) => ttl === Infinity || Date.now() - entry.timestamp <= ttl;
|
|
479
|
+
const [value, setValue] = createSignal(void 0, { equals: false });
|
|
480
|
+
const [resourceState, setResourceState] = createSignal("unresolved", { equals: false });
|
|
481
|
+
const [resourceError, setResourceError] = createSignal(void 0, { equals: false });
|
|
482
|
+
const [pendingP, setPendingP] = createSignal(void 0, { equals: false });
|
|
483
|
+
const [isStale, setIsStale] = createSignal(false);
|
|
484
|
+
const [tick, setTick] = createSignal(0, { equals: false });
|
|
485
|
+
const UNSET = /* @__PURE__ */ Symbol("unset");
|
|
486
|
+
let prevKey = UNSET;
|
|
487
|
+
let flagRefetch = false;
|
|
488
|
+
let flagInvalidate = false;
|
|
489
|
+
function doFetch(key, ck, background, isCancelled) {
|
|
490
|
+
if (inflight.has(ck)) {
|
|
491
|
+
inflight.get(ck).then(
|
|
492
|
+
(v) => {
|
|
493
|
+
if (!isCancelled()) settle(v, background);
|
|
494
|
+
},
|
|
495
|
+
(e) => {
|
|
496
|
+
if (!isCancelled() && !background) fail(e);
|
|
497
|
+
}
|
|
498
|
+
);
|
|
499
|
+
if (!background) setPendingP(inflight.get(ck));
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
const promise = Promise.resolve(fetchFn(key)).then(
|
|
503
|
+
(v) => {
|
|
504
|
+
ttlCache.set(ck, { value: v, timestamp: Date.now() });
|
|
505
|
+
inflight.delete(ck);
|
|
506
|
+
return v;
|
|
507
|
+
},
|
|
508
|
+
(e) => {
|
|
509
|
+
inflight.delete(ck);
|
|
510
|
+
throw e;
|
|
511
|
+
}
|
|
512
|
+
);
|
|
513
|
+
inflight.set(ck, promise);
|
|
514
|
+
if (!background) {
|
|
515
|
+
trackResourcePromise(promise);
|
|
516
|
+
setPendingP(promise);
|
|
517
|
+
}
|
|
518
|
+
promise.then(
|
|
519
|
+
(v) => {
|
|
520
|
+
if (!isCancelled()) settle(v, background);
|
|
521
|
+
},
|
|
522
|
+
(e) => {
|
|
523
|
+
if (!isCancelled() && !background) fail(e);
|
|
524
|
+
}
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
function settle(v, background) {
|
|
528
|
+
batch2(() => {
|
|
529
|
+
setValue(() => v);
|
|
530
|
+
setResourceState("ready");
|
|
531
|
+
setIsStale(false);
|
|
532
|
+
setPendingP(void 0);
|
|
533
|
+
setResourceError(void 0);
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
function fail(e) {
|
|
537
|
+
batch2(() => {
|
|
538
|
+
setResourceError(e);
|
|
539
|
+
setResourceState("errored");
|
|
540
|
+
setPendingP(void 0);
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
createEffect(() => {
|
|
544
|
+
const key = sourceFn();
|
|
545
|
+
tick();
|
|
546
|
+
if (key === false || key === null || key === void 0) {
|
|
547
|
+
prevKey = UNSET;
|
|
548
|
+
batch2(() => {
|
|
549
|
+
setResourceState("unresolved");
|
|
550
|
+
setPendingP(void 0);
|
|
551
|
+
setResourceError(void 0);
|
|
552
|
+
});
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
const doRefetch = flagRefetch;
|
|
556
|
+
const doInvalidate = flagInvalidate;
|
|
557
|
+
flagRefetch = false;
|
|
558
|
+
flagInvalidate = false;
|
|
559
|
+
const ck = buildKey(key);
|
|
560
|
+
if (doInvalidate) ttlCache.delete(ck);
|
|
561
|
+
const keyChanged = prevKey === UNSET || key !== prevKey;
|
|
562
|
+
prevKey = key;
|
|
563
|
+
let cancelled = false;
|
|
564
|
+
onCleanup(() => {
|
|
565
|
+
cancelled = true;
|
|
566
|
+
});
|
|
567
|
+
const cached = ttlCache.get(ck);
|
|
568
|
+
if (cached && isFresh(cached) && !doRefetch && !doInvalidate) {
|
|
569
|
+
const cur2 = untrack2(resourceState);
|
|
570
|
+
if (cur2 !== "ready" || untrack2(value) !== cached.value) {
|
|
571
|
+
batch2(() => {
|
|
572
|
+
setValue(() => cached.value);
|
|
573
|
+
setResourceState("ready");
|
|
574
|
+
setIsStale(false);
|
|
575
|
+
setResourceError(void 0);
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (cached && staleWhileRevalidate && !doInvalidate) {
|
|
581
|
+
batch2(() => {
|
|
582
|
+
setValue(() => cached.value);
|
|
583
|
+
setResourceState("ready");
|
|
584
|
+
setIsStale(true);
|
|
585
|
+
setResourceError(void 0);
|
|
586
|
+
setPendingP(void 0);
|
|
587
|
+
});
|
|
588
|
+
doFetch(key, ck, true, () => cancelled);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
const cur = untrack2(resourceState);
|
|
592
|
+
const isReload = cur === "ready" || cur === "refreshing";
|
|
593
|
+
batch2(() => {
|
|
594
|
+
setResourceState(isReload ? "refreshing" : "pending");
|
|
595
|
+
setResourceError(void 0);
|
|
596
|
+
setIsStale(false);
|
|
597
|
+
});
|
|
598
|
+
doFetch(key, ck, false, () => cancelled);
|
|
599
|
+
});
|
|
600
|
+
const resource = (() => {
|
|
601
|
+
const e = resourceError();
|
|
602
|
+
if (e) throw e;
|
|
603
|
+
const s = resourceState();
|
|
604
|
+
if (s === "pending") {
|
|
605
|
+
const p = pendingP();
|
|
606
|
+
if (p) throw p;
|
|
607
|
+
}
|
|
608
|
+
return value();
|
|
609
|
+
});
|
|
610
|
+
Object.defineProperty(resource, $RESOURCE, { value: true, enumerable: true });
|
|
611
|
+
Object.defineProperty(resource, $SIGNAL2, { value: true, enumerable: true });
|
|
612
|
+
Object.defineProperties(resource, {
|
|
613
|
+
state: { get: resourceState },
|
|
614
|
+
loading: { get: () => {
|
|
615
|
+
const s = resourceState();
|
|
616
|
+
return s === "pending" || s === "refreshing";
|
|
617
|
+
} },
|
|
618
|
+
error: { get: resourceError },
|
|
619
|
+
latest: { get: value },
|
|
620
|
+
stale: { get: isStale }
|
|
621
|
+
});
|
|
622
|
+
const refetch = () => {
|
|
623
|
+
flagRefetch = true;
|
|
624
|
+
setTick((v) => v + 1);
|
|
625
|
+
};
|
|
626
|
+
const invalidate = () => {
|
|
627
|
+
const key = untrack2(sourceFn);
|
|
628
|
+
if (key !== null && key !== void 0 && key !== false) {
|
|
629
|
+
ttlCache.delete(buildKey(key));
|
|
630
|
+
}
|
|
631
|
+
flagInvalidate = true;
|
|
632
|
+
setTick((v) => v + 1);
|
|
633
|
+
};
|
|
634
|
+
return [resource, { mutate: setValue, refetch, invalidate }];
|
|
635
|
+
}
|
|
636
|
+
function clearCachedResourceNamespace(name) {
|
|
637
|
+
const prefix = `${name}:`;
|
|
638
|
+
for (const key of ttlCache.keys()) {
|
|
639
|
+
if (key.startsWith(prefix)) ttlCache.delete(key);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function clearAllCachedResources() {
|
|
643
|
+
ttlCache.clear();
|
|
644
|
+
inflight.clear();
|
|
645
|
+
}
|
|
646
|
+
export {
|
|
647
|
+
clearAllCachedResources,
|
|
648
|
+
clearCachedResourceNamespace,
|
|
649
|
+
createCachedResource
|
|
650
|
+
};
|