@but212/atom-effect 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -54,10 +54,10 @@ Runs a function immediately and re-runs it whenever its dependencies change.
54
54
  - Returns a `dispose` function to stop the effect.
55
55
 
56
56
  ```typescript
57
- const stop = effect(() => {
57
+ const eff = effect(() => {
58
58
  console.log(count.value);
59
59
  });
60
- stop();
60
+ eff.dispose(); // Stop the effect
61
61
  ```
62
62
 
63
63
  ### `batch(fn)`
@@ -122,10 +122,10 @@ const user = computed(async () => {
122
122
  ### Type Guards
123
123
 
124
124
  ```typescript
125
- import { isAtom, isComputed, isReactive } from '@but212/atom-effect';
125
+ import { isAtom, isComputed, isEffect } from '@but212/atom-effect';
126
126
 
127
- isAtom(count); // true
128
- isReactive(double); // true
127
+ isAtom(count); // true
128
+ isComputed(double); // true
129
129
  ```
130
130
 
131
131
  ### Development Commands
@@ -143,14 +143,14 @@ pnpm build # Build production bundle
143
143
 
144
144
  | Operation | Performance |
145
145
  | --- | --- |
146
- | Atom creation (x1000) | ~13.9K ops/sec |
147
- | Atom read (x1000) | ~38.1K ops/sec |
148
- | Atom write (x1000) | ~343K ops/sec |
146
+ | Atom creation (x1000) | ~11.8K ops/sec |
147
+ | Atom read (x1000) | ~38.0K ops/sec |
148
+ | Atom write (x1000) | ~366K ops/sec |
149
149
  | Computed creation | ~2.17M ops/sec |
150
- | Computed recomputation | ~564K ops/sec |
151
- | Effect execution | ~2.78M ops/sec |
152
- | Batched updates (x2) | ~3.97M ops/sec |
153
- | Deep chain (100 levels) | ~8.16K ops/sec |
150
+ | Computed recomputation | ~589K ops/sec |
151
+ | Effect execution | ~2.46M ops/sec |
152
+ | Batched updates (x2) | ~3.55M ops/sec |
153
+ | Deep chain (100 levels) | ~9.68K ops/sec |
154
154
 
155
155
  ## Contributing
156
156
 
@@ -1,2 +1,2 @@
1
- (function(h,N){typeof exports=="object"&&typeof module<"u"?N(exports):typeof define=="function"&&define.amd?define(["exports"],N):(h=typeof globalThis<"u"?globalThis:h||self,N(h.AtomEffect={}))})(this,(function(h){"use strict";const N={ONE_SECOND_MS:1e3},O={IDLE:"idle",PENDING:"pending",RESOLVED:"resolved",REJECTED:"rejected"},P={DISPOSED:1,EXECUTING:2},u={DIRTY:1,IDLE:2,PENDING:4,RESOLVED:8,REJECTED:16,RECOMPUTING:32,HAS_ERROR:64},U={SYNC:1,NOTIFICATION_SCHEDULED:2},ne={MAX_SIZE:1e3,WARMUP_SIZE:100},b={MAX_EXECUTIONS_PER_SECOND:1e3,CLEANUP_THRESHOLD:1e3,MAX_EXECUTIONS_PER_EFFECT:100,MAX_EXECUTIONS_PER_FLUSH:1e4,MAX_FLUSH_ITERATIONS:1e3,MIN_FLUSH_ITERATIONS:10},w={MAX_DEPENDENCIES:1e3,WARN_INFINITE_LOOP:!0},y=1073741823,B=1<<20-1,f=typeof process<"u"&&process.env&&process.env.NODE_ENV!=="production",re=Object.freeze([]);class E extends Error{constructor(e,t=null,i=!0){super(e),this.name="AtomError",this.cause=t,this.recoverable=i,this.timestamp=new Date}}class D extends E{constructor(e,t=null){super(e,t,!0),this.name="ComputedError"}}class I extends E{constructor(e,t=null){super(e,t,!1),this.name="EffectError"}}class F extends E{constructor(e,t=null){super(e,t,!1),this.name="SchedulerError"}}const c={COMPUTED_MUST_BE_FUNCTION:"Computed function must be a function",COMPUTED_ASYNC_PENDING_NO_DEFAULT:"Async computation is pending. No default value provided",COMPUTED_COMPUTATION_FAILED:"Computed computation failed",COMPUTED_ASYNC_COMPUTATION_FAILED:"Async computed computation failed",ATOM_SUBSCRIBER_MUST_BE_FUNCTION:"Subscription listener must be a function or Subscriber object",ATOM_INDIVIDUAL_SUBSCRIBER_FAILED:"Error during individual atom subscriber execution",EFFECT_MUST_BE_FUNCTION:"Effect function must be a function",EFFECT_EXECUTION_FAILED:"Effect execution failed",EFFECT_CLEANUP_FAILED:"Effect cleanup function execution failed",CALLBACK_ERROR_IN_ERROR_HANDLER:"Error occurred during onError callback execution"},j=Symbol("debugName"),oe=Symbol("id"),z=Symbol("type"),q=Symbol("noDefaultValue");function he(s){return"dependencies"in s&&Array.isArray(s.dependencies)}let G=0;function X(s,e,t){if(s._visitedEpoch!==t){if(s._visitedEpoch=t,s===e)throw new D("Indirect circular dependency detected");if(he(s)){const i=s.dependencies;for(let n=0;n<i.length;n++){const r=i[n];r&&X(r,e,t)}}}}const d={enabled:typeof process<"u"&&process.env?.NODE_ENV==="development",maxDependencies:w.MAX_DEPENDENCIES,warnInfiniteLoop:w.WARN_INFINITE_LOOP,warn(s,e){this.enabled&&s&&console.warn(`[Atom Effect] ${e}`)},checkCircular(s,e){if(s===e)throw new D("Direct circular dependency detected");this.enabled&&(G++,X(s,e,G))},attachDebugInfo(s,e,t){if(!this.enabled)return;const i=s;i[j]=`${e}_${t}`,i[oe]=t,i[z]=e},getDebugName(s){if(s!=null&&j in s)return s[j]},getDebugType(s){if(s!=null&&z in s)return s[z]}};let ue=1;const ce=()=>ue++;class H{constructor(){this.flags=0,this.version=0,this._lastSeenEpoch=-1,this.id=ce()&y}rotatePhase(){return this.version=this.version+1&y,this.version}getShift(e){return this.version-e&y}}class Y extends H{subscribe(e){if(typeof e=="object"&&e!==null&&"execute"in e)return this._addSubscriber(this._getObjSubs(),e);if(typeof e!="function")throw new E(c.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);return this._addSubscriber(this._getFnSubs(),e)}subscriberCount(){return(this._fnSubs?.length??0)+(this._objSubs?.length??0)}_addSubscriber(e,t){if(e.indexOf(t)!==-1)return()=>{};e.push(t);let i=!1;return()=>{if(i)return;i=!0;const n=e.indexOf(t);if(n!==-1){const r=e.length-1;n!==r&&(e[n]=e[r]),e.pop()}}}_notifySubscribers(e,t){const i=this._fnSubs;if(i)for(let r=i.length-1;r>=0;r--)try{const o=i[r];o&&o(e,t)}catch(o){console.error(new E(c.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED,o))}const n=this._objSubs;if(n)for(let r=n.length-1;r>=0;r--)try{const o=n[r];o&&o.execute()}catch(o){console.error(new E(c.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED,o))}}}class k{constructor(){this.pool=[],this.maxPoolSize=50,this.maxReusableCapacity=256,this.stats=f?{acquired:0,released:0,rejected:{frozen:0,tooLarge:0,poolFull:0}}:null}acquire(){return f&&this.stats&&this.stats.acquired++,this.pool.pop()??[]}release(e,t){if(!(t&&e===t)){if(Object.isFrozen(e)){f&&this.stats&&this.stats.rejected.frozen++;return}if(e.length>this.maxReusableCapacity){f&&this.stats&&this.stats.rejected.tooLarge++;return}if(this.pool.length>=this.maxPoolSize){f&&this.stats&&this.stats.rejected.poolFull++;return}e.length=0,this.pool.push(e),f&&this.stats&&this.stats.released++}}getStats(){if(!f||!this.stats)return null;const{acquired:e,released:t,rejected:i}=this.stats,n=i.frozen+i.tooLarge+i.poolFull;return{acquired:e,released:t,rejected:i,leaked:e-t-n,poolSize:this.pool.length}}reset(){this.pool.length=0,f&&this.stats&&(this.stats.acquired=0,this.stats.released=0,this.stats.rejected={frozen:0,tooLarge:0,poolFull:0})}}const _=Object.freeze([]),p=Object.freeze([]),a=Object.freeze([]),C=new k,m=new k,g=new k;function $(s){return s!==null&&typeof s=="object"&&"value"in s&&"subscribe"in s&&typeof s.subscribe=="function"}function _e(s){if(d.enabled&&(s==null||typeof s=="object")){const e=d.getDebugType(s);if(e)return e==="computed"}return $(s)&&"invalidate"in s&&typeof s.invalidate=="function"}function ae(s){return s!==null&&typeof s=="object"&&"dispose"in s&&"run"in s&&typeof s.dispose=="function"&&typeof s.run=="function"}function J(s){return s!=null&&typeof s.then=="function"}function le(s){return typeof s=="object"&&s!==null}function fe(s){return(typeof s=="object"||typeof s=="function")&&s!==null&&typeof s.addDependency=="function"}function de(s){return typeof s=="function"&&typeof s.addDependency!="function"}function Ee(s){return le(s)&&typeof s.execute=="function"}function W(s,e,t,i){if(e){if(fe(e)){e.addDependency(s);return}if(de(e)){const n=e;t.indexOf(n)===-1&&t.push(n);return}Ee(e)&&i.indexOf(e)===-1&&i.push(e)}}function pe(s,e,t,i){if(e!==_&&t!==p)for(let r=0;r<e.length;r++){const o=e[r];o&&(o._tempUnsub=t[r])}const n=m.acquire();n.length=s.length;for(let r=0;r<s.length;r++){const o=s[r];o&&(o._tempUnsub?(n[r]=o._tempUnsub,o._tempUnsub=void 0):(d.checkCircular(o,i),n[r]=o.subscribe(i)))}if(e!==_)for(let r=0;r<e.length;r++){const o=e[r];o?._tempUnsub&&(o._tempUnsub(),o._tempUnsub=void 0)}return t!==p&&m.release(t),n}let v=0;function K(){return v=v+1&y||1,v}function ge(){return v}let L=0,Q=0,M=!1;function Z(){return M?(f&&console.warn("Warning: startFlush() called during flush - ignored to prevent infinite loop detection bypass"),!1):(M=!0,L=L+1&y,Q=0,!0)}function ee(){M=!1}function Se(){return M?++Q:0}class be{constructor(){this.queueA=[],this.queueB=[],this.queue=this.queueA,this.queueSize=0,this.urgentQueueA=[],this.urgentQueueB=[],this.urgentQueue=this.urgentQueueA,this.urgentQueueSize=0,this._epoch=0,this.isProcessing=!1,this.isBatching=!1,this.batchDepth=0,this.batchQueue=[],this.batchQueueSize=0,this.isFlushingSync=!1,this.maxFlushIterations=b.MAX_FLUSH_ITERATIONS}get phase(){return this.isProcessing||this.isFlushingSync?2:this.isBatching?1:0}schedule(e,t){if(typeof e!="function")throw new F("Scheduler callback must be a function");if(e._nextEpoch!==this._epoch)if(e._nextEpoch=this._epoch,this.isBatching||this.isFlushingSync)this.batchQueue[this.batchQueueSize++]=e;else{const i=this._calculateUrgency(e,t);this.urgentQueue[this.urgentQueueSize]=e,this.queue[this.queueSize]=e,this.urgentQueueSize+=i,this.queueSize+=i^1,this.isProcessing||this.flush()}}_calculateUrgency(e,t){if(!t||e._cachedVersion===void 0)return 0;const i=t.getShift(e._cachedVersion);return B-1-i>>>31&1}flush(){this.isProcessing||this.queueSize===0&&this.urgentQueueSize===0||(this.isProcessing=!0,queueMicrotask(()=>{try{if(this.queueSize===0&&this.urgentQueueSize===0)return;const e=Z();this._drainQueue(),e&&ee()}finally{this.isProcessing=!1,(this.queueSize>0||this.urgentQueueSize>0)&&!this.isBatching&&this.flush()}}))}flushSync(){this.isFlushingSync=!0;const e=Z();try{this._mergeBatchQueue(),this._drainQueue()}finally{this.isFlushingSync=!1,e&&ee()}}_mergeBatchQueue(){if(this._epoch++,this.batchQueueSize>0){for(let e=0;e<this.batchQueueSize;e++){const t=this.batchQueue[e];t&&t._nextEpoch!==this._epoch&&(t._nextEpoch=this._epoch,this.queue[this.queueSize++]=t)}this.batchQueueSize=0}}_drainQueue(){let e=0;for(;this.urgentQueueSize>0||this.queueSize>0;){if(++e>this.maxFlushIterations){this._handleFlushOverflow();return}this.urgentQueueSize>0&&this._processUrgentQueue(),this.queueSize>0&&this._processCurrentQueue(),this._mergeBatchQueue()}}_processUrgentQueue(){const e=this.urgentQueue,t=this.urgentQueueSize;this.urgentQueue=this.urgentQueue===this.urgentQueueA?this.urgentQueueB:this.urgentQueueA,this.urgentQueueSize=0,this._epoch++,this._processJobs(e,t)}_processCurrentQueue(){const e=this.queue,t=this.queueSize;this.queue=this.queue===this.queueA?this.queueB:this.queueA,this.queueSize=0,this._epoch++,this._processJobs(e,t)}_handleFlushOverflow(){console.error(new F(`Maximum flush iterations (${this.maxFlushIterations}) exceeded. Possible infinite loop.`)),this.queueSize=0,this.queue.length=0,this.urgentQueueSize=0,this.urgentQueue.length=0,this.batchQueueSize=0}_processJobs(e,t){for(let i=0;i<t;i++){const n=e[i];if(n)try{n()}catch(r){console.error(new F("Error occurred during scheduler execution",r))}}e.length=0}startBatch(){this.batchDepth++,this.isBatching=!0}endBatch(){this.batchDepth=Math.max(0,this.batchDepth-1),this.batchDepth===0&&(this.flushSync(),this.isBatching=!1)}setMaxFlushIterations(e){if(e<b.MIN_FLUSH_ITERATIONS)throw new F(`Max flush iterations must be at least ${b.MIN_FLUSH_ITERATIONS}`);this.maxFlushIterations=e}}const T=new be;class ye{constructor(){this.current=null}run(e,t){const i=this.current;this.current=e;try{return t()}finally{this.current=i}}getCurrent(){return this.current}}const A=new ye;function te(s){if(typeof s!="function")throw new E("Untracked callback must be a function");const e=A.current;A.current=null;try{return s()}finally{A.current=e}}class De extends Y{constructor(e,t){super(),this._fnSubs=null,this._objSubs=null,this._value=e,t&&(this.flags|=U.SYNC),d.attachDebugInfo(this,"atom",this.id)}_getFnSubs(){return this._fnSubs??=[],this._fnSubs}_getObjSubs(){return this._objSubs??=[],this._objSubs}get value(){const e=A.current;return e&&W(this,e,this._getFnSubs(),this._getObjSubs()),this._value}set value(e){if(Object.is(this._value,e))return;const t=this._value;this._value=e,this.rotatePhase(),((this._fnSubs?.length??0)>0||(this._objSubs?.length??0)>0)&&this._scheduleNotification(t)}_scheduleNotification(e){if(this.flags&U.NOTIFICATION_SCHEDULED||(this._pendingOldValue=e,this.flags|=U.NOTIFICATION_SCHEDULED),this.flags&U.SYNC&&!T.isBatching){this._flushNotifications();return}this._notifyTask||(this._notifyTask=()=>this._flushNotifications()),T.schedule(this._notifyTask)}_flushNotifications(){if(!(this.flags&U.NOTIFICATION_SCHEDULED))return;const e=this._pendingOldValue,t=this._value;this._pendingOldValue=void 0,this.flags&=-3,this._notifySubscribers(t,e)}peek(){return this._value}dispose(){this._fnSubs=null,this._objSubs=null,this._value=void 0,this._notifyTask=void 0}}function Ie(s,e={}){return new De(s,e.sync??!1)}function x(s,e,t){if(s instanceof TypeError)return new e(`Type error (${t}): ${s.message}`,s);if(s instanceof ReferenceError)return new e(`Reference error (${t}): ${s.message}`,s);if(s instanceof E)return s;const i=s instanceof Error?s.message:String(s),n=s instanceof Error?s:null;return new e(`Unexpected error (${t}): ${i}`,n)}const se=u.RESOLVED|u.PENDING|u.REJECTED,V=Array(se+1).fill(O.IDLE);V[u.RESOLVED]=O.RESOLVED,V[u.PENDING]=O.PENDING,V[u.REJECTED]=O.REJECTED;class ie extends Y{constructor(e,t={}){if(typeof e!="function")throw new D(c.COMPUTED_MUST_BE_FUNCTION);if(super(),this._fnSubs=null,this._objSubs=null,this._cachedErrors=null,this._errorCacheEpoch=-1,this._asyncStartAggregateVersion=0,this._asyncRetryCount=0,this.MAX_ASYNC_RETRIES=3,this._value=void 0,this.flags=u.DIRTY|u.IDLE,this._error=null,this._promiseId=0,this._equal=t.equal??Object.is,this._fn=e,this._defaultValue="defaultValue"in t?t.defaultValue:q,this._hasDefaultValue=this._defaultValue!==q,this._onError=t.onError??null,this.MAX_PROMISE_ID=Number.MAX_SAFE_INTEGER-1,this._dependencies=_,this._dependencyVersions=a,this._unsubscribes=p,this._notifyJob=()=>{const i=this._fnSubs;if(i)for(let r=i.length-1;r>=0;r--)try{const o=i[r];o&&o(void 0,void 0)}catch(o){console.error(o)}const n=this._objSubs;if(n)for(let r=n.length-1;r>=0;r--)try{const o=n[r];o&&o.execute()}catch(o){console.error(o)}},this._trackable=Object.assign(()=>this._markDirty(),{addDependency:i=>{}}),d.attachDebugInfo(this,"computed",this.id),d.enabled){const i=this;i.subscriberCount=this.subscriberCount.bind(this),i.isDirty=()=>this._isDirty(),i.dependencies=this._dependencies,i.stateFlags=this._getFlagsAsString()}if(t.lazy===!1)try{this._recompute()}catch{}}_getFnSubs(){return this._fnSubs??=[],this._fnSubs}_getObjSubs(){return this._objSubs??=[],this._objSubs}get value(){return this._registerTracking(),this._computeValue()}peek(){return this._value}get state(){return this._registerTracking(),this._getAsyncState()}get hasError(){if(this._registerTracking(),this._isRejected())return!0;for(let e=0;e<this._dependencies.length;e++){const t=this._dependencies[e];if(t&&"hasError"in t&&t.hasError)return!0}return!1}get isValid(){return!this.hasError}get errors(){if(this._registerTracking(),!this.hasError)return re;const e=ge();if(this._errorCacheEpoch===e&&this._cachedErrors!==null)return this._cachedErrors;const t=new Set;this._error&&t.add(this._error);for(let i=0;i<this._dependencies.length;i++){const n=this._dependencies[i];if(n&&"errors"in n){const r=n.errors;for(let o=0;o<r.length;o++){const l=r[o];l&&t.add(l)}}}return this._cachedErrors=Object.freeze([...t]),this._errorCacheEpoch=e,this._cachedErrors}get lastError(){return this._registerTracking(),this._error}get isPending(){return this._registerTracking(),this._isPending()}get isResolved(){return this._registerTracking(),this._isResolved()}invalidate(){this._markDirty(),this._dependencyVersions!==a&&(g.release(this._dependencyVersions),this._dependencyVersions=a),this._errorCacheEpoch=-1,this._cachedErrors=null}dispose(){if(this._unsubscribes!==p){for(let e=0;e<this._unsubscribes.length;e++){const t=this._unsubscribes[e];t&&t()}m.release(this._unsubscribes),this._unsubscribes=p}this._dependencies!==_&&(C.release(this._dependencies),this._dependencies=_),this._dependencyVersions!==a&&(g.release(this._dependencyVersions),this._dependencyVersions=a),this._fnSubs=null,this._objSubs=null,this.flags=u.DIRTY|u.IDLE,this._error=null,this._value=void 0,this._promiseId=(this._promiseId+1)%this.MAX_PROMISE_ID,this._cachedErrors=null,this._errorCacheEpoch=-1}_isDirty(){return(this.flags&u.DIRTY)!==0}_setDirty(){this.flags|=u.DIRTY}_clearDirty(){this.flags&=-2}_isIdle(){return(this.flags&u.IDLE)!==0}_setIdle(){this.flags|=u.IDLE,this.flags&=-29}_isPending(){return(this.flags&u.PENDING)!==0}_setPending(){this.flags|=u.PENDING,this.flags&=-27}_isResolved(){return(this.flags&u.RESOLVED)!==0}_setResolved(){this.flags|=u.RESOLVED,this.flags&=-87}_isRejected(){return(this.flags&u.REJECTED)!==0}_setRejected(){this.flags|=u.REJECTED|u.HAS_ERROR,this.flags&=-15}_isRecomputing(){return(this.flags&u.RECOMPUTING)!==0}_setRecomputing(e){const t=u.RECOMPUTING;this.flags=this.flags&~t|-Number(e)&t}_getAsyncState(){return V[this.flags&se]}_getFlagsAsString(){const e=[];return this._isDirty()&&e.push("DIRTY"),this._isIdle()&&e.push("IDLE"),this._isPending()&&e.push("PENDING"),this._isResolved()&&e.push("RESOLVED"),this._isRejected()&&e.push("REJECTED"),this._isRecomputing()&&e.push("RECOMPUTING"),e.join(" | ")}_computeValue(){return this._isRecomputing()?this._value:((this._isDirty()||this._isIdle())&&this._recompute(),this._isPending()?this._handlePending():this._isRejected()?this._handleRejected():this._value)}_recompute(){if(this._isRecomputing())return;this._setRecomputing(!0);const e=this._prepareComputationContext();let t=!1;try{const i=A.run(this._trackable,this._fn);this._commitDependencies(e),t=!0,J(i)?this._handleAsyncComputation(i):this._handleSyncResult(i)}catch(i){if(!t)try{this._commitDependencies(e),t=!0}catch(n){this._handleComputationError(n)}this._handleComputationError(i)}finally{this._cleanupContext(e,t),this._setRecomputing(!1)}}_prepareComputationContext(){const e=this._dependencies,t=this._dependencyVersions,i=C.acquire(),n=g.acquire(),r=K(),o={depCount:0},l=S=>{S._lastSeenEpoch!==r&&(S._lastSeenEpoch=r,o.depCount<i.length?(i[o.depCount]=S,n[o.depCount]=S.version):(i.push(S),n.push(S.version)),o.depCount++)},R=this._trackable.addDependency;return this._trackable.addDependency=l,{prevDeps:e,prevVersions:t,nextDeps:i,nextVersions:n,originalAdd:R,state:o}}_commitDependencies(e){const{nextDeps:t,nextVersions:i,state:n,prevDeps:r}=e;t.length=n.depCount,i.length=n.depCount,this._unsubscribes=pe(t,r,this._unsubscribes,this),this._dependencies=t,this._dependencyVersions=i}_cleanupContext(e,t){this._trackable.addDependency=e.originalAdd,t?(e.prevDeps!==_&&C.release(e.prevDeps),e.prevVersions!==a&&g.release(e.prevVersions)):(C.release(e.nextDeps),g.release(e.nextVersions))}_getAggregateShift(){let e=0;const t=this._dependencies,i=this._dependencyVersions;for(let n=0;n<t.length;n++){const r=t[n],o=i[n];r&&o!==void 0&&(e=e+r.getShift(o)&y)}return e}isUrgent(){return this._getAggregateShift()>=B}_handleSyncResult(e){this._finalizeResolution(e)}_handleAsyncComputation(e){this._setPending(),this._clearDirty(),this._notifyJob(),this._asyncStartAggregateVersion=this._captureVersionSnapshot(),this._asyncRetryCount=0,this._promiseId=this._promiseId>=this.MAX_PROMISE_ID?1:this._promiseId+1;const t=this._promiseId;e.then(i=>{if(t!==this._promiseId)return;const r=this._captureVersionSnapshot()-this._asyncStartAggregateVersion&y;if(B-1-r>>>31&1){if(this._asyncRetryCount<this.MAX_ASYNC_RETRIES){this._asyncRetryCount++,this._markDirty();return}const l=new D(`Async drift exceeded threshold after ${this.MAX_ASYNC_RETRIES} retries. Dependencies changed too rapidly for stable computation.`);this._handleAsyncRejection(l);return}this._handleAsyncResolution(i)}).catch(i=>{t===this._promiseId&&this._handleAsyncRejection(i)})}_captureVersionSnapshot(){let e=0;const t=this._dependencies;for(let i=0;i<t.length;i++){const n=t[i];n&&(e=e+n.version&y)}return e}_handleAsyncResolution(e){this._finalizeResolution(e),this._notifyJob()}_finalizeResolution(e){(!this._isResolved()||!this._equal(this._value,e))&&this.rotatePhase(),this._value=e,this._clearDirty(),this._setResolved(),this._error=null,this._setRecomputing(!1),this._cachedErrors=null,this._errorCacheEpoch=-1}_handleAsyncRejection(e){const t=x(e,D,c.COMPUTED_ASYNC_COMPUTATION_FAILED);if(!this._isRejected()&&this.rotatePhase(),this._error=t,this._setRejected(),this._clearDirty(),this._setRecomputing(!1),this._onError)try{this._onError(t)}catch(n){console.error(c.CALLBACK_ERROR_IN_ERROR_HANDLER,n)}this._notifyJob()}_handleComputationError(e){const t=x(e,D,c.COMPUTED_COMPUTATION_FAILED);if(this._error=t,this._setRejected(),this._clearDirty(),this._setRecomputing(!1),this._onError)try{this._onError(t)}catch(i){console.error(c.CALLBACK_ERROR_IN_ERROR_HANDLER,i)}throw t}_handlePending(){if(this._hasDefaultValue)return this._defaultValue;throw new D(c.COMPUTED_ASYNC_PENDING_NO_DEFAULT)}_handleRejected(){if(this._error?.recoverable&&this._hasDefaultValue)return this._defaultValue;throw this._error}execute(){this._markDirty()}_markDirty(){this._isRecomputing()||this._isDirty()||(this._setDirty(),this._notifyJob())}_registerTracking(){W(this,A.getCurrent(),this._getFnSubs(),this._getObjSubs())}}Object.freeze(ie.prototype);function Ce(s,e={}){return new ie(s,e)}class me extends H{constructor(e,t={}){super(),this.run=()=>{if(this.isDisposed)throw new I(c.EFFECT_MUST_BE_FUNCTION);this._dependencyVersions!==a&&(g.release(this._dependencyVersions),this._dependencyVersions=a),this.execute()},this.dispose=()=>{if(!this.isDisposed){if(this._setDisposed(),this._safeCleanup(),this._unsubscribes!==p){for(let n=0;n<this._unsubscribes.length;n++){const r=this._unsubscribes[n];r&&r()}m.release(this._unsubscribes),this._unsubscribes=p}this._dependencies!==_&&(C.release(this._dependencies),this._dependencies=_),this._dependencyVersions!==a&&(g.release(this._dependencyVersions),this._dependencyVersions=a)}},this.addDependency=n=>{if(this.isExecuting&&this._nextDeps&&this._nextUnsubs&&this._nextVersions){const r=this._currentEpoch;if(n._lastSeenEpoch===r)return;n._lastSeenEpoch=r,this._nextDeps.push(n),this._nextVersions.push(n.version),n._tempUnsub?(this._nextUnsubs.push(n._tempUnsub),n._tempUnsub=void 0):this._subscribeTo(n)}},this.execute=()=>{if(this.isDisposed||this.isExecuting||!this._shouldExecute())return;this._checkInfiniteLoop(),this._setExecuting(!0),this._safeCleanup();const n=this._prepareEffectExecutionContext();let r=!1;try{const o=A.run(this,this._fn);this._commitEffect(n),r=!0,this._checkLoopWarnings(),J(o)?o.then(l=>{!this.isDisposed&&typeof l=="function"&&(this._cleanup=l)}).catch(l=>{this._handleExecutionError(l)}):this._cleanup=typeof o=="function"?o:null}catch(o){r=!0,this._handleExecutionError(o),this._cleanup=null}finally{this._cleanupEffect(n,r),this._setExecuting(!1)}},this._cleanup=null,this._dependencies=_,this._dependencyVersions=a,this._unsubscribes=p,this._nextDeps=null,this._nextVersions=null,this._nextUnsubs=null,this._onError=t.onError??null,this._currentEpoch=-1,this._lastFlushEpoch=-1,this._executionsInEpoch=0,this._fn=e,this._sync=t.sync??!1,this._maxExecutions=t.maxExecutionsPerSecond??b.MAX_EXECUTIONS_PER_SECOND,this._maxExecutionsPerFlush=t.maxExecutionsPerFlush??b.MAX_EXECUTIONS_PER_EFFECT,this._trackModifications=t.trackModifications??!1,this._historyPtr=0;const i=Number.isFinite(this._maxExecutions);this._historyCapacity=i?Math.min(this._maxExecutions+1,b.MAX_EXECUTIONS_PER_SECOND+1):0,this._history=f&&i&&this._historyCapacity>0?new Array(this._historyCapacity).fill(0):null,this._executionCount=0,d.attachDebugInfo(this,"effect",this.id)}_prepareEffectExecutionContext(){const e=this._dependencies,t=this._dependencyVersions,i=this._unsubscribes,n=C.acquire(),r=g.acquire(),o=m.acquire(),l=K();if(e!==_&&i!==p)for(let R=0;R<e.length;R++){const S=e[R];S&&(S._tempUnsub=i[R])}return this._nextDeps=n,this._nextVersions=r,this._nextUnsubs=o,this._currentEpoch=l,{prevDeps:e,prevVersions:t,prevUnsubs:i,nextDeps:n,nextVersions:r,nextUnsubs:o}}_commitEffect(e){const t=e.nextDeps.length;e.nextDeps.length=t,e.nextVersions.length=t,this._dependencies=e.nextDeps,this._dependencyVersions=e.nextVersions,this._unsubscribes=e.nextUnsubs}_cleanupEffect(e,t){if(this._nextDeps=null,this._nextVersions=null,this._nextUnsubs=null,t){if(e.prevDeps!==_){for(let i=0;i<e.prevDeps.length;i++){const n=e.prevDeps[i];n?._tempUnsub&&(n._tempUnsub(),n._tempUnsub=void 0)}C.release(e.prevDeps)}e.prevUnsubs!==p&&m.release(e.prevUnsubs),e.prevVersions!==a&&g.release(e.prevVersions)}else{C.release(e.nextDeps),g.release(e.nextVersions);for(let i=0;i<e.nextUnsubs.length;i++)e.nextUnsubs[i]?.();if(m.release(e.nextUnsubs),e.prevDeps!==_)for(let i=0;i<e.prevDeps.length;i++){const n=e.prevDeps[i];n&&(n._tempUnsub=void 0)}}}_subscribeTo(e){try{const t=e.subscribe(()=>{this._trackModifications&&this.isExecuting&&(e._modifiedAtEpoch=this._currentEpoch),this._sync?this.execute():T.schedule(this.execute)});this._nextUnsubs&&this._nextUnsubs.push(t)}catch(t){console.error(x(t,I,c.EFFECT_EXECUTION_FAILED)),this._nextUnsubs&&this._nextUnsubs.push(()=>{})}}get isDisposed(){return(this.flags&P.DISPOSED)!==0}get executionCount(){return this._executionCount}get isExecuting(){return(this.flags&P.EXECUTING)!==0}_setDisposed(){this.flags|=P.DISPOSED}_setExecuting(e){const t=P.EXECUTING;this.flags=this.flags&~t|-Number(e)&t}_safeCleanup(){if(this._cleanup){try{this._cleanup()}catch(e){console.error(x(e,I,c.EFFECT_CLEANUP_FAILED))}this._cleanup=null}}_checkInfiniteLoop(){if(this._lastFlushEpoch!==L&&(this._lastFlushEpoch=L,this._executionsInEpoch=0),this._executionsInEpoch++,this._executionsInEpoch>this._maxExecutionsPerFlush&&this._throwInfiniteLoopError("per-effect"),Se()>b.MAX_EXECUTIONS_PER_FLUSH&&this._throwInfiniteLoopError("global"),this._executionCount++,this._history&&this._maxExecutions>0){const e=Date.now(),t=this._historyPtr,i=this._historyCapacity;this._history[t]=e;const n=(t+1)%i,r=this._history[n]??0;if(this._historyPtr=n,r>0&&e-r<N.ONE_SECOND_MS){const o=new I(`Effect executed ${i} times within 1 second. Infinite loop suspected`);if(this.dispose(),console.error(o),this._onError&&this._onError(o),f)throw o}}}_throwInfiniteLoopError(e){const t=new I(`Infinite loop detected (${e}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${Q}`);throw this.dispose(),console.error(t),t}_shouldExecute(){if(this._dependencies===_||this._dependencyVersions===a)return!0;for(let e=0;e<this._dependencies.length;e++){const t=this._dependencies[e];if(t){if("value"in t)try{te(()=>t.value)}catch{return!0}if(t.version!==this._dependencyVersions[e])return!0}}return!1}_handleExecutionError(e){const t=x(e,I,c.EFFECT_EXECUTION_FAILED);console.error(t),this._onError&&this._onError(t)}_checkLoopWarnings(){if(this._trackModifications&&d.enabled){const e=this._dependencies;for(let t=0;t<e.length;t++){const i=e[t];i&&i._modifiedAtEpoch===this._currentEpoch&&d.warn(!0,`Effect is reading a dependency (${d.getDebugName(i)||"unknown"}) that it just modified. Infinite loop may occur`)}}}}function Ae(s,e={}){if(typeof s!="function")throw new I(c.EFFECT_MUST_BE_FUNCTION);const t=new me(s,e);return t.execute(),t}function Te(s){if(typeof s!="function")throw new E("Batch callback must be a function");T.startBatch();try{return s()}finally{T.endBatch()}}h.AsyncState=O,h.AtomError=E,h.ComputedError=D,h.DEBUG_CONFIG=w,h.DEBUG_RUNTIME=d,h.EffectError=I,h.POOL_CONFIG=ne,h.SCHEDULER_CONFIG=b,h.SchedulerError=F,h.atom=Ie,h.batch=Te,h.computed=Ce,h.effect=Ae,h.isAtom=$,h.isComputed=_e,h.isEffect=ae,h.scheduler=T,h.untracked=te,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(l,F){typeof exports=="object"&&typeof module<"u"?F(exports):typeof define=="function"&&define.amd?define(["exports"],F):(l=typeof globalThis<"u"?globalThis:l||self,F(l.AtomEffect={}))})(this,(function(l){"use strict";const F={ONE_SECOND_MS:1e3},P={IDLE:"idle",PENDING:"pending",RESOLVED:"resolved",REJECTED:"rejected"},g={DISPOSED:1,HAS_FN_SUBS:2,HAS_OBJ_SUBS:4},d={...g,EXECUTING:8},u={...g,DIRTY:8,IDLE:16,PENDING:32,RESOLVED:64,REJECTED:128,RECOMPUTING:256,HAS_ERROR:512},T={...g,SYNC:8,NOTIFICATION_SCHEDULED:16},he={MAX_SIZE:1e3,WARMUP_SIZE:100},y={MAX_EXECUTIONS_PER_SECOND:1e3,CLEANUP_THRESHOLD:1e3,MAX_EXECUTIONS_PER_EFFECT:100,MAX_EXECUTIONS_PER_FLUSH:1e4,MAX_FLUSH_ITERATIONS:1e3,MIN_FLUSH_ITERATIONS:10,BATCH_QUEUE_SHRINK_THRESHOLD:1e3},z={MAX_DEPENDENCIES:1e3,WARN_INFINITE_LOOP:!0},I=1073741823,H=1<<20-1,D=typeof process<"u"&&process.env&&process.env.NODE_ENV!=="production",ue=Object.freeze([]);class C extends Error{constructor(e,t=null,s=!0){super(e),this.name="AtomError",this.cause=t,this.recoverable=s,this.timestamp=new Date}}class O extends C{constructor(e,t=null){super(e,t,!0),this.name="ComputedError"}}class N extends C{constructor(e,t=null){super(e,t,!1),this.name="EffectError"}}class L extends C{constructor(e,t=null){super(e,t,!1),this.name="SchedulerError"}}const a={COMPUTED_MUST_BE_FUNCTION:"Computed function must be a function",COMPUTED_ASYNC_PENDING_NO_DEFAULT:"Async computation is pending. No default value provided",COMPUTED_COMPUTATION_FAILED:"Computed computation failed",COMPUTED_ASYNC_COMPUTATION_FAILED:"Async computed computation failed",ATOM_SUBSCRIBER_MUST_BE_FUNCTION:"Subscription listener must be a function or Subscriber object",ATOM_INDIVIDUAL_SUBSCRIBER_FAILED:"Error during individual atom subscriber execution",EFFECT_MUST_BE_FUNCTION:"Effect function must be a function",EFFECT_EXECUTION_FAILED:"Effect execution failed",EFFECT_CLEANUP_FAILED:"Effect cleanup function execution failed",CALLBACK_ERROR_IN_ERROR_HANDLER:"Error occurred during onError callback execution"},k=Symbol("debugName"),_e=Symbol("id"),j=Symbol("type"),Y=Symbol("noDefaultValue");function le(n){return"dependencies"in n&&Array.isArray(n.dependencies)}let q=0;function J(n,e,t){if(n._visitedEpoch!==t){if(n._visitedEpoch=t,n===e)throw new O("Indirect circular dependency detected");if(le(n)){const s=n.dependencies;for(let i=0;i<s.length;i++){const r=s[i];r&&J(r,e,t)}}}}const b={enabled:typeof process<"u"&&process.env?.NODE_ENV==="development",maxDependencies:z.MAX_DEPENDENCIES,warnInfiniteLoop:z.WARN_INFINITE_LOOP,warn(n,e){this.enabled&&n&&console.warn(`[Atom Effect] ${e}`)},checkCircular(n,e){if(n===e)throw new O("Direct circular dependency detected");this.enabled&&(q++,J(n,e,q))},attachDebugInfo(n,e,t){if(!this.enabled)return;const s=n;s[k]=`${e}_${t}`,s[_e]=t,s[j]=e},getDebugName(n){if(n!=null&&k in n)return n[k]},getDebugType(n){if(n!=null&&j in n)return n[j]}};let ae=1;const fe=()=>ae++;class ${constructor(){this.flags=0,this.version=0,this._lastSeenEpoch=-1,this._modifiedAtEpoch=-1,this._visitedEpoch=-1,this.id=fe()&I,this._tempUnsub=void 0}getShift(e){return this.version-e&I}}class K extends ${subscribe(e){if(typeof e=="function")return this._addSubscriber(this._fnSubs,e,g.HAS_FN_SUBS);if(e!==null&&typeof e=="object"&&"execute"in e)return this._addSubscriber(this._objSubs,e,g.HAS_OBJ_SUBS);throw new C(a.ATOM_SUBSCRIBER_MUST_BE_FUNCTION)}subscriberCount(){return this._fnSubs.length+this._objSubs.length}_addSubscriber(e,t,s){if(e.indexOf(t)!==-1)return()=>{};e.push(t),this.flags|=s;let r=!1;return()=>{if(r)return;r=!0;const o=e.indexOf(t);if(o!==-1){const c=e.pop();o<e.length&&(e[o]=c),this.flags&=~(e.length===0?s:0)}}}_notifySubscribers(e,t){const s=this.flags,i=g.HAS_FN_SUBS|g.HAS_OBJ_SUBS;if(s&i){if(s&g.HAS_FN_SUBS){const r=this._fnSubs;for(let o=0,c=r.length;o<c;o++){const h=r[o];if(h)try{h(e,t)}catch(_){console.error(new C(a.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED,_))}}}if(s&g.HAS_OBJ_SUBS){const r=this._objSubs;for(let o=0,c=r.length;o<c;o++){const h=r[o];if(h)try{h.execute()}catch(_){console.error(new C(a.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED,_))}}}}}}class Ee{constructor(){this.acquired=0,this.released=0,this.rejected=new de}}class de{constructor(){this.frozen=0,this.tooLarge=0,this.poolFull=0}}class G{constructor(){this.pool=[],this.maxPoolSize=50,this.maxReusableCapacity=256,this.stats=D?new Ee:null}acquire(){const e=this.stats;return D&&e&&e.acquired++,this.pool.pop()??[]}release(e,t){if(t&&e===t||Object.isFrozen(e)){const c=this.stats;D&&c&&e!==t&&c.rejected.frozen++;return}const s=e.length,i=this.pool,r=i.length;if(s>this.maxReusableCapacity||r>=this.maxPoolSize){const c=this.stats;D&&c&&(s>this.maxReusableCapacity?c.rejected.tooLarge++:c.rejected.poolFull++);return}e.length=0,i.push(e);const o=this.stats;D&&o&&o.released++}getStats(){const e=this.stats;if(!D||!e)return null;const{acquired:t,released:s,rejected:i}=e,r=i.frozen+i.tooLarge+i.poolFull;return{acquired:t,released:s,rejected:{frozen:i.frozen,tooLarge:i.tooLarge,poolFull:i.poolFull},leaked:t-s-r,poolSize:this.pool.length}}reset(){this.pool.length=0;const e=this.stats;D&&e&&(e.acquired=0,e.released=0,e.rejected.frozen=0,e.rejected.tooLarge=0,e.rejected.poolFull=0)}}const f=Object.freeze([]),R=Object.freeze([]),p=Object.freeze([]),m=new G,x=new G,A=new G;function U(n,e,t,s){if(e!=null){if((typeof e=="object"||typeof e=="function")&&typeof e.addDependency=="function"){e.addDependency(n);return}if(typeof e=="function"){const i=e;t.indexOf(i)===-1&&(t.push(i),n.flags|=g.HAS_FN_SUBS);return}typeof e=="object"&&typeof e.execute=="function"&&s.indexOf(e)===-1&&(s.push(e),n.flags|=g.HAS_OBJ_SUBS)}}function W(n,e,t,s){const i=n.length,r=e.length,o=e!==f&&r>0;if(o)for(let h=0;h<r;h++){const _=e[h];_&&(_._tempUnsub=t[h])}const c=x.acquire();c.length=i;for(let h=0;h<i;h++){const _=n[h];if(!_)continue;const S=_._tempUnsub;S?(c[h]=S,_._tempUnsub=void 0):(b.checkCircular(_,s),c[h]=_.subscribe(s))}if(o)for(let h=0;h<r;h++){const _=e[h];if(_){const S=_._tempUnsub;S&&(S(),_._tempUnsub=void 0)}}return t!==R&&x.release(t),c}let V=0;function Z(){return V=V+1&I||1,V}function pe(){return V}let X=0,Q=0,w=!1;function ee(){return w?(D&&console.warn("Warning: startFlush() called during flush - ignored to prevent infinite loop detection bypass"),!1):(w=!0,X=X+1&I,Q=0,!0)}function te(){w=!1}function Se(){return w?++Q:0}class ge{constructor(){this._queueBuffers=[[[],[]],[[],[]]],this._bufferIndices=new Uint8Array(2),this._sizes=new Uint32Array(2),this._activeQueues=[this._queueBuffers[0][0],this._queueBuffers[1][0]],this._epoch=0,this.isProcessing=!1,this.isBatching=!1,this.batchDepth=0,this.batchQueue=[],this.batchQueueSize=0,this.isFlushingSync=!1,this.maxFlushIterations=y.MAX_FLUSH_ITERATIONS}get phase(){return this.isProcessing||this.isFlushingSync?2:this.isBatching?1:0}get queueSize(){return this._sizes[0]}get urgentQueueSize(){return this._sizes[1]}schedule(e,t){if(typeof e!="function")throw new L("Scheduler callback must be a function");const s=this._epoch;if(e._nextEpoch===s)return;if(e._nextEpoch=s,this.isBatching||this.isFlushingSync){this.batchQueue[this.batchQueueSize++]=e;return}const i=this._calculateUrgency(e,t);this._activeQueues[i][this._sizes[i]++]=e,this.isProcessing||this.flush()}_calculateUrgency(e,t){if(!t||e._cachedVersion===void 0)return 0;const s=t.getShift(e._cachedVersion);return H-1-s>>>31&1}flush(){this.isProcessing||this._sizes[0]===0&&this._sizes[1]===0||(this.isProcessing=!0,queueMicrotask(()=>{try{if(this._sizes[0]===0&&this._sizes[1]===0)return;const e=ee();this._drainQueue(),e&&te()}finally{this.isProcessing=!1,(this._sizes[0]>0||this._sizes[1]>0)&&!this.isBatching&&this.flush()}}))}flushSync(){this.isFlushingSync=!0;const e=ee();try{this._mergeBatchQueue(),this._drainQueue()}finally{this.isFlushingSync=!1,e&&te()}}_mergeBatchQueue(){const e=this.batchQueueSize;if(e===0)return;const t=++this._epoch,s=this.batchQueue,i=this._activeQueues[0];let r=this._sizes[0];for(let o=0;o<e;o++){const c=s[o];c._nextEpoch!==t&&(c._nextEpoch=t,i[r++]=c)}this._sizes[0]=r,this.batchQueueSize=0,s.length>y.BATCH_QUEUE_SHRINK_THRESHOLD&&(s.length=0)}_drainQueue(){let e=0;const t=this.maxFlushIterations;for(;this._sizes[1]>0||this._sizes[0]>0;){if(++e>t){this._handleFlushOverflow();return}this._sizes[1]>0&&this._processQueue(1),this._sizes[0]>0&&this._processQueue(0),this._mergeBatchQueue()}}_processQueue(e){const t=this._queueBuffers[e],s=this._bufferIndices[e],i=t[s],r=this._sizes[e],o=s^1;this._bufferIndices[e]=o,this._activeQueues[e]=t[o],this._sizes[e]=0,this._epoch++,this._processJobs(i,r)}_handleFlushOverflow(){console.error(new L(`Maximum flush iterations (${this.maxFlushIterations}) exceeded. Possible infinite loop.`)),this._sizes[0]=0,this._activeQueues[0].length=0,this._sizes[1]=0,this._activeQueues[1].length=0,this.batchQueueSize=0}_processJobs(e,t){for(let s=0;s<t;s++)try{e[s]()}catch(i){console.error(new L("Error occurred during scheduler execution",i))}e.length=0}startBatch(){this.batchDepth++,this.isBatching=!0}endBatch(){this.batchDepth=Math.max(0,this.batchDepth-1),this.batchDepth===0&&(this.flushSync(),this.isBatching=!1)}setMaxFlushIterations(e){if(e<y.MIN_FLUSH_ITERATIONS)throw new L(`Max flush iterations must be at least ${y.MIN_FLUSH_ITERATIONS}`);this.maxFlushIterations=e}}const v=new ge;class be{constructor(){this.current=null}run(e,t){const s=this.current;this.current=e;try{return t()}finally{this.current=s}}getCurrent(){return this.current}}const E=new be;function se(n){if(typeof n!="function")throw new C("Untracked callback must be a function");const e=E.current;E.current=null;try{return n()}finally{E.current=e}}class Ie extends K{constructor(e,t){super(),this._value=e,this._pendingOldValue=void 0,this._notifyTask=void 0,this._fnSubs=[],this._objSubs=[],t&&(this.flags|=T.SYNC),b.attachDebugInfo(this,"atom",this.id)}get value(){const e=E.current;return e&&U(this,e,this._fnSubs,this._objSubs),this._value}set value(e){const t=this._value;if(Object.is(t,e))return;this._value=e,this.version=this.version+1&I;const s=this.flags,i=T.HAS_FN_SUBS|T.HAS_OBJ_SUBS;s&i&&this._scheduleNotification(t)}_scheduleNotification(e){let t=this.flags;if(t&T.NOTIFICATION_SCHEDULED||(this._pendingOldValue=e,this.flags=t|=T.NOTIFICATION_SCHEDULED),t&T.SYNC&&!v.isBatching){this._flushNotifications();return}let s=this._notifyTask;s||(s=this._notifyTask=()=>this._flushNotifications()),v.schedule(s)}_flushNotifications(){if(!(this.flags&T.NOTIFICATION_SCHEDULED))return;const e=this._pendingOldValue,t=this._value;this._pendingOldValue=void 0,this.flags&=-17,this._notifySubscribers(t,e)}peek(){return this._value}dispose(){this.flags&T.DISPOSED||(this._fnSubs=[],this._objSubs=[],this.flags|=T.DISPOSED,this._value=void 0,this._pendingOldValue=void 0,this._notifyTask=void 0)}}function De(n,e={}){return new Ie(n,e.sync??!1)}function M(n,e,t){if(n instanceof TypeError)return new e(`Type error (${t}): ${n.message}`,n);if(n instanceof ReferenceError)return new e(`Reference error (${t}): ${n.message}`,n);if(n instanceof C)return n;const s=n instanceof Error?n.message:String(n),i=n instanceof Error?n:null;return new e(`Unexpected error (${t}): ${s}`,i)}function ie(n){return n!==null&&typeof n=="object"&&"value"in n&&"subscribe"in n&&typeof n.subscribe=="function"}function Ce(n){if(b.enabled&&(n==null||typeof n=="object")){const e=b.getDebugType(n);if(e)return e==="computed"}return ie(n)&&"invalidate"in n&&typeof n.invalidate=="function"}function Te(n){return n!==null&&typeof n=="object"&&"dispose"in n&&"run"in n&&typeof n.dispose=="function"&&typeof n.run=="function"}function ne(n){return n!=null&&typeof n.then=="function"}const re=u.RESOLVED|u.PENDING|u.REJECTED,B=Array(re+1).fill(P.IDLE);B[u.RESOLVED]=P.RESOLVED,B[u.PENDING]=P.PENDING,B[u.REJECTED]=P.REJECTED;class ye{constructor(e){this._owner=e,this._epoch=-1,this._nextDeps=f,this._nextVersions=p,this._depCount=0}execute(){this._owner._markDirty()}addDependency(e){if(e._lastSeenEpoch===this._epoch)return;e._lastSeenEpoch=this._epoch;const t=this._depCount,s=this._nextDeps,i=this._nextVersions;t<s.length?(s[t]=e,i[t]=e.version):(s.push(e),i.push(e.version)),this._depCount=t+1}reset(){this._epoch=-1,this._nextDeps=f,this._nextVersions=p,this._depCount=0}}class oe extends K{constructor(e,t={}){if(typeof e!="function")throw new O(a.COMPUTED_MUST_BE_FUNCTION);if(super(),this.MAX_ASYNC_RETRIES=3,this._value=void 0,this.flags=u.DIRTY|u.IDLE,this._error=null,this._promiseId=0,this._equal=t.equal??Object.is,this._fn=e,this._defaultValue="defaultValue"in t?t.defaultValue:Y,this._hasDefaultValue=this._defaultValue!==Y,this._onError=t.onError??null,this.MAX_PROMISE_ID=Number.MAX_SAFE_INTEGER-1,this._fnSubs=[],this._objSubs=[],this._dependencies=f,this._dependencyVersions=p,this._unsubscribes=R,this._cachedErrors=null,this._errorCacheEpoch=-1,this._asyncStartAggregateVersion=0,this._asyncRetryCount=0,this._trackable=new ye(this),b.attachDebugInfo(this,"computed",this.id),b.enabled){const s=this;s.subscriberCount=this.subscriberCount.bind(this),s.isDirty=()=>(this.flags&u.DIRTY)!==0,s.dependencies=this._dependencies,s.stateFlags=""}if(t.lazy===!1)try{this._recompute()}catch{}}get value(){const e=E.current;e&&U(this,e,this._fnSubs,this._objSubs);const t=this.flags;if(t&u.RECOMPUTING)return this._value;t&(u.DIRTY|u.IDLE)&&this._recompute();const s=this.flags;return s&u.PENDING?this._handlePending():s&u.REJECTED?this._handleRejected():this._value}peek(){return this._value}get state(){const e=E.current;return e&&U(this,e,this._fnSubs,this._objSubs),B[this.flags&re]}get hasError(){const e=E.current;if(e&&U(this,e,this._fnSubs,this._objSubs),this.flags&(u.REJECTED|u.HAS_ERROR))return!0;const s=this._dependencies;for(let i=0,r=s.length;i<r;i++){const o=s[i];if(o&&o.flags&u.HAS_ERROR)return!0}return!1}get isValid(){return!this.hasError}get errors(){const e=E.current;if(e&&U(this,e,this._fnSubs,this._objSubs),!this.hasError)return ue;const t=pe();if(this._errorCacheEpoch===t&&this._cachedErrors!==null)return this._cachedErrors;const s=new Set;this._error&&s.add(this._error);const i=this._dependencies;for(let o=0,c=i.length;o<c;o++){const h=i[o];if(h&&"errors"in h){const _=h.errors;for(let S=0,me=_.length;S<me;S++){const ce=_[S];ce&&s.add(ce)}}}const r=Object.freeze([...s]);return this._cachedErrors=r,this._errorCacheEpoch=t,r}get lastError(){const e=E.current;return e&&U(this,e,this._fnSubs,this._objSubs),this._error}get isPending(){const e=E.current;return e&&U(this,e,this._fnSubs,this._objSubs),(this.flags&u.PENDING)!==0}get isResolved(){const e=E.current;return e&&U(this,e,this._fnSubs,this._objSubs),(this.flags&u.RESOLVED)!==0}invalidate(){this._markDirty();const e=this._dependencyVersions;e!==p&&(A.release(e),this._dependencyVersions=p),this._errorCacheEpoch=-1,this._cachedErrors=null}dispose(){const e=this._unsubscribes;if(e!==R){for(let i=0,r=e.length;i<r;i++){const o=e[i];o&&o()}x.release(e),this._unsubscribes=R}const t=this._dependencies;t!==f&&(m.release(t),this._dependencies=f);const s=this._dependencyVersions;s!==p&&(A.release(s),this._dependencyVersions=p),this._fnSubs=[],this._objSubs=[],this.flags=u.DISPOSED|u.DIRTY|u.IDLE,this._error=null,this._value=void 0,this._promiseId=(this._promiseId+1)%this.MAX_PROMISE_ID,this._cachedErrors=null,this._errorCacheEpoch=-1}_clearDirty(){this.flags&=-9}_setPending(){this.flags=(this.flags|u.PENDING)&-209}_isResolved(){return(this.flags&u.RESOLVED)!==0}_setResolved(){this.flags=(this.flags|u.RESOLVED)&-689}_isRejected(){return(this.flags&u.REJECTED)!==0}_setRejected(){this.flags=this.flags&-113|(u.REJECTED|u.HAS_ERROR)}_setRecomputing(e){const t=u.RECOMPUTING;this.flags=this.flags&~t|(e?-1:0)&t}_recompute(){if(this.flags&u.RECOMPUTING)return;this._setRecomputing(!0);const e=this._trackable,t=this._dependencies,s=this._dependencyVersions;e._epoch=Z(),e._nextDeps=m.acquire(),e._nextVersions=A.acquire(),e._depCount=0;let i=!1;try{const r=E.run(e,this._fn),o=e._nextDeps,c=e._nextVersions,h=e._depCount;o.length=h,c.length=h,this._unsubscribes=W(o,t,this._unsubscribes,this),this._dependencies=o,this._dependencyVersions=c,i=!0,ne(r)?this._handleAsyncComputation(r):this._finalizeResolution(r)}catch(r){let o=r;if(!i)try{const c=e._nextDeps,h=e._nextVersions,_=e._depCount;c.length=_,h.length=_,this._unsubscribes=W(c,t,this._unsubscribes,this),this._dependencies=c,this._dependencyVersions=h,i=!0}catch(c){o=c}this._handleComputationError(o)}finally{i?(t!==f&&m.release(t),s!==p&&A.release(s)):(m.release(e._nextDeps),A.release(e._nextVersions)),e.reset(),this._setRecomputing(!1)}}_getAggregateShift(){let e=0;const t=this._dependencies,s=this._dependencyVersions;for(let i=0,r=t.length;i<r;i++){const o=t[i],c=s[i];o&&c!==void 0&&(e=e+o.getShift(c)&I)}return e}isUrgent(){return this._getAggregateShift()>=H}_handleAsyncComputation(e){this._setPending(),this._clearDirty(),this._notifySubscribers(void 0,void 0),this._asyncStartAggregateVersion=this._captureVersionSnapshot(),this._asyncRetryCount=0,this._promiseId=(this._promiseId+1)%this.MAX_PROMISE_ID;const t=this._promiseId;e.then(s=>{if(t!==this._promiseId)return;const r=this._captureVersionSnapshot()-this._asyncStartAggregateVersion&I;if(H-1-r>>>31&1){if(this._asyncRetryCount<this.MAX_ASYNC_RETRIES){this._asyncRetryCount++,this._markDirty();return}const c=new O(`Async drift exceeded threshold after ${this.MAX_ASYNC_RETRIES} retries.`);this._handleAsyncRejection(c);return}this._finalizeResolution(s),this._notifySubscribers(s,void 0)}).catch(s=>{t===this._promiseId&&this._handleAsyncRejection(s)})}_captureVersionSnapshot(){let e=0;const t=this._dependencies;for(let s=0,i=t.length;s<i;s++){const r=t[s];r&&(e=e+r.version&I)}return e}_handleAsyncRejection(e){const t=M(e,O,a.COMPUTED_ASYNC_COMPUTATION_FAILED);this.flags&u.REJECTED||(this.version=this.version+1&I),this._error=t,this._setRejected(),this._clearDirty(),this._setRecomputing(!1);const s=this._onError;if(s)try{s(t)}catch(i){console.error(a.CALLBACK_ERROR_IN_ERROR_HANDLER,i)}this._notifySubscribers(void 0,void 0)}_finalizeResolution(e){(!(this.flags&u.RESOLVED)||!this._equal(this._value,e))&&(this.version=this.version+1&I),this._value=e,this._clearDirty(),this._setResolved(),this._error=null,this._setRecomputing(!1),this._cachedErrors=null,this._errorCacheEpoch=-1}_handleComputationError(e){const t=M(e,O,a.COMPUTED_COMPUTATION_FAILED);this._error=t,this._setRejected(),this._clearDirty(),this._setRecomputing(!1);const s=this._onError;if(s)try{s(t)}catch(i){console.error(a.CALLBACK_ERROR_IN_ERROR_HANDLER,i)}throw t}_handlePending(){if(this._hasDefaultValue)return this._defaultValue;throw new O(a.COMPUTED_ASYNC_PENDING_NO_DEFAULT)}_handleRejected(){const e=this._error;if(e?.recoverable&&this._hasDefaultValue)return this._defaultValue;throw e}execute(){this._markDirty()}_markDirty(){const e=this.flags;e&(u.RECOMPUTING|u.DIRTY)||(this.flags=e|u.DIRTY,this._notifySubscribers(void 0,void 0))}}Object.freeze(oe.prototype);function Ae(n,e={}){return new oe(n,e)}class Oe extends ${constructor(e,t={}){super(),this._cleanup=null,this._dependencies=f,this._dependencyVersions=p,this._unsubscribes=R,this._nextDeps=null,this._nextVersions=null,this._nextUnsubs=null,this._executeTask=void 0,this._onError=t.onError??null,this._currentEpoch=-1,this._lastFlushEpoch=-1,this._executionsInEpoch=0,this._fn=e,this._sync=t.sync??!1,this._maxExecutions=t.maxExecutionsPerSecond??y.MAX_EXECUTIONS_PER_SECOND,this._maxExecutionsPerFlush=t.maxExecutionsPerFlush??y.MAX_EXECUTIONS_PER_EFFECT,this._trackModifications=t.trackModifications??!1,this._executionCount=0,this._historyPtr=0;const s=Number.isFinite(this._maxExecutions),i=s?Math.min(this._maxExecutions+1,y.MAX_EXECUTIONS_PER_SECOND+1):0;this._historyCapacity=i,this._history=D&&s&&i>0?new Array(i).fill(0):null,b.attachDebugInfo(this,"effect",this.id)}run(){if(this.flags&d.DISPOSED)throw new N(a.EFFECT_MUST_BE_FUNCTION);this.execute(!0)}dispose(){const e=this.flags;if(e&d.DISPOSED)return;this.flags=e|d.DISPOSED,this._safeCleanup();const t=this._unsubscribes;if(t!==R){for(let r=0,o=t.length;r<o;r++){const c=t[r];c&&c()}x.release(t),this._unsubscribes=R}const s=this._dependencies;s!==f&&(m.release(s),this._dependencies=f);const i=this._dependencyVersions;i!==p&&(A.release(i),this._dependencyVersions=p),this._executeTask=void 0}addDependency(e){if(!(this.flags&d.EXECUTING))return;const s=this._currentEpoch;if(e._lastSeenEpoch===s)return;e._lastSeenEpoch=s;const i=this._nextDeps,r=this._nextVersions,o=this._nextUnsubs;if(!i||!r||!o)return;i.push(e),r.push(e.version);const c=e._tempUnsub;c?(o.push(c),e._tempUnsub=void 0):this._subscribeTo(e)}execute(e=!1){if(this.flags&(d.DISPOSED|d.EXECUTING)||!e&&!this._shouldExecute())return;this._checkInfiniteLoop(),this._setExecuting(!0),this._safeCleanup();const s=this._prepareEffectExecutionContext();let i=!1;try{const r=E.run(this,this._fn),o=s.nextDeps.length;s.nextDeps.length=o,s.nextVersions.length=o,this._dependencies=s.nextDeps,this._dependencyVersions=s.nextVersions,this._unsubscribes=s.nextUnsubs,i=!0,this._checkLoopWarnings(),ne(r)?r.then(c=>{!(this.flags&d.DISPOSED)&&typeof c=="function"&&(this._cleanup=c)}).catch(c=>this._handleExecutionError(c)):this._cleanup=typeof r=="function"?r:null}catch(r){i=!0,this._handleExecutionError(r),this._cleanup=null}finally{this._cleanupEffect(s,i),this._setExecuting(!1)}}_prepareEffectExecutionContext(){const e=this._dependencies,t=this._dependencyVersions,s=this._unsubscribes,i=m.acquire(),r=A.acquire(),o=x.acquire(),c=Z();if(e!==f)for(let h=0,_=e.length;h<_;h++){const S=e[h];S&&(S._tempUnsub=s[h])}return this._nextDeps=i,this._nextVersions=r,this._nextUnsubs=o,this._currentEpoch=c,{prevDeps:e,prevVersions:t,prevUnsubs:s,nextDeps:i,nextVersions:r,nextUnsubs:o}}_cleanupEffect(e,t){this._nextDeps=null,this._nextVersions=null,this._nextUnsubs=null;const s=e.prevDeps;if(t){if(s!==f){for(let i=0,r=s.length;i<r;i++){const o=s[i],c=o?o._tempUnsub:void 0;c&&(c(),o&&(o._tempUnsub=void 0))}m.release(s)}e.prevUnsubs!==R&&x.release(e.prevUnsubs),e.prevVersions!==p&&A.release(e.prevVersions)}else{m.release(e.nextDeps),A.release(e.nextVersions);const i=e.nextUnsubs;for(let r=0,o=i.length;r<o;r++)i[r]?.();if(x.release(i),s!==f)for(let r=0,o=s.length;r<o;r++){const c=s[r];c&&(c._tempUnsub=void 0)}}}_subscribeTo(e){try{const t=e.subscribe(()=>{if(this._trackModifications&&this.flags&d.EXECUTING&&(e._modifiedAtEpoch=this._currentEpoch),this._sync){this.execute();return}let i=this._executeTask;i||(i=this._executeTask=()=>this.execute()),v.schedule(i)}),s=this._nextUnsubs;s&&s.push(t)}catch(t){console.error(M(t,N,a.EFFECT_EXECUTION_FAILED));const s=this._nextUnsubs;s&&s.push(()=>{})}}get isDisposed(){return(this.flags&d.DISPOSED)!==0}get executionCount(){return this._executionCount}get isExecuting(){return(this.flags&d.EXECUTING)!==0}_setDisposed(){this.flags|=d.DISPOSED}_setExecuting(e){const t=d.EXECUTING;this.flags=this.flags&~t|(e?-1:0)&t}_safeCleanup(){const e=this._cleanup;if(e){try{e()}catch(t){console.error(M(t,N,a.EFFECT_CLEANUP_FAILED))}this._cleanup=null}}_checkInfiniteLoop(){const e=X;this._lastFlushEpoch!==e&&(this._lastFlushEpoch=e,this._executionsInEpoch=0),++this._executionsInEpoch>this._maxExecutionsPerFlush&&this._throwInfiniteLoopError("per-effect"),Se()>y.MAX_EXECUTIONS_PER_FLUSH&&this._throwInfiniteLoopError("global"),this._executionCount++;const s=this._history;if(s){const i=Date.now(),r=this._historyPtr,o=this._historyCapacity;s[r]=i;const c=(r+1)%o;this._historyPtr=c;const h=s[c]??0;if(h>0&&i-h<F.ONE_SECOND_MS){const _=new N(`Effect executed ${o} times within 1 second. Infinite loop suspected`);if(this.dispose(),console.error(_),this._onError&&this._onError(_),D)throw _}}}_throwInfiniteLoopError(e){const t=new N(`Infinite loop detected (${e}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${Q}`);throw this.dispose(),console.error(t),t}_shouldExecute(){const e=this._dependencies;if(e.length===0)return!0;const t=this._dependencyVersions;for(let s=0,i=e.length;s<i;s++){const r=e[s];if(r){if(r.version!==t[s])return!0;if("value"in r)try{se(()=>r.value)}catch{return!0}}}return!1}_handleExecutionError(e){const t=M(e,N,a.EFFECT_EXECUTION_FAILED);console.error(t);const s=this._onError;s&&s(t)}_checkLoopWarnings(){if(this._trackModifications&&b.enabled){const e=this._dependencies,t=this._currentEpoch;for(let s=0,i=e.length;s<i;s++){const r=e[s];r&&r._modifiedAtEpoch===t&&b.warn(!0,`Effect is reading a dependency (${b.getDebugName(r)||"unknown"}) that it just modified. Infinite loop may occur`)}}}}function Ne(n,e={}){if(typeof n!="function")throw new N(a.EFFECT_MUST_BE_FUNCTION);const t=new Oe(n,e);return t.execute(),t}function Re(n){if(typeof n!="function")throw new C("Batch callback must be a function");v.startBatch();try{return n()}finally{v.endBatch()}}l.AsyncState=P,l.AtomError=C,l.ComputedError=O,l.DEBUG_CONFIG=z,l.DEBUG_RUNTIME=b,l.EffectError=N,l.POOL_CONFIG=he,l.SCHEDULER_CONFIG=y,l.SchedulerError=L,l.atom=De,l.batch=Re,l.computed=Ae,l.effect=Ne,l.isAtom=ie,l.isComputed=Ce,l.isEffect=Te,l.scheduler=v,l.untracked=se,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
2
2
  //# sourceMappingURL=atom-effect.min.js.map