@fluixi/reactive 1.0.0-alpha.82 → 1.0.0-alpha.84
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/README.md +4 -0
- package/dist/cdn/reactive.cjs +2673 -1
- package/dist/cdn/reactive.global.js +2 -1
- package/dist/cdn/reactive.mjs +2657 -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 +2673 -1
- package/dist/index.mjs +2657 -1
- package/dist/lib/index.cjs +2674 -1
- package/dist/lib/index.js +1 -1
- package/dist/lib/index.mjs +2659 -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 +51 -32
- 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 +45 -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 -0
- package/dist/lib/signal/graph/observe.d.ts +141 -0
- package/dist/lib/signal/graph/observe.d.ts.map +1 -0
- package/dist/lib/signal/graph/observe.js +149 -0
- package/dist/lib/signal/graph/observe.mjs +140 -0
- 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 +9 -9
- package/dist/lib/signal/graph/runtime.d.ts.map +1 -1
- package/dist/lib/signal/graph/runtime.js +56 -37
- 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 +43 -10
- package/dist/lib/signal/graph/state.d.ts.map +1 -1
- package/dist/lib/signal/graph/state.js +83 -21
- 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 +5 -2
- package/dist/lib/signal/index.d.ts.map +1 -1
- package/dist/lib/signal/index.js +8 -3
- 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 +10 -10
- 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 +612 -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 +596 -1
- package/dist/lib/store/index.cjs +691 -1
- package/dist/lib/store/index.js +1 -1
- package/dist/lib/store/index.mjs +676 -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 +658 -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 +294 -11
- package/dist/lib/store/store.mjs +644 -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 Re(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(t){this._closed=!1;this._parentOrParents=null;this._subscriptions=null;t&&(this._subscriptions=[t])}get closed(){return this._closed}unsubscribe(){if(this._closed)return;this._closed=!0;let{_parentOrParents:t,_subscriptions:n}=this;if(t instanceof e)t.remove(this);else if(t!==null)for(let o of t)o.remove(this);if(n){for(let o of n)if(ce(o)&&!(o instanceof e))try{o()}catch(r){console.error("Error in unsubscribe function:",r)}else if(Re(o)&&typeof o.unsubscribe=="function")try{o.unsubscribe()}catch(r){console.error("Error unsubscribing child:",r)}this._subscriptions=null}}add(t){if(!t||t===this)return this;if(t.closed)return this;let{_closed:n,_subscriptions:o}=this;if(n){if(ce(t))try{t()}catch(r){console.error("Error in unsubscribe function:",r)}else if(t.unsubscribe)try{t.unsubscribe()}catch(r){console.error("Error unsubscribing:",r)}return this}return o||(this._subscriptions=[]),this._subscriptions.push(t),t instanceof e&&t._addParent(this),this}remove(t){let{_subscriptions:n}=this;if(!n)return;let o=n.indexOf(t);o!==-1&&(n.splice(o,1),t._removeParent(this))}_addParent(t){let{_parentOrParents:n}=this;n?Array.isArray(n)?n.push(t):this._parentOrParents=[n,t]:this._parentOrParents=t}_removeParent(t){let{_parentOrParents:n}=this;if(n===t)this._parentOrParents=null;else if(Array.isArray(n)){let o=n.indexOf(t);o!==-1&&(n.splice(o,1),n.length===1&&(this._parentOrParents=n[0]))}}};var K=class extends j{constructor(n,o,r){super();this.isStopped=!1;let s;n?typeof n=="object"?s=n:s={next:n,error:o,complete:r}:s={},this.destination=s}next(n){if(!this.isStopped&&this.destination.next)try{this.destination.next(n)}catch(o){this.error(o)}}error(n){if(!this.isStopped){if(this.isStopped=!0,this.destination.error)try{this.destination.error(n)}catch(o){throw o}else throw n;this.unsubscribe()}}complete(){if(!this.isStopped){if(this.isStopped=!0,this.destination.complete)try{this.destination.complete()}catch(n){this.error(n);return}this.unsubscribe()}}unsubscribe(){this.closed||(this.isStopped=!0,super.unsubscribe())}},de=class extends K{constructor(n,o,r,s){super(n,o,r);this._isUnsubscribing=!1;this._parentSubscriber=null;this._parentSubscriber=s||null}next(n){if(!this.isStopped&&!this._isUnsubscribing)try{super.next(n)}catch(o){this.error(o)}}error(n){if(!this.isStopped&&!this._isUnsubscribing){this._isUnsubscribing=!0;try{super.error(n)}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(t){if(super(),t)this._subscribe=t;else throw new TypeError("Observable constructor requires a subscribe function")}subscribe(t,n,o){let r;t instanceof K?r=t:r=new de(t,n,o);let{_subscribe:s}=this;try{let a=s.call(this,r);a&&(typeof a=="function"||typeof a.unsubscribe=="function")&&r.add(a)}catch(a){r.error(a)}return r}pipe(...t){return t.length===0?this:t.reduce((n,o)=>o(n),this)}toPromise(){return new Promise((t,n)=>{let o;this.subscribe({next:r=>o=r,error:n,complete:()=>t(o)})})}};var $e=typeof window>"u"?!1:window.__FLUIXI_PROD__===!0,X=typeof process>"u"?$e:!1,We=Symbol.for("signal");function pe(e){return typeof e=="function"&&e[We]===!0}var ze="__fluixi_signal_next_state__",He=e=>{typeof queueMicrotask=="function"?queueMicrotask(e):Promise.resolve().then(e)},h=globalThis[ze]??={consumer:null,untracked:!1,owner:null,queue:[],scheduled:!1,flushing:!1,batchDepth:0,hostSchedule:He,onSuspend:null,onError:null};var Ce=0,_e=1,J=2,he=class{constructor(){this.observers=null;this.watchers=null;this.version=0}updateIfNecessary(){}track(){if(h.untracked)return;let t=h.consumer;t&&((this.observers??=new Set).add(t),(t.sources??=new Set).add(this))}};var Q=class extends he{constructor(n,o){super();this._threw=!1;this._initialized=!1;this.state=J;this.sources=null;this._fn=n,this._equals=o?.equals===!1?()=>!1:o?.equals??Object.is}get(){if(h.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===_e&&this.sources){for(let n of this.sources)if(n.updateIfNecessary(),this.state===J)break}(this.state===J||!this._initialized)&&this.recompute(),this.state=Ce}recompute(){if(this.sources){for(let d of this.sources)d.observers?.delete(this);this.sources.clear()}let n=h.consumer,o=h.untracked;h.consumer=this,h.untracked=!1;let r,s=!1,a;try{r=this._fn()}catch(d){s=!0,a=d,r=this._value}finally{h.consumer=n,h.untracked=o}let f=!this._initialized||this._threw!==s||(s?a!==this._error:!this._equals(this._value,r));if(this._initialized=!0,this._threw=s,this._error=a,this._value=r,f){if(this.version++,this.observers)for(let d of[...this.observers])d.markDirty(J);Ae(this)}}markDirty(n){if(this.state>=n)return;let o=this.state===Ce;if(this.state=n,o){if(this.observers)for(let r of[...this.observers])r.markDirty(_e);Ae(this)}}peek(){if(this.updateIfNecessary(),this._threw)throw this._error;return this._value}};function Ae(e){if(e.watchers)for(let t of[...e.watchers])t._notify(e)}function Pe(e){return h.onSuspend?(h.onSuspend(e,h.owner),!0):!1}function Ee(){return h.owner}function Fe(e,t){let n=h.owner;h.owner=e;try{return t()}finally{h.owner=n}}function ke(e){h.hostSchedule=e??(t=>{Promise.resolve().then(t)})}function Ke(e){return!!e&&typeof e.then=="function"}ke(()=>{});var Ye=Symbol.for("signal"),Xe=Symbol.for("memo");function Z(e,t,n){let o=t,r=Ee(),s=new Q(()=>Fe(r,()=>{try{return o=e(o)}catch(f){if(Ke(f))return Pe(f),o;throw f}}),{equals:n?.equals}),a=(()=>s.get());return Object.defineProperty(a,Ye,{value:!0,enumerable:!0}),Object.defineProperty(a,Xe,{value:!0,enumerable:!0}),a}function ee(e){if(typeof e=="function"&&!e.length){let t=e();return pe(t)?t:ee(t)}if(Array.isArray(e)){let t=[];for(let n=0;n<e.length;n++){let o=ee(e[n]);Array.isArray(o)?t.push.apply(t,o):t.push(o)}return t}return e}function De(e){let t=Z(e);return X?Z(()=>ee(t()),void 0,{name:"children"}):Z(()=>ee(t()))}var qe=null;function ve(e){qe&&qe(e)}var Je=Symbol.for("signal");var Ne=Symbol("fluixi-proxy"),Qe=Symbol.for("signal-node"),G=0,ye=1,oe=2,I=3,c=null,v=null,i=null,ne=null,re=0,Ze=!1,Te=!1,N=0,et=1e6,O=new WeakMap,te=null;var tt=new Map,nt=0;function rt(){return`node_${++nt}_${Date.now()}`}function ot(e){X&&tt.set(e.id,new WeakRef(e))}function st(e,t,n){let o={id:rt(),type:e,value:t,version:0,equals:n?.equals,name:n?.name,created:Date.now(),updated:Date.now()};return ot(o),o}function it(e,t){return e===t}function Ue(e){throw typeof console<"u"&&console.error("Reactive error:",e),e}function ut(e){if(i)i.indexOf(e)===-1&&i.push(e);else if(te&&te.running&&!e.pure)te.effects.indexOf(e)===-1&&te.effects.push(e);else if(!e.pure||e.computed)me(e);else if(e.pure&&!e.computed&&e.accessor){let t=v,n=i;v=null,i=[],e.accessor(),v=t;let o=i;if(i=n,o.length>0){let r=i;se(o),i=r}}}function Ie(e,t){for(let n=0;n<e.length;n++)t(e[n])}function C(e,t){let n=e;typeof n=="object"&&n&&Ne in n&&delete n[Ne];let o=st("signal",n,{equals:t?.equals===!1?void 0:t?.equals,name:t?.name}),r=[a,f];O.set(r,new Set);let s;function a(){let d=v;if(d){d.sources?d.sources.indexOf(r)===-1&&d.sources.push(r):d.sources=[r];let b=O.get(r);b&&!b.has(d)&&b.add(d)}return n}function f(d){let b=typeof d=="function"?d(n):d,B=o.equals||it;if(t?.equals===!1||!B(n,b)){n=b,o.value=b,o.version++,o.updated=Date.now();let R=O.get(r);if(R&&R.size>0){let F=function(l,ie=!1){let m=l.state;if(!(m===I&&l===v)){if(ie&&!l.pure){m===G&&(l.state=ye,Ze&&(i!==null&&i.indexOf(l)===-1?i.push(l):x.push(l)));return}if(!S.has(l)&&(S.add(l),l.state=oe,m===G||m===ye?x.push(l):!l.pure&&i!==null&&i.indexOf(l)===-1&&i.push(l),l.pure&&l.accessor)){let $=O.get(l.accessor);$&&Ie(Array.from($),k=>F(k,!0))}}};var V=F;let E=Array.from(R),x=[],S=new Set;Ie(E,F);for(let l of x)ut(l)}}}return a.toObservable=()=>new Y(d=>{let b=!0;return be(()=>{let V=a();b?b=!1:d.next(V)})}),Object.defineProperty(a,Je,{value:!0,enumerable:!0}),Object.defineProperty(a,Qe,{value:o,enumerable:!1}),r}function be(e,t,n){let o={fn:e,state:oe,sources:null,sourceSlots:null,owned:null,cleanups:null,owner:c,contexts:null,pure:!1,updatedAt:null};c&&(c.owned?c.owned.push(o):c.owned=[o]);let r=w&&M(w);return r&&(o.suspense=r),Le(o,t),()=>U(o)}function Le(e,t){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 n=v,o=c;if(v=e,c=e,re++,re>et)throw re=0,v=n,c=o,new Error("Potential infinite loop detected. Exceeded maximum iterations.");try{e.state=I;let r=e.fn(t);typeof r=="function"&&(e.cleanups?e.cleanups.push(r):e.cleanups=[r]),e.state===I&&(e.state=G)}catch(r){if(fe(r)){let s=w&&M(w);s&&(s.increment(),r.finally(()=>s.decrement()),e.suspense&&s.effects.push(e));return}Ue(r)}finally{v=n,c=o}}function me(e){if(!(e.disposed||!e.fn||e.state===G)&&(e.state===oe||e.state===ye||e.state===I)){if(e.cleanups){for(let t=0;t<e.cleanups.length;t++)e.cleanups[t]();e.cleanups=null}if(e.owned){for(let t=0;t<e.owned.length;t++)U(e.owned[t]);e.owned=null}e.renderEffect?Be(e):Le(e,e.value)}}function U(e){if(e.disposed=!0,e.sources)for(let t of e.sources){let n=O.get(t);n&&n.delete(e)}if(e.cleanups)for(let t=0;t<e.cleanups.length;t++)e.cleanups[t]();if(e.owned)for(let t=0;t<e.owned.length;t++)U(e.owned[t]);if(e.owner&&e.owner.owned){let t=e.owner.owned.indexOf(e);t!==-1&&e.owner.owned.splice(t,1)}}function se(e){let t=[],n=[];for(let r of e)r.pure&&!r.computed&&r.accessor?t.indexOf(r)===-1&&t.push(r):(r.computed||!r.pure)&&n.indexOf(r)===-1&&n.push(r);i=[];let o=0;for(;o<t.length;){let r=t[o++],s=O.get(r.accessor);if(s&&s.size>0){let f=v;v=null,r.accessor(),v=f}let a=i.splice(0);for(let f of a)f.pure&&!f.computed&&f.accessor?t.indexOf(f)===-1&&t.push(f):(f.computed||!f.pure)&&n.indexOf(f)===-1&&n.push(f)}i=null;for(let r of n)(r.computed||!r.pure)&&me(r)}function _(e){if(i&&N>0)return e();if(i&&N===0){let t=i,n=[];i=n,N++;let o;try{o=e()}finally{N--,i=t}let r=i;return se(n),i=r,o}N++,i=[],ne=[];try{return e()}finally{N--,at()}}function at(){if(!Te){Te=!0,re=0;try{for(;i&&i.length>0;){let e=i;i=null,se(e)}if(i=null,ne){let e=ne;ne=null;for(let t=0;t<e.length;t++)me(e[t])}}finally{Te=!1}}}function L(e){let t=v;v=null;try{return e()}finally{v=t}}function Me(e){c&&(c.cleanups?c.cleanups.push(e):c.cleanups=[e])}function lt(e){let t=Symbol("context"),n=ct(t);return{id:t,Provider:n,defaultValue:e}}function ct(e){return t=>{let n=c;for(;n;)n.contexts||(n.contexts=new Map),n.contexts.has(e)||n.contexts.set(e,t.value),n=n.owner;let o;return ft(()=>{o=L(()=>{let r=c;return r&&(r.contexts||(r.contexts=new Map),r.contexts.set(e,t.value)),De(()=>t.children)})}),o}}function M(e){let t=c;for(;t;){if(t.contexts&&t.contexts.has(e.id))return t.contexts.get(e.id);t=t.owner}if(e)return e.defaultValue}function ft(e,t,n){let o={fn:e,state:oe,sources:null,sourceSlots:null,value:t,owned:null,cleanups:null,owner:c,contexts:null,pure:!1,updatedAt:null,renderEffect:!0};c&&(c.owned?c.owned.push(o):c.owned=[o]);let r=w&&M(w);return r&&(o.suspense=r),Be(o),()=>U(o)}function Be(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 t=v,n=c,o=i;v=e,c=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=G)}catch(r){if(fe(r)){let s=w&&M(w);s&&(s.increment(),r.finally(()=>s.decrement()),e.suspense&&s.effects.push(e));return}Ue(r)}finally{v=t,c=n;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=lt())}var P=new Map,A=new Map,dt=0,pt=Symbol.for("signal"),ht=Symbol.for("resource");function cn(e,t,n={}){let o,r,s;typeof t=="function"?(o=e,r=t,s=n):(o=()=>!0,r=e,s=t??n);let{ttl:a=1/0,name:f=`_auto_${dt++}`,staleWhileRevalidate:d=!0}=s,b=f,B=u=>`${b}:${JSON.stringify(u)}`,V=u=>a===1/0||Date.now()-u.timestamp<=a,[R,E]=C(void 0,{equals:!1}),[x,S]=C("unresolved",{equals:!1}),[F,l]=C(void 0,{equals:!1}),[ie,m]=C(void 0,{equals:!1}),[$,k]=C(!1),[Ge,we]=C(0,{equals:!1}),ue=Symbol("unset"),W=ue,ae=!1,le=!1;function xe(u,T,y,g){if(A.has(T)){A.get(T).then(p=>{g()||Se(p,y)},p=>{!g()&&!y&&ge(p)}),y||m(A.get(T));return}let D=Promise.resolve(r(u)).then(p=>(P.set(T,{value:p,timestamp:Date.now()}),A.delete(T),p),p=>{throw A.delete(T),p});if(A.set(T,D),!y){ve(D),m(D);let p=z&&M(z);p&&(p.increment(),D.finally(()=>p.decrement()))}D.then(p=>{g()||Se(p,y)},p=>{!g()&&!y&&ge(p)})}function Se(u,T){_(()=>{E(()=>u),S("ready"),k(!1),m(void 0),l(void 0)})}function ge(u){_(()=>{l(u),S("errored"),m(void 0)})}let z;be(()=>{z===void 0&&(z=je());let u=o();if(Ge(),u===!1||u===null||u===void 0){W=ue,_(()=>{S("unresolved"),m(void 0),l(void 0)});return}let T=ae,y=le;ae=!1,le=!1;let g=B(u);y&&P.delete(g);let D=W===ue||u!==W;W=u;let p=!1;Me(()=>{p=!0});let q=P.get(g);if(q&&V(q)&&!T&&!y){(L(x)!=="ready"||L(R)!==q.value)&&_(()=>{E(()=>q.value),S("ready"),k(!1),l(void 0)});return}if(q&&d&&!y){_(()=>{E(()=>q.value),S("ready"),k(!0),l(void 0),m(void 0)}),xe(u,g,!0,()=>p);return}let Oe=L(x),Ve=Oe==="ready"||Oe==="refreshing";_(()=>{S(Ve?"refreshing":"pending"),l(void 0),k(!1)}),xe(u,g,!1,()=>p)});let H=(()=>{let u=F();if(u)throw u;if(x()==="pending"){let y=ie();if(y)throw y}return R()});return Object.defineProperty(H,ht,{value:!0,enumerable:!0}),Object.defineProperty(H,pt,{value:!0,enumerable:!0}),Object.defineProperties(H,{state:{get:x},loading:{get:()=>{let u=x();return u==="pending"||u==="refreshing"}},error:{get:F},latest:{get:R},stale:{get:$}}),[H,{mutate:E,refetch:()=>{ae=!0,we(u=>u+1)},invalidate:()=>{let u=L(o);u!=null&&u!==!1&&P.delete(B(u)),le=!0,we(T=>T+1)}}]}function fn(e){let t=`${e}:`;for(let n of P.keys())n.startsWith(t)&&P.delete(n)}function dn(){P.clear(),A.clear()}export{dn as clearAllCachedResources,fn as clearCachedResourceNamespace,cn as createCachedResource};
|
|
1
|
+
/*! @fluixi/reactive v1.0.0-alpha.84 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
|
|
3
|
+
// src/version.generated.ts
|
|
4
|
+
var VERSION = "1.0.0-alpha.84";
|
|
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
|
+
};
|