@yoltra/core 0.5.0 → 0.7.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/yoltra.cjs CHANGED
@@ -1,11 +1,6 @@
1
1
  /*!
2
- * @yoltra/core v0.5.0
2
+ * @yoltra/core v0.7.0
3
3
  * (c) 2026 Manu Ramirez <@pixerael>
4
- * License: MIT
5
- * Homepage: https://yoltra.dev
6
- *
7
- * This source code is licensed under the MIT license found in the
8
- * LICENSE file in the root directory of this source tree
9
- */
10
- "use strict";var K=Object.defineProperty;var W=(o,e,t)=>e in o?K(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var p=(o,e,t)=>W(o,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class _{constructor(){p(this,"handlers",new Map)}on(e,t,n){let s=this.handlers.get(e);s||(s=new Map,this.handlers.set(e,s));let i=s.get(t);return i||(i=new Set,s.set(t,i)),i.add(n),()=>this.off(e,t,n)}off(e,t,n){const s=this.handlers.get(e);if(!s)return;const i=s.get(t);i&&(i.delete(n),i.size===0&&s.delete(t),s.size===0&&this.handlers.delete(e))}emit(e,t,n,s){const i=this.handlers.get(e);if(!i)return;const d=i.get(t);if(!(!d||d.size===0))for(const a of[...d])try{a(n,s)}catch(r){console.error("EventBus handler error:",r)}}clear(){this.handlers.clear()}}class N{constructor(){p(this,"handlers",new Map);p(this,"patternHandlers",new Map);p(this,"patternIndex",new Map)}on(e,t,n){const s=String(t);if(this.isPattern(s)){const i=s;this.patternHandlers.has(e)||this.patternHandlers.set(e,new Map);const d=this.patternHandlers.get(e);return d.has(i)||(d.set(i,[]),this.indexPattern(e,i)),d.get(i).push(n),()=>this.offPattern(e,i,n)}else{const i=this.normalizeTypeKey(s);this.handlers.has(e)||this.handlers.set(e,new Map);const d=this.handlers.get(e);return d.has(i)||d.set(i,[]),d.get(i).push(n),()=>this.offExactNormalized(e,i,n)}}off(e,t,n){const s=this.normalizeTypeKey(String(t));this.offExactNormalized(e,s,n)}offExactNormalized(e,t,n){const s=this.handlers.get(e);if(!s)return;const i=s.get(t);if(!i)return;const d=i.indexOf(n);d!==-1&&i.splice(d,1),i.length===0&&s.delete(t),s.size===0&&this.handlers.delete(e)}offPattern(e,t,n){const s=this.patternHandlers.get(e);if(!s)return;const i=s.get(t);if(!i)return;const d=i.indexOf(n);d!==-1&&i.splice(d,1),i.length===0&&(s.delete(t),this.unindexPattern(e,t)),s.size===0&&(this.patternHandlers.delete(e),this.patternIndex.delete(e))}emit(e,t,n){const s=String(t),i=this.normalizeTypeKey(s),d=this.handlers.get(e)?.get(i)??[],a=this.matchingPatternHandlers(e,s),r=new Set,f=c=>{for(const u of[...c])if(!r.has(u)){r.add(u);try{u(n)}catch(l){console.error(l);continue}}};f(d);for(const c of a)f(c)}emitWith(e,t,n){const s=String(t),i=this.normalizeTypeKey(s),d=this.handlers.get(e)?.get(i)??[],a=this.matchingPatternHandlers(e,s);if(d.length===0&&a.length===0)return;const r=n(),f=new Set,c=u=>{for(const l of[...u])if(!f.has(l)){f.add(l);try{l(r)}catch(y){console.error(y);continue}}};c(d);for(const u of a)c(u)}isPattern(e){return e.includes("*")}normalizeTypeKey(e){return e.replace(/^\./,"")}splitPath(e){return this.normalizeTypeKey(e).split(".").filter(Boolean)}indexPattern(e,t){let n=this.patternIndex.get(e);n===void 0&&(n={byHead:new Map,anyHead:[]},this.patternIndex.set(e,n));const s=this.splitPath(t),i={pattern:t,segments:s},d=s[0];if(d===void 0||d==="*"||d==="**"){n.anyHead.push(i);return}const a=n.byHead.get(d);a===void 0?n.byHead.set(d,[i]):a.push(i)}unindexPattern(e,t){const n=this.patternIndex.get(e);if(n===void 0)return;const s=this.splitPath(t)[0],i=s===void 0||s==="*"||s==="**"?n.anyHead:n.byHead.get(s);if(i===void 0)return;const d=i.findIndex(a=>a.pattern===t);d!==-1&&i.splice(d,1),i.length===0&&i!==n.anyHead&&s!==void 0&&n.byHead.delete(s)}matchingPatternHandlers(e,t){const n=this.patternHandlers.get(e),s=this.patternIndex.get(e);if(n===void 0||n.size===0||s===void 0)return[];const i=this.splitPath(t),d=[],a=f=>{for(const c of f){if(!this.matchSegments(c.segments,i))continue;const u=n.get(c.pattern);u!==void 0&&d.push(u)}},r=i[0];if(r!==void 0){const f=s.byHead.get(r);f!==void 0&&a(f)}return a(s.anyHead),d}matchSegments(e,t){let n=0,s=0,i=-1,d=0;for(;s<t.length;)if(n<e.length&&(e[n]==="*"||e[n]===t[s]))n++,s++;else if(n<e.length&&e[n]==="**")i=n,d=s,n++;else if(i!==-1)n=i+1,s=++d;else return!1;for(;n<e.length&&e[n]==="**";)n++;return n===e.length}clear(){this.handlers.clear(),this.patternHandlers.clear(),this.patternIndex.clear()}__introspect(){const e=[];for(const[t,n]of this.handlers)for(const[s,i]of n)i.length>0&&e.push({channel:t,type:s,count:i.length});for(const[t,n]of this.patternHandlers)for(const[s,i]of n)i.length>0&&e.push({channel:t,type:s,count:i.length});return e}}class D{constructor(e){p(this,"_reduce");this._reduce=e}reduce(e,t){return this._reduce(e,t)}}const x=new Set;function M(o,e){const t=o?`${o}.${e}`:e;x.has(t)||(x.add(t),console.warn(`[yoltra] State key "${e}"${o?` under "${o}"`:""} contains a dot. Paths are dotted, so this key is indistinguishable from nested objects of the same name: a subscription to "${t}" may match the wrong value, and DevTools patches for it will address the wrong node. Rename the key, or nest it.`))}function k(o,e,t="",n=new Map){const s=[];return $(o,e,t,n,s),s}function $(o,e,t,n,s){if(o===e)return;if(typeof o!="object"||typeof e!="object"||o===null||e===null){if(typeof o=="number"&&Number.isNaN(o)&&Number.isNaN(e))return;s.push(t);return}if(o instanceof Date&&e instanceof Date){o.getTime()!==e.getTime()&&s.push(t);return}if(o instanceof RegExp&&e instanceof RegExp){(o.source!==e.source||e.flags!==o.flags)&&s.push(t);return}if(o instanceof Map||e instanceof Map){s.push(t);return}if(o instanceof Set||e instanceof Set){s.push(t);return}const i=o,d=e,a=n.get(i);if(a?.has(d))return;const r=a??new Set;r.add(d),a||n.set(i,r);try{const f=Array.isArray(o),c=Array.isArray(e);if(f!==c){s.push(t);return}if(f){const h=o,m=e;h.length!==m.length&&t&&s.push(t);const g=Math.min(h.length,m.length);for(let w=0;w<g;w++)h[w]!==m[w]&&$(h[w],m[w],t?`${t}.${w}`:`${w}`,n,s);for(let w=g;w<Math.max(h.length,m.length);w++)s.push(t?`${t}.${w}`:`${w}`);return}const u=Object.keys(o),l=Object.keys(e);if(u.length===0&&l.length===0){s.push(t);return}let y=u.length===l.length;if(y){for(let h=0;h<l.length;h++)if(!Object.prototype.hasOwnProperty.call(o,l[h])){y=!1;break}}if(y){for(const h of l)o[h]!==e[h]&&(process.env.NODE_ENV!=="production"&&h.includes(".")&&M(t,h),$(o[h],e[h],t?`${t}.${h}`:h,n,s));return}for(const h of l){const m=Object.prototype.hasOwnProperty.call(o,h);if(m&&o[h]===e[h])continue;process.env.NODE_ENV!=="production"&&h.includes(".")&&M(t,h);const g=t?`${t}.${h}`:h;if(!m){s.push(g);continue}$(o[h],e[h],g,n,s)}for(const h of u)Object.prototype.hasOwnProperty.call(e,h)||(process.env.NODE_ENV!=="production"&&h.includes(".")&&M(t,h),s.push(t?`${t}.${h}`:h))}finally{r.delete(d),r.size===0&&n.delete(i)}}function v(o,e=new WeakSet,t){if(o===null||typeof o!="object"||e.has(o)||(t!==void 0&&o===t.watch&&t.onFound(),Object.isFrozen(o)))return o;if(e.add(o),Array.isArray(o)){const n=o;for(let s=0;s<n.length;s++)n[s]=v(n[s],e,t);return Object.freeze(n)}for(const n of Object.getOwnPropertyNames(o)){const s=Object.getOwnPropertyDescriptor(o,n);!s||!("value"in s)||(o[n]=v(o[n],e,t))}for(const n of Object.getOwnPropertySymbols(o)){const s=Object.getOwnPropertyDescriptor(o,n);!s||!("value"in s)||(o[n]=v(o[n],e,t))}return Object.freeze(o)}function j(o,e){try{return structuredClone(e)}catch(t){throw new Error(`[yoltra] Initial state for slice "${String(o)}" could not be copied: ${t instanceof Error?t.message:String(t)}. State must be structured-cloneable — functions, class instances and DOM nodes are not. Keep behaviour out of state and store plain data.`)}}function P(o,e){return process.env.NODE_ENV==="production"?o:v(o,new WeakSet,e)}const z=100,I=()=>typeof performance<"u"&&typeof performance.now=="function"?performance.now():Date.now();class S{constructor(e){p(this,"name");p(this,"middleware");p(this,"reducers");p(this,"state");p(this,"reducerBus");p(this,"connectorBus");p(this,"listeners",new Set);p(this,"effects",new Map);p(this,"patternEffects",new Set);p(this,"committedEventSubscribers",new Map);p(this,"uncommittedEventSubscribers",new Map);p(this,"allEventSubscribers",new Map);p(this,"sliceUnsubs",new Map);p(this,"patternReducers",new Map);p(this,"replayEnabled");p(this,"idFactory");p(this,"onEffectError");p(this,"onReducerError");p(this,"warnedPayloadAliases",new Set);p(this,"reduceQueue",[]);p(this,"isReducing",!1);p(this,"instrumentObservers",new Set);p(this,"changedPathSink",null);p(this,"inFlightEffects",0);p(this,"processedEvents",new Map);p(this,"dedupCount",0);p(this,"effectMeta",new WeakMap);p(this,"dedupConfig");p(this,"eventCleanupTimer",null);if(this.name=e.name??"yoltra Store",this.reducerBus=new _,this.connectorBus=new N,this.middleware=[...e.middleware??[]],this.reducers={},this.state={},this.replayEnabled=e.devtools?.allowReplay??!1,this.idFactory=e.idFactory??(()=>crypto.randomUUID()),this.onEffectError=e.onEffectError,this.onReducerError=e.onReducerError,this.dedupConfig={windowMs:e.dedupWindowMs??0,maxCacheSize:1e3},Object.entries(e.reducer).forEach(([t,n])=>{this.mountSlice(t,n,{preserveState:!1})}),e.effects?.length)for(const t of e.effects)this.registerEffect(t);this.dispose=this.dispose.bind(this),this.notifyEffects=this.notifyEffects.bind(this),this.forwardEvent=this.forwardEvent.bind(this),this.__applyExternalState=this.__applyExternalState.bind(this),this.__replayEvents=this.__replayEvents.bind(this),this.__devtoolsIntrospect=this.__devtoolsIntrospect.bind(this),this.mountSlice=this.mountSlice.bind(this),this.unmountSlice=this.unmountSlice.bind(this),this.getAtPath=this.getAtPath.bind(this),this.emit=this.emit.bind(this),this.subscribe=this.subscribe.bind(this),this.connect=this.connect.bind(this),this.onEffect=this.onEffect.bind(this),this.onEvent=this.onEvent.bind(this),this.getState=this.getState.bind(this),this.registerEffect=this.registerEffect.bind(this),this.registerMiddleware=this.registerMiddleware.bind(this),this.registerReducer=this.registerReducer.bind(this),this.replaceMiddleware=this.replaceMiddleware.bind(this),this.replaceEffects=this.replaceEffects.bind(this),this.replaceReducers=this.replaceReducers.bind(this),this.hotReplace=this.hotReplace.bind(this)}dispose(){this.eventCleanupTimer&&(clearInterval(this.eventCleanupTimer),this.eventCleanupTimer=null),this.processedEvents.clear(),this.effects.clear(),this.patternEffects.clear(),this.effectMeta=new WeakMap,this.warnedPayloadAliases.clear(),this.listeners.clear(),this.committedEventSubscribers.clear(),this.uncommittedEventSubscribers.clear(),this.allEventSubscribers.clear(),this.instrumentObservers.clear(),this.connectorBus.clear(),this.reducerBus.clear(),this.patternReducers.clear(),this.sliceUnsubs.clear(),this.changedPathSink=null}fingerprint(e,t,n){const s=`${e}::${t}`;try{if(n==null)return`${s}::null`;if(typeof n!="object")return`${s}::${String(n)}`;const i=JSON.stringify(n);return`${s}::${i}`}catch{return`${s}::${Date.now()}::${Math.random()}`}}shouldDedupe(e,t){const n=Date.now(),s=this.processedEvents.get(e);return s!==void 0&&n-s<t?(this.dedupCount++,!0):(this.processedEvents.set(e,n),this.ensureCleanupTimer(),this.processedEvents.size>this.dedupConfig.maxCacheSize&&this.pruneProcessedEvents(n),!1)}ensureCleanupTimer(){this.eventCleanupTimer===null&&(this.eventCleanupTimer=setInterval(()=>{this.pruneProcessedEvents(Date.now())},5e3),this.eventCleanupTimer.unref?.())}pruneProcessedEvents(e){const t=Math.max(this.dedupConfig.windowMs,z),n=e-t*2;for(const[s,i]of this.processedEvents)i<n&&this.processedEvents.delete(s);this.processedEvents.size===0&&this.eventCleanupTimer!==null&&(clearInterval(this.eventCleanupTimer),this.eventCleanupTimer=null)}matchesWhen(e,t){return!e||"any"in e&&e.any===!0?!0:"keys"in e?e.keys.some(([n,s])=>t.channel===n&&t.type===s):"channel"in e?t.channel===e.channel:"channels"in e?e.channels.includes(t.channel):!1}getMiddlewareFunction(e){return typeof e=="function"?e:e.middleware}getMiddlewareWhen(e){if(typeof e!="function")return e.when}async notifyEffects(e){const t=`${String(e.channel)}::${String(e.type)}`,n=this.effects.get(t);if(n&&n.size>0)for(const s of[...n])try{await s(e,this.getState,this.emit)}catch(i){console.error("Effect error:",i),this.onEffectError?.(i,e)}for(const{effect:s,when:i}of this.patternEffects)if(this.matchesWhen(i,e))try{await s(e,this.getState,this.emit)}catch(d){console.error("Effect error:",d),this.onEffectError?.(d,e)}}notifyEventSubscribers(e,t){const n=`${String(e.channel)}::${String(e.type)}`,i=(t==="committed"?this.committedEventSubscribers:this.uncommittedEventSubscribers).get(n);if(i?.size)for(const a of[...i])this.invokeEventSubscriber(a,e,t);const d=this.allEventSubscribers.get(n);if(d?.size)for(const a of[...d])this.invokeEventSubscriber(a,e,t)}invokeEventSubscriber(e,t,n){try{const s=e(t,this.getState,this.emit,n);s&&typeof s.then=="function"&&s.catch(i=>console.error("Event subscription error:",i))}catch(s){console.error("Event subscription error:",s)}}forwardEventGuarded(e,t){try{return this.forwardEvent(e,t)}catch(n){return console.error(`Reducer error in slice "${e}":`,n),this.onReducerError?.(n,t,e),!1}}forwardEvent(e,t){const n=this.state[e],s=this.reducers[e].reduce(n,t);if(n===s)return!1;const i=k(n,s);if(i.length===0)return!1;const d=t.payload,a=process.env.NODE_ENV!=="production"&&d!==null&&typeof d=="object"?{watch:d,onFound:()=>{const c=`${e}:${t.channel}:${t.type}`;this.warnedPayloadAliases.has(c)||(this.warnedPayloadAliases.add(c),console.warn(`[yoltra] Slice "${e}" stored the payload of "${t.channel}/${t.type}" by reference. It is now frozen along with the rest of the state, so the emitter mutating it later will throw in development and silently corrupt state in production. Copy the payload in the reducer instead.`))}}:void 0,r=P(s,a);if(this.state={...this.state,[e]:r},this.changedPathSink)for(const c of i)this.changedPathSink.push(c?`${e}.${c}`:e);const f=new Set;for(const c of i){if(c===""){f.add("");continue}for(const u of S.buildAncestorPaths(c))f.add(u)}for(const c of f)this.connectorBus.emitWith(e,c,()=>({oldValue:this.getAtPath(n,c),newValue:this.getAtPath(r,c),path:c}));return!0}__devtoolsIntrospect(){const e=Object.keys(this.reducers).map(a=>{const r=this.patternReducers.get(a);return{name:a,when:r}}),t=[];for(const[a,r]of this.effects){if(r.size===0)continue;const[f,c]=a.split("::");for(const u of r){const l=this.effectMeta.get(u);t.push({channel:f,type:c,name:l?.name,description:l?.description})}}for(const a of this.patternEffects){const r=this.effectMeta.get(a.effect);t.push({channel:"*",type:"*",name:r?.name,description:r?.description})}const n=[];for(const a of this.middleware)typeof a=="function"?n.push({name:a.name||void 0}):n.push({name:a.meta?.name,description:a.meta?.description,when:a.when});const s=[];for(const a of this.connectorBus.__introspect())for(let r=0;r<a.count;r++)s.push({reducer:a.channel,property:a.type});const i=[];for(const[a,r]of this.committedEventSubscribers){if(r.size===0)continue;const[f,c]=a.split("::");for(let u=0;u<r.size;u++)i.push({channel:f,type:c,phase:"committed"})}for(const[a,r]of this.uncommittedEventSubscribers){if(r.size===0)continue;const[f,c]=a.split("::");for(let u=0;u<r.size;u++)i.push({channel:f,type:c,phase:"uncommitted"})}for(const[a,r]of this.allEventSubscribers){if(r.size===0)continue;const[f,c]=a.split("::");for(let u=0;u<r.size;u++)i.push({channel:f,type:c,phase:"all"})}const d=this.listeners.size;return{reducers:e,effects:t,middleware:n,atomic:s,event:i,coarse:d,dedupHits:this.dedupCount,queueDepth:this.reduceQueue.length+this.inFlightEffects}}__applyExternalState(e){if(!this.replayEnabled)throw new Error("[yoltra] External state apply (time-travel) is disabled. Enable it with createStore({ devtools: { allowReplay: true } })");const t=this.state,n=e,s={...this.state};let i=!1;Object.keys(this.reducers).forEach(d=>{const a=t?.[d],r=n?.[d];if(r===void 0){process.env.NODE_ENV!=="production"&&console.warn(`[yoltra] External state is missing slice "${String(d)}"; retaining its current value. Time-travel snapshots should contain all slices.`);return}if(a===r)return;const f=P(r);s[d]=f,i=!0;const c=k(a,r);if(c.length===0)return;const u=new Set;for(const l of c){if(l===""){u.add("");continue}for(const y of S.buildAncestorPaths(l))u.add(y)}for(const l of u){const y=this.getAtPath(a,l),h=this.getAtPath(f,l);this.connectorBus.emit(d,l,{oldValue:y,newValue:h,path:l})}}),i&&(this.state=s),i&&this.listeners.forEach(d=>d())}__replayEvents(e,t){if(!this.replayEnabled)throw new Error("[yoltra] Event replay is disabled. Enable it with createStore({ devtools: { allowReplay: true } })");this.__applyExternalState(e);for(const n of t){const s=n,i=this.state;this.reducerBus.emit(s.channel,s.type,s.payload,s);for(const[r,f]of this.patternReducers)this.matchesWhen(f,s)&&this.forwardEventGuarded(r,s);const d=this.state,a=i!==d;this.notifyEventSubscribers(s,"committed"),a&&this.listeners.forEach(r=>r())}}async emit(e,t,n,s){const i=s?.dedupKey,d=this.dedupConfig.windowMs;if(s?.skipDedup!==!0&&(d>0||i!==void 0)){const c=i!==void 0&&d<=0?z:d,u=i!==void 0?`${e}::${t}::#${i}`:this.fingerprint(e,t,n);if(this.shouldDedupe(u,c))return}const a=s?.id??this.idFactory();let r;const f=new Promise(c=>{r=c});return this.reduceQueue.push({channel:e,type:t,payload:n,id:a,meta:s?.meta,resolve:r}),this.drainReduce(),f}drainReduce(){if(!this.isReducing){this.isReducing=!0;try{for(;this.reduceQueue.length>0;){const{channel:e,type:t,payload:n,id:s,meta:i,resolve:d}=this.reduceQueue.shift(),a={channel:e,type:t,payload:n,id:s,...i!==void 0?{meta:i}:{}},r=this.instrumentObservers.size>0,f=r?this.state:void 0,c=r?[]:void 0;c!==void 0&&(this.changedPathSink=c);const u=r?I():0;let l=!1;try{l=this.applyEventSync(a)}catch(y){console.error("Emit reduce error:",y)}finally{r&&(this.changedPathSink=null)}r&&this.emitInstrumentation(a,l,c??[],f,I()-u),this.runEventEffects(a,l,d)}}finally{this.isReducing=!1}}}applyEventSync(e){for(const s of this.middleware){const i=this.getMiddlewareWhen(s);if(!this.matchesWhen(i,e))continue;const d=this.getMiddlewareFunction(s);let a;try{a=d(this.state,e,this.emit),process.env.NODE_ENV!=="production"&&typeof a?.then=="function"&&console.error(`[yoltra] Middleware for "${e.channel}/${e.type}" returned a Promise. Middleware is synchronous: a Promise is truthy, so this event was allowed without waiting and a "return false" inside it can never veto. Do the check synchronously, and put anything that must await in an effect.`)}catch(r){console.error("Middleware error:",r),a=!1}if(!a)return this.notifyEventSubscribers(e,"uncommitted"),!1}const t=this.state;this.reducerBus.emit(e.channel,e.type,e.payload,e);for(const[s,i]of this.patternReducers)this.matchesWhen(i,e)&&this.forwardEventGuarded(s,e);const n=t!==this.state;return this.notifyEventSubscribers(e,"committed"),n&&this.listeners.forEach(s=>s()),!0}async runEventEffects(e,t,n){this.inFlightEffects++;try{t&&await this.notifyEffects(e)}catch(s){console.error("Effect error:",s)}finally{this.inFlightEffects--,n()}}instrument(e){return this.instrumentObservers.add(e),()=>{this.instrumentObservers.delete(e)}}emitInstrumentation(e,t,n,s,i){const d={},a={};for(const f of n)d[f]=this.getAtPath(s,f),a[f]=this.getAtPath(this.state,f);const r={event:{id:e.id,channel:e.channel,type:e.type,payload:e.payload,...e.meta!==void 0?{meta:e.meta}:{}},committed:t,changedPaths:n,prevValues:d,nextValues:a,reduceTimeMs:i};for(const f of[...this.instrumentObservers])try{f(r)}catch(c){console.error("Instrumentation observer error:",c)}}connect(e,t){return this.connectorBus.on(e.reducer,e.property,t)}onEvent(e,t,n,s="committed"){const i=`${e}::${String(t)}`,d=s==="committed"?this.committedEventSubscribers:s==="uncommitted"?this.uncommittedEventSubscribers:this.allEventSubscribers;return d.has(i)||d.set(i,new Set),d.get(i).add(n),()=>{const a=d.get(i);a&&(a.delete(n),a.size===0&&d.delete(i))}}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}getState(){return this.state}registerMiddleware(e){return this.middleware.push(e),()=>{const t=this.middleware.indexOf(e);t!==-1&&this.middleware.splice(t,1)}}registerReducer(e,t){if(Object.prototype.hasOwnProperty.call(this.reducers,e))throw new Error(`Reducer ${e} already exists`);return this.mountSlice(e,t,{preserveState:!1}),this.listeners.forEach(n=>n()),()=>{this.unmountSlice(e,{deleteState:!0}),this.listeners.forEach(n=>n())}}registerEffect(e){const{effect:t,meta:n,when:s}=e,i=[];if(n&&this.effectMeta.set(t,n),s&&("any"in s&&s.any===!0||"channel"in s||"channels"in s)){const r={effect:t,when:s};return this.patternEffects.add(r),()=>{this.patternEffects.delete(r)}}const a=this.normalizeEventKeys(e);if(a.length===0&&!s){const r={effect:t,when:{any:!0}};return this.patternEffects.add(r),()=>{this.patternEffects.delete(r)}}for(const[r,f]of a){const c=`${String(r)}::${String(f)}`;this.effects.has(c)||this.effects.set(c,new Set),this.effects.get(c).add(t),i.push(()=>{const u=this.effects.get(c);u&&(u.delete(t),u.size===0&&this.effects.delete(c))})}return()=>{for(const r of i)r()}}onEffect(e,t,n){const s=async(i,d,a)=>{if(i.channel!==e||i.type!==t)return;const r=i;return n(r.payload,d,a,r)};return this.registerEffect({when:{keys:[[e,t]]},effect:s})}replaceMiddleware(e){this.middleware.length=0;for(const t of e)this.middleware.push(t)}replaceEffects(e){this.effects.clear(),this.patternEffects.clear();for(const t of e)this.registerEffect(t)}replaceReducers(e,t={}){const n=t.preserveState!==!1,s=new Set(Object.keys(this.reducers)),i=Object.entries(e),d=new Set(i.map(([a])=>a));for(const a of s)d.has(a)||this.unmountSlice(a,{deleteState:!0});for(const[a,r]of i)s.has(a)?(this.unmountSlice(a,{deleteState:!1}),this.mountSlice(a,r,{preserveState:n})):this.mountSlice(a,r,{preserveState:!1})}hotReplace(e){e.middleware&&this.replaceMiddleware(e.middleware),e.effects&&this.replaceEffects(e.effects),e.reducer&&this.replaceReducers(e.reducer,{preserveState:e.preserveState})}mountSlice(e,t,n){const s=e,{reducer:i,state:d,when:a}=t;if(this.reducers[e]=new D(i),(!n.preserveState||this.state[s]===void 0)&&(this.state={...this.state,[s]:P(j(s,d))}),a&&("any"in a&&a.any===!0||"channel"in a||"channels"in a)){this.patternReducers.set(e,a),this.sliceUnsubs.set(s,[]);return}const f=this.normalizeEventKeys(t);if(f.length===0&&!a){this.patternReducers.set(e,{any:!0}),this.sliceUnsubs.set(s,[]);return}const c=[];for(const[u,l]of f){const y=this.reducerBus.on(u,l,(h,m)=>{const g=m??{channel:u,type:l,payload:h,id:this.idFactory()};this.forwardEventGuarded(e,g)});c.push(y)}this.sliceUnsubs.set(s,c)}unmountSlice(e,t){const n=e;this.patternReducers.delete(e);const s=this.sliceUnsubs.get(n);if(s){for(const i of s)try{i()}catch(d){console.error(`[Store error]: ${d}`)}this.sliceUnsubs.delete(n)}if(delete this.reducers[e],t.deleteState){const{[n]:i,...d}=this.state;this.state=d}}normalizeEventKeys(e){if(e.when){const t=e.when;if("keys"in t)return t.keys}return[]}getAtPath(e,t){if(!t)return e;const s=(t[0]==="."?t.slice(1):t).split(".");let i=e;for(const d of s){if(i==null)return;i=i[d]}return i}static buildAncestorPaths(e){if(!e)return[];const n=(e[0]==="."?e.slice(1):e).split("."),s=[];for(let i=0;i<n.length;i++)s.push(n.slice(0,i+1).join("."));return s}}function F(o){return new S({name:o.name,reducer:o.reducer??{},middleware:o.middleware??[],effects:o.effects??[],dedupWindowMs:o.dedupWindowMs,idFactory:o.idFactory,devtools:o.devtools,onEffectError:o.onEffectError,onReducerError:o.onReducerError})}const V=o=>(e,t)=>t.map(n=>[e,n]),U=()=>o=>o,R=new Set;function L(o){const e=String(o);R.has(e)||(R.add(e),console.warn(`[yoltra] Entity id "${e}" contains a dot. Paths are dotted, so a subscription to "entities.${e}" is indistinguishable from one to a nested object of the same name. Use ids without dots.`))}function G(o,e){if(o.length!==e.length)return e;for(let t=0;t<o.length;t++)if(o[t]!==e[t])return e;return o}function Q(o={}){const e=o.selectId??(r=>r.id),{sortComparer:t}=o,n=(r,f)=>{if(t===void 0)return f;const c=[...f].sort((u,l)=>{const y=r.entities[u],h=r.entities[l];return y===void 0||h===void 0?0:t(y,h)});return G(f,c)},s=(r,f,c)=>{const u={...r,entities:f,ids:c};return{...u,ids:n(u,c)}},i=(r,f,c)=>{let u=null,l=null;for(const y of f){const h=e(y);process.env.NODE_ENV!=="production"&&String(h).includes(".")&&L(h);const m=(u??r.entities)[h];if(m!==void 0&&c==="add")continue;const g=m!==void 0&&c==="upsert"?{...m,...y}:y;u??(u={...r.entities}),u[h]=g,m===void 0&&(l??(l=[...r.ids]),l.push(h))}return u===null?r:s(r,u,l??r.ids)},d=(r,f)=>{let c=null;for(const{id:u,changes:l}of f){const y=(c??r.entities)[u];y!==void 0&&(c??(c={...r.entities}),c[u]={...y,...l})}return c===null?r:s(r,c,r.ids)},a=(r,f)=>{const c=new Set(f.filter(l=>r.entities[l]!==void 0));if(c.size===0)return r;const u={...r.entities};for(const l of c)delete u[l];return s(r,u,r.ids.filter(l=>!c.has(l)))};return{getInitialState(r){const f={ids:[],entities:{}};return r===void 0?f:{...f,...r}},addOne:(r,f)=>i(r,[f],"add"),addMany:(r,f)=>i(r,f,"add"),setOne:(r,f)=>i(r,[f],"set"),setMany:(r,f)=>i(r,f,"set"),setAll:(r,f)=>{const c={},u=[];for(const l of f){const y=e(l);c[y]===void 0&&u.push(y),c[y]=l}return s(r,c,u)},updateOne:(r,f)=>d(r,[f]),updateMany:(r,f)=>d(r,f),upsertOne:(r,f)=>i(r,[f],"upsert"),upsertMany:(r,f)=>i(r,f,"upsert"),removeOne:(r,f)=>a(r,[f]),removeMany:(r,f)=>a(r,f),removeAll:r=>r.ids.length===0?r:s(r,{},[]),selectIds:r=>r.ids,selectEntities:r=>r.entities,selectAll:r=>r.ids.map(f=>r.entities[f]),selectById:(r,f)=>r.entities[f],selectTotal:r=>r.ids.length,idsPath:"ids",pathTo:(r,f)=>f===void 0?`entities.${r}`:`entities.${r}.${f}`,anyField:r=>`entities.*.${r}`}}const E="$yoltra";function O(o,e={}){const t=e.maxNodes??1e5,n=e.sanitize,s=[],i=new Map;let d=0,a=!1;function r(c,u){if(n!==void 0&&(c=n(u,c)),d+=1,d>t)return a=!0,{[E]:"unsupported",kind:"truncated"};switch(typeof c){case"undefined":return{[E]:"undefined"};case"bigint":return{[E]:"bigint",value:c.toString()};case"number":return Number.isNaN(c)?{[E]:"nan"}:c===1/0?{[E]:"infinity",sign:1}:c===-1/0?{[E]:"infinity",sign:-1}:c;case"function":case"symbol":return s.push(u),{[E]:"unsupported",kind:typeof c};case"string":case"boolean":return c}if(c===null)return null;const l=c,y=i.get(l);if(y!==void 0)return{[E]:"ref",path:y};if(i.set(l,u),c instanceof Date)return{[E]:"date",iso:c.toISOString()};if(c instanceof RegExp)return{[E]:"regexp",source:c.source,flags:c.flags};if(c instanceof Error)return{[E]:"error",name:c.name,message:c.message};if(c instanceof Map){const m=[];let g=0;for(const[w,H]of c)m.push([r(w,`${u}/@k${g}`),r(H,`${u}/${g}`)]),g+=1;return{[E]:"map",entries:m}}if(c instanceof Set){const m=[];let g=0;for(const w of c)m.push(r(w,`${u}/${g}`)),g+=1;return{[E]:"set",values:m}}if(Array.isArray(c))return c.map((m,g)=>r(m,`${u}/${g}`));const h={};for(const[m,g]of Object.entries(c))h[m]=r(g,`${u}/${C(m)}`);return E in h?{[E]:"escaped",value:h}:h}return{value:r(o,""),report:{truncated:a,unsupported:s}}}function T(o){const e=new Map,t=[];function n(d,a){if(d===null||typeof d!="object")return d;if(Array.isArray(d)){const f=[];return e.set(a,f),d.forEach((c,u)=>{if(A(c)){t.push({target:f,key:u,path:c.path}),f[u]=void 0;return}f[u]=n(c,`${a}/${u}`)}),f}if(typeof d[E]=="string"){const f=d;switch(f[E]){case"undefined":return;case"nan":return Number.NaN;case"infinity":return f.sign===1?1/0:-1/0;case"bigint":return BigInt(f.value);case"date":return new Date(f.iso);case"regexp":return new RegExp(f.source,f.flags);case"error":{const c=new Error(f.message);return c.name=f.name,c}case"unsupported":return;case"ref":return;case"map":{const c=new Map;return e.set(a,c),f.entries.forEach(([u,l],y)=>{c.set(n(u,`${a}/@k${y}`),n(l,`${a}/${y}`))}),c}case"set":{const c=new Set;return e.set(a,c),f.values.forEach((u,l)=>c.add(n(u,`${a}/${l}`))),c}case"escaped":return s(f.value,a);default:return}}return s(d,a)}function s(d,a){const r={};e.set(a,r);for(const[f,c]of Object.entries(d)){const u=`${a}/${C(f)}`;if(A(c)){t.push({target:r,key:f,path:c.path}),r[f]=void 0;continue}r[f]=n(c,u)}return r}const i=n(o,"");e.set("",i);for(const{target:d,key:a,path:r}of t)d[a]=e.get(r);return i}function A(o){return o!==null&&typeof o=="object"&&o[E]==="ref"&&typeof o.path=="string"}function C(o){return o.replace(/~/g,"~0").replace(/\//g,"~1")}function J(o,e,t={}){let n=t.maxNodes??1e5;for(let s=0;s<8;s+=1){const{value:i,report:d}=O(o,{...t,maxNodes:n});let a;try{a=JSON.stringify(i)?.length??0}catch{a=Number.POSITIVE_INFINITY}if(a<=e)return d.truncated?{value:i,truncated:!0,note:`State was too large to send in full; parts beyond ${n} nodes are omitted.`}:{value:i,truncated:!1};const r=Math.floor(n*e*.8/a);if(n=Math.max(1,Math.min(r,n-1)),n<=1&&s>0)break}return{value:{[E]:"unsupported",kind:"truncated"},truncated:!0,note:`State exceeds the ${e}-byte transport limit and could not be reduced to fit.`}}function b(o,e,t){o.onError?.(e,t)}async function Y(o){const e={slices:{},restored:!1};let t;try{t=o.source??await o.adapter.read(o.key)}catch(s){return b(o,s,"read"),e}if(t==null||t==="")return e;let n;try{n=T(JSON.parse(t))}catch(s){return b(o,s,"decode"),e}if(n===null||typeof n!="object"||typeof n.version!="number")return b(o,new Error("persisted payload is not a recognisable envelope"),"decode"),e;if(n.version!==o.version){if(o.migrate===void 0)return b(o,new Error(`persisted state is version ${n.version}, this build expects ${o.version}, and no migrate was supplied`),"migrate"),e;try{const s=o.migrate(n.slices,n.version);return s===null?e:{slices:s,restored:!0}}catch(s){return b(o,s,"migrate"),e}}return{slices:n.slices??{},restored:!0}}function q(o,e){if(!e.restored)return o;const t={};for(const[n,s]of Object.entries(o)){const i=e.slices[n];t[n]=i===void 0?s:{...s,state:i}}return t}function B(o,e){const t=o??{},n=e.slices===void 0?t:Object.fromEntries(e.slices.filter(s=>s in t).map(s=>[s,t[s]]));return JSON.stringify(O({version:e.version,slices:n}).value)}function X(o,e){const t=e.throttleMs??250,n=e.slices;let s=null,i=!1;const d=()=>{if(i){i=!1;try{const f=e.adapter.write(e.key,B(o.getState(),e));f instanceof Promise&&f.catch(c=>b(e,c,"write"))}catch(f){b(e,f,"write")}}},a=()=>{if(i=!0,t<=0){d();return}s===null&&(s=setTimeout(()=>{s=null,d()},t),s.unref?.())},r=o.instrument(f=>{if(n===void 0){a();return}(f.changedPaths??[]).some(u=>n.some(l=>u===l||u.startsWith(`${l}.`)))&&a()});return()=>{r(),s!==null&&(clearTimeout(s),s=null),d()}}function Z(o,e){return B(o.getState(),e)}function ee(o){return{read:e=>o.getItem(e),write:(e,t)=>o.setItem(e,t),remove:e=>o.removeItem(e)}}function te(o){const e=new Map(Object.entries(o??{}));return{read:t=>e.get(t)??null,write:(t,n)=>{e.set(t,n)},remove:t=>{e.delete(t)}}}exports.EventBus=_;exports.LooseEventBus=N;exports.Reducer=D;exports.Store=S;exports.createEntityAdapter=Q;exports.createMemoryAdapter=te;exports.createStore=F;exports.createWebStorageAdapter=ee;exports.decodeState=T;exports.dehydrate=Z;exports.detectChangedProps=k;exports.encodeState=O;exports.encodeStateBounded=J;exports.eventKeys=U;exports.freezeState=v;exports.hydrate=Y;exports.persist=X;exports.typedEvents=V;exports.withHydration=q;
4
+ * License: MIT */
5
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class H{handlers=new Map;on(e,t,s){let n=this.handlers.get(e);n||(n=new Map,this.handlers.set(e,n));let r=n.get(t);return r||(r=new Set,n.set(t,r)),r.add(s),()=>this.off(e,t,s)}off(e,t,s){const n=this.handlers.get(e);if(!n)return;const r=n.get(t);r&&(r.delete(s),r.size===0&&n.delete(t),n.size===0&&this.handlers.delete(e))}emit(e,t,s,n){const r=this.handlers.get(e);if(!r)return;const c=r.get(t);if(!(!c||c.size===0))for(const d of[...c])try{d(s,n)}catch(i){console.error("EventBus handler error:",i)}}clear(){this.handlers.clear()}}class K{handlers=new Map;patternHandlers=new Map;patternIndex=new Map;on(e,t,s){const n=String(t);if(this.isPattern(n)){const r=n;this.patternHandlers.has(e)||this.patternHandlers.set(e,new Map);const c=this.patternHandlers.get(e);return c.has(r)||(c.set(r,[]),this.indexPattern(e,r)),c.get(r).push(s),()=>this.offPattern(e,r,s)}else{const r=this.normalizeTypeKey(n);this.handlers.has(e)||this.handlers.set(e,new Map);const c=this.handlers.get(e);return c.has(r)||c.set(r,[]),c.get(r).push(s),()=>this.offExactNormalized(e,r,s)}}off(e,t,s){const n=this.normalizeTypeKey(String(t));this.offExactNormalized(e,n,s)}offExactNormalized(e,t,s){const n=this.handlers.get(e);if(!n)return;const r=n.get(t);if(!r)return;const c=r.indexOf(s);c!==-1&&r.splice(c,1),r.length===0&&n.delete(t),n.size===0&&this.handlers.delete(e)}offPattern(e,t,s){const n=this.patternHandlers.get(e);if(!n)return;const r=n.get(t);if(!r)return;const c=r.indexOf(s);c!==-1&&r.splice(c,1),r.length===0&&(n.delete(t),this.unindexPattern(e,t)),n.size===0&&(this.patternHandlers.delete(e),this.patternIndex.delete(e))}emit(e,t,s){const n=String(t),r=this.normalizeTypeKey(n),c=this.handlers.get(e)?.get(r)??[],d=this.matchingPatternHandlers(e,n),i=new Set,a=l=>{for(const u of[...l])if(!i.has(u)){i.add(u);try{u(s)}catch(h){console.error(h);continue}}};a(c);for(const l of d)a(l)}emitWith(e,t,s){const n=String(t),r=this.normalizeTypeKey(n),c=this.handlers.get(e)?.get(r)??[],d=this.matchingPatternHandlers(e,n);if(c.length===0&&d.length===0)return;const i=s(),a=new Set,l=u=>{for(const h of[...u])if(!a.has(h)){a.add(h);try{h(i)}catch(p){console.error(p);continue}}};l(c);for(const u of d)l(u)}isPattern(e){return e.includes("*")}normalizeTypeKey(e){return e.replace(/^\./,"")}splitPath(e){return this.normalizeTypeKey(e).split(".").filter(Boolean)}indexPattern(e,t){let s=this.patternIndex.get(e);s===void 0&&(s={byHead:new Map,anyHead:[]},this.patternIndex.set(e,s));const n=this.splitPath(t),r={pattern:t,segments:n},c=n[0];if(c===void 0||c==="*"||c==="**"){s.anyHead.push(r);return}const d=s.byHead.get(c);d===void 0?s.byHead.set(c,[r]):d.push(r)}unindexPattern(e,t){const s=this.patternIndex.get(e);if(s===void 0)return;const n=this.splitPath(t)[0],r=n===void 0||n==="*"||n==="**"?s.anyHead:s.byHead.get(n);if(r===void 0)return;const c=r.findIndex(d=>d.pattern===t);c!==-1&&r.splice(c,1),r.length===0&&r!==s.anyHead&&n!==void 0&&s.byHead.delete(n)}matchingPatternHandlers(e,t){const s=this.patternHandlers.get(e),n=this.patternIndex.get(e);if(s===void 0||s.size===0||n===void 0)return[];const r=this.splitPath(t),c=[],d=a=>{for(const l of a){if(!this.matchSegments(l.segments,r))continue;const u=s.get(l.pattern);u!==void 0&&c.push(u)}},i=r[0];if(i!==void 0){const a=n.byHead.get(i);a!==void 0&&d(a)}return d(n.anyHead),c}matchSegments(e,t){let s=0,n=0,r=-1,c=0;for(;n<t.length;)if(s<e.length&&(e[s]==="*"||e[s]===t[n]))s++,n++;else if(s<e.length&&e[s]==="**")r=s,c=n,s++;else if(r!==-1)s=r+1,n=++c;else return!1;for(;s<e.length&&e[s]==="**";)s++;return s===e.length}clear(){this.handlers.clear(),this.patternHandlers.clear(),this.patternIndex.clear()}__introspect(){const e=[];for(const[t,s]of this.handlers)for(const[n,r]of s)r.length>0&&e.push({channel:t,type:n,count:r.length});for(const[t,s]of this.patternHandlers)for(const[n,r]of s)r.length>0&&e.push({channel:t,type:n,count:r.length});return e}}class W{_reduce;constructor(e){this._reduce=e}reduce(e,t){return this._reduce(e,t)}}const D=new Set;function x(o,e){const t=o?`${o}.${e}`:e;D.has(t)||(D.add(t),console.warn(`[yoltra] State key "${e}"${o?` under "${o}"`:""} contains a dot. Paths are dotted, so this key is indistinguishable from nested objects of the same name: a subscription to "${t}" may match the wrong value, and DevTools patches for it will address the wrong node. Rename the key, or nest it.`))}function T(o,e,t="",s=new Map){const n=[];return R(o,e,t,s,n),n}function R(o,e,t,s,n){if(o===e)return;if(typeof o!="object"||typeof e!="object"||o===null||e===null){if(typeof o=="number"&&Number.isNaN(o)&&Number.isNaN(e))return;n.push(t);return}if(o instanceof Date&&e instanceof Date){o.getTime()!==e.getTime()&&n.push(t);return}if(o instanceof RegExp&&e instanceof RegExp){(o.source!==e.source||e.flags!==o.flags)&&n.push(t);return}if(o instanceof Map||e instanceof Map){n.push(t);return}if(o instanceof Set||e instanceof Set){n.push(t);return}const r=o,c=e,d=s.get(r);if(d?.has(c))return;const i=d??new Set;i.add(c),d||s.set(r,i);try{const a=Array.isArray(o),l=Array.isArray(e);if(a!==l){n.push(t);return}if(a){const f=o,m=e;f.length!==m.length&&t&&n.push(t);const y=Math.min(f.length,m.length);for(let g=0;g<y;g++)f[g]!==m[g]&&R(f[g],m[g],t?`${t}.${g}`:`${g}`,s,n);for(let g=y;g<Math.max(f.length,m.length);g++)n.push(t?`${t}.${g}`:`${g}`);return}const u=Object.keys(o),h=Object.keys(e);if(u.length===0&&h.length===0){n.push(t);return}let p=u.length===h.length;if(p){for(let f=0;f<h.length;f++)if(!Object.prototype.hasOwnProperty.call(o,h[f])){p=!1;break}}if(p){for(const f of h)o[f]!==e[f]&&(process.env.NODE_ENV!=="production"&&f.includes(".")&&x(t,f),R(o[f],e[f],t?`${t}.${f}`:f,s,n));return}for(const f of h){const m=Object.prototype.hasOwnProperty.call(o,f);if(m&&o[f]===e[f])continue;process.env.NODE_ENV!=="production"&&f.includes(".")&&x(t,f);const y=t?`${t}.${f}`:f;if(!m){n.push(y);continue}R(o[f],e[f],y,s,n)}for(const f of u)Object.prototype.hasOwnProperty.call(e,f)||(process.env.NODE_ENV!=="production"&&f.includes(".")&&x(t,f),n.push(t?`${t}.${f}`:f))}finally{i.delete(c),i.size===0&&s.delete(r)}}function $(o,e=new WeakSet,t){if(o===null||typeof o!="object"||e.has(o)||(t!==void 0&&o===t.watch&&t.onFound(),Object.isFrozen(o)))return o;if(e.add(o),Array.isArray(o)){const s=o;for(let n=0;n<s.length;n++)s[n]=$(s[n],e,t);return Object.freeze(s)}for(const s of Object.getOwnPropertyNames(o)){const n=Object.getOwnPropertyDescriptor(o,s);!n||!("value"in n)||(o[s]=$(o[s],e,t))}for(const s of Object.getOwnPropertySymbols(o)){const n=Object.getOwnPropertyDescriptor(o,s);!n||!("value"in n)||(o[s]=$(o[s],e,t))}return Object.freeze(o)}const F=Symbol.for("yoltra.rejected");function J(o){return{[F]:!0,reason:o}}function U(o){return typeof o=="object"&&o!==null&&o[F]===!0}class L extends Error{channel;type;idleMs;constructor(e,t,s){super(`[yoltra] call to "${e}/${t}" saw no correlated reply for ${s}ms. The timeout is idle rather than total, so this means the responder went quiet, not that it was slow. Check that something handles "${e}/${t}" and that its reply is emitted through the \`emit\` it was handed — a reply emitted from an unrelated context carries no causal link, and needs an explicit correlationId instead.`),this.name="CallTimeoutError",this.channel=e,this.type=t,this.idleMs=s}}class I extends Error{constructor(e){super(`[yoltra] call aborted: ${e}`),this.name="CallAbortedError"}}function q(o){const[e,t]=o;if(t===void 0)return{channel:e,isTerminal:()=>!0};if(typeof t=="string")return{channel:e,isTerminal:n=>n===t};const s=new Set(t);return{channel:e,isTerminal:n=>s.has(n)}}function Y(o,e,t){return o.parentId===e?!0:t===void 0?!1:o.meta?.correlationId===t}class X{constructor(e){this.highWaterMark=e}highWaterMark;buffer=[];takers=[];putters=[];consuming=!1;ended=!1;closed=!1;dropped=0;get droppedCount(){return this.dropped}beginConsuming(){this.consuming=!0}put(e){if(this.closed||this.ended)return Promise.resolve();const t=this.takers.shift();return t!==void 0?(t({value:e,done:!1}),Promise.resolve()):this.buffer.length<this.highWaterMark?(this.buffer.push(e),Promise.resolve()):this.consuming?new Promise(s=>{this.putters.push({item:e,release:s})}):(this.dropped++,Promise.resolve())}take(){this.consuming=!0;const e=this.buffer.shift();if(e!==void 0){const s=this.putters.shift();return s!==void 0&&(this.buffer.push(s.item),s.release()),Promise.resolve({value:e,done:!1})}const t=this.putters.shift();return t!==void 0?(t.release(),Promise.resolve({value:t.item,done:!1})):this.closed||this.ended?Promise.resolve({value:void 0,done:!0}):new Promise(s=>{this.takers.push(s)})}end(){if(this.ended||this.closed)return;this.ended=!0;let e=this.putters.shift();for(;e!==void 0;)this.buffer.push(e.item),e.release(),e=this.putters.shift();let t=this.takers.shift();for(;t!==void 0;){const s=this.buffer.shift();t(s!==void 0?{value:s,done:!1}:{value:void 0,done:!0}),t=this.takers.shift()}}close(){if(this.closed)return;this.closed=!0,this.buffer.length=0;let e=this.takers.shift();for(;e!==void 0;)e({value:void 0,done:!0}),e=this.takers.shift();let t=this.putters.shift();for(;t!==void 0;)t.release(),t=this.putters.shift()}}const Z=3e4,ee=16;function te(o,e,t,s,n){const{channel:r,isTerminal:c}=q(n.reply),d=n.timeoutMs??Z,i=new X(n.highWaterMark??ee),a=o.idFactory();let l,u,h=!1;const p=new Promise((b,v)=>{l=b,u=v});p.catch(()=>{});let f=null,m=null;const y=(b,v=!1)=>{h||(h=!0,f!==null&&clearTimeout(f),f=null,m?.(),m=null,v?i.end():i.close(),n.signal?.removeEventListener("abort",g),b())};function g(){y(()=>u(new I(String(n.signal?.reason??"signal aborted"))))}const P=()=>{f!==null&&clearTimeout(f),f=setTimeout(()=>{y(()=>u(new L(e,t,d)))},d),f.unref?.()};return m=o.registerEffect({when:{channel:r},effect:async b=>{if(!h&&Y(b,a,n.correlationId)){if(P(),c(String(b.type))){y(()=>l(b),!0);return}await i.put(b)}}}),n.signal!==void 0&&(n.signal.aborted?g():n.signal.addEventListener("abort",g,{once:!0})),P(),o.emit(e,t,s,{id:a,...n.correlationId!==void 0?{meta:{correlationId:n.correlationId}}:{}}),{then:(b,v)=>p.then(b,v),catch:b=>p.catch(b),finally:b=>p.finally(b),get dropped(){return i.droppedCount},cancel:(b="cancelled")=>{y(()=>u(new I(b)))},[Symbol.asyncIterator]:()=>(i.beginConsuming(),{next:()=>i.take(),return:async()=>(i.close(),{value:void 0,done:!0})})}}function ne(o,e){if(!e)return o;const s=(e[0]==="."?e.slice(1):e).split(".");let n=o;for(const r of s){if(n==null)return;n=n[r]}return n}function se(o){if(!o)return[];const t=(o[0]==="."?o.slice(1):o).split("."),s=[];for(let n=0;n<t.length;n++)s.push(t.slice(0,n+1).join("."));return s}function M(o,e){return!o||"any"in o&&o.any===!0?!0:"keys"in o?o.keys.some(([t,s])=>e.channel===t&&e.type===s):"channel"in o?e.channel===o.channel:"channels"in o?o.channels.includes(e.channel):!1}function re(o){return typeof o=="function"?o:o.middleware}function ie(o){if(typeof o!="function")return o.when}function A(o){if(o.when){const e=o.when;if("keys"in e)return e.keys}return[]}function oe(o,e){try{return structuredClone(e)}catch(t){throw new Error(`[yoltra] Initial state for slice "${String(o)}" could not be copied: ${t instanceof Error?t.message:String(t)}. State must be structured-cloneable — functions, class instances and DOM nodes are not. Keep behaviour out of state and store plain data.`)}}function O(o,e){return process.env.NODE_ENV==="production"?o:$(o,new WeakSet,e)}const j=100,ce=64,z=16,S=Object.freeze({committed:!1,written:!1}),ae=Object.freeze({committed:!0,written:!1}),de=Object.freeze({committed:!0,written:!0}),_=()=>typeof performance<"u"&&typeof performance.now=="function"?performance.now():Date.now();class k{name;middleware;reducers;state;reducerBus;connectorBus;listeners=new Set;effects=new Map;patternEffects=new Set;committedEventSubscribers=new Map;uncommittedEventSubscribers=new Map;writtenEventSubscribers=new Map;allEventSubscribers=new Map;sliceUnsubs=new Map;patternReducers=new Map;replayEnabled;idFactory;onEffectError;onReducerError;warnedPayloadAliases=new Set;reduceQueue=[];isReducing=!1;currentEvent=null;transitionsThisDrain=0;maxReduceDepth;maxTransitionsPerDrain;onCascade;onRejected;instrumentObservers=new Set;changedPathSink=null;stagingSink=null;stagedRejection=null;stagedRejectedBy="";inFlightEffects=0;processedEvents=new Map;dedupCount=0;effectMeta=new WeakMap;dedupConfig;eventCleanupTimer=null;constructor(e){if(this.name=e.name??"yoltra Store",this.reducerBus=new H,this.connectorBus=new K,this.middleware=[...e.middleware??[]],this.reducers={},this.state={},this.replayEnabled=e.devtools?.allowReplay??!1,this.idFactory=e.idFactory??(()=>crypto.randomUUID()),this.onEffectError=e.onEffectError,this.onReducerError=e.onReducerError,this.maxReduceDepth=e.maxReduceDepth??ce,this.maxTransitionsPerDrain=e.maxTransitionsPerDrain??1/0,this.onCascade=e.onCascade,this.onRejected=e.onRejected,this.dedupConfig={windowMs:e.dedupWindowMs??0,maxCacheSize:1e3},Object.entries(e.reducer).forEach(([t,s])=>{this.mountSlice(t,s,{preserveState:!1})}),e.effects?.length)for(const t of e.effects)this.registerEffect(t);this.dispose=this.dispose.bind(this),this.notifyEffects=this.notifyEffects.bind(this),this.__applyExternalState=this.__applyExternalState.bind(this),this.__replayEvents=this.__replayEvents.bind(this),this.__devtoolsIntrospect=this.__devtoolsIntrospect.bind(this),this.mountSlice=this.mountSlice.bind(this),this.unmountSlice=this.unmountSlice.bind(this),this.getAtPath=this.getAtPath.bind(this),this.emit=this.emit.bind(this),this.subscribe=this.subscribe.bind(this),this.connect=this.connect.bind(this),this.onEffect=this.onEffect.bind(this),this.onEvent=this.onEvent.bind(this),this.getState=this.getState.bind(this),this.registerEffect=this.registerEffect.bind(this),this.registerMiddleware=this.registerMiddleware.bind(this),this.registerReducer=this.registerReducer.bind(this),this.replaceMiddleware=this.replaceMiddleware.bind(this),this.replaceEffects=this.replaceEffects.bind(this),this.replaceReducers=this.replaceReducers.bind(this),this.hotReplace=this.hotReplace.bind(this)}dispose(){this.eventCleanupTimer&&(clearInterval(this.eventCleanupTimer),this.eventCleanupTimer=null),this.processedEvents.clear(),this.effects.clear(),this.patternEffects.clear(),this.effectMeta=new WeakMap,this.warnedPayloadAliases.clear(),this.listeners.clear(),this.committedEventSubscribers.clear(),this.uncommittedEventSubscribers.clear(),this.writtenEventSubscribers.clear(),this.allEventSubscribers.clear(),this.instrumentObservers.clear(),this.connectorBus.clear(),this.reducerBus.clear(),this.patternReducers.clear(),this.sliceUnsubs.clear(),this.changedPathSink=null}fingerprint(e,t,s){const n=`${e}::${t}`;try{if(s==null)return`${n}::null`;if(typeof s!="object")return`${n}::${String(s)}`;const r=JSON.stringify(s);return`${n}::${r}`}catch{return`${n}::${Date.now()}::${Math.random()}`}}shouldDedupe(e,t){const s=Date.now(),n=this.processedEvents.get(e);return n!==void 0&&s-n<t?(this.dedupCount++,!0):(this.processedEvents.set(e,s),this.ensureCleanupTimer(),this.processedEvents.size>this.dedupConfig.maxCacheSize&&this.pruneProcessedEvents(s),!1)}ensureCleanupTimer(){this.eventCleanupTimer===null&&(this.eventCleanupTimer=setInterval(()=>{this.pruneProcessedEvents(Date.now())},5e3),this.eventCleanupTimer.unref?.())}pruneProcessedEvents(e){const t=Math.max(this.dedupConfig.windowMs,j),s=e-t*2;for(const[n,r]of this.processedEvents)r<s&&this.processedEvents.delete(n);this.processedEvents.size===0&&this.eventCleanupTimer!==null&&(clearInterval(this.eventCleanupTimer),this.eventCleanupTimer=null)}reportCascade(e,t,s,n,r){console.error(`[yoltra] Cascade stopped: "${s.channel}/${s.type}" would exceed ${e} (${t}). This event was refused and the chain ends here. A chain this long is almost always two consumers emitting into each other — check what reacts to "${s.channel}/${s.type}" and what that emits in turn.`+(r.length>0?` Recent causal chain: ${r.join(" → ")} → (refused).`:""));try{this.onCascade?.({limit:e,limitValue:t,event:s,depth:n,chain:r})}catch(c){console.error("onCascade handler error:",c)}}async notifyEffects(e){const t=this.scopedEmit(e),s=`${String(e.channel)}::${String(e.type)}`,n=this.effects.get(s);if(n&&n.size>0)for(const r of[...n])try{await r(e,this.getState,t)}catch(c){console.error("Effect error:",c),this.onEffectError?.(c,e)}for(const{effect:r,when:c}of this.patternEffects)if(M(c,e))try{await r(e,this.getState,t)}catch(d){console.error("Effect error:",d),this.onEffectError?.(d,e)}}scopedEmit(e){const t={id:e.id,depth:e.depth??0,chain:[...this.currentEvent?.chain??[],e.id].slice(-z)};return((s,n,r,c)=>this.emitCaused(t,s,n,r,c))}notifyEventSubscribers(e,t){const s=`${String(e.channel)}::${String(e.type)}`,r=(t==="committed"?this.committedEventSubscribers:t==="written"?this.writtenEventSubscribers:this.uncommittedEventSubscribers).get(s);if(r?.size)for(const d of[...r])this.invokeEventSubscriber(d,e,t);if(t==="written")return;const c=this.allEventSubscribers.get(s);if(c?.size)for(const d of[...c])this.invokeEventSubscriber(d,e,t)}invokeEventSubscriber(e,t,s){try{const n=e(t,this.getState,this.emit,s);n&&typeof n.then=="function"&&n.catch(r=>console.error("Event subscription error:",r))}catch(n){console.error("Event subscription error:",n)}}stageSliceGuarded(e,t,s){try{return this.stageSlice(e,t,s)}catch(n){return console.error(`Reducer error in slice "${e}":`,n),this.onReducerError?.(n,t,e),null}}stageSlice(e,t,s){const n=this.state[e],r=this.reducers[e].reduce(n,t);if(U(r))return r;if(n===r)return null;const c=T(n,r);if(c.length===0)return null;const d=t.payload,i=process.env.NODE_ENV!=="production"&&d!==null&&typeof d=="object"?{watch:d,onFound:()=>{const a=`${e}:${t.channel}:${t.type}`;this.warnedPayloadAliases.has(a)||(this.warnedPayloadAliases.add(a),console.warn(`[yoltra] Slice "${e}" stored the payload of "${t.channel}/${t.type}" by reference. It is now frozen along with the rest of the state, so the emitter mutating it later will throw in development and silently corrupt state in production. Copy the payload in the reducer instead.`))}}:void 0;return s.push({name:e,prev:n,frozen:O(r,i),leafPaths:c}),null}commitStaged(e,t){if(e.length===0)return!1;const s={...this.state};for(const n of e)s[n.name]=n.frozen;if(this.state=s,this.changedPathSink)for(const n of e)for(const r of n.leafPaths)this.changedPathSink.push(r?`${n.name}.${r}`:n.name);for(const n of e){const r=new Set;for(const c of n.leafPaths){if(c===""){r.add("");continue}for(const d of k.buildAncestorPaths(c))r.add(d)}for(const c of r)this.connectorBus.emitWith(n.name,c,()=>({oldValue:this.getAtPath(n.prev,c),newValue:this.getAtPath(n.frozen,c),path:c,eventId:t.id,channel:t.channel,type:t.type}))}return!0}__devtoolsIntrospect(){const e=Object.keys(this.reducers).map(d=>{const i=this.patternReducers.get(d);return{name:d,when:i}}),t=[];for(const[d,i]of this.effects){if(i.size===0)continue;const[a,l]=d.split("::");for(const u of i){const h=this.effectMeta.get(u);t.push({channel:a,type:l,name:h?.name,description:h?.description})}}for(const d of this.patternEffects){const i=this.effectMeta.get(d.effect);t.push({channel:"*",type:"*",name:i?.name,description:i?.description})}const s=[];for(const d of this.middleware)typeof d=="function"?s.push({name:d.name||void 0}):s.push({name:d.meta?.name,description:d.meta?.description,when:d.when});const n=[];for(const d of this.connectorBus.__introspect())for(let i=0;i<d.count;i++)n.push({reducer:d.channel,property:d.type});const r=[];for(const[d,i]of this.committedEventSubscribers){if(i.size===0)continue;const[a,l]=d.split("::");for(let u=0;u<i.size;u++)r.push({channel:a,type:l,phase:"committed"})}for(const[d,i]of this.uncommittedEventSubscribers){if(i.size===0)continue;const[a,l]=d.split("::");for(let u=0;u<i.size;u++)r.push({channel:a,type:l,phase:"uncommitted"})}for(const[d,i]of this.allEventSubscribers){if(i.size===0)continue;const[a,l]=d.split("::");for(let u=0;u<i.size;u++)r.push({channel:a,type:l,phase:"all"})}const c=this.listeners.size;return{reducers:e,effects:t,middleware:s,atomic:n,event:r,coarse:c,dedupHits:this.dedupCount,queueDepth:this.reduceQueue.length+this.inFlightEffects}}__applyExternalState(e){if(!this.replayEnabled)throw new Error("[yoltra] External state apply (time-travel) is disabled. Enable it with createStore({ devtools: { allowReplay: true } })");const t=this.state,s=e,n={...this.state};let r=!1;Object.keys(this.reducers).forEach(c=>{const d=t?.[c],i=s?.[c];if(i===void 0){process.env.NODE_ENV!=="production"&&console.warn(`[yoltra] External state is missing slice "${String(c)}"; retaining its current value. Time-travel snapshots should contain all slices.`);return}if(d===i)return;const a=O(i);n[c]=a,r=!0;const l=T(d,i);if(l.length===0)return;const u=new Set;for(const h of l){if(h===""){u.add("");continue}for(const p of k.buildAncestorPaths(h))u.add(p)}for(const h of u){const p=this.getAtPath(d,h),f=this.getAtPath(a,h);this.connectorBus.emit(c,h,{oldValue:p,newValue:f,path:h})}}),r&&(this.state=n),r&&this.listeners.forEach(c=>c())}__replayEvents(e,t){if(!this.replayEnabled)throw new Error("[yoltra] Event replay is disabled. Enable it with createStore({ devtools: { allowReplay: true } })");this.__applyExternalState(e);for(const s of t){const n=s,r=[];this.stagingSink=r;let c=null;try{this.reducerBus.emit(n.channel,n.type,n.payload,n),c=this.stagedRejection;for(const[i,a]of this.patternReducers){if(c!==null)break;if(M(a,n)){const l=this.stageSliceGuarded(i,n,r);l!==null&&(c=l)}}}finally{this.stagingSink=null,this.stagedRejection=null,this.stagedRejectedBy=""}const d=c===null&&this.commitStaged(r,n);this.notifyEventSubscribers(n,"committed"),d&&(this.notifyEventSubscribers(n,"written"),this.listeners.forEach(i=>i()))}}async emit(e,t,s,n){return this.emitCaused(null,e,t,s,n)}async emitCaused(e,t,s,n,r){const c=r?.dedupKey,d=this.dedupConfig.windowMs;if(r?.skipDedup!==!0&&(d>0||c!==void 0)){const p=c!==void 0&&d<=0?j:d,f=c!==void 0?`${t}::${s}::#${c}`:this.fingerprint(t,s,n);if(this.shouldDedupe(f,p))return S}const i=r?.id??this.idFactory(),a=this.currentEvent??e,l=a===null?0:a.depth+1;if(a!==null&&l>this.maxReduceDepth)return this.reportCascade("maxReduceDepth",this.maxReduceDepth,{channel:t,type:s,payload:n,id:i,...r?.meta!==void 0?{meta:r.meta}:{},parentId:a.id,depth:l},l,a.chain),S;let u;const h=new Promise(p=>{u=p});return this.reduceQueue.push({channel:t,type:s,payload:n,id:i,meta:r?.meta,resolve:u,...a!==null?{parentId:a.id,depth:l,chain:a.chain}:{}}),this.drainReduce(),h}drainReduce(){if(!this.isReducing){this.isReducing=!0,this.transitionsThisDrain=0;try{for(;this.reduceQueue.length>0;){const e=this.reduceQueue.shift(),{channel:t,type:s,payload:n,id:r,meta:c,resolve:d,parentId:i,depth:a,chain:l}=e,u={channel:t,type:s,payload:n,id:r,...c!==void 0?{meta:c}:{},...i!==void 0?{parentId:i,depth:a}:{}};if(i!==void 0&&++this.transitionsThisDrain>this.maxTransitionsPerDrain){this.reportCascade("maxTransitionsPerDrain",this.maxTransitionsPerDrain,u,a,l),d(S);continue}this.currentEvent={id:r,depth:a??0,chain:[...l??[],r].slice(-z)};const h=this.instrumentObservers.size>0,p=h?this.state:void 0,f=h?[]:void 0;f!==void 0&&(this.changedPathSink=f);const m=h?_():0;let y=S;try{y=this.applyEventSync(u)}catch(g){console.error("Emit reduce error:",g)}finally{h&&(this.changedPathSink=null),this.currentEvent=null}h&&this.emitInstrumentation(u,y,f??[],p,_()-m),this.runEventEffects(u,y,d)}}finally{this.isReducing=!1}}}applyEventSync(e){for(const c of this.middleware){const d=ie(c);if(!M(d,e))continue;const i=re(c);let a;try{a=i(this.state,e,this.emit),process.env.NODE_ENV!=="production"&&typeof a?.then=="function"&&console.error(`[yoltra] Middleware for "${e.channel}/${e.type}" returned a Promise. Middleware is synchronous: a Promise is truthy, so this event was allowed without waiting and a "return false" inside it can never veto. Do the check synchronously, and put anything that must await in an effect.`)}catch(l){console.error("Middleware error:",l),a=!1}if(!a)return this.notifyEventSubscribers(e,"uncommitted"),S}const t=[];this.stagingSink=t;let s=null,n="";try{this.reducerBus.emit(e.channel,e.type,e.payload,e),s=this.stagedRejection,n=this.stagedRejectedBy;for(const[c,d]of this.patternReducers){if(s!==null)break;if(M(d,e)){const i=this.stageSliceGuarded(c,e,t);i!==null&&(s=i,n=c)}}}finally{this.stagingSink=null,this.stagedRejection=null,this.stagedRejectedBy=""}if(s!==null)return this.onRejected?.(s,e,n),this.notifyEventSubscribers(e,"committed"),{committed:!0,written:!1,rejected:s};const r=this.commitStaged(t,e);return this.notifyEventSubscribers(e,"committed"),r&&(this.notifyEventSubscribers(e,"written"),this.listeners.forEach(c=>c())),r?de:ae}async runEventEffects(e,t,s){this.inFlightEffects++;try{t.committed&&await this.notifyEffects(e)}catch(n){console.error("Effect error:",n)}finally{this.inFlightEffects--,s(t)}}instrument(e){return this.instrumentObservers.add(e),()=>{this.instrumentObservers.delete(e)}}emitInstrumentation(e,t,s,n,r){const c={},d={};for(const a of s)c[a]=this.getAtPath(n,a),d[a]=this.getAtPath(this.state,a);const i={event:{id:e.id,channel:e.channel,type:e.type,payload:e.payload,...e.meta!==void 0?{meta:e.meta}:{}},committed:t.committed,changedPaths:s,prevValues:c,nextValues:d,reduceTimeMs:r,...t.rejected!==void 0?{rejected:t.rejected}:{}};for(const a of[...this.instrumentObservers])try{a(i)}catch(l){console.error("Instrumentation observer error:",l)}}connect(e,t,s){const n=this.connectorBus.on(e.reducer,e.property,t);if(s?.immediate===!0){const r=this.state[e.reducer],c=e.property.includes("*")?"":e.property;t({oldValue:void 0,newValue:this.getAtPath(r,c),path:c})}return n}onEvent(e,t,s,n="committed"){const r=`${e}::${String(t)}`,c=n==="committed"?this.committedEventSubscribers:n==="uncommitted"?this.uncommittedEventSubscribers:n==="written"?this.writtenEventSubscribers:this.allEventSubscribers;return c.has(r)||c.set(r,new Set),c.get(r).add(s),()=>{const d=c.get(r);d&&(d.delete(s),d.size===0&&c.delete(r))}}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}getState(){return this.state}registerMiddleware(e){return this.middleware.push(e),()=>{const t=this.middleware.indexOf(e);t!==-1&&this.middleware.splice(t,1)}}registerReducer(e,t){if(Object.prototype.hasOwnProperty.call(this.reducers,e))throw new Error(`Reducer ${e} already exists`);return this.mountSlice(e,t,{preserveState:!1}),this.listeners.forEach(s=>s()),()=>{this.unmountSlice(e,{deleteState:!0}),this.listeners.forEach(s=>s())}}call(e,t,s,n){return te({idFactory:this.idFactory,registerEffect:this.registerEffect,emit:this.emit},e,t,s,n)}registerEffect(e){const{effect:t,meta:s,when:n}=e,r=[];if(s&&this.effectMeta.set(t,s),n&&("any"in n&&n.any===!0||"channel"in n||"channels"in n)){const i={effect:t,when:n};return this.patternEffects.add(i),()=>{this.patternEffects.delete(i)}}const d=A(e);if(d.length===0&&!n){const i={effect:t,when:{any:!0}};return this.patternEffects.add(i),()=>{this.patternEffects.delete(i)}}for(const[i,a]of d){const l=`${String(i)}::${String(a)}`;this.effects.has(l)||this.effects.set(l,new Set),this.effects.get(l).add(t),r.push(()=>{const u=this.effects.get(l);u&&(u.delete(t),u.size===0&&this.effects.delete(l))})}return()=>{for(const i of r)i()}}onEffect(e,t,s){const n=async(r,c,d)=>{if(r.channel!==e||r.type!==t)return;const i=r;return s(i.payload,c,d,i)};return this.registerEffect({when:{keys:[[e,t]]},effect:n})}replaceMiddleware(e){this.middleware.length=0;for(const t of e)this.middleware.push(t)}replaceEffects(e){this.effects.clear(),this.patternEffects.clear();for(const t of e)this.registerEffect(t)}replaceReducers(e,t={}){const s=t.preserveState!==!1,n=new Set(Object.keys(this.reducers)),r=Object.entries(e),c=new Set(r.map(([d])=>d));for(const d of n)c.has(d)||this.unmountSlice(d,{deleteState:!0});for(const[d,i]of r)n.has(d)?(this.unmountSlice(d,{deleteState:!1}),this.mountSlice(d,i,{preserveState:s})):this.mountSlice(d,i,{preserveState:!1})}hotReplace(e){e.middleware&&this.replaceMiddleware(e.middleware),e.effects&&this.replaceEffects(e.effects),e.reducer&&this.replaceReducers(e.reducer,{preserveState:e.preserveState})}mountSlice(e,t,s){const n=e,{reducer:r,state:c,when:d}=t;if(this.reducers[e]=new W(r),(!s.preserveState||this.state[n]===void 0)&&(this.state={...this.state,[n]:O(oe(n,c))}),d&&("any"in d&&d.any===!0||"channel"in d||"channels"in d)){this.patternReducers.set(e,d),this.sliceUnsubs.set(n,[]);return}const a=A(t);if(a.length===0&&!d){this.patternReducers.set(e,{any:!0}),this.sliceUnsubs.set(n,[]);return}const l=[];for(const[u,h]of a){const p=this.reducerBus.on(u,h,(f,m)=>{const y=m??{channel:u,type:h,payload:f,id:this.idFactory()};if(this.stagingSink===null)return;const g=this.stageSliceGuarded(e,y,this.stagingSink);g!==null&&this.stagedRejection===null&&(this.stagedRejection=g,this.stagedRejectedBy=e)});l.push(p)}this.sliceUnsubs.set(n,l)}unmountSlice(e,t){const s=e;this.patternReducers.delete(e);const n=this.sliceUnsubs.get(s);if(n){for(const r of n)try{r()}catch(c){console.error(`[Store error]: ${c}`)}this.sliceUnsubs.delete(s)}if(delete this.reducers[e],t.deleteState){const{[s]:r,...c}=this.state;this.state=c}}getAtPath(e,t){return ne(e,t)}static buildAncestorPaths(e){return se(e)}}function le(o){return new k({...o,reducer:o.reducer??{},middleware:o.middleware??[],effects:o.effects??[]})}const ue=o=>(e,t)=>t.map(s=>[e,s]),fe=()=>o=>o,N=new Set;function he(o){const e=String(o);N.has(e)||(N.add(e),console.warn(`[yoltra] Entity id "${e}" contains a dot. Paths are dotted, so a subscription to "entities.${e}" is indistinguishable from one to a nested object of the same name. Use ids without dots.`))}function pe(o,e){if(o.length!==e.length)return e;for(let t=0;t<o.length;t++)if(o[t]!==e[t])return e;return o}function me(o={}){const e=o.selectId??(i=>i.id),{sortComparer:t}=o,s=(i,a)=>{if(t===void 0)return a;const l=[...a].sort((u,h)=>{const p=i.entities[u],f=i.entities[h];return p===void 0||f===void 0?0:t(p,f)});return pe(a,l)},n=(i,a,l)=>{const u={...i,entities:a,ids:l};return{...u,ids:s(u,l)}},r=(i,a,l)=>{let u=null,h=null;for(const p of a){const f=e(p);process.env.NODE_ENV!=="production"&&String(f).includes(".")&&he(f);const m=(u??i.entities)[f];if(m!==void 0&&l==="add")continue;const y=m!==void 0&&l==="upsert"?{...m,...p}:p;u??={...i.entities},u[f]=y,m===void 0&&(h??=[...i.ids],h.push(f))}return u===null?i:n(i,u,h??i.ids)},c=(i,a)=>{let l=null;for(const{id:u,changes:h}of a){const p=(l??i.entities)[u];p!==void 0&&(l??={...i.entities},l[u]={...p,...h})}return l===null?i:n(i,l,i.ids)},d=(i,a)=>{const l=new Set(a.filter(h=>i.entities[h]!==void 0));if(l.size===0)return i;const u={...i.entities};for(const h of l)delete u[h];return n(i,u,i.ids.filter(h=>!l.has(h)))};return{getInitialState(i){const a={ids:[],entities:{}};return i===void 0?a:{...a,...i}},addOne:(i,a)=>r(i,[a],"add"),addMany:(i,a)=>r(i,a,"add"),setOne:(i,a)=>r(i,[a],"set"),setMany:(i,a)=>r(i,a,"set"),setAll:(i,a)=>{const l={},u=[];for(const h of a){const p=e(h);l[p]===void 0&&u.push(p),l[p]=h}return n(i,l,u)},updateOne:(i,a)=>c(i,[a]),updateMany:(i,a)=>c(i,a),upsertOne:(i,a)=>r(i,[a],"upsert"),upsertMany:(i,a)=>r(i,a,"upsert"),removeOne:(i,a)=>d(i,[a]),removeMany:(i,a)=>d(i,a),removeAll:i=>i.ids.length===0?i:n(i,{},[]),selectIds:i=>i.ids,selectEntities:i=>i.entities,selectAll:i=>i.ids.map(a=>i.entities[a]),selectById:(i,a)=>i.entities[a],selectTotal:i=>i.ids.length,idsPath:"ids",pathTo:(i,a)=>a===void 0?`entities.${i}`:`entities.${i}.${a}`,anyField:i=>`entities.*.${i}`}}const E="$yoltra";function C(o,e={}){const t=e.maxNodes??1e5,s=e.sanitize,n=[],r=new Map;let c=0,d=!1;function i(l,u){if(s!==void 0&&(l=s(u,l)),c+=1,c>t)return d=!0,{[E]:"unsupported",kind:"truncated"};switch(typeof l){case"undefined":return{[E]:"undefined"};case"bigint":return{[E]:"bigint",value:l.toString()};case"number":return Number.isNaN(l)?{[E]:"nan"}:l===1/0?{[E]:"infinity",sign:1}:l===-1/0?{[E]:"infinity",sign:-1}:l;case"function":case"symbol":return n.push(u),{[E]:"unsupported",kind:typeof l};case"string":case"boolean":return l}if(l===null)return null;const h=l,p=r.get(h);if(p!==void 0)return{[E]:"ref",path:p};if(r.set(h,u),l instanceof Date)return{[E]:"date",iso:l.toISOString()};if(l instanceof RegExp)return{[E]:"regexp",source:l.source,flags:l.flags};if(l instanceof Error)return{[E]:"error",name:l.name,message:l.message};if(l instanceof Map){const m=[];let y=0;for(const[g,P]of l)m.push([i(g,`${u}/@k${y}`),i(P,`${u}/${y}`)]),y+=1;return{[E]:"map",entries:m}}if(l instanceof Set){const m=[];let y=0;for(const g of l)m.push(i(g,`${u}/${y}`)),y+=1;return{[E]:"set",values:m}}if(Array.isArray(l))return l.map((m,y)=>i(m,`${u}/${y}`));const f={};for(const[m,y]of Object.entries(l))f[m]=i(y,`${u}/${Q(m)}`);return E in f?{[E]:"escaped",value:f}:f}return{value:i(o,""),report:{truncated:d,unsupported:n}}}function V(o){const e=new Map,t=[];function s(c,d){if(c===null||typeof c!="object")return c;if(Array.isArray(c)){const a=[];return e.set(d,a),c.forEach((l,u)=>{if(B(l)){t.push({target:a,key:u,path:l.path}),a[u]=void 0;return}a[u]=s(l,`${d}/${u}`)}),a}if(typeof c[E]=="string"){const a=c;switch(a[E]){case"undefined":return;case"nan":return Number.NaN;case"infinity":return a.sign===1?1/0:-1/0;case"bigint":return BigInt(a.value);case"date":return new Date(a.iso);case"regexp":return new RegExp(a.source,a.flags);case"error":{const l=new Error(a.message);return l.name=a.name,l}case"unsupported":return;case"ref":return;case"map":{const l=new Map;return e.set(d,l),a.entries.forEach(([u,h],p)=>{l.set(s(u,`${d}/@k${p}`),s(h,`${d}/${p}`))}),l}case"set":{const l=new Set;return e.set(d,l),a.values.forEach((u,h)=>l.add(s(u,`${d}/${h}`))),l}case"escaped":return n(a.value,d);default:return}}return n(c,d)}function n(c,d){const i={};e.set(d,i);for(const[a,l]of Object.entries(c)){const u=`${d}/${Q(a)}`;if(B(l)){t.push({target:i,key:a,path:l.path}),i[a]=void 0;continue}i[a]=s(l,u)}return i}const r=s(o,"");e.set("",r);for(const{target:c,key:d,path:i}of t)c[d]=e.get(i);return r}function B(o){return o!==null&&typeof o=="object"&&o[E]==="ref"&&typeof o.path=="string"}function Q(o){return o.replace(/~/g,"~0").replace(/\//g,"~1")}function ye(o,e,t={}){let s=t.maxNodes??1e5;for(let n=0;n<8;n+=1){const{value:r,report:c}=C(o,{...t,maxNodes:s});let d;try{d=JSON.stringify(r)?.length??0}catch{d=Number.POSITIVE_INFINITY}if(d<=e)return c.truncated?{value:r,truncated:!0,note:`State was too large to send in full; parts beyond ${s} nodes are omitted.`}:{value:r,truncated:!1};const i=Math.floor(s*e*.8/d);if(s=Math.max(1,Math.min(i,s-1)),s<=1&&n>0)break}return{value:{[E]:"unsupported",kind:"truncated"},truncated:!0,note:`State exceeds the ${e}-byte transport limit and could not be reduced to fit.`}}function w(o,e,t){o.onError?.(e,t)}async function ge(o){const e={slices:{},restored:!1};let t;try{t=o.source??await o.adapter.read(o.key)}catch(n){return w(o,n,"read"),e}if(t==null||t==="")return e;let s;try{s=V(JSON.parse(t))}catch(n){return w(o,n,"decode"),e}if(s===null||typeof s!="object"||typeof s.version!="number")return w(o,new Error("persisted payload is not a recognisable envelope"),"decode"),e;if(s.version!==o.version){if(o.migrate===void 0)return w(o,new Error(`persisted state is version ${s.version}, this build expects ${o.version}, and no migrate was supplied`),"migrate"),e;try{const n=o.migrate(s.slices,s.version);return n===null?e:{slices:n,restored:!0}}catch(n){return w(o,n,"migrate"),e}}return{slices:s.slices??{},restored:!0}}function Ee(o,e){if(!e.restored)return o;const t={};for(const[s,n]of Object.entries(o)){const r=e.slices[s];t[s]=r===void 0?n:{...n,state:r}}return t}function G(o,e){const t=o??{},s=e.slices===void 0?t:Object.fromEntries(e.slices.filter(n=>n in t).map(n=>[n,t[n]]));return JSON.stringify(C({version:e.version,slices:s}).value)}function be(o,e){const t=e.throttleMs??250,s=e.slices;let n=null,r=!1;const c=()=>{if(r){r=!1;try{const a=e.adapter.write(e.key,G(o.getState(),e));a instanceof Promise&&a.catch(l=>w(e,l,"write"))}catch(a){w(e,a,"write")}}},d=()=>{if(r=!0,t<=0){c();return}n===null&&(n=setTimeout(()=>{n=null,c()},t),n.unref?.())},i=o.instrument(a=>{if(s===void 0){d();return}(a.changedPaths??[]).some(u=>s.some(h=>u===h||u.startsWith(`${h}.`)))&&d()});return()=>{i(),n!==null&&(clearTimeout(n),n=null),c()}}function we(o,e){return G(o.getState(),e)}function ve(o){return{read:e=>o.getItem(e),write:(e,t)=>o.setItem(e,t),remove:e=>o.removeItem(e)}}function Se(o){const e=new Map(Object.entries(o??{}));return{read:t=>e.get(t)??null,write:(t,s)=>{e.set(t,s)},remove:t=>{e.delete(t)}}}exports.CallAbortedError=I;exports.CallTimeoutError=L;exports.EventBus=H;exports.LooseEventBus=K;exports.Reducer=W;exports.Rejected=J;exports.Store=k;exports.createEntityAdapter=me;exports.createMemoryAdapter=Se;exports.createStore=le;exports.createWebStorageAdapter=ve;exports.decodeState=V;exports.dehydrate=we;exports.detectChangedProps=T;exports.encodeState=C;exports.encodeStateBounded=ye;exports.eventKeys=fe;exports.freezeState=$;exports.hydrate=ge;exports.isRejected=U;exports.persist=be;exports.typedEvents=ue;exports.withHydration=Ee;
11
6
  //# sourceMappingURL=yoltra.cjs.map