@but212/atom-effect 0.25.0 → 0.27.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/dist/atom-effect.min.js +1 -1
- package/dist/atom-effect.min.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -11
- package/dist/index.mjs +560 -464
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var b={IDLE:"idle",PENDING:"pending",RESOLVED:"resolved",REJECTED:"rejected"},f={DISPOSED:1,EXECUTING:8},H={DISPOSED:1,IS_COMPUTED:2,DIRTY:8,IDLE:16,PENDING:32,RESOLVED:64,REJECTED:128,RECOMPUTING:256,HAS_ERROR:512,FORCE_COMPUTE:1024},d={DISPOSED:1,SYNC:8,NOTIFICATION_SCHEDULED:16},g={MAX_EXECUTIONS_PER_SECOND:1e3,MAX_EXECUTIONS_PER_EFFECT:100,MAX_EXECUTIONS_PER_FLUSH:1e4,MAX_FLUSH_ITERATIONS:1e3,MIN_FLUSH_ITERATIONS:10,BATCH_QUEUE_SHRINK_THRESHOLD:1e3},X={WARN_INFINITE_LOOP:!0,EFFECT_FREQUENCY_WINDOW:1e3},q={MAX_ASYNC_RETRIES:3,MAX_PROMISE_ID:Number.MAX_SAFE_INTEGER-1},v={UNINITIALIZED:-1,MIN:1},V=1073741823,W={VERSION_BITS:16},Nt=(typeof process<"u"&&process.env,!1),K=Object.freeze([]),z=class{constructor(t,s,e=void 0){this.node=t,this.version=s,this.unsub=e}},ut=class{constructor(t,s){this.fn=t,this.sub=s}notify(t,s){this.fn?this.fn(t,s):this.sub&&this.sub.execute()}},m=class extends Error{constructor(t,s=null,e=!0){super(t),this.cause=s,this.recoverable=e,this.name="AtomError"}},S=class extends m{constructor(t,s=null){super(t,s,!0),this.name="ComputedError"}},C=class extends m{constructor(t,s=null){super(t,s,!1),this.name="EffectError"}},F=class extends m{constructor(t,s=null){super(t,s,!1),this.name="SchedulerError"}},l={COMPUTED_MUST_BE_FUNCTION:"Computed target must be a function",COMPUTED_ASYNC_PENDING_NO_DEFAULT:"Async computation pending with no default value",COMPUTED_COMPUTATION_FAILED:"Computation execution failed",COMPUTED_ASYNC_COMPUTATION_FAILED:"Async computation execution failed",COMPUTED_CIRCULAR_DEPENDENCY:"Circular dependency detected",COMPUTED_DISPOSED:"Attempted to access disposed computed",ATOM_SUBSCRIBER_MUST_BE_FUNCTION:"Subscriber must be a function or Subscriber object",ATOM_INDIVIDUAL_SUBSCRIBER_FAILED:"Subscriber execution failed",EFFECT_MUST_BE_FUNCTION:"Effect target must be a function",EFFECT_EXECUTION_FAILED:"Effect execution failed",EFFECT_CLEANUP_FAILED:"Effect cleanup failed",EFFECT_DISPOSED:"Attempted to run disposed effect",SCHEDULER_FLUSH_OVERFLOW:(t,s)=>`Maximum flush iterations (${t}) exceeded. ${s} jobs dropped. Possible infinite loop.`,CALLBACK_ERROR_IN_ERROR_HANDLER:"Exception encountered in onError handler",EFFECT_FREQUENCY_LIMIT_EXCEEDED:"Effect executed too frequently within 1 second. Suspected infinite loop.",SCHEDULER_CALLBACK_MUST_BE_FUNCTION:"Scheduler callback must be a function",SCHEDULER_END_BATCH_WITHOUT_START:"endBatch() called without matching startBatch(). Ignoring.",BATCH_CALLBACK_MUST_BE_FUNCTION:"Batch callback must be a function"},et=class{constructor(){this._s0=null,this._s1=null,this._s2=null,this._s3=null,this._count=0,this._overflow=null}get size(){return this._count}getAt(t){switch(t){case 0:return this._s0;case 1:return this._s1;case 2:return this._s2;case 3:return this._s3;default:{const s=this._overflow;if(s!==null&&t>=4){const e=t-4;if(e<s.length)return s[e]??null}return null}}}setAt(t,s){switch(t){case 0:this._s0=s;break;case 1:this._s1=s;break;case 2:this._s2=s;break;case 3:this._s3=s;break;default:{this._overflow??=[];const e=this._overflow;e[t-4]=s}}t>=this._count&&(this._count=t+1)}truncateFrom(t){const s=this._count;if(t>=s)return;if(t<=3)switch(t){case 0:{const i=this._s0;i!=null&&(this._onItemRemoved(i),this._s0=null)}case 1:{const i=this._s1;i!=null&&(this._onItemRemoved(i),this._s1=null)}case 2:{const i=this._s2;i!=null&&(this._onItemRemoved(i),this._s2=null)}case 3:{const i=this._s3;i!=null&&(this._onItemRemoved(i),this._s3=null)}}const e=this._overflow;if(e!==null&&s>4){const i=t>4?t-4:0,n=e.length;for(let r=i;r<n;r++){const o=e[r];o!=null&&(this._onItemRemoved(o),e[r]=null)}t<=4?(e.length=0,this._overflow=null):e.length=t-4}this._count=t}_onItemRemoved(t){}add(t){if(this._s0===null){this._s0=t,this._count++;return}if(this._s1===null){this._s1=t,this._count++;return}if(this._s2===null){this._s2=t,this._count++;return}if(this._s3===null){this._s3=t,this._count++;return}if(this._overflow===null)this._overflow=[t];else{const s=this._overflow;for(let e=0,i=s.length;e<i;e++)if(s[e]===null){s[e]=t,this._count++;return}s.push(t)}this._count++}remove(t){if(this._s0===t)return this._s0=null,this._count--,!0;if(this._s1===t)return this._s1=null,this._count--,!0;if(this._s2===t)return this._s2=null,this._count--,!0;if(this._s3===t)return this._s3=null,this._count--,!0;const s=this._overflow;if(s==null)return!1;for(let e=0,i=s.length;e<i;e++)if(s[e]===t)return s[e]=null,this._count--,!0;return!1}has(t){const s=this._count;if(s===0)return!1;if(this._s0===t||this._s1===t||this._s2===t||this._s3===t)return!0;if(s<=4)return!1;const e=this._overflow;if(e!=null){let i=0;this._s0!=null&&i++,this._s1!=null&&i++,this._s2!=null&&i++,this._s3!=null&&i++;for(let n=0,r=e.length;n<r;n++){const o=e[n];if(o!=null){if(o===t)return!0;if(++i===s)break}}}return!1}forEach(t){const s=this._count;if(s===0)return;const e=this._s0;e!=null&&t(e);const i=this._s1;i!=null&&t(i);const n=this._s2;n!=null&&t(n);const r=this._s3;if(r!=null&&t(r),s<=4)return;const o=this._overflow;if(o!=null){let h=0;e!=null&&h++,i!=null&&h++,n!=null&&h++,r!=null&&h++;for(let u=0,a=o.length;u<a;u++){const _=o[u];if(_!=null&&(t(_),++h===s))return}}}forEachIndexed(t){const s=this._count;if(s===0)return 0;let e=0;const i=this._s0;i!=null&&(t(i),e++);const n=this._s1;n!=null&&(t(n),e++);const r=this._s2;r!=null&&(t(r),e++);const o=this._s3;if(o!=null&&(t(o),e++),s<=4||e===s)return e;const h=this._overflow;if(h!=null)for(let u=0,a=h.length;u<a;u++){const _=h[u];if(_!=null&&(t(_),++e===s))break}return e}compact(){const t=this._overflow;if(t===null||t.length===0)return;let s=0;for(;s<t.length;)if(t[s]===null){const e=t.pop();s<t.length&&e!=null&&(t[s]=e)}else s++;t.length===0&&(this._overflow=null)}clear(){this._s0=null,this._s1=null,this._s2=null,this._s3=null,this._count=0,this._overflow!==null&&(this._overflow.length=0,this._overflow=null)}dispose(){this.clear()}},Z=Symbol("AtomEffect.DebugName"),lt=Symbol("AtomEffect.Id"),J=Symbol("AtomEffect.Type"),w=Symbol("AtomEffect.NoDefaultValue"),U={enabled:!1,warnInfiniteLoop:X.WARN_INFINITE_LOOP,warn(t,s){},attachDebugInfo(t,s,e){},getDebugName:t=>t?.[Z],getDebugType:t=>t?.[J]},ct=1,_t=()=>ct++;function A(t,s,e){if(t instanceof m)return t;const i=t instanceof Error,n=i?t.message:String(t),r=i?t:void 0;return new s(`${i?t.constructor.name:"Unexpected error"} (${e}): ${n}`,r)}var Q=class{constructor(){this.flags=0,this.version=0,this._lastSeenEpoch=v.UNINITIALIZED,this._notifying=0,this._hotIndex=-1,this._slots=null,this._deps=null,this.id=_t()&V}subscribe(t){const s=typeof t=="function";if(!s&&(!t||typeof t.execute!="function"))throw A(new TypeError("Invalid subscriber"),m,l.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);let e=this._slots;e||(e=new et,this._slots=e);let i=!1;if(e.forEach(r=>{(s?r.fn===t:r.sub===t)&&(i=!0)}),i)return()=>{};const n=new ut(s?t:void 0,s?void 0:t);return e.add(n),()=>this._unsubscribe(n)}_unsubscribe(t){if(this._slots){if(this._notifying>0){this._slots.remove(t);return}this._slots.remove(t),this._slots.compact()}}subscriberCount(){return this._slots?this._slots.size:0}_notifySubscribers(t,s){const e=this._slots;if(!(!e||e.size===0)){this._notifying++;try{e.forEach(i=>{try{i.notify(t,s)}catch(n){console.error(A(n,m,l.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED))}})}finally{this._notifying--,this._notifying===0&&e.compact()}}}_isDirty(){const t=this._deps;if(!t||t.size===0)return!1;if(this._hotIndex!==-1){const s=t.getAt(this._hotIndex);if(s!=null&&s.node.version!==s.version)return!0}return!t.hasComputeds&&!t.isDirtyFast()?!1:this._deepDirtyCheck()}},L=0,Y=()=>(L=L+1&1073741823||1,L),k=t=>t+1&V,$=0,R=!1,it=0,nt=()=>it;function tt(){return R?!1:(R=!0,it=Y(),$=0,!0)}var st=()=>{R=!1},ft=()=>R?++$:0,M=(function(t){return t[t.IDLE=0]="IDLE",t[t.BATCHING=1]="BATCHING",t[t.FLUSHING=2]="FLUSHING",t})({}),at=class{constructor(){this._queueBuffer=[[],[]],this._bufferIndex=0,this._size=0,this._epoch=0,this._isProcessing=!1,this._isBatching=!1,this._isFlushingSync=!1,this._batchDepth=0,this._batchQueue=[],this._batchQueueSize=0,this._maxFlushIterations=g.MAX_FLUSH_ITERATIONS,this.onOverflow=null,this._boundRunLoop=this._runLoop.bind(this)}get phase(){return this._isProcessing||this._isFlushingSync?M.FLUSHING:this._isBatching?M.BATCHING:M.IDLE}get queueSize(){return this._size}get isBatching(){return this._isBatching}schedule(t){if(t._nextEpoch!==this._epoch){if(t._nextEpoch=this._epoch,this._isBatching||this._isFlushingSync){this._batchQueue[this._batchQueueSize++]=t;return}this._queueBuffer[this._bufferIndex][this._size++]=t,this._isProcessing||this._flush()}}_flush(){this._isProcessing||this._size===0||(this._isProcessing=!0,queueMicrotask(this._boundRunLoop))}_runLoop(){try{if(this._size===0)return;const t=tt();this._drainQueue(),t&&st()}finally{this._isProcessing=!1,this._size>0&&!this._isBatching&&this._flush()}}_flushSync(){this._isFlushingSync=!0;const t=tt();try{this._mergeBatchQueue(),this._drainQueue()}finally{this._isFlushingSync=!1,t&&st()}}_mergeBatchQueue(){if(this._batchQueueSize===0)return;const t=++this._epoch,s=this._batchQueue,e=this._queueBuffer[this._bufferIndex];let i=this._size;for(let n=0;n<this._batchQueueSize;n++){const r=s[n];r._nextEpoch!==t&&(r._nextEpoch=t,e[i++]=r)}this._size=i,this._batchQueueSize=0,s.length=0}_drainQueue(){let t=0;for(;this._size>0;){if(++t>this._maxFlushIterations){this._handleFlushOverflow();return}this._processQueue(),this._mergeBatchQueue()}}_processQueue(){const t=this._bufferIndex,s=this._queueBuffer[t],e=this._size;this._bufferIndex=t^1,this._size=0,this._epoch++;for(let i=0;i<e;i++)try{const n=s[i];typeof n=="function"?n():n.execute()}catch(n){console.error(new F("Error occurred during scheduler execution",n))}s.length=0}_handleFlushOverflow(){const t=this._size+this._batchQueueSize;if(console.error(new F(l.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations,t))),this._size=0,this._queueBuffer[this._bufferIndex].length=0,this._batchQueueSize=0,this.onOverflow)try{this.onOverflow(t)}catch{}}startBatch(){this._batchDepth++,this._isBatching=!0}endBatch(){this._batchDepth!==0&&--this._batchDepth===0&&(this._flushSync(),this._isBatching=!1)}setMaxFlushIterations(t){if(t<g.MIN_FLUSH_ITERATIONS)throw new F(`Max flush iterations must be at least ${g.MIN_FLUSH_ITERATIONS}`);this._maxFlushIterations=t}},N=new at,j=Symbol.for("atom-effect/atom"),rt=Symbol.for("atom-effect/computed"),ot=Symbol.for("atom-effect/effect"),Et=Symbol.for("atom-effect/writable"),c={current:null,run(t,s){const e=this.current;this.current=t;try{return s()}finally{this.current=e}}};function dt(t){const s=c.current;if(s===null)return t();c.current=null;try{return t()}finally{c.current=s}}var pt=class extends Q{constructor(t,s){super(),this[j]=!0,this[Et]=!0,this._value=t,this._pendingOldValue=void 0,s&&(this.flags|=d.SYNC),U.attachDebugInfo(this,"atom",this.id)}get value(){const t=c.current;return t?.addDependency(this),this._value}set value(t){const s=this._value;if(Object.is(s,t))return;this._value=t,this.version=k(this.version);const e=this._slots,i=this.flags;if(e==null||e.size===0||i&d.NOTIFICATION_SCHEDULED)return;this._pendingOldValue=s;const n=i|d.NOTIFICATION_SCHEDULED;if(this.flags=n,(n&d.SYNC)!==0&&!N.isBatching){this._flushNotifications();return}N.schedule(this)}execute(){this._flushNotifications()}_flushNotifications(){const t=this.flags;if(!(t&d.NOTIFICATION_SCHEDULED)||t&d.DISPOSED)return;const s=this._pendingOldValue;this._pendingOldValue=void 0,this.flags&=~d.NOTIFICATION_SCHEDULED,this._notifySubscribers(this._value,s)}peek(){return this._value}dispose(){this.flags&d.DISPOSED||(this._slots?.clear(),this.flags|=d.DISPOSED,this._value=void 0,this._pendingOldValue=void 0)}_deepDirtyCheck(){return!1}[Symbol.dispose](){this.dispose()}};function It(t,s={}){return new pt(t,s.sync??!1)}var G=class extends et{constructor(){super(),this._map=null,this._SCAN_THRESHOLD=32,this.hasComputeds=!1,this._depsHash=0,this._map=null,this.hasComputeds=!1,this._depsHash=0}prepareTracking(){this.hasComputeds=!1}_onItemRemoved(t){const s=t.unsub;s&&s()}claimExisting(t,s){const e=this._count;if(s>=e)return!1;const i=e-s;if(this._map!==null||i>this._SCAN_THRESHOLD)return this._claimViaMap(t,s);if(s<4)switch(s){case 0:{const o=this._s0;if(o&&o.node===t&&o.unsub)return o.version=t.version,!0}case 1:if(e>1){const o=this._s1;if(o&&o.node===t&&o.unsub)return o.version=t.version,s!==1&&(this._s1=this._s0,this._s0=o),!0}case 2:if(e>2){const o=this._s2;if(o&&o.node===t&&o.unsub){if(o.version=t.version,s!==2){const h=s===0?this._s0:this._s1;s===0?this._s0=o:this._s1=o,this._s2=h}return!0}}case 3:if(e>3){const o=this._s3;if(o&&o.node===t&&o.unsub){if(o.version=t.version,s!==3){let h;s===0?(h=this._s0,this._s0=o):s===1?(h=this._s1,this._s1=o):(h=this._s2,this._s2=o),this._s3=h}return!0}}}const n=s>4?s:4,r=this._overflow;if(r)for(let o=n-4,h=r.length;o<h;o++){const u=r[o];if(u&&u.node===t&&u.unsub)return u.version=t.version,this._swapGeneral(o+4,s,u),!0}return!1}_claimViaMap(t,s){if(this._map===null){this._map=new Map;const n=this._count;s<4&&(s<=0&&this._s0?.unsub&&this._map.set(this._s0.node,0),s<=1&&this._s1?.unsub&&this._map.set(this._s1.node,1),s<=2&&this._s2?.unsub&&this._map.set(this._s2.node,2),s<=3&&this._s3?.unsub&&this._map.set(this._s3.node,3));const r=this._overflow;if(r&&n>4){const o=s>4?s:4;for(let h=o-4,u=r.length;h<u;h++){const a=r[h];a?.unsub&&this._map.set(a.node,h+4)}}}const e=this._map.get(t);if(e===void 0||e<s)return!1;const i=this.getAt(e);if(i==null||!i.unsub)return!1;if(i.version=t.version,e!==s){const n=this.getAt(s);this.setAt(s,i),this.setAt(e,n),n?.unsub&&this._map.set(n.node,e),this._map.set(t,s)}return!0}_swapInline(t,s,e){const i=this.getAt(s);this.setAt(s,e),this.setAt(t,i)}_swapGeneral(t,s,e){if(t===s)return;const i=this.getAt(s);if(this.setAt(s,e),t===0)this._s0=i;else if(t===1)this._s1=i;else if(t===2)this._s2=i;else if(t===3)this._s3=i;else{const n=this._overflow;n[t-4]=i}}insertNew(t,s){const e=this._count;if(t<e){const i=this.getAt(t);i!=null&&(this.add(i),this._map!==null&&i.unsub&&this._map.set(i.node,this._count-1))}if(t===0)this._s0=s;else if(t===1)this._s1=s;else if(t===2)this._s2=s;else if(t===3)this._s3=s;else{let i=this._overflow;i||(i=[],this._overflow=i),i[t-4]=s}t>=e&&(this._count=t+1)}truncateFrom(t){t>=this._count||(super.truncateFrom(t),this._map!==null&&(this._map.clear(),this._map=null))}seal(){const t=this._count,s=W.VERSION_BITS;let e=0;switch(t){case 0:this._depsHash=0;return;case 1:{const i=this._s0;e=e+(i.version<<s)+i.node.id|0;break}case 2:{const i=this._s0,n=this._s1;e=e+(i.version<<s)+i.node.id|0,e=e+(n.version<<s)+n.node.id|0;break}case 3:{const i=this._s0,n=this._s1,r=this._s2;e=e+(i.version<<s)+i.node.id|0,e=e+(n.version<<s)+n.node.id|0,e=e+(r.version<<s)+r.node.id|0;break}default:{const i=this._s0,n=this._s1,r=this._s2,o=this._s3;if(e=e+(i.version<<s)+i.node.id|0,e=e+(n.version<<s)+n.node.id|0,e=e+(r.version<<s)+r.node.id|0,e=e+(o.version<<s)+o.node.id|0,t>4){const h=this._overflow;for(let u=0,a=h.length;u<a;u++){const _=h[u];e=e+(_.version<<s)+_.node.id|0}}}}this._depsHash=e}isDirtyFast(){const t=this._count,s=W.VERSION_BITS;let e=0;switch(t){case 0:return!1;case 1:{const i=this._s0.node;e=e+(i.version<<s)+i.id|0;break}case 2:{const i=this._s0.node,n=this._s1.node;e=e+(i.version<<s)+i.id|0,e=e+(n.version<<s)+n.id|0;break}case 3:{const i=this._s0.node,n=this._s1.node,r=this._s2.node;e=e+(i.version<<s)+i.id|0,e=e+(n.version<<s)+n.id|0,e=e+(r.version<<s)+r.id|0;break}default:{const i=this._s0.node,n=this._s1.node,r=this._s2.node,o=this._s3.node;if(e=e+(i.version<<s)+i.id|0,e=e+(n.version<<s)+n.id|0,e=e+(r.version<<s)+r.id|0,e=e+(o.version<<s)+o.id|0,t>4){const h=this._overflow;for(let u=0,a=h.length;u<a;u++){const _=h[u].node;e=e+(_.version<<s)+_.id|0}}}}return e!==this._depsHash}disposeAll(){this._count>0&&this.truncateFrom(0),this.hasComputeds=!1,this._map!==null&&(this._map.clear(),this._map=null)}remove(t){throw new Error("remove() is strictly prohibited in DepSlotBuffer to preserve sequential cache paths.")}compact(){}};function Ct(t){return t!==null&&typeof t=="object"&&j in t}function vt(t){return t!==null&&typeof t=="object"&&rt in t}function Dt(t){return t!==null&&typeof t=="object"&&ot in t}function ht(t){return t!==null&&typeof t=="object"&&typeof t.then=="function"}var{IDLE:I,DIRTY:E,PENDING:T,RESOLVED:p,REJECTED:D,HAS_ERROR:O,RECOMPUTING:y,DISPOSED:P,IS_COMPUTED:B,FORCE_COMPUTE:x}=H,St=class extends Q{constructor(t,s={}){if(typeof t!="function")throw new S(l.COMPUTED_MUST_BE_FUNCTION);if(super(),this[j]=!0,this[rt]=!0,this._error=null,this._promiseId=0,this._deps=new G,this._asyncRetryCount=0,this._lastDriftEpoch=v.UNINITIALIZED,this._trackEpoch=v.UNINITIALIZED,this._trackCount=0,this._value=void 0,this.flags=B|E|I,this._equal=s.equal??Object.is,this._fn=t,this._defaultValue="defaultValue"in s?s.defaultValue:w,this._onError=s.onError??null,this._maxAsyncRetries=(s.maxAsyncRetries??q.MAX_ASYNC_RETRIES)&V,U.attachDebugInfo(this,"computed",this.id),s.lazy===!1)try{this._recompute()}catch{}}_track(){c.current?.addDependency(this)}get value(){this._track();const t=this.flags;if((t&(p|E|I))===p)return this._value;if(t&P)throw new S(l.COMPUTED_DISPOSED);if(t&y){if(this._defaultValue!==w)return this._defaultValue;throw new S(l.COMPUTED_CIRCULAR_DEPENDENCY)}if(t&(E|I)&&((t&I)===0&&(t&x)===0&&this._deps.size>0&&!this._isDirty()?this.flags&=~E:this._recompute(),this.flags&p))return this._value;const s=this._defaultValue,e=s!==w;if(this.flags&T){if(e)return s;throw new S(l.COMPUTED_ASYNC_PENDING_NO_DEFAULT)}if(this.flags&D){if(e)return s;throw this._error}return this._value}peek(){return this._value}get state(){this._track();const t=this.flags;return t&p?b.RESOLVED:t&T?b.PENDING:t&D?b.REJECTED:b.IDLE}get hasError(){if(this._track(),this.flags&(D|O))return!0;const t=this._deps;if(!t.hasComputeds)return!1;const s=t.size;for(let e=0;e<s;e++){const i=t.getAt(e);if(i!=null&&i.node.flags&O)return!0}return!1}get isValid(){return!this.hasError}get errors(){this._track();const t=[];this._error&&t.push(this._error);const s=this._deps;if(!s.hasComputeds)return t.length===0?K:Object.freeze(t);const e=s.size;for(let i=0;i<e;i++){const n=s.getAt(i);if(n==null)continue;const r=n.node;r.flags&O&&this._collectErrorsFromDep(r,t)}return t.length===0?K:Object.freeze(t)}_collectErrorsFromDep(t,s){const e=t.errors,i=e.length;for(let n=0;n<i;n++){const r=e[n];r!=null&&!s.includes(r)&&s.push(r)}}get lastError(){return this._track(),this._error}get isPending(){return this._track(),(this.flags&T)!==0}get isResolved(){return this._track(),(this.flags&p)!==0}invalidate(){this.flags|=x,this._markDirty()}dispose(){this.flags&P||(this._deps.disposeAll(),this._slots?.clear(),this.flags=P|E|I,this._error=null,this._value=void 0,this._hotIndex=-1)}[Symbol.dispose](){this.dispose()}addDependency(t){if(t._lastSeenEpoch===this._trackEpoch)return;t._lastSeenEpoch=this._trackEpoch;const s=this._trackCount,e=this._deps.getAt(s);if(e!=null&&e.node===t)e.version=t.version;else if(!this._deps.claimExisting(t,s)){const i=new z(t,t.version,t.subscribe(this));this._deps.insertNew(s,i)}t.flags&B&&(this._deps.hasComputeds=!0),this._trackCount=s+1}_recompute(){if(this.flags&y)return;this.flags=(this.flags|y)&~x,this._trackEpoch=Y(),this._trackCount=0,this._deps.prepareTracking(),this._hotIndex=-1;let t=!1;try{const s=c.run(this,this._fn);this._deps.truncateFrom(this._trackCount),this._deps.seal(),t=!0,ht(s)?this._handleAsyncComputation(s):this._finalizeResolution(s)}catch(s){if(!t)try{this._deps.truncateFrom(this._trackCount)}catch{}this._handleError(s,l.COMPUTED_COMPUTATION_FAILED,!0)}finally{this._trackEpoch=v.UNINITIALIZED,this._trackCount=0,this.flags&=~y}}_handleAsyncComputation(t){this.flags=(this.flags|T)&~(I|E|p|D),this._notifySubscribers(void 0,void 0),this._asyncRetryCount=0,this._promiseId=(this._promiseId+1)%q.MAX_PROMISE_ID;const s=this._promiseId;t.then(e=>{if(s===this._promiseId){if(this._isDirty()){const i=nt();return this._lastDriftEpoch!==i&&(this._lastDriftEpoch=i,this._asyncRetryCount=0),this._asyncRetryCount++<this._maxAsyncRetries?this._markDirty():this._handleError(new S(`Async drift threshold exceeded after ${this._maxAsyncRetries} retries.`),l.COMPUTED_ASYNC_COMPUTATION_FAILED)}this._finalizeResolution(e),this._notifySubscribers(e,void 0)}},e=>s===this._promiseId&&this._handleError(e,l.COMPUTED_ASYNC_COMPUTATION_FAILED))}_handleError(t,s,e=!1){const i=A(t,S,s);if(!e&&!(this.flags&D)&&(this.version=k(this.version)),this._error=i,this.flags=this.flags&~(I|E|T|p)|D|O,this._onError)try{this._onError(i)}catch(n){console.error(l.CALLBACK_ERROR_IN_ERROR_HANDLER,n)}if(e)throw i;this._notifySubscribers(void 0,void 0)}_finalizeResolution(t){(!(this.flags&p)||!this._equal(this._value,t))&&(this.version=k(this.version)),this._value=t,this._error=null,this.flags=(this.flags|p)&~(I|E|T|D|O)}execute(){this._markDirty()}_markDirty(){this.flags&(y|E)||(this.flags|=E,this._notifySubscribers(void 0,void 0))}_isDirty(){const t=this._deps;return t.hasComputeds?this._deepDirtyCheck():t.isDirtyFast()}_deepDirtyCheck(){const t=this._deps,s=c.current;c.current=null;try{const e=t.size;for(let i=0;i<e;i++){const n=t.getAt(i);if(n==null)continue;const r=n.node;if(r.flags&B)try{r.value}catch{}if(r.version!==n.version)return this._hotIndex=i,!0}return this._hotIndex=-1,!1}finally{c.current=s}}};function gt(t,s={}){return new St(t,s)}var mt=class extends Q{constructor(t,s={}){super(),this[ot]=!0,this._cleanup=null,this._deps=new G,this._currentEpoch=v.UNINITIALIZED,this._lastFlushEpoch=v.UNINITIALIZED,this._fn=t,this._onError=s.onError??null,this._sync=s.sync??!1,this._maxExecutions=s.maxExecutionsPerSecond??g.MAX_EXECUTIONS_PER_SECOND,this._maxExecutionsPerFlush=s.maxExecutionsPerFlush??g.MAX_EXECUTIONS_PER_EFFECT,this._cleanup=null,this._deps=new G,this._currentEpoch=v.UNINITIALIZED,this._lastFlushEpoch=v.UNINITIALIZED,this._executionsInEpoch=0,this._executionCount=0,this._windowStart=0,this._windowCount=0,this._execId=0,this._trackCount=0,this._sync?this._notifyCallback=()=>this.execute():this._notifyCallback=()=>N.schedule(this),U.attachDebugInfo(this,"effect",this.id)}run(){if(this.flags&f.DISPOSED)throw new C(l.EFFECT_DISPOSED);this.execute(!0)}dispose(){this.flags&f.DISPOSED||(this.flags|=f.DISPOSED,this._execCleanup(),this._deps?.disposeAll())}[Symbol.dispose](){this.dispose()}addDependency(t){if(!(this.flags&f.EXECUTING))return;const s=this._currentEpoch;if(t._lastSeenEpoch===s)return;t._lastSeenEpoch=s;const e=this._trackCount,i=this._deps;let n;e===0?n=i._s0:e===1?n=i._s1:e===2?n=i._s2:e===3?n=i._s3:n=i.getAt(e),n!=null&&n.node===t?n.version=t.version:i.claimExisting(t,e)||this._insertNewDependency(t,e),t.flags&H.IS_COMPUTED&&(i.hasComputeds=!0),this._trackCount=e+1}_insertNewDependency(t,s){let e;try{const i=t.subscribe(this._notifyCallback);e=new z(t,t.version,i)}catch(i){const n=A(i,C,l.EFFECT_EXECUTION_FAILED);if(console.error(n),this._onError)try{this._onError(n)}catch{}e=new z(t,t.version,void 0)}this._deps.insertNew(s,e)}execute(t=!1){if(this.flags&(f.DISPOSED|f.EXECUTING))return;const s=this._deps;if(!t&&s.size>0&&!this._isDirty())return;this._checkInfiniteLoops(),this.flags|=f.EXECUTING,this._execCleanup(),this._currentEpoch=Y(),this._trackCount=0,s.prepareTracking(),this._hotIndex=-1;let e=!1;try{const i=c.run(this,this._fn);s.truncateFrom(this._trackCount),s.seal(),e=!0,ht(i)?this._handleAsyncResult(i):this._cleanup=typeof i=="function"?i:null}catch(i){if(!e)try{s.truncateFrom(this._trackCount)}catch{}this._handleExecutionError(i),this._cleanup=null}finally{this.flags&=~f.EXECUTING}}_isDirty(){const t=this._deps;return t.hasComputeds?this._deepDirtyCheck():t.isDirtyFast()}_handleAsyncResult(t){const s=++this._execId;t.then(e=>{if(s!==this._execId||this.flags&f.DISPOSED){if(typeof e=="function")try{e()}catch(i){this._handleExecutionError(i,l.EFFECT_CLEANUP_FAILED)}return}typeof e=="function"&&(this._cleanup=e)},e=>s===this._execId&&this._handleExecutionError(e))}_deepDirtyCheck(){const t=c.current;c.current=null;const s=this._deps;try{const e=s.size;for(let i=0;i<e;i++){const n=s.getAt(i);if(n==null)continue;const r=n.node;if(r.flags&H.IS_COMPUTED&&this._tryPullComputed(r),r.version!==n.version)return this._hotIndex=i,!0}return!1}finally{c.current=t}}_tryPullComputed(t){try{t.value}catch{}}_execCleanup(){if(this._cleanup){try{this._cleanup()}catch(t){this._handleExecutionError(t,l.EFFECT_CLEANUP_FAILED)}this._cleanup=null}}_checkInfiniteLoops(){const t=nt();this._lastFlushEpoch!==t&&(this._lastFlushEpoch=t,this._executionsInEpoch=0),++this._executionsInEpoch>this._maxExecutionsPerFlush&&this._throwInfiniteLoopError("per-effect"),ft()>g.MAX_EXECUTIONS_PER_FLUSH&&this._throwInfiniteLoopError("global"),this._executionCount++}_checkFrequencyLimit(){if(!Number.isFinite(this._maxExecutions))return;const t=Date.now();if(t-this._windowStart>=X.EFFECT_FREQUENCY_WINDOW){this._windowStart=t,this._windowCount=1;return}if(++this._windowCount>this._maxExecutions){const s=new C(l.EFFECT_FREQUENCY_LIMIT_EXCEEDED);throw this.dispose(),this._handleExecutionError(s),s}}get isDisposed(){return(this.flags&f.DISPOSED)!==0}get executionCount(){return this._executionCount}get isExecuting(){return(this.flags&f.EXECUTING)!==0}_throwInfiniteLoopError(t){const s=new C(`Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${$}`);throw this.dispose(),console.error(s),s}_handleExecutionError(t,s=l.EFFECT_EXECUTION_FAILED){const e=A(t,C,s);if(console.error(e),this._onError)try{this._onError(e)}catch(i){console.error(A(i,C,l.CALLBACK_ERROR_IN_ERROR_HANDLER))}}};function Tt(t,s={}){if(typeof t!="function")throw new C(l.EFFECT_MUST_BE_FUNCTION);const e=new mt(t,s);return e.execute(),e}function At(t){if(typeof t!="function")throw new TypeError(l.BATCH_CALLBACK_MUST_BE_FUNCTION);N.startBatch();try{return t()}finally{N.endBatch()}}exports.AsyncState=b;exports.AtomError=m;exports.ComputedError=S;exports.DEBUG_CONFIG=X;exports.DEBUG_RUNTIME=U;exports.EffectError=C;exports.SCHEDULER_CONFIG=g;exports.SchedulerError=F;exports.atom=It;exports.batch=At;exports.computed=gt;exports.effect=Tt;exports.isAtom=Ct;exports.isComputed=vt;exports.isEffect=Dt;exports.scheduler=N;exports.untracked=dt;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var A={IDLE:"idle",PENDING:"pending",RESOLVED:"resolved",REJECTED:"rejected"},D={DISPOSED:1,EXECUTING:8},z={DISPOSED:1,IS_COMPUTED:2,DIRTY:8,IDLE:16,PENDING:32,RESOLVED:64,REJECTED:128,RECOMPUTING:256,HAS_ERROR:512,FORCE_COMPUTE:1024},a={DISPOSED:1,SYNC:8,NOTIFICATION_SCHEDULED:16},p={MAX_EXECUTIONS_PER_SECOND:1e3,MAX_EXECUTIONS_PER_EFFECT:100,MAX_EXECUTIONS_PER_FLUSH:1e4,MAX_FLUSH_ITERATIONS:1e3,MIN_FLUSH_ITERATIONS:10,BATCH_QUEUE_SHRINK_THRESHOLD:1e3},Q={WARN_INFINITE_LOOP:!0,EFFECT_FREQUENCY_WINDOW:1e3},W={MAX_ASYNC_RETRIES:3,MAX_PROMISE_ID:Number.MAX_SAFE_INTEGER-1},m={UNINITIALIZED:-1,MIN:1},w=1073741823,_t={VERSION_BITS:16},Ut=(typeof process<"u"&&process.env,!1),K=Object.freeze([]),X=class{constructor(t,s,e=void 0){this.node=t,this.version=s,this.unsub=e}},ft=class{constructor(t,s){this.fn=t,this.sub=s}notify(t,s){const e=this.fn;if(e!==void 0){e(t,s);return}const i=this.sub;i!==void 0&&i.execute()}},g=class extends Error{constructor(t,s=null,e=!0){super(t),this.cause=s,this.recoverable=e,this.name="AtomError"}},S=class extends g{constructor(t,s=null){super(t,s,!0),this.name="ComputedError"}},I=class extends g{constructor(t,s=null){super(t,s,!1),this.name="EffectError"}},F=class extends g{constructor(t,s=null){super(t,s,!1),this.name="SchedulerError"}},c={COMPUTED_MUST_BE_FUNCTION:"Computed target must be a function",COMPUTED_ASYNC_PENDING_NO_DEFAULT:"Async computation pending with no default value",COMPUTED_COMPUTATION_FAILED:"Computation execution failed",COMPUTED_ASYNC_COMPUTATION_FAILED:"Async computation execution failed",COMPUTED_CIRCULAR_DEPENDENCY:"Circular dependency detected",COMPUTED_DISPOSED:"Attempted to access disposed computed",ATOM_SUBSCRIBER_MUST_BE_FUNCTION:"Subscriber must be a function or Subscriber object",ATOM_INDIVIDUAL_SUBSCRIBER_FAILED:"Subscriber execution failed",EFFECT_MUST_BE_FUNCTION:"Effect target must be a function",EFFECT_EXECUTION_FAILED:"Effect execution failed",EFFECT_CLEANUP_FAILED:"Effect cleanup failed",EFFECT_DISPOSED:"Attempted to run disposed effect",SCHEDULER_FLUSH_OVERFLOW:(t,s)=>`Maximum flush iterations (${t}) exceeded. ${s} jobs dropped. Possible infinite loop.`,CALLBACK_ERROR_IN_ERROR_HANDLER:"Exception encountered in onError handler",EFFECT_FREQUENCY_LIMIT_EXCEEDED:"Effect executed too frequently within 1 second. Suspected infinite loop.",SCHEDULER_CALLBACK_MUST_BE_FUNCTION:"Scheduler callback must be a function",SCHEDULER_END_BATCH_WITHOUT_START:"endBatch() called without matching startBatch(). Ignoring.",BATCH_CALLBACK_MUST_BE_FUNCTION:"Batch callback must be a function"},et=class{constructor(){this._s0=null,this._s1=null,this._s2=null,this._s3=null,this._count=0,this._overflow=null,this._freeIndices=null}get size(){return this._count}getAt(t){if(t<4)switch(t){case 0:return this._s0;case 1:return this._s1;case 2:return this._s2;case 3:return this._s3}const s=this._overflow;if(s!==null){const e=s[t-4];return e===void 0?null:e}return null}setAt(t,s){switch(t){case 0:this._s0=s;break;case 1:this._s1=s;break;case 2:this._s2=s;break;case 3:this._s3=s;break;default:this._overflow??=[],this._overflow[t-4]=s}t>=this._count&&(this._count=t+1)}truncateFrom(t){const s=this._count;if(t>=s)return;if(t<=3){if(t<=0){const i=this._s0;i!=null&&(this._onItemRemoved(i),this._s0=null)}if(t<=1){const i=this._s1;i!=null&&(this._onItemRemoved(i),this._s1=null)}if(t<=2){const i=this._s2;i!=null&&(this._onItemRemoved(i),this._s2=null)}if(t<=3){const i=this._s3;i!=null&&(this._onItemRemoved(i),this._s3=null)}}const e=this._overflow;if(e!==null&&s>4){const i=t>4?t-4:0,r=e.length;for(let n=i;n<r;n++){const o=e[n];o!=null&&(this._onItemRemoved(o),e[n]=null)}t<=4?(e.length=0,this._overflow=null):e.length=t-4}this._freeIndices!==null&&(this._freeIndices=null),this._count=t}_onItemRemoved(t){}add(t){if(this._s0===null){this._s0=t,this._count++;return}if(this._s1===null){this._s1=t,this._count++;return}if(this._s2===null){this._s2=t,this._count++;return}if(this._s3===null){this._s3=t,this._count++;return}this._addToOverflow(t)}_addToOverflow(t){const s=this._overflow;if(s===null)this._overflow=[t];else{const e=this._freeIndices;e!==null&&e.length>0?s[e.pop()]=t:s.push(t)}this._count++}remove(t){if(this._s0===t)return this._s0=null,this._count--,!0;if(this._s1===t)return this._s1=null,this._count--,!0;if(this._s2===t)return this._s2=null,this._count--,!0;if(this._s3===t)return this._s3=null,this._count--,!0;const s=this._overflow;if(s==null)return!1;for(let e=0,i=s.length;e<i;e++)if(s[e]===t){s[e]=null,this._count--;let r=this._freeIndices;return r===null&&(r=this._freeIndices=[]),r.push(e),!0}return!1}has(t){if(this._count===0)return!1;if(this._s0===t||this._s1===t||this._s2===t||this._s3===t)return!0;const s=this._overflow;if(s!=null){for(let e=0,i=s.length;e<i;e++)if(s[e]===t)return!0}return!1}forEach(t){const s=this._count;if(s===0)return;let e=0;const i=this._s0;if(i!=null&&(t(i),++e===s))return;const r=this._s1;if(r!=null&&(t(r),++e===s))return;const n=this._s2;if(n!=null&&(t(n),++e===s))return;const o=this._s3;if(o!=null&&(t(o),++e===s))return;const u=this._overflow;if(u!=null)for(let h=0,l=u.length;h<l;h++){const f=u[h];if(f!=null&&(t(f),++e===s))return}}forEachIndexed(t){const s=this._count;if(s===0)return 0;let e=0;const i=this._s0;if(i!=null&&(t(i),++e===s))return e;const r=this._s1;if(r!=null&&(t(r),++e===s))return e;const n=this._s2;if(n!=null&&(t(n),++e===s))return e;const o=this._s3;if(o!=null&&(t(o),++e===s))return e;const u=this._overflow;if(u!=null)for(let h=0,l=u.length;h<l;h++){const f=u[h];if(f!=null&&(t(f),++e===s))return e}return e}compact(){const t=this._overflow;if(t===null)return;let s=t.length;if(s===0)return;let e=0;for(;e<s;)if(t[e]===null){for(;s>e&&t[s-1]===null;)t.pop(),s--;s>e&&(t[e]=t.pop(),s--,e++)}else e++;this._freeIndices=null,s===0&&(this._overflow=null)}clear(){this._s0=null,this._s1=null,this._s2=null,this._s3=null,this._count=0,this._overflow!==null&&(this._overflow.length=0,this._overflow=null),this._freeIndices=null}dispose(){this.clear()}},Z=Symbol("AtomEffect.DebugName"),at=Symbol("AtomEffect.Id"),J=Symbol("AtomEffect.Type"),M=Symbol("AtomEffect.NoDefaultValue"),Et=class{constructor(){this.enabled=!1,this.warnInfiniteLoop=Q.WARN_INFINITE_LOOP}warn(t,s){}attachDebugInfo(t,s,e){}getDebugName(t){if(t!=null)return t[Z]}getDebugType(t){if(t!=null)return t[J]}},L=new Et,dt=1,pt=()=>dt++|0;function y(t,s,e){return t instanceof g?t:t instanceof Error?new s(`${t.name||t.constructor.name||"Error"} (${e}): ${t.message}`,t):new s(`Unexpected error (${e}): ${String(t)}`)}var Y=class{constructor(){this.flags=0,this.version=0,this._lastSeenEpoch=m.UNINITIALIZED,this._notifying=0,this._hotIndex=-1,this._slots=null,this._deps=null,this.id=pt()&w}get isDisposed(){return(this.flags&z.DISPOSED)!==0}get isComputed(){return(this.flags&z.IS_COMPUTED)!==0}get hasError(){return!1}subscribe(t){const s=typeof t=="function";if(!s&&(!t||typeof t.execute!="function"))throw y(new TypeError("Invalid subscriber"),g,c.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);let e=this._slots;e||(e=new et,this._slots=e);let i=!1;if(e._s0!=null&&(s?e._s0.fn===t:e._s0.sub===t))i=!0;else if(e._s1!=null&&(s?e._s1.fn===t:e._s1.sub===t))i=!0;else if(e._s2!=null&&(s?e._s2.fn===t:e._s2.sub===t))i=!0;else if(e._s3!=null&&(s?e._s3.fn===t:e._s3.sub===t))i=!0;else{const n=e._overflow;if(n!=null)for(let o=0,u=n.length;o<u;o++){const h=n[o];if(h!=null&&(s?h.fn===t:h.sub===t)){i=!0;break}}}if(i)return()=>{};const r=new ft(s?t:void 0,s?void 0:t);return e.add(r),()=>this._unsubscribe(r)}_unsubscribe(t){const s=this._slots;s&&(s.remove(t),this._notifying===0&&s.compact())}subscriberCount(){const t=this._slots;return t===null?0:t.size}_notifySubscribers(t,s){const e=this._slots;if(!(e===null||e.size===0)){this._notifying++;try{let i=e._s0;if(i!=null)try{i.notify(t,s)}catch(n){this._logNotifyError(n)}if(i=e._s1,i!=null)try{i.notify(t,s)}catch(n){this._logNotifyError(n)}if(i=e._s2,i!=null)try{i.notify(t,s)}catch(n){this._logNotifyError(n)}if(i=e._s3,i!=null)try{i.notify(t,s)}catch(n){this._logNotifyError(n)}const r=e._overflow;if(r!=null)for(let n=0,o=r.length;n<o;n++){const u=r[n];if(u!=null)try{u.notify(t,s)}catch(h){this._logNotifyError(h)}}}finally{--this._notifying===0&&e.compact()}}}_logNotifyError(t){console.error(y(t,g,c.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED))}_isDirty(){const t=this._deps;if(t===null||t.size===0)return!1;const s=this._hotIndex;if(s!==-1){const e=t.getAt(s);if(e!=null&&e.node.version!==e.version)return!0}return!t.hasComputeds&&!t.isDirtyFast()?!1:this._deepDirtyCheck()}},x=0;function k(){const t=x+1&w;return x=t===0?1:t,x}function G(t){const s=t+1&w;return s===0?1:s}var V=0,U=!1,it=0;function nt(){return it}function tt(){return U?!1:(U=!0,it=k(),V=0,!0)}function st(){U=!1}function Ct(){if(!U)return 0;const t=++V;if(t<=p.MAX_EXECUTIONS_PER_FLUSH)return t;throw new Error(`[atom-effect] Infinite loop detected: flush execution count exceeded ${p.MAX_EXECUTIONS_PER_FLUSH}`)}var It=class{constructor(){this._queueBuffer=[[],[]],this._bufferIndex=0,this._size=0,this._epoch=0,this._isProcessing=!1,this._isFlushingSync=!1,this._batchDepth=0,this._batchQueue=[],this._batchQueueSize=0,this._maxFlushIterations=p.MAX_FLUSH_ITERATIONS,this.onOverflow=null,this._boundRunLoop=this._runLoop.bind(this)}get queueSize(){return this._size}get isBatching(){return this._batchDepth>0}schedule(t){const s=this._epoch;if(t._nextEpoch===s)return;if(t._nextEpoch=s,this._batchDepth>0||this._isFlushingSync){this._batchQueue[this._batchQueueSize++]=t;return}const e=this._bufferIndex,i=this._queueBuffer[e];i[this._size++]=t,this._isProcessing||this._flush()}_flush(){this._isProcessing||this._size===0||(this._isProcessing=!0,queueMicrotask(this._boundRunLoop))}_runLoop(){try{if(this._size===0)return;const t=tt();this._drainQueue(),t&&st()}finally{this._isProcessing=!1,this._size>0&&this._batchDepth===0&&this._flush()}}_flushSync(){this._isFlushingSync=!0;const t=tt();try{this._mergeBatchQueue(),this._drainQueue()}finally{this._isFlushingSync=!1,t&&st()}}_mergeBatchQueue(){const t=this._batchQueueSize;if(t===0)return;const s=++this._epoch,e=this._batchQueue,i=this._bufferIndex,r=this._queueBuffer[i];let n=this._size;for(let o=0;o<t;o++){const u=e[o];u._nextEpoch!==s&&(u._nextEpoch=s,r[n++]=u)}this._size=n,this._batchQueueSize=0,e.length=0}_drainQueue(){let t=0;for(;this._size>0;){if(++t>this._maxFlushIterations){this._handleFlushOverflow();return}this._processQueue(),this._mergeBatchQueue()}}_processQueue(){const t=this._bufferIndex,s=this._queueBuffer[t],e=this._size;this._bufferIndex=t^1,this._size=0,this._epoch=this._epoch+1|0;for(let i=0;i<e;i++){const r=s[i];try{typeof r=="function"?r():r.execute()}catch(n){console.error(new F("Error occurred during scheduler execution",n))}}s.length=0}_handleFlushOverflow(){const t=this._size+this._batchQueueSize,s=this._maxFlushIterations;console.error(new F(c.SCHEDULER_FLUSH_OVERFLOW(s,t))),this._size=0;const e=this._bufferIndex;this._queueBuffer[e].length=0,this._batchQueueSize=0;const i=this.onOverflow;if(i)try{i(t)}catch{}}startBatch(){this._batchDepth++}endBatch(){this._batchDepth!==0&&--this._batchDepth===0&&this._flushSync()}setMaxFlushIterations(t){if(t<p.MIN_FLUSH_ITERATIONS)throw new F(`Max flush iterations must be at least ${p.MIN_FLUSH_ITERATIONS}`);this._maxFlushIterations=t}},O=new It,$=Symbol.for("atom-effect/atom"),rt=Symbol.for("atom-effect/computed"),ot=Symbol.for("atom-effect/effect"),Dt=Symbol.for("atom-effect/writable"),vt=class{constructor(){this.current=null}run(t,s){const e=this.current;this.current=t;try{return s()}finally{this.current=e}}},_=new vt;function St(t){const s=_,e=s.current;if(e===null)return t();s.current=null;try{return t()}finally{s.current=e}}var gt=class extends Y{constructor(t,s){super(),this[$]=!0,this[Dt]=!0,this._value=t,s&&(this.flags|=a.SYNC),L.attachDebugInfo(this,"atom",this.id)}get isNotificationScheduled(){return(this.flags&a.NOTIFICATION_SCHEDULED)!==0}get isSync(){return(this.flags&a.SYNC)!==0}get value(){const t=_.current;return t?.addDependency(this),this._value}set value(t){const s=this._value;if(Object.is(s,t))return;this._value=t,this.version=G(this.version);const e=this.flags;if((e&a.NOTIFICATION_SCHEDULED)!==0)return;const i=this._slots;i==null||i.size===0||(this._pendingOldValue=s,this.flags=e|a.NOTIFICATION_SCHEDULED,(e&a.SYNC)!==0&&!O.isBatching?this._flushNotifications():O.schedule(this))}execute(){this._flushNotifications()}_flushNotifications(){const t=this.flags;if((t&(a.NOTIFICATION_SCHEDULED|a.DISPOSED))!==a.NOTIFICATION_SCHEDULED)return;const s=this._pendingOldValue;this._pendingOldValue=void 0,this.flags=t&~a.NOTIFICATION_SCHEDULED,this._notifySubscribers(this._value,s)}peek(){return this._value}dispose(){const t=this.flags;(t&a.DISPOSED)===0&&(this._slots?.clear(),this.flags=t|a.DISPOSED,this._value=void 0,this._pendingOldValue=void 0)}_deepDirtyCheck(){return!1}[Symbol.dispose](){this.dispose()}};function Tt(t,s={}){return new gt(t,s.sync??!1)}var ut=class extends et{constructor(...t){super(...t),this._map=null,this._SCAN_THRESHOLD=32,this.hasComputeds=!1,this._depsHash=0}prepareTracking(){this.hasComputeds=!1}_onItemRemoved(t){const s=t.unsub;s&&s()}claimExisting(t,s){const e=this._count;if(s>=e)return!1;const i=e-s;if(this._map!==null||i>this._SCAN_THRESHOLD)return this._claimViaMap(t,s);if(s<4)switch(s){case 0:{const n=this._s0;if(n&&n.node===t&&n.unsub)return n.version=t.version,!0}case 1:if(e>1){const n=this._s1;if(n&&n.node===t&&n.unsub)return n.version=t.version,s!==1&&(this._s1=this._s0,this._s0=n),!0}case 2:if(e>2){const n=this._s2;if(n&&n.node===t&&n.unsub){if(n.version=t.version,s!==2){const o=s===0?this._s0:this._s1;s===0?this._s0=n:this._s1=n,this._s2=o}return!0}}case 3:if(e>3){const n=this._s3;if(n&&n.node===t&&n.unsub){if(n.version=t.version,s!==3){let o;s===0?(o=this._s0,this._s0=n):s===1?(o=this._s1,this._s1=n):(o=this._s2,this._s2=n),this._s3=o}return!0}}}const r=this._overflow;if(r){const n=t.version,o=s>4?s:4,u=r.length;for(let h=o-4;h<u;h++){const l=r[h];if(l&&l.node===t&&l.unsub)return l.version=n,this._swapGeneral(h+4,s,l),!0}}return!1}_claimViaMap(t,s){let e=this._map;if(e===null){e=this._map=new Map;const n=this._count;if(s<4){const u=this._s0;s<=0&&u?.unsub&&e.set(u.node,0);const h=this._s1;s<=1&&h?.unsub&&e.set(h.node,1);const l=this._s2;s<=2&&l?.unsub&&e.set(l.node,2);const f=this._s3;s<=3&&f?.unsub&&e.set(f.node,3)}const o=this._overflow;if(o&&n>4){const u=s>4?s:4,h=o.length;for(let l=u-4;l<h;l++){const f=o[l];f?.unsub&&e.set(f.node,l+4)}}}const i=e.get(t);if(i===void 0||i<s)return!1;const r=this.getAt(i);if(r==null||!r.unsub)return!1;if(r.version=t.version,i!==s){let n;s===0?n=this._s0:s===1?n=this._s1:s===2?n=this._s2:s===3?n=this._s3:n=this._overflow[s-4]??null,this.setAt(s,r),this.setAt(i,n),n?.unsub&&e.set(n.node,i),e.set(t,s)}return!0}_swapGeneral(t,s,e){if(t===s)return;let i;if(s===0?i=this._s0:s===1?i=this._s1:s===2?i=this._s2:s===3?i=this._s3:i=this._overflow[s-4]??null,this.setAt(s,e),t===0)this._s0=i;else if(t===1)this._s1=i;else if(t===2)this._s2=i;else if(t===3)this._s3=i;else{const r=this._overflow;r[t-4]=i}}insertNew(t,s){const e=this._count;if(t<e){let i;t===0?i=this._s0:t===1?i=this._s1:t===2?i=this._s2:t===3?i=this._s3:i=this._overflow[t-4]??null,i!=null&&(this._addToOverflow(i),this._map!==null&&i.unsub&&this._map.set(i.node,this._count-1))}if(t===0)this._s0=s;else if(t===1)this._s1=s;else if(t===2)this._s2=s;else if(t===3)this._s3=s;else{let i=this._overflow;i||(i=[],this._overflow=i),i[t-4]=s}t>=e&&(this._count=t+1)}truncateFrom(t){t>=this._count||(super.truncateFrom(t),this._map!==null&&(this._map.clear(),this._map=null))}seal(){this._depsHash=this._calculateHash(!1)}isDirtyFast(){return this._calculateHash(!0)!==this._depsHash}_calculateHash(t){const s=this._count;if(s===0)return 0;const e=_t.VERSION_BITS;let i=0;const r=this._s0;if(r!=null){const n=r.node,o=t?n.version:r.version;i=i+(o<<e)+n.id|0;const u=this._s1;if(s>1&&u!=null){const h=u.node,l=t?h.version:u.version;i=i+(l<<e)+h.id|0;const f=this._s2;if(s>2&&f!=null){const j=f.node,lt=t?j.version:f.version;i=i+(lt<<e)+j.id|0;const P=this._s3;if(s>3&&P!=null){const q=P.node,ct=t?q.version:P.version;i=i+(ct<<e)+q.id|0}}}}if(s>4){const n=this._overflow;for(let o=0,u=n.length;o<u;o++){const h=n[o],l=h.node,f=t?l.version:h.version;i=i+(f<<e)+l.id|0}}return i}disposeAll(){this._count>0&&this.truncateFrom(0),this.hasComputeds=!1}remove(t){throw new Error("remove() is strictly prohibited in DepSlotBuffer to preserve sequential cache paths.")}compact(){}};function mt(t){return typeof t=="object"&&t!==null&&$ in t}function yt(t){return typeof t=="object"&&t!==null&&rt in t}function Nt(t){return typeof t=="object"&&t!==null&&ot in t}function ht(t){return typeof t=="object"&&t!==null&&typeof t.then=="function"}var{IDLE:C,DIRTY:E,PENDING:T,RESOLVED:d,REJECTED:v,HAS_ERROR:b,RECOMPUTING:N,DISPOSED:B,IS_COMPUTED:R,FORCE_COMPUTE:H}=z,At=class extends Y{constructor(t,s={}){if(typeof t!="function")throw new S(c.COMPUTED_MUST_BE_FUNCTION);if(super(),this[$]=!0,this[rt]=!0,this._error=null,this._promiseId=0,this._deps=new ut,this._asyncRetryCount=0,this._lastDriftEpoch=m.UNINITIALIZED,this._trackEpoch=m.UNINITIALIZED,this._trackCount=0,this._value=void 0,this.flags=R|E|C,this._equal=s.equal??Object.is,this._fn=t,this._defaultValue="defaultValue"in s?s.defaultValue:M,this._onError=s.onError??null,this._maxAsyncRetries=(s.maxAsyncRetries??W.MAX_ASYNC_RETRIES)&w,L.attachDebugInfo(this,"computed",this.id),s.lazy===!1)try{this._recompute()}catch{}}get isDirty(){return(this.flags&E)!==0}get isRejected(){return(this.flags&v)!==0}get isRecomputing(){return(this.flags&N)!==0}get _hasErrorInternal(){return(this.flags&b)!==0}_track(){_.current?.addDependency(this)}get value(){const t=_.current;t?.addDependency(this);let s=this.flags;if((s&(d|E|C))===d)return this._value;if((s&B)!==0)throw new S(c.COMPUTED_DISPOSED);if((s&N)!==0){const r=this._defaultValue;if(r!==M)return r;throw new S(c.COMPUTED_CIRCULAR_DEPENDENCY)}if((s&(E|C))!==0){const r=this._deps;if((s&C)===0&&(s&H)===0&&r.size>0&&!this._isDirty()?s=this.flags&=~E:(this._recompute(),s=this.flags),(s&d)!==0)return this._value}const e=this._defaultValue,i=e!==M;if((s&T)!==0){if(i)return e;throw new S(c.COMPUTED_ASYNC_PENDING_NO_DEFAULT)}if((s&v)!==0){if(i)return e;throw this._error}return this._value}peek(){return this._value}get state(){const t=_.current;t?.addDependency(this);const s=this.flags;return(s&d)!==0?A.RESOLVED:(s&T)!==0?A.PENDING:(s&v)!==0?A.REJECTED:A.IDLE}get hasError(){const t=_.current;if(t?.addDependency(this),(this.flags&(v|b))!==0)return!0;const s=this._deps;if(!s.hasComputeds)return!1;const e=s.size;for(let i=0;i<e;i++)if(s.getAt(i)?.node.hasError)return!0;return!1}get isValid(){return!this.hasError}get errors(){const t=_.current;t?.addDependency(this);const s=this._error,e=this._deps;if(!e.hasComputeds)return s==null?K:Object.freeze([s]);const i=[];s!=null&&i.push(s);const r=e.size;for(let n=0;n<r;n++){const o=e.getAt(n);if(o==null)continue;const u=o.node;(u.flags&R)!==0&&u.hasError&&this._collectErrorsFromDep(u,i)}return i.length===0?K:Object.freeze(i)}_collectErrorsFromDep(t,s){const e=t.errors,i=e.length;for(let r=0;r<i;r++){const n=e[r];n!=null&&!s.includes(n)&&s.push(n)}}get lastError(){const t=_.current;return t?.addDependency(this),this._error}get isPending(){const t=_.current;return t?.addDependency(this),(this.flags&T)!==0}get isResolved(){const t=_.current;return t?.addDependency(this),(this.flags&d)!==0}invalidate(){this.flags|=H,this._markDirty()}dispose(){(this.flags&B)===0&&(this._deps.disposeAll(),this._slots!=null&&this._slots.clear(),this.flags=B|E|C,this._error=null,this._value=void 0,this._hotIndex=-1)}[Symbol.dispose](){this.dispose()}addDependency(t){const s=this._trackEpoch;if(t._lastSeenEpoch===s)return;t._lastSeenEpoch=s;const e=this._trackCount++,i=this._deps,r=i.getAt(e);if(r!=null&&r.node===t)r.version=t.version;else if(!i.claimExisting(t,e)){const n=new X(t,t.version,t.subscribe(this));i.insertNew(e,n)}(t.flags&R)!==0&&(i.hasComputeds=!0)}_recompute(){if(this.isRecomputing)return;this.flags=(this.flags|N)&~H,this._trackEpoch=k(),this._trackCount=0,this._deps.prepareTracking(),this._hotIndex=-1;let t=!1;try{const s=_.run(this,this._fn);this._deps.truncateFrom(this._trackCount),this._deps.seal(),t=!0,ht(s)?this._handleAsyncComputation(s):this._finalizeResolution(s)}catch(s){if(!t)try{this._deps.truncateFrom(this._trackCount)}catch{}this._handleError(s,c.COMPUTED_COMPUTATION_FAILED,!0)}finally{this._trackEpoch=m.UNINITIALIZED,this._trackCount=0,this.flags&=~N}}_handleAsyncComputation(t){this.flags=(this.flags|T)&~(C|E|d|v),this._notifySubscribers(void 0,void 0),this._asyncRetryCount=0,this._promiseId=(this._promiseId+1)%W.MAX_PROMISE_ID;const s=this._promiseId;t.then(e=>{if(s===this._promiseId){if(this._isDirty()){const i=nt();return this._lastDriftEpoch!==i&&(this._lastDriftEpoch=i,this._asyncRetryCount=0),this._asyncRetryCount++<this._maxAsyncRetries?this._markDirty():this._handleError(new S(`Async drift threshold exceeded after ${this._maxAsyncRetries} retries.`),c.COMPUTED_ASYNC_COMPUTATION_FAILED)}this._finalizeResolution(e),this._notifySubscribers(e,void 0)}},e=>s===this._promiseId&&this._handleError(e,c.COMPUTED_ASYNC_COMPUTATION_FAILED))}_handleError(t,s,e=!1){const i=y(t,S,s);if(!e&&!this.isRejected&&(this.version=G(this.version)),this._error=i,this.flags=this.flags&~(C|E|T|d)|v|b,this._onError)try{this._onError(i)}catch(r){console.error(c.CALLBACK_ERROR_IN_ERROR_HANDLER,r)}if(e)throw i;this._notifySubscribers(void 0,void 0)}_finalizeResolution(t){const s=this.flags;((s&d)===0||!this._equal(this._value,t))&&(this.version=G(this.version)),this._value=t,this._error=null,this.flags=(s|d)&~(C|E|T|v|b)}execute(){this._markDirty()}_markDirty(){const t=this.flags;(t&(N|E))===0&&(this.flags=t|E,this._notifySubscribers(void 0,void 0))}_isDirty(){const t=this._deps;return t.hasComputeds?this._deepDirtyCheck():t.isDirtyFast()}_deepDirtyCheck(){const t=this._deps,s=_.current;_.current=null;try{const e=t.size;for(let i=0;i<e;i++){const r=t.getAt(i);if(r==null)continue;const n=r.node;if((n.flags&R)!==0)try{n.value}catch{}if(n.version!==r.version)return this._hotIndex=i,!0}return this._hotIndex=-1,!1}finally{_.current=s}}};function Ot(t,s={}){return new At(t,s)}var bt=class extends Y{constructor(t,s={}){super(),this[ot]=!0,this._cleanup=null,this._deps=new ut,this._currentEpoch=m.UNINITIALIZED,this._lastFlushEpoch=m.UNINITIALIZED,this._fn=t,this._onError=s.onError??null,this._sync=s.sync??!1,this._maxExecutions=s.maxExecutionsPerSecond??p.MAX_EXECUTIONS_PER_SECOND,this._maxExecutionsPerFlush=s.maxExecutionsPerFlush??p.MAX_EXECUTIONS_PER_EFFECT,this._executionsInEpoch=0,this._executionCount=0,this._windowStart=0,this._windowCount=0,this._execId=0,this._trackCount=0,this._sync?this._notifyCallback=()=>this.execute():this._notifyCallback=()=>O.schedule(this),L.attachDebugInfo(this,"effect",this.id)}run(){if(this.isDisposed)throw new I(c.EFFECT_DISPOSED);this.execute(!0)}dispose(){this.isDisposed||(this.flags|=D.DISPOSED,this._execCleanup(),this._deps?.disposeAll())}[Symbol.dispose](){this.dispose()}addDependency(t){if((this.flags&D.EXECUTING)===0)return;const s=this._currentEpoch;if(t._lastSeenEpoch===s)return;t._lastSeenEpoch=s;const e=this._trackCount++,i=this._deps;let r;switch(e){case 0:r=i._s0;break;case 1:r=i._s1;break;case 2:r=i._s2;break;case 3:r=i._s3;break;default:r=i.getAt(e)}r!=null&&r.node===t?r.version=t.version:i.claimExisting(t,e)||this._insertNewDependency(t,e),t.isComputed&&(i.hasComputeds=!0)}_insertNewDependency(t,s){let e;try{const i=t.subscribe(this._notifyCallback);e=new X(t,t.version,i)}catch(i){const r=y(i,I,c.EFFECT_EXECUTION_FAILED);if(console.error(r),this._onError)try{this._onError(r)}catch{}e=new X(t,t.version,void 0)}this._deps.insertNew(s,e)}execute(t=!1){const s=this.flags;if((s&(D.DISPOSED|D.EXECUTING))!==0)return;const e=this._deps;if(!t&&e.size>0&&!this._isDirty())return;this._checkInfiniteLoops(),this.flags=s|D.EXECUTING,this._execCleanup(),this._currentEpoch=k(),this._trackCount=0,e.prepareTracking(),this._hotIndex=-1;let i=!1;try{const r=_.run(this,this._fn);e.truncateFrom(this._trackCount),e.seal(),i=!0,ht(r)?this._handleAsyncResult(r):this._cleanup=typeof r=="function"?r:null}catch(r){if(!i)try{e.truncateFrom(this._trackCount)}catch{}this._handleExecutionError(r),this._cleanup=null}finally{this.flags&=~D.EXECUTING}}_isDirty(){const t=this._deps;return t.hasComputeds?this._deepDirtyCheck():t.isDirtyFast()}_handleAsyncResult(t){const s=++this._execId;t.then(e=>{if(s!==this._execId||this.isDisposed){if(typeof e=="function")try{e()}catch(i){this._handleExecutionError(i,c.EFFECT_CLEANUP_FAILED)}return}typeof e=="function"&&(this._cleanup=e)},e=>s===this._execId&&this._handleExecutionError(e))}_deepDirtyCheck(){const t=_.current;_.current=null;const s=this._deps;try{const e=s.size;for(let i=0;i<e;i++){const r=s.getAt(i);if(r==null)continue;const n=r.node;if(n.isComputed&&this._tryPullComputed(n),n.version!==r.version)return this._hotIndex=i,!0}return!1}finally{_.current=t}}_tryPullComputed(t){try{t.value}catch{}}_execCleanup(){const t=this._cleanup;if(t!=null){this._cleanup=null;try{t()}catch(s){this._handleExecutionError(s,c.EFFECT_CLEANUP_FAILED)}}}_checkInfiniteLoops(){const t=nt();this._lastFlushEpoch!==t&&(this._lastFlushEpoch=t,this._executionsInEpoch=0),++this._executionsInEpoch>this._maxExecutionsPerFlush&&this._throwInfiniteLoopError("per-effect"),Ct()>p.MAX_EXECUTIONS_PER_FLUSH&&this._throwInfiniteLoopError("global"),this._executionCount++}_checkFrequencyLimit(){if(!Number.isFinite(this._maxExecutions))return;const t=Date.now();if(t-this._windowStart>=Q.EFFECT_FREQUENCY_WINDOW){this._windowStart=t,this._windowCount=1;return}if(++this._windowCount>this._maxExecutions){const s=new I(c.EFFECT_FREQUENCY_LIMIT_EXCEEDED);throw this.dispose(),this._handleExecutionError(s),s}}get executionCount(){return this._executionCount}get isExecuting(){return(this.flags&D.EXECUTING)!==0}_throwInfiniteLoopError(t){const s=new I(`Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${V}`);throw this.dispose(),console.error(s),s}_handleExecutionError(t,s=c.EFFECT_EXECUTION_FAILED){const e=y(t,I,s);if(console.error(e),this._onError)try{this._onError(e)}catch(i){console.error(y(i,I,c.CALLBACK_ERROR_IN_ERROR_HANDLER))}}};function Rt(t,s={}){if(typeof t!="function")throw new I(c.EFFECT_MUST_BE_FUNCTION);const e=new bt(t,s);return e.execute(),e}function Ft(t){if(typeof t!="function")throw new TypeError(c.BATCH_CALLBACK_MUST_BE_FUNCTION);const s=O;s.startBatch();try{return t()}finally{s.endBatch()}}exports.AsyncState=A;exports.AtomError=g;exports.ComputedError=S;exports.DEBUG_CONFIG=Q;exports.DEBUG_RUNTIME=L;exports.EffectError=I;exports.SCHEDULER_CONFIG=p;exports.SchedulerError=F;exports.atom=Tt;exports.batch=Ft;exports.computed=Ot;exports.effect=Rt;exports.isAtom=mt;exports.isComputed=yt;exports.isEffect=Nt;exports.scheduler=O;exports.untracked=St;
|
|
2
2
|
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|