@fluixi/reactive 1.0.0-alpha.83 → 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/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 +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 +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 +148 -5
- 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,990 @@
|
|
|
1
|
-
var $="__fluixi_signal_next_state__",X=n=>{typeof queueMicrotask=="function"?queueMicrotask(n):Promise.resolve().then(n)},i=globalThis[$]??={consumer:null,untracked:!1,owner:null,queue:[],scheduled:!1,flushing:!1,batchDepth:0,hostSchedule:X,onSuspend:null,onError:null,observer:null};var E="1.0.0-alpha.83";function J(n){return i.observer=n,()=>{i.observer===n&&(i.observer=null)}}var x=[],Q;function f(n){let e=i.observer;e&&e.created(n.parents===void 0?{...n,parents:x,origin:n.origin??Q}:n)}function q(n){return i.observer?(x.push(n),!0):!1}function M(){x.pop()}function B(n){i.observer?.ran?.(n)}function I(n,e){i.observer?.wrote?.(n,e)}function v(n){i.observer?.disposed?.(n)}var Z="__FLUIXI_DEVTOOLS_HOOK__",U=globalThis[Z];if(typeof U?.inject=="function")try{U.inject({observeReactiveNodes:J,version:E})}catch{}function g(){return i.observer!=null}var N=0,K=1,b=2,T=class{constructor(){this.observers=null;this.watchers=null;this.version=0}updateIfNecessary(){}track(){if(i.untracked)return;let e=i.consumer;e&&((this.observers??=new Set).add(e),(e.sources??=new Set).add(this))}},w=class extends T{constructor(e,r){super(),this._value=e,this._equals=r?.equals===!1?()=>!1:r?.equals??Object.is}get(){return this.track(),this._value}set(e){if(!this._equals(this._value,e)){if(this._value=e,this.version++,I(this,i.consumer),this.observers)for(let r of[...this.observers])r.markDirty(b);S(this)}}peek(){return this._value}},p=class extends T{constructor(r,t){super();this._threw=!1;this._initialized=!1;this.state=b;this.sources=null;this._fn=r,this._equals=t?.equals===!1?()=>!1:t?.equals??Object.is}get(){if(i.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===K&&this.sources){for(let r of this.sources)if(r.updateIfNecessary(),this.state===b)break}(this.state===b||!this._initialized)&&this.recompute(),this.state=N}recompute(){if(this.sources){for(let d of this.sources)d.observers?.delete(this);this.sources.clear()}let r=i.consumer,t=i.untracked,o=q(this);i.consumer=this,i.untracked=!1;let s,u=!1,c;try{s=this._fn()}catch(d){u=!0,c=d,s=this._value}finally{i.consumer=r,i.untracked=t,o&&M()}let l=!this._initialized||this._threw!==u||(u?c!==this._error:!this._equals(this._value,s));if(this._initialized=!0,this._threw=u,this._error=c,this._value=s,B(this),l){if(this.version++,this.observers)for(let d of[...this.observers])d.markDirty(b);S(this)}}markDirty(r){if(this.state>=r)return;let t=this.state===N;if(this.state=r,t){if(this.observers)for(let o of[...this.observers])o.markDirty(K);S(this)}}peek(){if(this.updateIfNecessary(),this._threw)throw this._error;return this._value}};function S(n){if(n.watchers)for(let e of[...n.watchers])e._notify(n)}var m=class{constructor(e){this._watched=new Set;this._pending=new Set;this._notifyFn=e}watch(...e){for(let r of e)this._watched.add(r),(r.watchers??=new Set).add(this)}unwatch(...e){for(let r of e)this._watched.delete(r),r.watchers?.delete(this),this._pending.delete(r)}getPending(){let e=[...this._pending];return this._pending.clear(),e}_notify(e){if(!this._watched.has(e)||this._pending.has(e))return;let r=this._pending.size===0;this._pending.add(e),r&&this._notifyFn()}};function ee(n){return{cleanups:null,owned:null,parent:n,contexts:null}}function j(n){return i.onSuspend?(i.onSuspend(n,i.owner),!0):!1}function G(){return i.owner}function _(n,e){let r=i.owner;i.owner=n;try{return e()}finally{i.owner=r}}function R(n){if(i.owner){if(i.owner.disposed){n();return}(i.owner.cleanups??=[]).push(n)}}function k(n,e=!0){if(!n.disposed){if(n.owned){for(let r=n.owned.length-1;r>=0;r--)k(n.owned[r]);n.owned=null}if(n.cleanups){for(let r=n.cleanups.length-1;r>=0;r--)n.cleanups[r]();n.cleanups=null}e&&(n.disposed=!0)}}function V(n){i.hostSchedule=n??(e=>{Promise.resolve().then(e)})}function re(n){n.queued||(n.queued=!0,i.queue.push(n),!i.scheduled&&i.batchDepth===0&&(i.scheduled=!0,i.hostSchedule(F)))}function F(){if(i.scheduled=!1,i.flushing)return;i.flushing=!0;let n=0;try{for(;i.queue.length;){if(++n>1e5)throw new Error("[fluixi] effect flush did not settle (cycle?)");let e=i.queue;i.queue=[];for(let r of e)r.queued=!1,r.disposed||r.run()}}finally{i.flushing=!1}}function H(){i.batchDepth===0&&F()}var A=class{constructor(e,r=!1){this.queued=!1;this.disposed=!1;this.owner=ee(i.owner),i.owner&&(i.owner.cleanups??=[]).push(()=>this.dispose()),this.computed=new p(()=>{k(this.owner,!1),_(this.owner,()=>{let t=e();typeof t=="function"&&(this.owner.cleanups??=[]).push(t)})}),f({kind:"effect",node:this.computed,handle:this,internal:!r}),this.watcher=new m(()=>re(this)),this.run(),this.watcher.watch(this.computed)}run(){if(!this.disposed){this.watcher.getPending();try{this.computed.get()}catch(e){if(e&&typeof e.then=="function"&&i.onSuspend){i.onSuspend(e,this.owner);return}if(i.onError&&i.onError(e,this.owner))return;throw e}}}dispose(){this.disposed||(this.disposed=!0,v(this.computed),this.watcher.unwatch(this.computed),k(this.owner))}};function L(n,e=!1){let r=new A(n,e);return()=>r.dispose()}function ne(n){return!!n&&typeof n.then=="function"}V(()=>{});var W=Symbol.for("signal"),te=Symbol.for("memo"),oe=Symbol.for("signal-node");function C(n,e){let r=new w(n,{equals:e?.equals}),t=(()=>r.get()),o=(s=>{let u=typeof s=="function"?s(r.peek()):s;return r.set(u),H(),u});return Object.defineProperty(t,W,{value:!0,enumerable:!0}),Object.defineProperty(t,oe,{value:{get value(){return r.peek()},get version(){return r.version}},enumerable:!1}),f({kind:"signal",node:r,handle:t,name:e?.name,set:o}),g()&&R(()=>v(r)),[t,o]}function a(n,e,r){let t=e,o=G(),s=new p(()=>_(o,()=>{try{return t=n(t)}catch(c){if(ne(c))return j(c),t;throw c}}),{equals:r?.equals}),u=(()=>s.get());return Object.defineProperty(u,W,{value:!0,enumerable:!0}),Object.defineProperty(u,te,{value:!0,enumerable:!0}),f({kind:"memo",node:s,handle:u,name:r?.name}),g()&&R(()=>v(s)),u}function z(n,e){let r=e;return L(()=>{r=n(r)},!0)}function D(n){return typeof n=="function"&&!/^class\s/.test(Function.prototype.toString.call(n))}function Y(n){return n==null||Array.isArray(n)||typeof n=="function"||n instanceof Date||n instanceof RegExp||n instanceof Map||n instanceof Set||n instanceof Error?!1:typeof n=="object"}var h=class n{constructor(e){this._closed=!1;this._parentOrParents=null;this._subscriptions=null;e&&(this._subscriptions=[e])}get closed(){return this._closed}unsubscribe(){if(this._closed)return;this._closed=!0;let{_parentOrParents:e,_subscriptions:r}=this;if(e instanceof n)e.remove(this);else if(e!==null)for(let t of e)t.remove(this);if(r){for(let t of r)if(D(t)&&!(t instanceof n))try{t()}catch(o){console.error("Error in unsubscribe function:",o)}else if(Y(t)&&typeof t.unsubscribe=="function")try{t.unsubscribe()}catch(o){console.error("Error unsubscribing child:",o)}this._subscriptions=null}}add(e){if(!e||e===this)return this;if(e.closed)return this;let{_closed:r,_subscriptions:t}=this;if(r){if(D(e))try{e()}catch(o){console.error("Error in unsubscribe function:",o)}else if(e.unsubscribe)try{e.unsubscribe()}catch(o){console.error("Error unsubscribing:",o)}return this}return t||(this._subscriptions=[]),this._subscriptions.push(e),e instanceof n&&e._addParent(this),this}remove(e){let{_subscriptions:r}=this;if(!r)return;let t=r.indexOf(e);t!==-1&&(r.splice(t,1),e._removeParent(this))}_addParent(e){let{_parentOrParents:r}=this;r?Array.isArray(r)?r.push(e):this._parentOrParents=[r,e]:this._parentOrParents=e}_removeParent(e){let{_parentOrParents:r}=this;if(r===e)this._parentOrParents=null;else if(Array.isArray(r)){let t=r.indexOf(e);t!==-1&&(r.splice(t,1),r.length===1&&(this._parentOrParents=r[0]))}}};var y=class extends h{constructor(r,t,o){super();this.isStopped=!1;let s;r?typeof r=="object"?s=r:s={next:r,error:t,complete:o}:s={},this.destination=s}next(r){if(!this.isStopped&&this.destination.next)try{this.destination.next(r)}catch(t){this.error(t)}}error(r){if(!this.isStopped){if(this.isStopped=!0,this.destination.error)try{this.destination.error(r)}catch(t){throw t}else throw r;this.unsubscribe()}}complete(){if(!this.isStopped){if(this.isStopped=!0,this.destination.complete)try{this.destination.complete()}catch(r){this.error(r);return}this.unsubscribe()}}unsubscribe(){this.closed||(this.isStopped=!0,super.unsubscribe())}},P=class extends y{constructor(r,t,o,s){super(r,t,o);this._isUnsubscribing=!1;this._parentSubscriber=null;this._parentSubscriber=s||null}next(r){if(!this.isStopped&&!this._isUnsubscribing)try{super.next(r)}catch(t){this.error(t)}}error(r){if(!this.isStopped&&!this._isUnsubscribing){this._isUnsubscribing=!0;try{super.error(r)}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 O=class extends h{constructor(e){if(super(),e)this._subscribe=e;else throw new TypeError("Observable constructor requires a subscribe function")}subscribe(e,r,t){let o;e instanceof y?o=e:o=new P(e,r,t);let{_subscribe:s}=this;try{let u=s.call(this,o);u&&(typeof u=="function"||typeof u.unsubscribe=="function")&&o.add(u)}catch(u){o.error(u)}return o}pipe(...e){return e.length===0?this:e.reduce((r,t)=>t(r),this)}toPromise(){return new Promise((e,r)=>{let t;this.subscribe({next:o=>t=o,error:r,complete:()=>e(t)})})}};function Pe(n,e,r){return a(()=>e(n()),void 0,r)}function Ee(n,e,r){return a(()=>n().filter(e),void 0,r)}function Ue(n,e,r,t){return a(()=>n().reduce(e,r),void 0,t)}function qe(n,e,r){return a(()=>n().find(e),void 0,r)}function Me(n,e,r){return a(()=>n().some(e),void 0,r)}function Be(n,e,r){return a(()=>n().every(e),void 0,r)}function Ie(n,e,r){return a(()=>[...n()].sort(e),void 0,r)}function Ne(n,e){return a(()=>[...new Set(n())],void 0,e)}function Ke(n,e,r){return a(()=>{let t=n.map(o=>o());return e(t)},void 0,r)}function je(...n){return new O(e=>{let r=()=>n.map(o=>typeof o=="function"?o():o.get()),t=n.map(o=>typeof o=="function"&&o.subscribe?o.subscribe(()=>{e.next(r())}):typeof o!="function"?o.subscribe(()=>{e.next(r())}):z(()=>{e.next(r())}));return()=>{t.forEach(o=>{typeof o=="function"?o():o&&typeof o.unsubscribe=="function"&&o.unsubscribe()})}})}function Ge(n,e,r){return a(()=>n()||e(),void 0,r)}function Ve(n,e,r,t){return a(()=>r(n(),e()),void 0,t)}function He(n,e,r){return a(()=>e(n()),void 0,{equals:r,internal:!0})}function Le(n,e,r){return a(()=>{let t=n(),o={};for(let s of e)o[s]=t[s];return o},void 0,r)}function We(n,e,r){return a(()=>{let o={...n()};for(let s of e)delete o[s];return o},void 0,r)}function ze(n,e,r,t){return a(()=>n()?e():r(),void 0,t)}function Ye(n,e,r){return a(()=>n()?e():void 0,void 0,r)}function $e(n,e,r){let[t,o]=C(n()),s;return a(()=>{let u=n();clearTimeout(s),s=setTimeout(()=>o(()=>u),e)}),t}function Xe(n,e,r){let[t,o]=C(n()),s=0,u;return a(()=>{let c=n(),l=Date.now();l-s>=e?(s=l,o(()=>c)):(clearTimeout(u),u=setTimeout(()=>{s=Date.now(),o(()=>c)},e-(l-s)))}),t}function Je(n){let e=n;for(;typeof e=="function";)e=e();return e}export{Ke as combineSignal,je as combineSignals,$e as debounceSignal,Ne as distinctSignal,Be as everySignal,Ee as filterSignal,qe as findSignal,Ye as guardSignal,Pe as mapSignal,Ge as mergeSignal,We as omitSignal,Le as pickSignal,Je as readSignal,Ue as reduceSignal,He as selectSignal,Me as someSignal,Ie as sortSignal,Xe as throttleSignal,ze as whenSignal,Ve as zipSignal};
|
|
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
|
+
|
|
267
|
+
// src/lib/signal/graph/runtime.ts
|
|
268
|
+
function makeOwner(parent) {
|
|
269
|
+
return { cleanups: null, owned: null, parent, contexts: null };
|
|
270
|
+
}
|
|
271
|
+
function reportSuspend(promise) {
|
|
272
|
+
if (G.onSuspend) {
|
|
273
|
+
G.onSuspend(promise, G.owner);
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
function getOwner() {
|
|
279
|
+
return G.owner;
|
|
280
|
+
}
|
|
281
|
+
function runWithOwner(owner, fn) {
|
|
282
|
+
const prev = G.owner;
|
|
283
|
+
G.owner = owner;
|
|
284
|
+
try {
|
|
285
|
+
return fn();
|
|
286
|
+
} finally {
|
|
287
|
+
G.owner = prev;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function onCleanup(fn) {
|
|
291
|
+
if (!G.owner) return;
|
|
292
|
+
if (G.owner.disposed) {
|
|
293
|
+
fn();
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
(G.owner.cleanups ??= []).push(fn);
|
|
297
|
+
}
|
|
298
|
+
function disposeOwner(owner, final = true) {
|
|
299
|
+
if (owner.disposed) return;
|
|
300
|
+
if (owner.owned) {
|
|
301
|
+
for (let i = owner.owned.length - 1; i >= 0; i--) disposeOwner(owner.owned[i]);
|
|
302
|
+
owner.owned = null;
|
|
303
|
+
}
|
|
304
|
+
if (owner.cleanups) {
|
|
305
|
+
for (let i = owner.cleanups.length - 1; i >= 0; i--) owner.cleanups[i]();
|
|
306
|
+
owner.cleanups = null;
|
|
307
|
+
}
|
|
308
|
+
if (final) owner.disposed = true;
|
|
309
|
+
}
|
|
310
|
+
function setScheduler(fn) {
|
|
311
|
+
G.hostSchedule = fn ?? ((run) => void Promise.resolve().then(run));
|
|
312
|
+
}
|
|
313
|
+
function enqueue(e) {
|
|
314
|
+
if (e.queued) return;
|
|
315
|
+
e.queued = true;
|
|
316
|
+
G.queue.push(e);
|
|
317
|
+
if (!G.scheduled && G.batchDepth === 0) {
|
|
318
|
+
G.scheduled = true;
|
|
319
|
+
G.hostSchedule(flushSync);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function flushSync() {
|
|
323
|
+
G.scheduled = false;
|
|
324
|
+
if (G.flushing) return;
|
|
325
|
+
G.flushing = true;
|
|
326
|
+
let guard = 0;
|
|
327
|
+
try {
|
|
328
|
+
while (G.queue.length) {
|
|
329
|
+
if (++guard > 1e5) throw new Error("[fluixi] effect flush did not settle (cycle?)");
|
|
330
|
+
const batchOf = G.queue;
|
|
331
|
+
G.queue = [];
|
|
332
|
+
for (const e of batchOf) {
|
|
333
|
+
e.queued = false;
|
|
334
|
+
if (!e.disposed) e.run();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
} finally {
|
|
338
|
+
G.flushing = false;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
function afterWrite() {
|
|
342
|
+
if (G.batchDepth === 0) flushSync();
|
|
343
|
+
}
|
|
344
|
+
var EffectNode = class {
|
|
345
|
+
constructor(fn, report = false) {
|
|
346
|
+
this.queued = false;
|
|
347
|
+
this.disposed = false;
|
|
348
|
+
this.owner = makeOwner(G.owner);
|
|
349
|
+
if (G.owner) (G.owner.cleanups ??= []).push(() => this.dispose());
|
|
350
|
+
this.computed = new Computed(() => {
|
|
351
|
+
disposeOwner(this.owner, false);
|
|
352
|
+
runWithOwner(this.owner, () => {
|
|
353
|
+
const r = fn();
|
|
354
|
+
if (typeof r === "function") (this.owner.cleanups ??= []).push(r);
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
reportReactiveNode({ kind: "effect", node: this.computed, handle: this, internal: !report });
|
|
358
|
+
this.watcher = new Watcher(() => enqueue(this));
|
|
359
|
+
this.run();
|
|
360
|
+
this.watcher.watch(this.computed);
|
|
361
|
+
}
|
|
362
|
+
run() {
|
|
363
|
+
if (this.disposed) return;
|
|
364
|
+
this.watcher.getPending();
|
|
365
|
+
try {
|
|
366
|
+
this.computed.get();
|
|
367
|
+
} catch (e) {
|
|
368
|
+
if (e && typeof e.then === "function" && G.onSuspend) {
|
|
369
|
+
G.onSuspend(e, this.owner);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (G.onError && G.onError(e, this.owner)) return;
|
|
373
|
+
throw e;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
dispose() {
|
|
377
|
+
if (this.disposed) return;
|
|
378
|
+
this.disposed = true;
|
|
379
|
+
reportReactiveDispose(this.computed);
|
|
380
|
+
this.watcher.unwatch(this.computed);
|
|
381
|
+
disposeOwner(this.owner);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
function effect(fn, report = false) {
|
|
385
|
+
const node = new EffectNode(fn, report);
|
|
386
|
+
return () => node.dispose();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// src/lib/signal/graph/compat.ts
|
|
390
|
+
function isThenable(v) {
|
|
391
|
+
return !!v && typeof v.then === "function";
|
|
392
|
+
}
|
|
393
|
+
setScheduler(() => {
|
|
394
|
+
});
|
|
395
|
+
var $SIGNAL = /* @__PURE__ */ Symbol.for("signal");
|
|
396
|
+
var $MEMO = /* @__PURE__ */ Symbol.for("memo");
|
|
397
|
+
var $NODE = /* @__PURE__ */ Symbol.for("signal-node");
|
|
398
|
+
function createSignal(value, options) {
|
|
399
|
+
const state = new State(value, { equals: options?.equals });
|
|
400
|
+
const read = (() => state.get());
|
|
401
|
+
const write = ((next) => {
|
|
402
|
+
const v = typeof next === "function" ? next(state.peek()) : next;
|
|
403
|
+
state.set(v);
|
|
404
|
+
afterWrite();
|
|
405
|
+
return v;
|
|
406
|
+
});
|
|
407
|
+
Object.defineProperty(read, $SIGNAL, { value: true, enumerable: true });
|
|
408
|
+
Object.defineProperty(read, $NODE, {
|
|
409
|
+
value: {
|
|
410
|
+
get value() {
|
|
411
|
+
return state.peek();
|
|
412
|
+
},
|
|
413
|
+
get version() {
|
|
414
|
+
return state.version;
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
enumerable: false
|
|
418
|
+
});
|
|
419
|
+
reportReactiveNode({
|
|
420
|
+
kind: "signal",
|
|
421
|
+
node: state,
|
|
422
|
+
handle: read,
|
|
423
|
+
name: options?.name,
|
|
424
|
+
set: write
|
|
425
|
+
});
|
|
426
|
+
if (isObserved()) onCleanup(() => reportReactiveDispose(state));
|
|
427
|
+
return [read, write];
|
|
428
|
+
}
|
|
429
|
+
function createMemo(fn, value, options) {
|
|
430
|
+
let prev = value;
|
|
431
|
+
const owner = getOwner();
|
|
432
|
+
const c = new Computed(
|
|
433
|
+
() => runWithOwner(owner, () => {
|
|
434
|
+
try {
|
|
435
|
+
return prev = fn(prev);
|
|
436
|
+
} catch (e) {
|
|
437
|
+
if (isThenable(e)) {
|
|
438
|
+
reportSuspend(e);
|
|
439
|
+
return prev;
|
|
440
|
+
}
|
|
441
|
+
throw e;
|
|
442
|
+
}
|
|
443
|
+
}),
|
|
444
|
+
{ equals: options?.equals }
|
|
445
|
+
);
|
|
446
|
+
const read = (() => c.get());
|
|
447
|
+
Object.defineProperty(read, $SIGNAL, { value: true, enumerable: true });
|
|
448
|
+
Object.defineProperty(read, $MEMO, { value: true, enumerable: true });
|
|
449
|
+
reportReactiveNode({ kind: "memo", node: c, handle: read, name: options?.name });
|
|
450
|
+
if (isObserved()) onCleanup(() => reportReactiveDispose(c));
|
|
451
|
+
return read;
|
|
452
|
+
}
|
|
453
|
+
function createEffect(fn, value) {
|
|
454
|
+
let prev = value;
|
|
455
|
+
return effect(() => {
|
|
456
|
+
const r = fn(prev);
|
|
457
|
+
prev = r;
|
|
458
|
+
}, true);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// src/lib/predicates.ts
|
|
462
|
+
function isFunction(value) {
|
|
463
|
+
return typeof value === "function" && !/^class\s/.test(Function.prototype.toString.call(value));
|
|
464
|
+
}
|
|
465
|
+
function isObject(value) {
|
|
466
|
+
if (value == null) return false;
|
|
467
|
+
if (Array.isArray(value)) return false;
|
|
468
|
+
if (typeof value === "function") return false;
|
|
469
|
+
if (value instanceof Date) return false;
|
|
470
|
+
if (value instanceof RegExp) return false;
|
|
471
|
+
if (value instanceof Map) return false;
|
|
472
|
+
if (value instanceof Set) return false;
|
|
473
|
+
if (value instanceof Error) return false;
|
|
474
|
+
return typeof value === "object";
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// src/lib/observable/subscription.ts
|
|
478
|
+
var Subscription = class _Subscription {
|
|
479
|
+
/**
|
|
480
|
+
* Creates a new Subscription.
|
|
481
|
+
*/
|
|
482
|
+
constructor(unsubscribe) {
|
|
483
|
+
/** @private */
|
|
484
|
+
this._closed = false;
|
|
485
|
+
/** @private */
|
|
486
|
+
this._parentOrParents = null;
|
|
487
|
+
/** @private */
|
|
488
|
+
this._subscriptions = null;
|
|
489
|
+
if (unsubscribe) {
|
|
490
|
+
this._subscriptions = [unsubscribe];
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Whether the subscription is closed.
|
|
495
|
+
*/
|
|
496
|
+
get closed() {
|
|
497
|
+
return this._closed;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Disposes the resources held by the subscription.
|
|
501
|
+
* May be called multiple times (idempotent).
|
|
502
|
+
*/
|
|
503
|
+
unsubscribe() {
|
|
504
|
+
if (this._closed) {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
this._closed = true;
|
|
508
|
+
const { _parentOrParents, _subscriptions } = this;
|
|
509
|
+
if (_parentOrParents instanceof _Subscription) {
|
|
510
|
+
_parentOrParents.remove(this);
|
|
511
|
+
} else if (_parentOrParents !== null) {
|
|
512
|
+
for (const parent of _parentOrParents) {
|
|
513
|
+
parent.remove(this);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (_subscriptions) {
|
|
517
|
+
for (const sub of _subscriptions) {
|
|
518
|
+
if (isFunction(sub) && !(sub instanceof _Subscription)) {
|
|
519
|
+
try {
|
|
520
|
+
sub();
|
|
521
|
+
} catch (error) {
|
|
522
|
+
console.error("Error in unsubscribe function:", error);
|
|
523
|
+
}
|
|
524
|
+
} else if (isObject(sub) && typeof sub.unsubscribe === "function") {
|
|
525
|
+
try {
|
|
526
|
+
sub.unsubscribe();
|
|
527
|
+
} catch (error) {
|
|
528
|
+
console.error("Error unsubscribing child:", error);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
this._subscriptions = null;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Adds a child subscription or cleanup function to this subscription.
|
|
537
|
+
* When this subscription is unsubscribed, all added subscriptions/functions will be unsubscribed/called.
|
|
538
|
+
*
|
|
539
|
+
* @throws {TypeError} If the argument is not a subscription-like object or function
|
|
540
|
+
*/
|
|
541
|
+
add(subscription) {
|
|
542
|
+
if (!subscription || subscription === this) {
|
|
543
|
+
return this;
|
|
544
|
+
}
|
|
545
|
+
if (subscription.closed) {
|
|
546
|
+
return this;
|
|
547
|
+
}
|
|
548
|
+
let { _closed, _subscriptions } = this;
|
|
549
|
+
if (_closed) {
|
|
550
|
+
if (isFunction(subscription)) {
|
|
551
|
+
try {
|
|
552
|
+
subscription();
|
|
553
|
+
} catch (error) {
|
|
554
|
+
console.error("Error in unsubscribe function:", error);
|
|
555
|
+
}
|
|
556
|
+
} else if (subscription.unsubscribe) {
|
|
557
|
+
try {
|
|
558
|
+
subscription.unsubscribe();
|
|
559
|
+
} catch (error) {
|
|
560
|
+
console.error("Error unsubscribing:", error);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return this;
|
|
564
|
+
}
|
|
565
|
+
if (!_subscriptions) {
|
|
566
|
+
this._subscriptions = [];
|
|
567
|
+
}
|
|
568
|
+
this._subscriptions.push(subscription);
|
|
569
|
+
if (subscription instanceof _Subscription) {
|
|
570
|
+
subscription._addParent(this);
|
|
571
|
+
}
|
|
572
|
+
return this;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Removes a child subscription from this subscription.
|
|
576
|
+
*/
|
|
577
|
+
remove(subscription) {
|
|
578
|
+
const { _subscriptions } = this;
|
|
579
|
+
if (!_subscriptions) {
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
const index = _subscriptions.indexOf(subscription);
|
|
583
|
+
if (index !== -1) {
|
|
584
|
+
_subscriptions.splice(index, 1);
|
|
585
|
+
subscription._removeParent(this);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
/** @private */
|
|
589
|
+
_addParent(parent) {
|
|
590
|
+
const { _parentOrParents } = this;
|
|
591
|
+
if (!_parentOrParents) {
|
|
592
|
+
this._parentOrParents = parent;
|
|
593
|
+
} else if (Array.isArray(_parentOrParents)) {
|
|
594
|
+
_parentOrParents.push(parent);
|
|
595
|
+
} else {
|
|
596
|
+
this._parentOrParents = [_parentOrParents, parent];
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
/** @private */
|
|
600
|
+
_removeParent(parent) {
|
|
601
|
+
const { _parentOrParents } = this;
|
|
602
|
+
if (_parentOrParents === parent) {
|
|
603
|
+
this._parentOrParents = null;
|
|
604
|
+
} else if (Array.isArray(_parentOrParents)) {
|
|
605
|
+
const index = _parentOrParents.indexOf(parent);
|
|
606
|
+
if (index !== -1) {
|
|
607
|
+
_parentOrParents.splice(index, 1);
|
|
608
|
+
if (_parentOrParents.length === 1) {
|
|
609
|
+
this._parentOrParents = _parentOrParents[0];
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
// src/lib/observable/observable.ts
|
|
617
|
+
var Subscriber = class extends Subscription {
|
|
618
|
+
/**
|
|
619
|
+
* Creates a new Subscriber.
|
|
620
|
+
*/
|
|
621
|
+
constructor(destinationOrNext, error, complete) {
|
|
622
|
+
super();
|
|
623
|
+
/** @private */
|
|
624
|
+
this.isStopped = false;
|
|
625
|
+
let observer;
|
|
626
|
+
if (!destinationOrNext) {
|
|
627
|
+
observer = {};
|
|
628
|
+
} else if (typeof destinationOrNext === "object") {
|
|
629
|
+
observer = destinationOrNext;
|
|
630
|
+
} else {
|
|
631
|
+
observer = {
|
|
632
|
+
next: destinationOrNext,
|
|
633
|
+
error,
|
|
634
|
+
complete
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
this.destination = observer;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Sends the next value to the observer.
|
|
641
|
+
*/
|
|
642
|
+
next(value) {
|
|
643
|
+
if (!this.isStopped && this.destination.next) {
|
|
644
|
+
try {
|
|
645
|
+
this.destination.next(value);
|
|
646
|
+
} catch (error) {
|
|
647
|
+
this.error(error);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Sends an error notification to the observer.
|
|
653
|
+
*/
|
|
654
|
+
error(err) {
|
|
655
|
+
if (!this.isStopped) {
|
|
656
|
+
this.isStopped = true;
|
|
657
|
+
if (this.destination.error) {
|
|
658
|
+
try {
|
|
659
|
+
this.destination.error(err);
|
|
660
|
+
} catch (error) {
|
|
661
|
+
throw error;
|
|
662
|
+
}
|
|
663
|
+
} else {
|
|
664
|
+
throw err;
|
|
665
|
+
}
|
|
666
|
+
this.unsubscribe();
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Sends a complete notification to the observer.
|
|
671
|
+
*/
|
|
672
|
+
complete() {
|
|
673
|
+
if (!this.isStopped) {
|
|
674
|
+
this.isStopped = true;
|
|
675
|
+
if (this.destination.complete) {
|
|
676
|
+
try {
|
|
677
|
+
this.destination.complete();
|
|
678
|
+
} catch (error) {
|
|
679
|
+
this.error(error);
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
this.unsubscribe();
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Unsubscribes the subscriber.
|
|
688
|
+
*/
|
|
689
|
+
unsubscribe() {
|
|
690
|
+
if (this.closed) {
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
this.isStopped = true;
|
|
694
|
+
super.unsubscribe();
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
var SafeSubscriber = class extends Subscriber {
|
|
698
|
+
constructor(observerOrNext, error, complete, parentSubscriber) {
|
|
699
|
+
super(observerOrNext, error, complete);
|
|
700
|
+
/** @private */
|
|
701
|
+
this._isUnsubscribing = false;
|
|
702
|
+
/** @private */
|
|
703
|
+
this._parentSubscriber = null;
|
|
704
|
+
this._parentSubscriber = parentSubscriber || null;
|
|
705
|
+
}
|
|
706
|
+
next(value) {
|
|
707
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
708
|
+
try {
|
|
709
|
+
super.next(value);
|
|
710
|
+
} catch (error) {
|
|
711
|
+
this.error(error);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
error(err) {
|
|
716
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
717
|
+
this._isUnsubscribing = true;
|
|
718
|
+
try {
|
|
719
|
+
super.error(err);
|
|
720
|
+
} finally {
|
|
721
|
+
this._isUnsubscribing = false;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
complete() {
|
|
726
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
727
|
+
this._isUnsubscribing = true;
|
|
728
|
+
try {
|
|
729
|
+
super.complete();
|
|
730
|
+
} finally {
|
|
731
|
+
this._isUnsubscribing = false;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
unsubscribe() {
|
|
736
|
+
if (this.closed) {
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
this._isUnsubscribing = true;
|
|
740
|
+
super.unsubscribe();
|
|
741
|
+
this._isUnsubscribing = false;
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
var Observable = class extends Subscription {
|
|
745
|
+
/**
|
|
746
|
+
* Creates a new Observable.
|
|
747
|
+
*/
|
|
748
|
+
constructor(subscribe) {
|
|
749
|
+
super();
|
|
750
|
+
if (subscribe) {
|
|
751
|
+
this._subscribe = subscribe;
|
|
752
|
+
} else {
|
|
753
|
+
throw new TypeError("Observable constructor requires a subscribe function");
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
subscribe(observerOrNext, error, complete) {
|
|
757
|
+
let subscriber;
|
|
758
|
+
if (observerOrNext instanceof Subscriber) {
|
|
759
|
+
subscriber = observerOrNext;
|
|
760
|
+
} else {
|
|
761
|
+
subscriber = new SafeSubscriber(observerOrNext, error, complete);
|
|
762
|
+
}
|
|
763
|
+
const { _subscribe } = this;
|
|
764
|
+
try {
|
|
765
|
+
const result = _subscribe.call(this, subscriber);
|
|
766
|
+
if (result) {
|
|
767
|
+
if (typeof result === "function") {
|
|
768
|
+
subscriber.add(result);
|
|
769
|
+
} else if (typeof result.unsubscribe === "function") {
|
|
770
|
+
subscriber.add(result);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
} catch (err) {
|
|
774
|
+
subscriber.error(err);
|
|
775
|
+
}
|
|
776
|
+
return subscriber;
|
|
777
|
+
}
|
|
778
|
+
pipe(...operations) {
|
|
779
|
+
if (operations.length === 0) {
|
|
780
|
+
return this;
|
|
781
|
+
}
|
|
782
|
+
return operations.reduce(
|
|
783
|
+
(prev, fn) => fn(prev),
|
|
784
|
+
this
|
|
785
|
+
);
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Converts the Observable to a Promise.
|
|
789
|
+
* The Promise resolves with the last emitted value when the Observable completes,
|
|
790
|
+
* or rejects if the Observable errors.
|
|
791
|
+
*
|
|
792
|
+
* @example
|
|
793
|
+
* // Convert Observable to Promise
|
|
794
|
+
* const value = await of(1, 2, 3).toPromise();
|
|
795
|
+
* console.log(value); // 3
|
|
796
|
+
*
|
|
797
|
+
* @example
|
|
798
|
+
* // Handle empty Observable
|
|
799
|
+
* const value = await EMPTY.toPromise();
|
|
800
|
+
* console.log(value); // undefined
|
|
801
|
+
*/
|
|
802
|
+
toPromise() {
|
|
803
|
+
return new Promise((resolve, reject) => {
|
|
804
|
+
let lastValue;
|
|
805
|
+
this.subscribe({
|
|
806
|
+
next: (value) => lastValue = value,
|
|
807
|
+
error: reject,
|
|
808
|
+
complete: () => resolve(lastValue)
|
|
809
|
+
});
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
// src/lib/signal/utilities.ts
|
|
815
|
+
function mapSignal(signal, fn, options) {
|
|
816
|
+
return createMemo(() => fn(signal()), void 0, options);
|
|
817
|
+
}
|
|
818
|
+
function filterSignal(signal, predicate, options) {
|
|
819
|
+
return createMemo(() => signal().filter(predicate), void 0, options);
|
|
820
|
+
}
|
|
821
|
+
function reduceSignal(signal, reducer, initialValue, options) {
|
|
822
|
+
return createMemo(
|
|
823
|
+
() => signal().reduce(reducer, initialValue),
|
|
824
|
+
void 0,
|
|
825
|
+
options
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
function findSignal(signal, predicate, options) {
|
|
829
|
+
return createMemo(() => signal().find(predicate), void 0, options);
|
|
830
|
+
}
|
|
831
|
+
function someSignal(signal, predicate, options) {
|
|
832
|
+
return createMemo(() => signal().some(predicate), void 0, options);
|
|
833
|
+
}
|
|
834
|
+
function everySignal(signal, predicate, options) {
|
|
835
|
+
return createMemo(() => signal().every(predicate), void 0, options);
|
|
836
|
+
}
|
|
837
|
+
function sortSignal(signal, compareFn, options) {
|
|
838
|
+
return createMemo(() => [...signal()].sort(compareFn), void 0, options);
|
|
839
|
+
}
|
|
840
|
+
function distinctSignal(signal, options) {
|
|
841
|
+
return createMemo(() => [...new Set(signal())], void 0, options);
|
|
842
|
+
}
|
|
843
|
+
function combineSignal(signals, combiner, options) {
|
|
844
|
+
return createMemo(
|
|
845
|
+
() => {
|
|
846
|
+
const values = signals.map((s) => s());
|
|
847
|
+
return combiner(values);
|
|
848
|
+
},
|
|
849
|
+
void 0,
|
|
850
|
+
options
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
function combineSignals(...signals) {
|
|
854
|
+
return new Observable((observer) => {
|
|
855
|
+
const getValues = () => signals.map((s) => typeof s === "function" ? s() : s.get());
|
|
856
|
+
const disposers = signals.map((signal) => {
|
|
857
|
+
if (typeof signal === "function" && signal.subscribe) {
|
|
858
|
+
return signal.subscribe(() => {
|
|
859
|
+
observer.next(getValues());
|
|
860
|
+
});
|
|
861
|
+
} else if (typeof signal !== "function") {
|
|
862
|
+
return signal.subscribe(() => {
|
|
863
|
+
observer.next(getValues());
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
return createEffect(() => {
|
|
867
|
+
observer.next(getValues());
|
|
868
|
+
});
|
|
869
|
+
});
|
|
870
|
+
return () => {
|
|
871
|
+
disposers.forEach((dispose) => {
|
|
872
|
+
if (typeof dispose === "function") dispose();
|
|
873
|
+
else if (dispose && typeof dispose.unsubscribe === "function") {
|
|
874
|
+
dispose.unsubscribe();
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
};
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
function mergeSignal(primary, fallback, options) {
|
|
881
|
+
return createMemo(() => primary() || fallback(), void 0, options);
|
|
882
|
+
}
|
|
883
|
+
function zipSignal(a, b, operator, options) {
|
|
884
|
+
return createMemo(() => operator(a(), b()), void 0, options);
|
|
885
|
+
}
|
|
886
|
+
function selectSignal(signal, selector, equals) {
|
|
887
|
+
return createMemo(() => selector(signal()), void 0, {
|
|
888
|
+
equals,
|
|
889
|
+
internal: true
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
function pickSignal(signal, keys, options) {
|
|
893
|
+
return createMemo(
|
|
894
|
+
() => {
|
|
895
|
+
const obj = signal();
|
|
896
|
+
const result = {};
|
|
897
|
+
for (const key of keys) {
|
|
898
|
+
result[key] = obj[key];
|
|
899
|
+
}
|
|
900
|
+
return result;
|
|
901
|
+
},
|
|
902
|
+
void 0,
|
|
903
|
+
options
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
function omitSignal(signal, keys, options) {
|
|
907
|
+
return createMemo(
|
|
908
|
+
() => {
|
|
909
|
+
const obj = signal();
|
|
910
|
+
const result = { ...obj };
|
|
911
|
+
for (const key of keys) {
|
|
912
|
+
delete result[key];
|
|
913
|
+
}
|
|
914
|
+
return result;
|
|
915
|
+
},
|
|
916
|
+
void 0,
|
|
917
|
+
options
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
function whenSignal(condition, whenTrue, whenFalse, options) {
|
|
921
|
+
return createMemo(
|
|
922
|
+
() => condition() ? whenTrue() : whenFalse(),
|
|
923
|
+
void 0,
|
|
924
|
+
options
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
function guardSignal(condition, accessor, options) {
|
|
928
|
+
return createMemo(
|
|
929
|
+
() => condition() ? accessor() : void 0,
|
|
930
|
+
void 0,
|
|
931
|
+
options
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
function debounceSignal(signal, delay, options) {
|
|
935
|
+
const [debounced, setDebounced] = createSignal(signal());
|
|
936
|
+
let timeoutId;
|
|
937
|
+
createMemo(() => {
|
|
938
|
+
const value = signal();
|
|
939
|
+
clearTimeout(timeoutId);
|
|
940
|
+
timeoutId = setTimeout(() => setDebounced(() => value), delay);
|
|
941
|
+
});
|
|
942
|
+
return debounced;
|
|
943
|
+
}
|
|
944
|
+
function throttleSignal(signal, delay, options) {
|
|
945
|
+
const [throttled, setThrottled] = createSignal(signal());
|
|
946
|
+
let lastUpdate = 0;
|
|
947
|
+
let timeoutId;
|
|
948
|
+
createMemo(() => {
|
|
949
|
+
const value = signal();
|
|
950
|
+
const now = Date.now();
|
|
951
|
+
if (now - lastUpdate >= delay) {
|
|
952
|
+
lastUpdate = now;
|
|
953
|
+
setThrottled(() => value);
|
|
954
|
+
} else {
|
|
955
|
+
clearTimeout(timeoutId);
|
|
956
|
+
timeoutId = setTimeout(() => {
|
|
957
|
+
lastUpdate = Date.now();
|
|
958
|
+
setThrottled(() => value);
|
|
959
|
+
}, delay - (now - lastUpdate));
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
return throttled;
|
|
963
|
+
}
|
|
964
|
+
function readSignal(signal) {
|
|
965
|
+
let value = signal;
|
|
966
|
+
while (typeof value === "function") value = value();
|
|
967
|
+
return value;
|
|
968
|
+
}
|
|
969
|
+
export {
|
|
970
|
+
combineSignal,
|
|
971
|
+
combineSignals,
|
|
972
|
+
debounceSignal,
|
|
973
|
+
distinctSignal,
|
|
974
|
+
everySignal,
|
|
975
|
+
filterSignal,
|
|
976
|
+
findSignal,
|
|
977
|
+
guardSignal,
|
|
978
|
+
mapSignal,
|
|
979
|
+
mergeSignal,
|
|
980
|
+
omitSignal,
|
|
981
|
+
pickSignal,
|
|
982
|
+
readSignal,
|
|
983
|
+
reduceSignal,
|
|
984
|
+
selectSignal,
|
|
985
|
+
someSignal,
|
|
986
|
+
sortSignal,
|
|
987
|
+
throttleSignal,
|
|
988
|
+
whenSignal,
|
|
989
|
+
zipSignal
|
|
990
|
+
};
|