@raphy-js/raph 1.0.0-dev.18 → 1.0.1-dev.20

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.
@@ -1 +1 @@
1
- "use strict";var z=Object.defineProperty;var P=(c,e,t)=>e in c?z(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var b=(c,e,t)=>P(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const B=require("./reactive-object.cjs"),R=require("./scope.cjs"),G=require("./watch.cjs"),w=1,p=2,C=4,S=8,W=16,y=32,x=64,O=128,m=256,v=512,E=w|p,_=16,M=2;function U(c){c.dependencyLookupDebt===void 0&&c.dependencyIndex===void 0||(c.dependencyIndex=void 0,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0)}function Q(c,e){if(e<=_||c.flags&O||c.dependencyIndex!==void 0)return;const t=(c.dependencyLookupSearches??0)+1,i=(c.dependencyLookupDebt??0)+e;if(c.dependencyLookupSearches=t,c.dependencyLookupDebt=i,t<M||i<c.depCount)return;const s=new Map;for(let n=c.firstDep;n!==null;n=n.nextDep)s.set(n.dep,n);c.dependencyIndex=s,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0}function j(c,e,t){var r;const i=c.firstSub;if(i===null)return;if(i===c.lastSub)return i.sub===e?i:void 0;const s=c.subSearchCursor??i;let n=1;if(s.sub===e)return c.subSearchCursor=s.nextSub??i,s;if(t){const l=(r=e.dependencyIndex)==null?void 0:r.get(c);if(l!==void 0)return c.subSearchCursor=l.nextSub??i,l;if(e.dependencyIndex!==void 0)return}let a=s.nextSub??i;for(;a!==s;){if(++n,a.sub===e)return c.subSearchCursor=a.nextSub??i,t&&Q(e,n),a;a=a.nextSub??i}t&&Q(e,n)}class N{constructor(e={}){b(this,"adaptiveDependencyIndex");b(this,"activeSub",null);b(this,"batchDepth",0);b(this,"effectReadIndex",0);b(this,"effectWriteIndex",0);b(this,"invalidationDepth",0);b(this,"dependencyDepth",0);b(this,"flushingEffects",!1);b(this,"effectQueue",new Array(16).fill(void 0));b(this,"invalidationStack",new Array(512).fill(null));b(this,"dependencyStack",[]);b(this,"handleReaders",this.createHandleReaders());b(this,"effectScheduler");b(this,"flushEffects");b(this,"_effectImmediate");this.adaptiveDependencyIndex=e.adaptiveDependencyIndex??!1,this.effectScheduler=e.effectScheduler,this._effectImmediate=this.effect.bind(this),this.signal=this.signal.bind(this),this.computed=this.computed.bind(this),this.reactive=this.reactive.bind(this),this.watch=this.watch.bind(this),this.batch=this.batch.bind(this),this.untracked=this.untracked.bind(this),this.scope=this.scope.bind(this),this._signalAccess=this._signalAccess.bind(this),this._computedAccess=this._computedAccess.bind(this),this._ownedSignal=this._ownedSignal.bind(this),this._ownedComputed=this._ownedComputed.bind(this),this._isTracking=this._isTracking.bind(this),this._isFlushingEffects=this._isFlushingEffects.bind(this),this._startBatch=this._startBatch.bind(this),this._endBatch=this._endBatch.bind(this),this.effect=this.effectScheduler===void 0?this._effectImmediate:this.createScheduledEffect.bind(this),this.flushEffects=this.effectScheduler===void 0?this.flushSyncEffects:this.flushExternalEffects}signal(e){let t=e,i;const s=()=>{const a=i;if(a!==void 0)return this.readSignalValue(a);const r=this.activeSub;return r===null||(i=this.materializeSignal(t,r)),t},n=a=>{const r=i;if(r!==void 0){this.writeSignalValue(r,a);return}t!==a&&(t=a)};return{get:s,peek:this.handleReaders.peek,set:n}}reactive(e){return B.reactiveObject(this,e,()=>this.createReactivePropertySourceBridge())}computed(e){if(typeof e=="function")return this.createComputedHandle(e);const t=this.createComputedHandle(e.get);return t.set=e.set,t}effect(e,t){const i=this.allocateGraphRecord(void 0,e,p|w|y);t!==void 0&&(i.onStop=t);const s=this.activeSub;s!==null&&s.flags&y&&R.appendScopeChild(s,i);try{this.executeEffect(i)}catch(n){try{this.disposeEffect(i)}catch(a){throw new AggregateError([n,a],"Effect registration and cleanup failed.")}throw n}return this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}watch(e,t,i){return G.watchWithReactivity(this.effect,this.untracked,e,t,i)}batch(e){++this.batchDepth;let t;try{t=e()}catch(i){if(!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(s){throw new AggregateError([i,s],"Batch operation and effect queue processing failed.")}throw i}return!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),t}untracked(e){if(this.activeSub===null)return e();const t=this.activeSub;this.activeSub=null;try{return e()}finally{this.activeSub=t}}scope(){return new R.ScopeNode(this)}allocateGraphRecord(e,t,i){return{flags:i,firstSub:null,lastSub:null,firstDep:null,tailDep:null,depCount:0,evaluate:t,cachedValue:e}}createReactivePropertySourceBridge(){return{batch:this.batch,create:()=>{if(this.activeSub===null)throw new Error("[Raph] Reactive property source requires a tracked read.");return{firstSub:null,lastSub:null}},isTracking:this._isTracking,track:e=>{const t=this.activeSub;t!==null&&this.trackDependency(e,t)},trigger:e=>{const t=e.firstSub;t!==null&&(this.propagateInvalidation(t),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}}}materializeSignal(e,t,i){const s=this.allocateGraphRecord(e,void 0,0);return i!==void 0&&(s.sourceHooks=i,s.flags|=v),t.flags&O?this.appendInitialDependency(s,t):this.trackDependency(s,t),s}createComputedHandle(e,t){const i={evaluate:e,get:this.handleReaders.initializeComputed,peek:this.handleReaders.peek};return t!==void 0&&(i.sourceHooks=t),i}createScheduledEffect(e,t){const i=this.allocateGraphRecord(void 0,e,p|w|y);let s=!1;t!==void 0&&(i.onStop=t);const n=this.activeSub;n!==null&&n.flags&y&&R.appendScopeChild(n,i);const a=()=>{if(!(!(i.flags&x)||i.evaluate===void 0)){i.flags&=~x,s=!0;try{this.executeEffect(i)}catch(r){if(!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(l){throw new AggregateError([r,l],"Effect execution and queue processing failed.")}throw r}!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}};try{const r=this.effectScheduler.bind(a);r.cancelPending=()=>i.evaluate===void 0||!s?!1:(this.settleCancelledDependencies(i),i.flags&=~(x|E|C),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),!0),r.rearmPending=()=>{i.evaluate!==void 0&&(i.flags|=p|x,r.schedule())},i.effectTask=r,this.requestEffectTask(i)}catch(r){try{this.disposeEffect(i)}catch(l){throw new AggregateError([r,l],"Effect registration and cleanup failed.")}throw r}return!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}readSignalValue(e){const t=this.activeSub;let i=null;return t!==null&&(i=this.trackDependency(e,t)),e.flags&p&&this.commitSignalValue(e,i),e.cachedValue}writeSignalValue(e,t){if(e.flags&p||this.batchDepth){const s=!!(e.flags&p);if((e.flags&p?e.pendingValue:e.cachedValue)===t)return;if(e.pendingValue=t,e.cachedValue===t)e.flags&=~p,e.pendingAuthor=void 0;else{e.flags|=p;const r=this.activeSub!==null&&this.activeSub.flags&S?this.activeSub:null;s?e.pendingAuthor!==r&&(e.pendingAuthor=null):e.pendingAuthor=r}const a=e.firstSub;a!=null&&(!s||this.activeSub!==null&&this.activeSub.flags&S||e.flags&v)&&this.propagateInvalidation(a,!0);return}if(e.cachedValue===t)return;e.cachedValue=t;const i=e.firstSub;i!=null&&(this.propagateInvalidation(i),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}readComputedValue(e){const t=e.flags,i=t&(W|E|m),s=i===0?e.cachedValue:i&W?this.evaluateFirstValue(e):this.refreshComputedValue(e,t);if(e.evaluate===void 0)return s;const n=this.activeSub;if(n!==null){const a=n.tailDep;n.flags&O?(a===null||a.dep!==e)&&this.appendInitialDependency(e,n):this.trackDependency(e,n)}return s}evaluateFirstValue(e){const t=e.flags&v,i=e.evaluate;e.flags=t|S|O;const s=this.activeSub;this.activeSub=e;try{const n=i();return e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,n):(e.cachedValue=n,e.flags=t|(e.flags&C?p:0),n)}catch(n){throw e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,n):(e.flags=t|m,s!==null&&this.trackDependency(e,s),n)}finally{this.activeSub=s}}refreshComputedValue(e,t,i=null){try{if(t&(p|C|m))return this.recomputeValue(e,i)||(e.flags&=~E),e.cachedValue;const s=e.firstDep;return(s===null||!this.refreshDependencies(e,s)||!this.recomputeValue(e,i))&&(e.flags&=~E),e.cachedValue}catch(s){throw this.activeSub!==null&&this.trackDependency(e,this.activeSub),s}}commitSignalValue(e,t){const i=e.cachedValue;e.cachedValue=e.pendingValue,e.flags&=~p;const s=e.pendingAuthor;return e.pendingAuthor=void 0,i===e.cachedValue?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t,s??null),!0)}trackDependency(e,t){const i=t.tailDep;if(i!==null){const n=i.nextDep;return n!==null&&n.dep===e?(t.tailDep=n,n):i.dep===e?i:this.reconcileDependency(e,t,i,n)}const s=t.firstDep;return s===null?t.tailDep=this.insertDependency(e,t,null):s.dep===e?(t.tailDep=s,s):this.reconcileDependency(e,t,null,s)}reconcileDependency(e,t,i,s){if(t.depCount<=_){for(let l=s;l!==null;l=l.nextDep)if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;for(let l=i;l!==null;l=l.prevDep)if(l.dep===e)return l;return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}let n=0,a=s;for(;a!==null&&n<_;){const l=a;if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;a=l.nextDep,++n}let r=i;for(;r!==null&&n<_;){const l=r;if(l.dep===e)return l;r=l.prevDep,++n}if(a!==null||r!==null){const l=j(e,t,this.adaptiveDependencyIndex);if(l!==void 0)return this.reorderDependency(t,l,i),t.tailDep=l,l}return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}appendInitialDependency(e,t){var a;const i=t.tailDep,s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};return s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n,i===null?t.firstDep=n:i.nextDep=n,t.tailDep=n,++t.depCount,(a=t.dependencyIndex)==null||a.set(e,n),e.flags&v&&this.attachDependencyCleanup(n),n}insertDependency(e,t,i){var r;const s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n;const a=i===null?t.firstDep:i.nextDep;return n.nextDep=a,a!==null&&(a.prevDep=n),i===null?t.firstDep=n:i.nextDep=n,++t.depCount,(r=t.dependencyIndex)==null||r.set(e,n),e.flags&v&&this.attachDependencyCleanup(n),n}reuseDepLink(e,t){const i=e.sub.dependencyIndex;this.detachSubLink(e);const s=t.lastSub;return e.dep=t,e.prevSub=s,e.nextSub=null,s===null?t.firstSub=e:s.nextSub=e,t.lastSub=e,i==null||i.set(t,e),t.flags&v&&this.attachDependencyCleanup(e),e}reorderDependency(e,t,i){if(t.prevDep===i)return;const s=t.prevDep,n=t.nextDep;s===null?e.firstDep=n:s.nextDep=n,n!==null&&(n.prevDep=s);const a=i===null?e.firstDep:i.nextDep;t.prevDep=i,t.nextDep=a,a!==null&&(a.prevDep=t),i===null?e.firstDep=t:i.nextDep=t}pruneUnreadDependencies(e){const t=e.tailDep;let i=t===null?e.firstDep:t.nextDep;if(i!==null){for(t===null?e.firstDep=null:t.nextDep=null;i!==null;){const s=i.nextDep;this.detachSubLink(i),--e.depCount,i=s}e.depCount<=_&&U(e)}}detachSubLink(e){var t;if((t=e.sub.dependencyIndex)==null||t.delete(e.dep),e.dep.flags&v){const i=e.dependencyCleanup;e.dependencyCleanup=void 0,i==null||i()}e.dep.subSearchCursor===e&&(e.dep.subSearchCursor=e.nextSub??(e.dep.firstSub===e?void 0:e.dep.firstSub??void 0)),e.prevSub===null?e.dep.firstSub=e.nextSub:e.prevSub.nextSub=e.nextSub,e.nextSub===null?e.dep.lastSub=e.prevSub:e.nextSub.prevSub=e.prevSub}attachDependencyCleanup(e){var n,a;const t=e.dep.sourceHooks,i=t==null?void 0:t.onDependency;if(i===void 0)return;const s=((n=e.sub.sourceHooks)==null?void 0:n.dependencyOwner)??((a=e.sub.effectTask)==null?void 0:a.dependencyOwner);s!==void 0&&(e.dependencyCleanup=i(s))}releaseSourceSubs(e){if(!(e.flags&v)){let i=e.firstSub;for(;i!=null;){const s=i.nextSub,n=i.sub;this.detachDependencyLink(i),this.detachSubLink(i);const a=n.flags;(a&(E|S))===w&&(n.flags=a&~w|p,a&y&&this.enqueueEffect(n,e)),i=s}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0;return}let t=e.firstSub;for(;t!=null;){const i=t.nextSub;this.detachDependencyLink(t),this.detachSubLink(t),t=i}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0}detachDependencyLink(e){const t=e.sub,i=e.prevDep,s=e.nextDep;i===null?t.firstDep=s:i.nextDep=s,s!==null&&(s.prevDep=i),t.tailDep===e&&(t.tailDep=i),--t.depCount,t.depCount<=_&&U(t)}enqueueEffect(e,t){var s,n;const i=t!==void 0&&t.flags&v?t.sourceHooks.token:void 0;i!==void 0&&((n=(s=e.effectTask)==null?void 0:s.notify)==null||n.call(s,i)),!(e.flags&x)&&(e.flags|=x,this.effectQueue[this.effectWriteIndex]=e,++this.effectWriteIndex)}propagateInvalidation(e,t=!1){var n,a,r,l,h,g,u,o,I,A,L,q,F;const i=this.invalidationDepth;let s=i;for(let d=e;d!==null;d=d.nextSub){const f=d.sub,D=f.flags;let k=0;if(D&(E|S)?D&S&&(k=this.invalidateReadDependency(d,f,D)):(k=D&~C|(t?w:p),f.flags=k),k===0){if(D&y&&D&x){const T=d.dep.flags&v?d.dep.sourceHooks.token:void 0;T!==void 0&&((a=(n=f.effectTask)==null?void 0:n.notify)==null||a.call(n,T))}else f.flags&v&&((l=(r=f.sourceHooks).onInvalidate)==null||l.call(r,f.firstSub!==null),f.firstSub!==null&&(this.invalidationStack[s++]=f.firstSub));continue}k&y?(!(k&S)||((h=f.effectTask)==null?void 0:h.allowScheduleDuringRun)===!0)&&this.enqueueEffect(f,d.dep):(!(D&E)&&f.flags&v&&((u=(g=f.sourceHooks).onInvalidate)==null||u.call(g,f.firstSub!==null)),f.firstSub!==null&&(this.invalidationStack[s++]=f.firstSub))}for(;s!==i;){let d=this.invalidationStack[--s];do{const f=d.sub,D=f.flags;let k=0;if(D&(E|S)?D&S&&(k=this.invalidateReadDependency(d,f,D)):(k=D&~C|w,f.flags=k),k===0){if(D&y&&D&x){const T=d.dep.flags&v?d.dep.sourceHooks.token:void 0;T!==void 0&&((I=(o=f.effectTask)==null?void 0:o.notify)==null||I.call(o,T))}else if(f.flags&v&&((L=(A=f.sourceHooks).onInvalidate)==null||L.call(A,f.firstSub!==null),f.firstSub!==null)){d.nextSub!==null&&(this.invalidationStack[s++]=d.nextSub),d=f.firstSub;continue}}else if(k&y)this.enqueueEffect(f,d.dep);else if(!(D&E)&&f.flags&v&&((F=(q=f.sourceHooks).onInvalidate)==null||F.call(q,f.firstSub!==null)),f.firstSub!==null){d.nextSub!==null&&(this.invalidationStack[s++]=d.nextSub),d=f.firstSub;continue}d=d.nextSub}while(d!==null)}this.invalidationDepth=i}invalidateReadDependency(e,t,i){const s=t.tailDep;if(s===null)return 0;if(e!==s){for(let a=e.prevDep;a!==null;a=a.prevDep)if(a===s)return 0}const n=i|C|w;return t.flags=n,n}confirmSubUpdates(e,t,i=null){var s,n;for(let a=e;a!==null;a=a.nextSub){if(a===t||a.sub===i)continue;const r=a.sub,l=r.flags;l&p||(r.flags=l&~w|p,l&y?this.enqueueEffect(r,a.dep):l&E||r.flags&v&&((n=(s=r.sourceHooks).onInvalidate)==null||n.call(s,r.firstSub!==null)))}}recomputeValue(e,t=null){const i=e.evaluate;e.tailDep=null;const s=e.flags&~C|S;e.flags=s;const n=this.activeSub;this.activeSub=e;let a;try{a=i()}catch(u){throw e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,u):(this.restoreComputedFailure(e,t),u)}finally{this.activeSub=n}if(e.evaluate!==i)return e.cachedValue=void 0,e.flags=0,!1;const r=e.flags&C,l=s&-268,h=e.tailDep;(h===null?e.firstDep:h.nextDep)!==null&&this.pruneUnreadDependencies(e);const g=e.cachedValue;return e.cachedValue=a,e.flags=s&v|(r?p:l),g===a?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t),!0)}restoreComputedFailure(e,t){e.flags=e.flags&-144|m,this.pruneUnreadDependencies(e),t!==null&&this.restoreFailedDependency(t.sub)}refreshDependencies(e,t){const i=this.dependencyDepth;try{return this.walkDependencyChain(e,t)}catch(s){this.restoreFailedDependency(e);for(let n=i;n<this.dependencyDepth;n++)this.restoreFailedDependency(this.dependencyStack[n].sub);throw this.dependencyDepth=i,this.dependencyStack.length=i,s}}settleCancelledDependencies(e){for(let t=e.firstDep;t!==null;t=t.nextDep){const i=t.dep,s=i.flags;s&(W|E|m)&&(i.evaluate===void 0?s&p&&this.commitSignalValue(i,t):this.refreshComputedValue(i,s,t))}}walkDependencyChain(e,t){const i=this.dependencyDepth;let s=i,n=!1;e:for(;;){if(e.flags&p)n=!0;else{const a=t.dep,r=a.flags;if(r&(p|m))this.dependencyDepth=s,n=a.evaluate===void 0?this.commitSignalValue(a,t):this.recomputeValue(a,t);else if(r&w){if(a.firstDep!==null){this.dependencyStack[s++]=t,e=a,t=a.firstDep;continue e}this.dependencyDepth=s,n=this.recomputeValue(a,t)}else n=!1}if(!n&&t.nextDep!==null){t=t.nextDep;continue}if(n){for(;s!==i;){this.dependencyDepth=--s;const a=this.dependencyStack[s];if(n=this.recomputeValue(e,a),e=a.sub,!n){if(a.nextDep!==null){t=a.nextDep;continue e}break}}if(n)return this.dependencyDepth=i,!0}for(;s!==i;){this.dependencyDepth=--s;const a=this.dependencyStack[s];if(e.flags&=~w,e=a.sub,a.nextDep!==null){t=a.nextDep;continue e}}return e.flags&=~w,this.dependencyDepth=i,!1}}restoreFailedDependency(e){e.flags&=-16,e.flags&y||(e.flags|=m)}executeEffect(e){const t=e.flags;if(!(t&E))return;if(!(t&(p|C))){const h=e.firstDep;try{if(h===null||!this.refreshDependencies(e,h)){e.flags&=~E;return}}catch(g){throw e.flags&=-80,g}}const i=e.evaluate;if(i===void 0){e.flags&=~E;return}let s;if(e.scopeLastChild!==void 0)try{this.disposeNestedEffects(e)}catch(h){s=[h]}const n=typeof e.cachedValue=="function"?e.cachedValue:void 0;if(e.cachedValue=void 0,e.flags&=~C,n!==void 0)try{this.untracked(n)}catch(h){s??(s=[]),s.push(h)}if(s!==void 0){try{this.disposeEffect(e)}catch(h){s.push(h)}R.throwScopeErrors(s,"Effect cleanup failed.")}if(e.evaluate===void 0)return;e.tailDep=null,e.flags=e.flags&~C|S;const a=this.activeSub;this.activeSub=e;let r;try{r=i()}catch(h){this.activeSub=a,e.flags&=~S,this.failEffectRun(e,h,s)}finally{this.activeSub=a,e.flags&=~S}const l=e.tailDep;if((l===null?e.firstDep:l.nextDep)!==null&&this.pruneUnreadDependencies(e),typeof r=="function")if(e.evaluate===void 0)try{this.untracked(r)}catch(h){s??(s=[]),s.push(h)}else e.cachedValue=r;e.flags&x?e.flags=e.flags&-7|w:e.flags&=-8}failEffectRun(e,t,i){const s=i??[];s.push(t),this.pruneUnreadDependencies(e);try{this.disposeNestedEffects(e)}catch(n){s.push(n)}throw e.flags&=-72,s.length===1?s[0]:new AggregateError(s,"Effect execution failed.")}flushSyncEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(y|S))===(y|S))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex,t.flags&=~x,this.executeEffect(t)}}catch(e){this.drainQueueAfterError(e,!1)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}flushExternalEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(y|S))===(y|S))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex;const i=t.effectTask;i===void 0?(t.flags&=~x,this.executeEffect(t)):t.evaluate!==void 0?this.scheduleEffectTask(t,i):t.flags&=~x}}catch(e){this.drainQueueAfterError(e,!0)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}drainQueueAfterError(e,t){const i=[e];for(;this.effectReadIndex!==this.effectWriteIndex;){const s=this.effectReadIndex++,n=this.effectQueue[s];this.effectQueue[s]=void 0;try{const a=t?n.effectTask:void 0;a!==void 0&&n.evaluate!==void 0?this.scheduleEffectTask(n,a):(n.flags&=~x,this.executeEffect(n))}catch(a){i.push(a)}}throw i.length===1?e:new AggregateError(i,"Effect queue processing failed.")}requestEffectTask(e){const t=e.effectTask;t===void 0||e.evaluate===void 0||(e.flags|=x,this.scheduleEffectTask(e,t))}scheduleEffectTask(e,t){try{t.schedule()}catch(i){throw e.flags&=-72,i}}disposeEffect(e){if(e.evaluate===void 0)return;this.activeSub===e&&(this.activeSub=null);const t=e.effectTask,i=e.onStop,s=typeof e.cachedValue=="function"?e.cachedValue:void 0;e.evaluate=void 0,e.flags=0,e.cachedValue=void 0,e.effectTask=void 0,i!==void 0&&(e.onStop=void 0),R.detachScopeChild(e);let n=e.firstDep;for(;n!==null;){const r=n.nextDep;this.detachSubLink(n),n=r}e.firstDep=e.tailDep=null,e.depCount=0,U(e);let a;if(t!==void 0)try{t.dispose()}catch(r){a=[r]}try{this.disposeNestedEffects(e)}catch(r){a??(a=[]),a.push(r)}if(s!==void 0)try{this.untracked(s)}catch(r){a??(a=[]),a.push(r)}if(i!==void 0)try{this.untracked(i)}catch(r){a??(a=[]),a.push(r)}this.invalidationStack.length=this.invalidationDepth,this.dependencyStack.length=this.dependencyDepth,a!==void 0&&R.throwScopeErrors(a,"Effect disposal failed.")}disposeNestedEffects(e){let t;for(;e.scopeLastChild!==void 0;)try{this.disposeEffect(e.scopeLastChild)}catch(i){t??(t=[]),t.push(i)}t!==void 0&&R.throwScopeErrors(t,"Nested effect disposal failed.")}static materializeComputed(e){e.flags=W|(e.sourceHooks===void 0?0:v),e.firstSub=null,e.lastSub=null,e.firstDep=null,e.tailDep=null,e.depCount=0,e.cachedValue=void 0}createHandleReaders(){const e=this;function t(){return e.readComputedValue(this)}return{initializeComputed(){const i=this;return N.materializeComputed(i),this.get=t,e.readComputedValue(i)},initializeRaw(){const i=this;N.materializeComputed(i);const s=()=>e.readComputedValue(i);return this.read=s,s()},peek(){return e.peekHandle(this)}}}peekHandle(e){const t=this.activeSub;if(t===null)return e.get();this.activeSub=null;try{return e.get()}finally{this.activeSub=t}}_signalAccess(e){const t=this;let i=e,s;function n(a){if(arguments.length!==0){const h=s;h!==void 0?t.writeSignalValue(h,a):i!==a&&(i=a);return}const r=s;if(r!==void 0)return t.readSignalValue(r);const l=t.activeSub;return l===null||(s=t.materializeSignal(i,l)),i}return{read:n,write:n}}_computedAccess(e){return{evaluate:e,read:this.handleReaders.initializeRaw}}_ownedSignal(e,t){const i=this;let s=e,n,a=!1;const r=()=>{const u=n;if(u!==void 0)return this.readSignalValue(u);const o=this.activeSub;if(o===null)return s;const I=s;return n=this.materializeSignal(I,o,t),s=void 0,I},l=t===void 0?u=>{const o=n;o!==void 0?this.writeSignalValue(o,u):s!==u&&(s=u)}:u=>{var I,A;const o=n;if(o!==void 0){const L=o.flags&p?o.pendingValue:o.cachedValue;L!==u&&((I=t.onChange)==null||I.call(t,L,u,o.firstSub!==null)),this.writeSignalValue(o,u);return}s!==u&&((A=t.onChange)==null||A.call(t,s,u,!1),s=u)},g={source:{get:r,peek:this.handleReaders.peek,set:l},dispose(){var o;if(a)return!0;a=!0,s=void 0;let u;try{(o=t==null?void 0:t.onDispose)==null||o.call(t,(n==null?void 0:n.firstSub)!=null)}catch(I){(u??(u=[])).push(I)}if(n!==void 0){try{i.releaseSourceSubs(n)}catch(I){(u??(u=[])).push(I)}n.cachedValue=void 0,n.pendingValue=void 0,n.pendingAuthor=void 0,n.flags=0}if(n=void 0,(u==null?void 0:u.length)===1)throw u[0];if(u!==void 0&&u.length>1)throw new AggregateError(u,"Owned signal disposal failed.");return!0}};return t!==void 0&&(g.hasSubscribers=()=>(n==null?void 0:n.firstSub)!=null),g}_ownedComputed(e,t){const i=this,s=this.createComputedHandle(e,t);let n=!1;const a={source:s,dispose(){var h;if(n)return!0;n=!0,i.activeSub===s&&(i.activeSub=null);const r=[];try{(h=t==null?void 0:t.onDispose)==null||h.call(t,s.firstSub!=null)}catch(g){r.push(g)}if(s.flags!==void 0)try{i.releaseSourceSubs(s)}catch(g){r.push(g)}let l=s.firstDep;for(;l!=null;){const g=l.nextDep;i.detachSubLink(l),l=g}if(s.firstDep=s.tailDep=null,s.depCount=0,U(s),s.evaluate=void 0,s.cachedValue=void 0,s.flags=0,r.length===1)throw r[0];if(r.length>1)throw new AggregateError(r,"Owned computed disposal failed.");return!0}};return t!==void 0&&(a.hasSubscribers=()=>s.firstSub!=null),a}_startBatch(){++this.batchDepth}_endBatch(){!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}_isTracking(){return this.activeSub!==null}_isFlushingEffects(){return this.flushingEffects}}let K;function V(){return K??(K=new N)}const Y=c=>V().signal(c),X=c=>V().reactive(c);function Z(c){return V().computed(c)}const J=(c,e)=>V().effect(c,e),$=(c,e,t)=>V().watch(c,e,t),H=c=>V().batch(c),ee=c=>V().untracked(c),te=()=>V().scope();exports.ScopeNode=R.ScopeNode;exports.RaphReactivity=N;exports.batch=H;exports.computed=Z;exports.effect=J;exports.reactive=X;exports.scope=te;exports.signal=Y;exports.untracked=ee;exports.watch=$;
1
+ "use strict";var z=Object.defineProperty;var P=(c,e,t)=>e in c?z(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var g=(c,e,t)=>P(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const B=require("./reactive-object.cjs"),R=require("./scope.cjs"),G=require("./watch.cjs"),w=1,d=2,C=4,b=8,W=16,y=32,x=64,O=128,m=256,v=512,E=w|d,_=16,M=2;function U(c){c.dependencyLookupDebt===void 0&&c.dependencyIndex===void 0||(c.dependencyIndex=void 0,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0)}function Q(c,e){if(e<=_||c.flags&O||c.dependencyIndex!==void 0)return;const t=(c.dependencyLookupSearches??0)+1,i=(c.dependencyLookupDebt??0)+e;if(c.dependencyLookupSearches=t,c.dependencyLookupDebt=i,t<M||i<c.depCount)return;const s=new Map;for(let n=c.firstDep;n!==null;n=n.nextDep)s.set(n.dep,n);c.dependencyIndex=s,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0}function j(c,e,t){var r;const i=c.firstSub;if(i===null)return;if(i===c.lastSub)return i.sub===e?i:void 0;const s=c.subSearchCursor??i;let n=1;if(s.sub===e)return c.subSearchCursor=s.nextSub??i,s;if(t){const l=(r=e.dependencyIndex)==null?void 0:r.get(c);if(l!==void 0)return c.subSearchCursor=l.nextSub??i,l;if(e.dependencyIndex!==void 0)return}let a=s.nextSub??i;for(;a!==s;){if(++n,a.sub===e)return c.subSearchCursor=a.nextSub??i,t&&Q(e,n),a;a=a.nextSub??i}t&&Q(e,n)}class N{constructor(e={}){g(this,"adaptiveDependencyIndex");g(this,"activeSub",null);g(this,"batchDepth",0);g(this,"effectReadIndex",0);g(this,"effectWriteIndex",0);g(this,"invalidationDepth",0);g(this,"dependencyDepth",0);g(this,"flushingEffects",!1);g(this,"effectQueue",new Array(16).fill(void 0));g(this,"invalidationStack",new Array(512).fill(null));g(this,"dependencyStack",[]);g(this,"handleReaders",this.createHandleReaders());g(this,"effectScheduler");g(this,"flushEffects");g(this,"_effectImmediate");this.adaptiveDependencyIndex=e.adaptiveDependencyIndex??!1,this.effectScheduler=e.effectScheduler,this._effectImmediate=this.effect.bind(this),this.signal=this.signal.bind(this),this.computed=this.computed.bind(this),this.reactive=this.reactive.bind(this),this.watch=this.watch.bind(this),this.batch=this.batch.bind(this),this.untracked=this.untracked.bind(this),this.scope=this.scope.bind(this),this._signalAccess=this._signalAccess.bind(this),this._computedAccess=this._computedAccess.bind(this),this._ownedSignal=this._ownedSignal.bind(this),this._ownedComputed=this._ownedComputed.bind(this),this._isTracking=this._isTracking.bind(this),this._isFlushingEffects=this._isFlushingEffects.bind(this),this._startBatch=this._startBatch.bind(this),this._endBatch=this._endBatch.bind(this),this.effect=this.effectScheduler===void 0?this._effectImmediate:this.createScheduledEffect.bind(this),this.flushEffects=this.effectScheduler===void 0?this.flushSyncEffects:this.flushExternalEffects}signal(e){let t=e,i;const s=()=>{const a=i;if(a!==void 0)return this.readSignalValue(a);const r=this.activeSub;return r===null||(i=this.materializeSignal(t,r)),t},n=a=>{const r=i;if(r!==void 0){this.writeSignalValue(r,a);return}t!==a&&(t=a)};return{get:s,peek:this.handleReaders.peek,set:n}}reactive(e){return B.reactiveObject(this,e,()=>this.createReactivePropertySourceBridge())}computed(e){if(typeof e=="function")return this.createComputedHandle(e);const t=this.createComputedHandle(e.get);return t.set=e.set,t}effect(e,t){const i=this.allocateGraphRecord(void 0,e,d|w|y);t!==void 0&&(i.onStop=t);const s=this.activeSub;s!==null&&s.flags&y&&R.appendScopeChild(s,i);try{this.executeEffect(i)}catch(n){try{this.disposeEffect(i)}catch(a){throw new AggregateError([n,a],"Effect registration and cleanup failed.")}throw n}return this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}watch(e,t,i){return G.watchWithReactivity(this.effect,this.untracked,e,t,i)}batch(e){++this.batchDepth;let t;try{t=e()}catch(i){if(!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(s){throw new AggregateError([i,s],"Batch operation and effect queue processing failed.")}throw i}return!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),t}untracked(e){if(this.activeSub===null)return e();const t=this.activeSub;this.activeSub=null;try{return e()}finally{this.activeSub=t}}scope(){return new R.ScopeNode(this)}allocateGraphRecord(e,t,i){return{flags:i,firstSub:null,lastSub:null,firstDep:null,tailDep:null,depCount:0,evaluate:t,cachedValue:e}}createReactivePropertySourceBridge(){return{batch:this.batch,create:()=>{if(this.activeSub===null)throw new Error("[Raph] Reactive property source requires a tracked read.");return{firstSub:null,lastSub:null}},isTracking:this._isTracking,track:e=>{const t=this.activeSub;t!==null&&this.trackDependency(e,t)},trigger:e=>{const t=e.firstSub;t!==null&&(this.propagateInvalidation(t),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}}}materializeSignal(e,t,i){const s=this.allocateGraphRecord(e,void 0,0);return i!==void 0&&(s.sourceHooks=i,s.flags|=v),t.flags&O?this.appendInitialDependency(s,t):this.trackDependency(s,t),s}createComputedHandle(e,t){const i={evaluate:e,get:this.handleReaders.initializeComputed,peek:this.handleReaders.peek};return t!==void 0&&(i.sourceHooks=t),i}createScheduledEffect(e,t){const i=this.allocateGraphRecord(void 0,e,d|w|y);let s=!1;t!==void 0&&(i.onStop=t);const n=this.activeSub;n!==null&&n.flags&y&&R.appendScopeChild(n,i);const a=()=>{if(!(!(i.flags&x)||i.evaluate===void 0)){i.flags&=~x,s=!0;try{this.executeEffect(i)}catch(r){if(!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(l){throw new AggregateError([r,l],"Effect execution and queue processing failed.")}throw r}!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}};try{const r=this.effectScheduler.bind(a);r.cancelPending=()=>i.evaluate===void 0||!s?!1:(this.settleCancelledDependencies(i),i.flags&=~(x|E|C),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),!0),r.rearmPending=()=>{i.evaluate!==void 0&&(i.flags|=d|x,r.schedule())},i.effectTask=r,this.requestEffectTask(i)}catch(r){try{this.disposeEffect(i)}catch(l){throw new AggregateError([r,l],"Effect registration and cleanup failed.")}throw r}return!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}readSignalValue(e){const t=this.activeSub;let i=null;return t!==null&&(i=this.trackDependency(e,t)),e.flags&d&&this.commitSignalValue(e,i),e.cachedValue}writeSignalValue(e,t){if(e.flags&d||this.batchDepth){const s=!!(e.flags&d);if((e.flags&d?e.pendingValue:e.cachedValue)===t)return;if(e.pendingValue=t,e.cachedValue===t)e.flags&=~d,e.pendingAuthor=void 0;else{e.flags|=d;const r=this.activeSub!==null&&this.activeSub.flags&b?this.activeSub:null;s?e.pendingAuthor!==r&&(e.pendingAuthor=null):e.pendingAuthor=r}const a=e.firstSub;a!=null&&(!s||this.activeSub!==null&&this.activeSub.flags&b||e.flags&v)&&this.propagateInvalidation(a,!0);return}if(e.cachedValue===t)return;e.cachedValue=t;const i=e.firstSub;i!=null&&(this.propagateInvalidation(i),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}readComputedValue(e){const t=e.flags,i=t&(W|E|m),s=i===0?e.cachedValue:i&W?this.evaluateFirstValue(e):this.refreshComputedValue(e,t);if(e.evaluate===void 0)return s;const n=this.activeSub;if(n!==null){const a=n.tailDep;n.flags&O?(a===null||a.dep!==e)&&this.appendInitialDependency(e,n):this.trackDependency(e,n)}return s}evaluateFirstValue(e){const t=e.flags&v,i=e.evaluate;e.flags=t|b|O;const s=this.activeSub;this.activeSub=e;try{const n=i();return e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,n):(e.cachedValue=n,e.flags=t|(e.flags&C?d:0),n)}catch(n){throw e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,n):(e.flags=t|m,s!==null&&this.trackDependency(e,s),n)}finally{this.activeSub=s}}refreshComputedValue(e,t,i=null){try{if(t&(d|C|m))return this.recomputeValue(e,i)||(e.flags&=~E),e.cachedValue;const s=e.firstDep;return(s===null||!this.refreshDependencies(e,s)||!this.recomputeValue(e,i))&&(e.flags&=~E),e.cachedValue}catch(s){throw this.activeSub!==null&&this.trackDependency(e,this.activeSub),s}}commitSignalValue(e,t){const i=e.cachedValue;e.cachedValue=e.pendingValue,e.flags&=~d;const s=e.pendingAuthor;return e.pendingAuthor=void 0,i===e.cachedValue?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t,s??null),!0)}trackDependency(e,t){const i=t.tailDep;if(i!==null){const n=i.nextDep;return n!==null&&n.dep===e?(t.tailDep=n,n):i.dep===e?i:this.reconcileDependency(e,t,i,n)}const s=t.firstDep;return s===null?t.tailDep=this.insertDependency(e,t,null):s.dep===e?(t.tailDep=s,s):this.reconcileDependency(e,t,null,s)}reconcileDependency(e,t,i,s){if(t.depCount<=_){for(let l=s;l!==null;l=l.nextDep)if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;for(let l=i;l!==null;l=l.prevDep)if(l.dep===e)return l;return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}let n=0,a=s;for(;a!==null&&n<_;){const l=a;if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;a=l.nextDep,++n}let r=i;for(;r!==null&&n<_;){const l=r;if(l.dep===e)return l;r=l.prevDep,++n}if(a!==null||r!==null){const l=j(e,t,this.adaptiveDependencyIndex);if(l!==void 0)return this.reorderDependency(t,l,i),t.tailDep=l,l}return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}appendInitialDependency(e,t){var a;const i=t.tailDep,s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};return s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n,i===null?t.firstDep=n:i.nextDep=n,t.tailDep=n,++t.depCount,(a=t.dependencyIndex)==null||a.set(e,n),e.flags&v&&this.attachDependencyCleanup(n),n}insertDependency(e,t,i){var r;const s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n;const a=i===null?t.firstDep:i.nextDep;return n.nextDep=a,a!==null&&(a.prevDep=n),i===null?t.firstDep=n:i.nextDep=n,++t.depCount,(r=t.dependencyIndex)==null||r.set(e,n),e.flags&v&&this.attachDependencyCleanup(n),n}reuseDepLink(e,t){const i=e.sub.dependencyIndex;this.detachSubLink(e);const s=t.lastSub;return e.dep=t,e.prevSub=s,e.nextSub=null,s===null?t.firstSub=e:s.nextSub=e,t.lastSub=e,i==null||i.set(t,e),t.flags&v&&this.attachDependencyCleanup(e),e}reorderDependency(e,t,i){if(t.prevDep===i)return;const s=t.prevDep,n=t.nextDep;s===null?e.firstDep=n:s.nextDep=n,n!==null&&(n.prevDep=s);const a=i===null?e.firstDep:i.nextDep;t.prevDep=i,t.nextDep=a,a!==null&&(a.prevDep=t),i===null?e.firstDep=t:i.nextDep=t}pruneUnreadDependencies(e){const t=e.tailDep;let i=t===null?e.firstDep:t.nextDep;if(i!==null){for(t===null?e.firstDep=null:t.nextDep=null;i!==null;){const s=i.nextDep;this.detachSubLink(i),--e.depCount,i=s}e.depCount<=_&&U(e)}}detachSubLink(e){var t;if((t=e.sub.dependencyIndex)==null||t.delete(e.dep),e.dep.flags&v){const i=e.dependencyCleanup;e.dependencyCleanup=void 0,i==null||i()}e.dep.subSearchCursor===e&&(e.dep.subSearchCursor=e.nextSub??(e.dep.firstSub===e?void 0:e.dep.firstSub??void 0)),e.prevSub===null?e.dep.firstSub=e.nextSub:e.prevSub.nextSub=e.nextSub,e.nextSub===null?e.dep.lastSub=e.prevSub:e.nextSub.prevSub=e.prevSub}attachDependencyCleanup(e){var n,a;const t=e.dep.sourceHooks,i=t==null?void 0:t.onDependency;if(i===void 0)return;const s=((n=e.sub.sourceHooks)==null?void 0:n.dependencyOwner)??((a=e.sub.effectTask)==null?void 0:a.dependencyOwner);s!==void 0&&(e.dependencyCleanup=i(s))}releaseSourceSubs(e){if(!(e.flags&v)){let i=e.firstSub;for(;i!=null;){const s=i.nextSub,n=i.sub;this.detachDependencyLink(i),this.detachSubLink(i);const a=n.flags;(a&(E|b))===w&&(n.flags=a&~w|d,a&y&&this.enqueueEffect(n,e)),i=s}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0;return}let t=e.firstSub;for(;t!=null;){const i=t.nextSub;this.detachDependencyLink(t),this.detachSubLink(t),t=i}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0}detachDependencyLink(e){const t=e.sub,i=e.prevDep,s=e.nextDep;i===null?t.firstDep=s:i.nextDep=s,s!==null&&(s.prevDep=i),t.tailDep===e&&(t.tailDep=i),--t.depCount,t.depCount<=_&&U(t)}enqueueEffect(e,t){var s,n;const i=t!==void 0&&t.flags&v?t.sourceHooks.token:void 0;i!==void 0&&((n=(s=e.effectTask)==null?void 0:s.notify)==null||n.call(s,i)),!(e.flags&x)&&(e.flags|=x,this.effectQueue[this.effectWriteIndex]=e,++this.effectWriteIndex)}propagateInvalidation(e,t=!1){var n,a,r,l,h,D,u,o,I,A,L,q,F;const i=this.invalidationDepth;let s=i;for(let p=e;p!==null;p=p.nextSub){const f=p.sub,S=f.flags;let k=0;if(S&(E|b)?S&b?k=this.invalidateReadDependency(p,f,S):!t&&!(S&d)&&(f.flags=S&~w|d):(k=S&~C|(t?w:d),f.flags=k),k===0){if(S&y&&S&x){const T=p.dep.flags&v?p.dep.sourceHooks.token:void 0;T!==void 0&&((a=(n=f.effectTask)==null?void 0:n.notify)==null||a.call(n,T))}else f.flags&v&&((l=(r=f.sourceHooks).onInvalidate)==null||l.call(r,f.firstSub!==null),f.firstSub!==null&&(this.invalidationStack[s++]=f.firstSub));continue}k&y?(!(k&b)||((h=f.effectTask)==null?void 0:h.allowScheduleDuringRun)===!0)&&this.enqueueEffect(f,p.dep):(!(S&E)&&f.flags&v&&((u=(D=f.sourceHooks).onInvalidate)==null||u.call(D,f.firstSub!==null)),f.firstSub!==null&&(this.invalidationStack[s++]=f.firstSub))}for(;s!==i;){let p=this.invalidationStack[--s];do{const f=p.sub,S=f.flags;let k=0;if(S&(E|b)?S&b&&(k=this.invalidateReadDependency(p,f,S)):(k=S&~C|w,f.flags=k),k===0){if(S&y&&S&x){const T=p.dep.flags&v?p.dep.sourceHooks.token:void 0;T!==void 0&&((I=(o=f.effectTask)==null?void 0:o.notify)==null||I.call(o,T))}else if(f.flags&v&&((L=(A=f.sourceHooks).onInvalidate)==null||L.call(A,f.firstSub!==null),f.firstSub!==null)){p.nextSub!==null&&(this.invalidationStack[s++]=p.nextSub),p=f.firstSub;continue}}else if(k&y)this.enqueueEffect(f,p.dep);else if(!(S&E)&&f.flags&v&&((F=(q=f.sourceHooks).onInvalidate)==null||F.call(q,f.firstSub!==null)),f.firstSub!==null){p.nextSub!==null&&(this.invalidationStack[s++]=p.nextSub),p=f.firstSub;continue}p=p.nextSub}while(p!==null)}this.invalidationDepth=i}invalidateReadDependency(e,t,i){const s=t.tailDep;if(s===null)return 0;if(e!==s){for(let a=e.prevDep;a!==null;a=a.prevDep)if(a===s)return 0}const n=i|C|w;return t.flags=n,n}confirmSubUpdates(e,t,i=null){var s,n;for(let a=e;a!==null;a=a.nextSub){if(a===t||a.sub===i)continue;const r=a.sub,l=r.flags;l&d||(r.flags=l&~w|d,l&y?this.enqueueEffect(r,a.dep):l&E||r.flags&v&&((n=(s=r.sourceHooks).onInvalidate)==null||n.call(s,r.firstSub!==null)))}}recomputeValue(e,t=null){const i=e.evaluate;e.tailDep=null;const s=e.flags&~C|b;e.flags=s;const n=this.activeSub;this.activeSub=e;let a;try{a=i()}catch(u){throw e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,u):(this.restoreComputedFailure(e,t),u)}finally{this.activeSub=n}if(e.evaluate!==i)return e.cachedValue=void 0,e.flags=0,!1;const r=e.flags&C,l=s&-268,h=e.tailDep;(h===null?e.firstDep:h.nextDep)!==null&&this.pruneUnreadDependencies(e);const D=e.cachedValue;return e.cachedValue=a,e.flags=s&v|(r?d:l),D===a?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t),!0)}restoreComputedFailure(e,t){e.flags=e.flags&-144|m,this.pruneUnreadDependencies(e),t!==null&&this.restoreFailedDependency(t.sub)}refreshDependencies(e,t){const i=this.dependencyDepth;try{return this.walkDependencyChain(e,t)}catch(s){this.restoreFailedDependency(e);for(let n=i;n<this.dependencyDepth;n++)this.restoreFailedDependency(this.dependencyStack[n].sub);throw this.dependencyDepth=i,this.dependencyStack.length=i,s}}settleCancelledDependencies(e){for(let t=e.firstDep;t!==null;t=t.nextDep){const i=t.dep,s=i.flags;s&(W|E|m)&&(i.evaluate===void 0?s&d&&this.commitSignalValue(i,t):this.refreshComputedValue(i,s,t))}}walkDependencyChain(e,t){const i=this.dependencyDepth;let s=i,n=!1;e:for(;;){if(e.flags&d)n=!0;else{const a=t.dep,r=a.flags;if(r&(d|m))this.dependencyDepth=s,n=a.evaluate===void 0?this.commitSignalValue(a,t):this.recomputeValue(a,t);else if(r&w){if(a.firstDep!==null){this.dependencyStack[s++]=t,e=a,t=a.firstDep;continue e}this.dependencyDepth=s,n=this.recomputeValue(a,t)}else n=!1}if(!n&&t.nextDep!==null){t=t.nextDep;continue}if(n){for(;s!==i;){this.dependencyDepth=--s;const a=this.dependencyStack[s];if(n=this.recomputeValue(e,a),e=a.sub,!n){if(a.nextDep!==null){t=a.nextDep;continue e}break}}if(n)return this.dependencyDepth=i,!0}for(;s!==i;){this.dependencyDepth=--s;const a=this.dependencyStack[s];if(e.flags&=~w,e=a.sub,a.nextDep!==null){t=a.nextDep;continue e}}return e.flags&=~w,this.dependencyDepth=i,!1}}restoreFailedDependency(e){e.flags&=-16,e.flags&y||(e.flags|=m)}executeEffect(e){const t=e.flags;if(!(t&E))return;if(!(t&(d|C))){const h=e.firstDep;try{if(h===null||!this.refreshDependencies(e,h)){e.flags&=~E;return}}catch(D){throw e.flags&=-80,D}}const i=e.evaluate;if(i===void 0){e.flags&=~E;return}let s;if(e.scopeLastChild!==void 0)try{this.disposeNestedEffects(e)}catch(h){s=[h]}const n=typeof e.cachedValue=="function"?e.cachedValue:void 0;if(e.cachedValue=void 0,e.flags&=~C,n!==void 0)try{this.untracked(n)}catch(h){s??(s=[]),s.push(h)}if(s!==void 0){try{this.disposeEffect(e)}catch(h){s.push(h)}R.throwScopeErrors(s,"Effect cleanup failed.")}if(e.evaluate===void 0)return;e.tailDep=null,e.flags=e.flags&~C|b;const a=this.activeSub;this.activeSub=e;let r;try{r=i()}catch(h){this.activeSub=a,e.flags&=~b,this.failEffectRun(e,h,s)}finally{this.activeSub=a,e.flags&=~b}const l=e.tailDep;if((l===null?e.firstDep:l.nextDep)!==null&&this.pruneUnreadDependencies(e),typeof r=="function")if(e.evaluate===void 0)try{this.untracked(r)}catch(h){s??(s=[]),s.push(h)}else e.cachedValue=r;e.flags&x?e.flags=e.flags&-7|w:e.flags&=-8}failEffectRun(e,t,i){const s=i??[];s.push(t),this.pruneUnreadDependencies(e);try{this.disposeNestedEffects(e)}catch(n){s.push(n)}throw e.flags&=-72,s.length===1?s[0]:new AggregateError(s,"Effect execution failed.")}flushSyncEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(y|b))===(y|b))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex,t.flags&=~x,this.executeEffect(t)}}catch(e){this.drainQueueAfterError(e,!1)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}flushExternalEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(y|b))===(y|b))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex;const i=t.effectTask;i===void 0?(t.flags&=~x,this.executeEffect(t)):t.evaluate!==void 0?this.scheduleEffectTask(t,i):t.flags&=~x}}catch(e){this.drainQueueAfterError(e,!0)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}drainQueueAfterError(e,t){const i=[e];for(;this.effectReadIndex!==this.effectWriteIndex;){const s=this.effectReadIndex++,n=this.effectQueue[s];this.effectQueue[s]=void 0;try{const a=t?n.effectTask:void 0;a!==void 0&&n.evaluate!==void 0?this.scheduleEffectTask(n,a):(n.flags&=~x,this.executeEffect(n))}catch(a){i.push(a)}}throw i.length===1?e:new AggregateError(i,"Effect queue processing failed.")}requestEffectTask(e){const t=e.effectTask;t===void 0||e.evaluate===void 0||(e.flags|=x,this.scheduleEffectTask(e,t))}scheduleEffectTask(e,t){try{t.schedule()}catch(i){throw e.flags&=-72,i}}disposeEffect(e){if(e.evaluate===void 0)return;this.activeSub===e&&(this.activeSub=null);const t=e.effectTask,i=e.onStop,s=typeof e.cachedValue=="function"?e.cachedValue:void 0;e.evaluate=void 0,e.flags=0,e.cachedValue=void 0,e.effectTask=void 0,i!==void 0&&(e.onStop=void 0),R.detachScopeChild(e);let n=e.firstDep;for(;n!==null;){const r=n.nextDep;this.detachSubLink(n),n=r}e.firstDep=e.tailDep=null,e.depCount=0,U(e);let a;if(t!==void 0)try{t.dispose()}catch(r){a=[r]}try{this.disposeNestedEffects(e)}catch(r){a??(a=[]),a.push(r)}if(s!==void 0)try{this.untracked(s)}catch(r){a??(a=[]),a.push(r)}if(i!==void 0)try{this.untracked(i)}catch(r){a??(a=[]),a.push(r)}this.invalidationStack.length=this.invalidationDepth,this.dependencyStack.length=this.dependencyDepth,a!==void 0&&R.throwScopeErrors(a,"Effect disposal failed.")}disposeNestedEffects(e){let t;for(;e.scopeLastChild!==void 0;)try{this.disposeEffect(e.scopeLastChild)}catch(i){t??(t=[]),t.push(i)}t!==void 0&&R.throwScopeErrors(t,"Nested effect disposal failed.")}static materializeComputed(e){e.flags=W|(e.sourceHooks===void 0?0:v),e.firstSub=null,e.lastSub=null,e.firstDep=null,e.tailDep=null,e.depCount=0,e.cachedValue=void 0}createHandleReaders(){const e=this;function t(){return e.readComputedValue(this)}return{initializeComputed(){const i=this;return N.materializeComputed(i),this.get=t,e.readComputedValue(i)},initializeRaw(){const i=this;N.materializeComputed(i);const s=()=>e.readComputedValue(i);return this.read=s,s()},peek(){return e.peekHandle(this)}}}peekHandle(e){const t=this.activeSub;if(t===null)return e.get();this.activeSub=null;try{return e.get()}finally{this.activeSub=t}}_signalAccess(e){const t=this;let i=e,s;function n(a){if(arguments.length!==0){const h=s;h!==void 0?t.writeSignalValue(h,a):i!==a&&(i=a);return}const r=s;if(r!==void 0)return t.readSignalValue(r);const l=t.activeSub;return l===null||(s=t.materializeSignal(i,l)),i}return{read:n,write:n}}_computedAccess(e){return{evaluate:e,read:this.handleReaders.initializeRaw}}_ownedSignal(e,t){const i=this;let s=e,n,a=!1;const r=()=>{const u=n;if(u!==void 0)return this.readSignalValue(u);const o=this.activeSub;if(o===null)return s;const I=s;return n=this.materializeSignal(I,o,t),s=void 0,I},l=t===void 0?u=>{const o=n;o!==void 0?this.writeSignalValue(o,u):s!==u&&(s=u)}:u=>{var I,A;const o=n;if(o!==void 0){const L=o.flags&d?o.pendingValue:o.cachedValue;L!==u&&((I=t.onChange)==null||I.call(t,L,u,o.firstSub!==null)),this.writeSignalValue(o,u);return}s!==u&&((A=t.onChange)==null||A.call(t,s,u,!1),s=u)},D={source:{get:r,peek:this.handleReaders.peek,set:l},dispose(){var o;if(a)return!0;a=!0,s=void 0;let u;try{(o=t==null?void 0:t.onDispose)==null||o.call(t,(n==null?void 0:n.firstSub)!=null)}catch(I){(u??(u=[])).push(I)}if(n!==void 0){try{i.releaseSourceSubs(n)}catch(I){(u??(u=[])).push(I)}n.cachedValue=void 0,n.pendingValue=void 0,n.pendingAuthor=void 0,n.flags=0}if(n=void 0,(u==null?void 0:u.length)===1)throw u[0];if(u!==void 0&&u.length>1)throw new AggregateError(u,"Owned signal disposal failed.");return!0}};return t!==void 0&&(D.hasSubscribers=()=>(n==null?void 0:n.firstSub)!=null),D}_ownedComputed(e,t){const i=this,s=this.createComputedHandle(e,t);let n=!1;const a={source:s,dispose(){var h;if(n)return!0;n=!0,i.activeSub===s&&(i.activeSub=null);const r=[];try{(h=t==null?void 0:t.onDispose)==null||h.call(t,s.firstSub!=null)}catch(D){r.push(D)}if(s.flags!==void 0)try{i.releaseSourceSubs(s)}catch(D){r.push(D)}let l=s.firstDep;for(;l!=null;){const D=l.nextDep;i.detachSubLink(l),l=D}if(s.firstDep=s.tailDep=null,s.depCount=0,U(s),s.evaluate=void 0,s.cachedValue=void 0,s.flags=0,r.length===1)throw r[0];if(r.length>1)throw new AggregateError(r,"Owned computed disposal failed.");return!0}};return t!==void 0&&(a.hasSubscribers=()=>s.firstSub!=null),a}_startBatch(){++this.batchDepth}_endBatch(){!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}_isTracking(){return this.activeSub!==null}_isFlushingEffects(){return this.flushingEffects}}let K;function V(){return K??(K=new N)}const Y=c=>V().signal(c),X=c=>V().reactive(c);function Z(c){return V().computed(c)}const J=(c,e)=>V().effect(c,e),$=(c,e,t)=>V().watch(c,e,t),H=c=>V().batch(c),ee=c=>V().untracked(c),te=()=>V().scope();exports.ScopeNode=R.ScopeNode;exports.RaphReactivity=N;exports.batch=H;exports.computed=Z;exports.effect=J;exports.reactive=X;exports.scope=te;exports.signal=Y;exports.untracked=ee;exports.watch=$;