march-hare 0.13.10 → 0.13.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- var global,factory;global=this,factory=function(e,t,n,r,o){"use strict";function s(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e)for(const n in e)if("default"!==n){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}return t.default=e,Object.freeze(t)}const c=s(n);function i(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var a,u={exports:{}},l=(a||(a=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,r,s,c){if("function"!=typeof r)throw new TypeError("The listener must be a function");var i=new o(r,s||e,c),a=n?n+t:t;return e._events[a]?e._events[a].fn?e._events[a]=[e._events[a],i]:e._events[a].push(i):(e._events[a]=i,e._eventsCount++),e}function c(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function i(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),i.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},i.prototype.listeners=function(e){var t=this._events[n?n+e:e];if(!t)return[];if(t.fn)return[t.fn];for(var r=0,o=t.length,s=new Array(o);r<o;r++)s[r]=t[r].fn;return s},i.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},i.prototype.emit=function(e,t,r,o,s,c){var i=n?n+e:e;if(!this._events[i])return!1;var a,u,l=this._events[i],f=arguments.length;if(l.fn){switch(l.once&&this.removeListener(e,l.fn,void 0,!0),f){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,t),!0;case 3:return l.fn.call(l.context,t,r),!0;case 4:return l.fn.call(l.context,t,r,o),!0;case 5:return l.fn.call(l.context,t,r,o,s),!0;case 6:return l.fn.call(l.context,t,r,o,s,c),!0}for(u=1,a=new Array(f-1);u<f;u++)a[u-1]=arguments[u];l.fn.apply(l.context,a)}else{var d,p=l.length;for(u=0;u<p;u++)switch(l[u].once&&this.removeListener(e,l[u].fn,void 0,!0),f){case 1:l[u].fn.call(l[u].context);break;case 2:l[u].fn.call(l[u].context,t);break;case 3:l[u].fn.call(l[u].context,t,r);break;case 4:l[u].fn.call(l[u].context,t,r,o);break;default:if(!a)for(d=1,a=new Array(f-1);d<f;d++)a[d-1]=arguments[d];l[u].fn.apply(l[u].context,a)}}return!0},i.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},i.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},i.prototype.removeListener=function(e,t,r,o){var s=n?n+e:e;if(!this._events[s])return this;if(!t)return c(this,s),this;var i=this._events[s];if(i.fn)i.fn!==t||o&&!i.once||r&&i.context!==r||c(this,s);else{for(var a=0,u=[],l=i.length;a<l;a++)(i[a].fn!==t||o&&!i[a].once||r&&i[a].context!==r)&&u.push(i[a]);u.length?this._events[s]=1===u.length?u[0]:u:c(this,s)}return this},i.prototype.removeAllListeners=function(e){var t;return e?this._events[t=n?n+e:e]&&c(this,t):(this._events=new r,this._eventsCount=0),this},i.prototype.off=i.prototype.removeListener,i.prototype.addListener=i.prototype.on,i.prefixed=n,i.EventEmitter=i,e.exports=i}(u)),u.exports);const f=i(l);class d extends f{cache=new Map;emit(e,...t){return this.cache.set(e,t[0]),super.emit(e,...t)}setCache(e,t){this.cache.set(e,t)}getCached(e){return this.cache.get(e)}fire(e,...t){return super.emit(e,...t)}}const p=c.createContext(new d);function h(){return c.useContext(p)}function m({children:e}){const n=c.useMemo(()=>new d,[]);return t.jsx(p.Provider,{value:n,children:e})}const y=c.createContext(new Set);function b({children:e}){const n=c.useMemo(()=>new Set,[]);return t.jsx(y.Provider,{value:n,children:e})}const v=c.createContext({current:{}});function g(){const e=c.useContext(v);return c.useMemo(()=>new Proxy({},{get:(t,n)=>Reflect.get(e.current,n),has:(t,n)=>n in e.current,ownKeys:()=>Reflect.ownKeys(e.current),getOwnPropertyDescriptor(t,n){const o=Object.getOwnPropertyDescriptor(e.current,n);if(!r.G.isUndefined(o))return{...o,configurable:!0}},set(){throw new TypeError("Env is read-only outside `context.actions.produce`. Mutate via produce(({ env }) => { env.x = ... }) instead.")}}),[e])}const w=(e="")=>`march-hare.action/${e}`,j=(e="")=>`march-hare.action/broadcast/${e}`,O=(e="")=>`march-hare.action/multicast/${e}`,x=(e="")=>`march-hare.action.lifecycle/${e}`;class P{static Payload=Symbol("march-hare.brand/Payload");static Broadcast=Symbol("march-hare.brand/Broadcast");static Multicast=Symbol("march-hare.brand/Multicast");static Action=Symbol("march-hare.brand/Action");static Channel=Symbol("march-hare.brand/Channel");static Name=Symbol("march-hare.brand/Name");static Lifecycle=Symbol("march-hare.brand/Lifecycle")}function S(e){const t=Symbol(`march-hare.action.lifecycle/${e}`),n=function(n){return{[P.Action]:t,[P.Payload]:void 0,[P.Channel]:n,[P.Name]:e,channel:n}};return Object.defineProperty(n,P.Action,{value:t,enumerable:!1}),Object.defineProperty(n,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(n,P.Name,{value:e,enumerable:!1}),Object.defineProperty(n,P.Lifecycle,{value:e,enumerable:!1}),n}const G=Symbol(j("Fault")),E=Symbol(j("Env"));class k{static Mount(){return S("Mount")}static Unmount(){return S("Unmount")}static Error(){return S("Error")}static Update(){return S("Update")}static Fault=(()=>{const e={};return Object.defineProperty(e,P.Action,{value:G,enumerable:!1}),Object.defineProperty(e,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,P.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,P.Name,{value:"Fault",enumerable:!1}),e})();static Env=(()=>{const e={};return Object.defineProperty(e,P.Action,{value:E,enumerable:!1}),Object.defineProperty(e,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,P.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,P.Name,{value:"Env",enumerable:!1}),e})()}var C=(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(C||{}),M=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(M||{});function N({initial:e,children:n}){const o=c.useRef(e),s=h();return r.G.isUndefined(s.getCached(E))&&s.setCache(E,o.current),t.jsx(v.Provider,{value:o,children:n})}const A=c.createContext(new WeakMap);function _({children:e}){const n=c.useMemo(()=>new WeakMap,[]);return t.jsx(A.Provider,{value:n,children:e})}const R=c.createContext(()=>{});function U({tap:e,children:n}){const r=c.useRef(e);c.useLayoutEffect(()=>{r.current=e},[e]);const o=c.useMemo(()=>e=>r.current?.(e),[]);return t.jsx(R.Provider,{value:o,children:n})}const L=c.createContext(new Map);function T({env:e,tap:n,children:r}){const o=c.useMemo(()=>new Map,[]);return t.jsx(m,{children:t.jsx(L.Provider,{value:o,children:t.jsx(N,{initial:e??{},children:t.jsx(b,{children:t.jsx(U,{tap:n,children:t.jsx(_,{children:r})})})})})})}const $=e=>"symbol"==typeof e;function B(e){return r.G.isString(e)||$(e)?e:(r.G.isObject(e)||r.G.isFunction(e))&&P.Action in e?e[P.Action]:e}function W(e){if(r.G.isString(e))return e.startsWith(j());if($(e))return e.description?.startsWith(j())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(P.Broadcast in e&&e[P.Broadcast])return!0;if(P.Action in e){const t=e[P.Action];return t.description?.startsWith(j())??!1}}return!1}function F(e){return r.G.isObject(e)&&P.Channel in e&&"channel"in e}function z(e){const t=B(e),n=$(t)?t.description??"":t;return n.startsWith(x())&&n.slice(x().length)||null}function D(e){if(r.G.isString(e))return e.startsWith(O());if($(e))return e.description?.startsWith(O())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(P.Multicast in e&&e[P.Multicast])return!0;if(P.Action in e){const t=e[P.Action];return t.description?.startsWith(O())??!1}}return!1}const I=Symbol(((e="")=>`march-hare/replay${e}`)());function H(e,t,...n){e instanceof d&&e.setCache(t,n[0]);const r=e.listeners(t);return 0===r.length?Promise.resolve():Promise.all(r.map(e=>Promise.resolve(e(...n)))).then(()=>{})}function J(e,t){for(const n of e.keys())if(z(n)===t)return n;return null}const q=Symbol("march-hare.unset");function K(){const[,e]=c.useReducer(e=>e+1,0);return e}function V(){return{data:q,at:null}}function Q(e,t){return{data:e,at:t}}var X=(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(X||{});class Y extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Z=c.createContext(null);function ee(e){const t=r.G.isUndefined(e)?function(){const e=new Map;return{get:t=>e.get(t)??null,set:(t,n)=>{e.set(t,n)},remove:t=>{e.delete(t)},clear:()=>{e.clear()},keys:()=>e.keys()}}():e,n=e?.key;return{get(e){try{const n=t.get(e);if(r.G.isNull(n))return V();const o=JSON.parse(n);return Q(o.data,Temporal.Instant.from(o.at))}catch{return V()}},set(e,n){if(n.data!==q&&!r.G.isNull(n.at))try{t.set(e,JSON.stringify({data:n.data,at:n.at.toString()}))}catch{return}},remove(e){try{t.remove(e)}catch{return}},clear(){try{t.clear()}catch{return}},keys(){try{return t.keys?.()??[]}catch{return[]}},scope(e){if(r.G.isUndefined(n)||r.G.isNullable(e))return"";try{const t=n({env:e});return r.G.isNullable(t)?"":t}catch{return""}}}}const te=new WeakMap,ne=new Map,re=[];function oe(e,t,n,o){const s=r.G.isNull(n)?"":`${n}:`,c=(e,n)=>{const r=t.scope(e);return`${""===r?"":`${r}:`}${s}${function(e){return JSON.stringify(e)}(n)}`},i=(e,n)=>{const o=t.get(c(n,e));return o.data===q||r.G.isNull(o.at)?{data:q,at:null}:{data:o.data,at:o.at}},a=(n,r,o,s)=>e({env:n,controller:r,params:o,dispatch:s}).then(e=>(t.set(c(n,o),Q(e,Temporal.Now.instant())),e)),u=e=>{const n=o(),r=t.scope(n),c=""===r?s:`${r}:${s}`,i=Object.entries(e);for(const o of[...t.keys()])if(o.startsWith(c))try{const e=JSON.parse(o.slice(c.length));i.every(([t,n])=>e[t]===n)&&t.remove(o)}catch{continue}};function l(e){return{run:a,read:e=>i(e,o()),evict:u,params:e??{}}}return re.push(u),Object.defineProperty(l,"get",{value:function(e){const{data:t}=i(e??{},o());return t===q?null:t},enumerable:!1}),l}function se(e,t,n){const o=e,s=n??(()=>{});return r.G.isUndefined(t)?oe(o,function(e){const t=te.get(e);if(r.G.isNotNullable(t))return t;const n=ee();return te.set(e,n),n}(o),null,s):oe(o,t,function(e){const t=ne.get(e);if(r.G.isNotNullable(t))return t;const n=String(ne.size);return ne.set(e,n),n}(o),s)}const ce=Symbol("march-hare.coalesce/default");function ie(e,t){return new Promise((n,r)=>{if(t.aborted)return void r(t.reason);const o=()=>r(t.reason);t.addEventListener("abort",o,{once:!0}),e.then(e=>{t.removeEventListener("abort",o),n(e)},e=>{t.removeEventListener("abort",o),r(e)})})}let ae=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var ue=(e=>(e[e.Add=1]="Add",e[e.Remove=2]="Remove",e[e.Update=4]="Update",e[e.Move=8]="Move",e[e.Replace=16]="Replace",e[e.Sort=32]="Sort",e[e.Create=64]="Create",e[e.Fetch=128]="Fetch",e[e.Clone=256]="Clone",e[e.Archive=512]="Archive",e[e.Restore=1024]="Restore",e[e.Merge=2048]="Merge",e[e.Reorder=4096]="Reorder",e[e.Sync=8192]="Sync",e[e.Publish=16384]="Publish",e[e.Link=32768]="Link",e[e.Unlink=65536]="Unlink",e[e.Lock=131072]="Lock",e[e.Unlock=262144]="Unlock",e[e.Import=524288]="Import",e[e.Export=1048576]="Export",e[e.Transfer=2097152]="Transfer",e))(ue||{}),le=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(le||{}),fe=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(fe||{});class de{[o.immerable]=!0;static keys=new Set(Object.values(fe));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new de(this.value,this.operation);return n.property=e,n.process=t,n}}class pe{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=ae}const he=Symbol("Box");function me(e,t){const n="string"==typeof t?""===t?[]:t.split("."):t;let r=e;for(const o of n){if(null==r)return;r=r[o]}return r}function ye(e){if(r.G.isNullable(e)||ge(e))return e;if(r.G.isArray(e))return e.map(e=>ye(e));if(r.G.isObject(e)&&ve(e)){const t=Object.entries(e).map(([e,t])=>[e,ye(t)]);return{...Object.fromEntries(t),[pe.tag]:e[pe.tag]??pe.id()}}return e}function be(e){if(Array.isArray(e))return e.filter(e=>pe.tag in e).map(e=>e[pe.tag]??"").join(",");const t=e[pe.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function ve(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function ge(e){return r.G.isNullable(e)||r.G.isString(e)||r.G.isNumber(e)||r.G.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function we(e,t,n,o,s,c){return function i(a,u=t.path){if(a instanceof de){const t=me(n,u.join("."));if(Object.entries(a).filter(([e,t])=>!de.keys.has(e)&&t instanceof de).forEach(([e,t])=>i(t,u.concat(e))),ge(a.value)){if(e===le.Hydrate)return a.value;const i=u.slice(0,-1),l=i.length>0?me(n,i.join(".")):n;return r.G.isNullable(l)||je(l,a,u.at(-1),o,s,c),t??a.value}if(e===le.Hydrate){const e=ye(i(a.value,u));return je(e,a,null,o,s,c),e}const l=t??ye(a.value);return je(l,a,null,o,s,c),r.G.isNullable(t)?l:(i(a.value,u),t)}if(r.G.isArray(a))return a.map((e,t)=>i(e,u.concat(t)));if(r.G.isObject(a)&&!ve(a))return a;if(r.G.isObject(a)){const t=Object.entries(a).map(([e,t])=>[e,i(t,u.concat(e))]),n=Object.fromEntries(t);if(e===le.Hydrate){const e=ye(n);return Object.entries(a).forEach(([t,n])=>{n instanceof de&&ge(n.value)&&je(e,n,t,o,s,c)}),e}return n}return a}(t.value)}function je(e,t,n,r,o,s){const c=s(e),i=o.get(c)??[];o.set(c,[t.assign(n,r),...i])}class Oe{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=be){this.#t=e}static pk(){return ae()}static"κ"=Oe.pk;annotate(e,t){return new de(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,t,n,o,s){function c(o){const s=o.at(-1),c=me(e(),o),i=o.slice(0,-1),a=r.A.isNotEmpty(i)?me(e(),i):e();return[...r.G.isObject(c)||r.G.isArray(c)?t.get(n(c))?.filter(e=>r.G.isNullable(e.property))??[]:[],...r.G.isObject(a)?t.get(n(a))?.filter(e=>e.property===s)??[]:[]]}return function t(n){return new Proxy(()=>{},{get:(i,a)=>"pending"===a?()=>!r.A.isEmpty(c(n)):"remaining"===a?()=>r.A.length(c(n)):"box"===a?()=>({value:me(e(),n),inspect:t(n),[he]:!0}):"is"===a?e=>c(n).some(t=>0!==(t.operation&e)):"draft"===a?()=>r.A.head(c(n))?.value??me(e(),n):"settled"===a?()=>new Promise(t=>{if(r.A.isEmpty(c(n)))return t(me(e(),n));const i=()=>{r.A.isEmpty(c(n))&&(s(i),t(me(e(),n)))};o(i)}):t([...n,String(a)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#s(le.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(le.Produce,e)}#s(e,t){const n=Symbol("process"),[,r]=pe.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>pe.immer.applyPatches(t,[{...r,value:we(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=ye(this.#e),this.#c(),n}prune(e){this.#n.forEach((t,n)=>{const o=t.filter(t=>t.process!==e);r.A.isEmpty(o)?this.#n.delete(n):this.#n.set(n,o)}),this.#c()}#c(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}function xe({action:e,renderer:t}){const n=h(),o=c.useContext(L),s=K(),i=c.useMemo(()=>{const t=o.get(e);if(t)return t;const s=new Oe,c=n.getCached(e);r.G.isNotNullable(c)&&s.hydrate({value:c});const i={state:s,listeners:new Set};return o.set(e,i),i},[e,n,o]);c.useLayoutEffect(()=>{function t(e){i.state.hydrate({value:e}),i.listeners.forEach(e=>e())}return i.listeners.add(s),n.on(e,t),()=>{i.listeners.delete(s),n.off(e,t)}},[e,n,i]);const a=i.state.model?.value;return r.G.isNullable(a)?null:t(a,i.state.inspect.value)}function Pe(e,t){const n=t.split(".");let r=e;for(let o=0;o<n.length-1;o++)r=r[n[o]];return{cursor:r,key:n[n.length-1]}}function Se(e,t,n){const{cursor:r,key:o}=Pe(e,t);r[o]=n}function Ge(e){return(t,n)=>{t.actions.produce(t=>{Se(t.model,e,n)})}}function Ee(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=Pe(e,t);n[r]=!n[r]}(t.model,e)})}}function ke(e,t){return n=>{n.actions.produce(n=>{Se(n.model,e,t)})}}function Ce(){const e=c.useRef(null);return c.useMemo(()=>({actions:{dispatch:function(t,n){const r=e.current;if(!r)throw new Error("march-hare: useContext handle dispatched before its paired context.useActions(...) ran. Call context.actions.dispatch from event handlers, not synchronously during render.");return r(t,n)}},useActions:function(...t){const n=function(...e){const t=r.G.isUndefined(e[0])||r.G.isFunction(e[0])?{}:e[0],n=r.G.isFunction(e[0])?e[0]:e[1]??(()=>({})),s=h(),i=c.useContext(Z),a=c.useContext(y),u=g(),l=c.useContext(v),d=c.useContext(A),p=c.useContext(R),m=K(),b=c.useRef(!1),j=c.useRef(null),O=c.useRef(new Oe);b.current||(b.current=!0,j.current=O.current.hydrate(t));const[x,P]=c.useState(()=>O.current.model),S=function(e){const t=c.useRef(e);return t.current=e,c.useMemo(()=>{return n=t,Object.keys(e).reduce((e,t)=>(Object.defineProperty(e,t,{get:()=>n.current[t],enumerable:!0}),e),{});var n},[e])}(n()),k=c.useMemo(()=>new f,[]),C=c.useRef({handlers:new Map});C.current.handlers=new Map;const N=function(){const e=c.useRef(new Set),t=c.useRef(new Set);return c.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),_=c.useRef(M.Mounting),U=c.useRef(new Set),L=c.useRef(0),T=c.useCallback((e,t,n)=>{const c=new AbortController,f={controller:c,action:e,payload:t};return a.add(f),U.current.add(f),{model:O.current.model,get phase(){return _.current},task:f,data:S,tasks:a,env:u,actions:{produce(e){if(c.signal.aborted)return;const t=l.current,r=O.current.produce(t=>{l.current=o.produce(l.current,n=>{e({model:t,inspect:O.current.inspect,env:n})})});P(O.current.model),l.current!==t&&s.emit(E,l.current),n.processes.add(r),j.current&&(n.processes.add(j.current),j.current=null)},dispatch(e,t){if(c.signal.aborted)return Promise.resolve();const n=B(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H(W(e)?s:k,n,t,r)},annotate:(e,t=ue.Update)=>O.current.annotate(t,e),get inspect(){return O.current.inspect},resource:Object.assign(function(e){const t=(e,t)=>{if(c.signal.aborted)return Promise.resolve();const n=e,r=B(n);return D(n)?i?H(i.emitter,r,t,void 0):Promise.resolve():W(n)?H(s,r,t,void 0):Promise.resolve()},n={exceedsWindow:null,coalesceToken:void 0},o={then:(o,s)=>(()=>{if(r.G.isNotNullable(n.exceedsWindow)){const{data:t,at:o}=e.read(e.params);if(t!==q&&r.G.isNotNullable(o)){const e=Temporal.Now.instant().since(o),r=Temporal.Duration.from(n.exceedsWindow);if(Temporal.Duration.compare(e,r)<=0)return Promise.resolve(t)}}if(r.G.isUndefined(n.coalesceToken))return e.run(u,c,e.params,t);let o=d.get(e.run);r.G.isUndefined(o)&&(o=new Map,d.set(e.run,o));const s=o,i=`${JSON.stringify(e.params)}|${function(e){switch(typeof e){case"string":return`s:${e}`;case"number":return`n:${e}`;case"bigint":return`i:${e.toString()}`;case"boolean":return`b:${e}`;case"symbol":return`y:${e.description??String(e)}`;default:return`o:${JSON.stringify(e)}`}}(n.coalesceToken)}`,a=s.get(i);if(a)return ie(a,c.signal);const l=new AbortController,f=e.run(u,l,e.params,t).finally(()=>{s.delete(i)});return s.set(i,f),ie(f,c.signal)})().then(o,s),exceeds:e=>(n.exceedsWindow=e,o),coalesce:e=>(n.coalesceToken=e??ce,o),evict(t){e.evict(t??e.params)}};return o},{nuke:e=>function(e){const t=e??{};for(const n of re)n(t)}(e)}),async final(e){if(c.signal.aborted)return null;const t=B(e),n=D(e)?i?.emitter??null:s;if(!n)return null;const o=n.getCached(t);if(r.G.isUndefined(o))return null;const a=O.current.inspect;return a.pending()&&await new Promise((e,t)=>{if(c.signal.aborted)return void t(c.signal.reason);const n=()=>t(c.signal.reason);c.signal.addEventListener("abort",n,{once:!0}),a.settled().then(()=>{c.signal.removeEventListener("abort",n),e()})}),n.getCached(t)??null},peek(e){if(c.signal.aborted)return null;const t=B(e),n=D(e)?i?.emitter??null:s;return n?n.getCached(t)??null:null}}}},[x]);c.useLayoutEffect(()=>{function e(e,t,n){return function(o,c){const u=n();if(c===I&&r.G.isNotNullable(u))return;if(r.G.isNotNullable(c)&&c!==I&&r.G.isNotNullable(u)&&!function(e,t){for(const n of Object.keys(e))if(t[n]!==e[n])return!1;return!0}(c,u))return;const f={processes:new Set},d=Promise.withResolvers(),h=T(e,o,f),y=function(e){const t=B(e),n=r.G.isString(t)?t:t.description??"";return n.startsWith(w())&&n.slice(n.lastIndexOf("/")+1)||"unknown"}(e),b=performance.now(),v=O.current.model,g=l.current;let j,x=!1;function P(){const e=O.current.model,t=l.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function S(){const t=c===I?void 0:c;return D(e)?i?H(i.emitter,e,o,t):Promise.resolve():H(W(e)?s:k,e,o,t)}function E(e){x=!0;const t=J(C.current.handlers,"Error"),n=r.G.isNotNullable(t),c=function(e){return e instanceof Error&&"AbortError"===e.name?X.Aborted:X.Errored}(e),i=function(e){return e instanceof Error?e:new Error(String(e))}(e),u={reason:c,error:i,action:y,handled:n,tasks:a,retry:S};s.fire(G,u),n&&t&&k.emit(t,u),p({stage:"end",result:"error",action:{name:y,payload:o},details:{task:h.task,elapsed:performance.now()-b,mutations:P(),error:i,reason:c}})}function M(){for(const e of a)if(e===h.task){a.delete(e),U.current.delete(e);break}f.processes.forEach(e=>O.current.prune(e)),f.processes.size>0&&m(),x||p({stage:"end",result:"success",action:{name:y,payload:o},details:{task:h.task,elapsed:performance.now()-b,mutations:P()}}),d.resolve()}p({stage:"start",action:{name:y,payload:o},details:{task:h.task}});try{j=t(h,o)}catch(N){return E(N),M(),d.promise}return function(e){if(!e||"object"!=typeof e)return!1;const t=Object.prototype.toString.call(e);return"[object Generator]"===t||"[object AsyncGenerator]"===t}(j)?((async()=>{for await(const e of j);})().catch(E).finally(M),d.promise):(Promise.resolve(j).catch(E).finally(M),d.promise)}}L.current++;const t=new Set;return C.current.handlers.forEach((n,r)=>{for(const{getChannel:o,handler:c}of n){const n=e(r,c,o);if(D(r)){if(i){const e=i.emitter;e.on(r,n),t.add(()=>e.off(r,n))}k.on(r,n),N.multicast.add(r),t.add(()=>k.off(r,n))}else W(r)?(s.on(r,n),k.on(r,n),N.broadcast.add(r),t.add(()=>{s.off(r,n),k.off(r,n)})):(k.on(r,n),t.add(()=>k.off(r,n)))}}),()=>{const e=++L.current,n=new Set(t);queueMicrotask(()=>{if(L.current!==e){for(const e of n)e();return}for(const e of U.current)e.controller.abort(new Y("Component unmounted")),a.delete(e);U.current.clear(),_.current=M.Unmounting;const t=J(C.current.handlers,"Unmount");t&&k.emit(t),_.current=M.Unmounted;for(const e of n)e()})}},[k]),function({unicast:e,broadcast:t,dispatchers:n,scope:o,phase:s,data:i,handlers:a}){const u=c.useRef(null);c.useLayoutEffect(()=>{if(s.current===M.Mounted)return;s.current=M.Mounting;const c=J(a,"Mount");c&&e.emit(c),n.broadcast.forEach(n=>{const o=t.getCached(n);r.G.isNullable(o)||e.emit(n,o,I)}),o&&n.multicast.forEach(t=>{const n=o.emitter.getCached(t);r.G.isNullable(n)||e.emit(t,n,I)}),s.current=M.Mounted},[]),c.useLayoutEffect(()=>{if(r.G.isNotNullable(u.current)){const t=function(e,t){return Object.keys(t).reduce((n,r)=>e[r]!==t[r]?{...n,[r]:t[r]}:n,{})}(u.current,i);if(r.A.isNotEmpty(Object.keys(t))){const n=J(a,"Update");n&&e.emit(n,t)}}u.current=i},[i,e])}({unicast:k,broadcast:s,dispatchers:N,scope:i,phase:_,data:n(),handlers:C.current.handlers});const $=c.useMemo(()=>({dispatch(e,t){const n=B(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H(W(e)?s:k,n,t,r)},get inspect(){return O.current.inspect},stream:(e,t)=>c.createElement(xe,{action:B(e),renderer:t})}),[x,k]),z=c.useMemo(()=>[x,$,S],[x,$,S]);return z.useAction=(e,t)=>{!function(e,t,n){const r=c.useRef(n);c.useLayoutEffect(()=>{r.current=n});const o=c.useRef(t);c.useLayoutEffect(()=>{o.current=t});const s=c.useCallback((e,t)=>r.current(e,t),[]),i=c.useCallback(()=>F(o.current)?o.current.channel:void 0,[]),a=B(t),u=e.current.handlers.get(a)??new Set;0===u.size&&e.current.handlers.set(a,u),u.add({getChannel:i,handler:s})}(C,e,t)},z.dispatch=z[1].dispatch,z}(...t);return e.current=n.dispatch,n},with:{update:e=>Ge(e),invert:e=>Ee(e),always:(e,t)=>ke(e,t)}}),[])}function Me(e,n){return{Boundary:function({children:e}){const n=c.useMemo(()=>({id:Symbol("march-hare.scope/instance"),emitter:new d}),[]);return t.jsx(Z.Provider,{value:n,children:e})},useContext:function(){return Ce()},useEnv:function(){return g()},Resource:function(t){return se(t,e,n)}}}function Ne({holder:e}){const t=g();return e.current=t,null}const Ae={Update:e=>Ge(e),Invert:e=>Ee(e),Always:(e,t)=>ke(e,t)},_e=new Oe;function Re(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new Y);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new Y)},{once:!0})})}async function Ue(e,t,n){if(t?.aborted)throw new Y;for(;;){if(await n())return;await Re(e,t)}}function Le(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const Te=Object.freeze(Object.defineProperty({__proto__:null,pk:Le,poll:Ue,sleep:Re,unset:q,"ζ":Re,"κ":Le,"π":Ue},Symbol.toStringTag,{value:"Module"})),$e=Object.freeze(Object.defineProperty({__proto__:null,Resource:function(e){return se(e)},Scope:function(){return Me()},useContext:function(){return Ce()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));e.Aborted=Y,e.Action=(e="",t=C.Unicast)=>{const n=t===C.Broadcast?Symbol(j(e)):t===C.Multicast?Symbol(O(e)):Symbol(w(e)),r=function(t){return{[P.Action]:n,[P.Payload]:void 0,[P.Channel]:t,[P.Name]:e,channel:t}};return Object.defineProperty(r,P.Action,{value:n,enumerable:!1}),Object.defineProperty(r,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,P.Name,{value:e,enumerable:!1}),t===C.Broadcast&&Object.defineProperty(r,P.Broadcast,{value:!0,enumerable:!1}),t===C.Multicast&&Object.defineProperty(r,P.Multicast,{value:!0,enumerable:!1}),r},e.App=function(e){const n={current:void 0};return{Boundary:function({children:r}){return t.jsxs(T,{env:e?.env,tap:e?.tap,children:[t.jsx(Ne,{holder:n}),r]})},useContext:function(){return Ce()},useEnv:function(){return g()},Resource:function(t){return se(t,e?.cache,()=>n.current)},Scope:()=>Me(e?.cache,()=>n.current)}},e.Boundary=T,e.Cache=ee,e.Distribution=C,e.Lifecycle=k,e.Op=ue,e.Operation=ue,e.Reason=X,e.State=Oe,e.With=Ae,e.annotate=function(e,t=ue.Update){return _e.annotate(t,e)},e.isBox=function(e){return r.G.isObject(e)&&he in e},e.shared=$e,e.utils=Te,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})},"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("react/jsx-runtime"),require("react"),require("@mobily/ts-belt"),require("immer")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","react","@mobily/ts-belt","immer"],factory):factory((global="undefined"!=typeof globalThis?globalThis:global||self).MarchHare={},global.jsxRuntime,global.React,global.TsBelt,global.Immer);
1
+ var global,factory;global=this,factory=function(e,t,n,r,o){"use strict";function s(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e)for(const n in e)if("default"!==n){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}return t.default=e,Object.freeze(t)}const c=s(n);function i(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var a,u={exports:{}},l=(a||(a=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,r,s,c){if("function"!=typeof r)throw new TypeError("The listener must be a function");var i=new o(r,s||e,c),a=n?n+t:t;return e._events[a]?e._events[a].fn?e._events[a]=[e._events[a],i]:e._events[a].push(i):(e._events[a]=i,e._eventsCount++),e}function c(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function i(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),i.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},i.prototype.listeners=function(e){var t=this._events[n?n+e:e];if(!t)return[];if(t.fn)return[t.fn];for(var r=0,o=t.length,s=new Array(o);r<o;r++)s[r]=t[r].fn;return s},i.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},i.prototype.emit=function(e,t,r,o,s,c){var i=n?n+e:e;if(!this._events[i])return!1;var a,u,l=this._events[i],f=arguments.length;if(l.fn){switch(l.once&&this.removeListener(e,l.fn,void 0,!0),f){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,t),!0;case 3:return l.fn.call(l.context,t,r),!0;case 4:return l.fn.call(l.context,t,r,o),!0;case 5:return l.fn.call(l.context,t,r,o,s),!0;case 6:return l.fn.call(l.context,t,r,o,s,c),!0}for(u=1,a=new Array(f-1);u<f;u++)a[u-1]=arguments[u];l.fn.apply(l.context,a)}else{var d,p=l.length;for(u=0;u<p;u++)switch(l[u].once&&this.removeListener(e,l[u].fn,void 0,!0),f){case 1:l[u].fn.call(l[u].context);break;case 2:l[u].fn.call(l[u].context,t);break;case 3:l[u].fn.call(l[u].context,t,r);break;case 4:l[u].fn.call(l[u].context,t,r,o);break;default:if(!a)for(d=1,a=new Array(f-1);d<f;d++)a[d-1]=arguments[d];l[u].fn.apply(l[u].context,a)}}return!0},i.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},i.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},i.prototype.removeListener=function(e,t,r,o){var s=n?n+e:e;if(!this._events[s])return this;if(!t)return c(this,s),this;var i=this._events[s];if(i.fn)i.fn!==t||o&&!i.once||r&&i.context!==r||c(this,s);else{for(var a=0,u=[],l=i.length;a<l;a++)(i[a].fn!==t||o&&!i[a].once||r&&i[a].context!==r)&&u.push(i[a]);u.length?this._events[s]=1===u.length?u[0]:u:c(this,s)}return this},i.prototype.removeAllListeners=function(e){var t;return e?this._events[t=n?n+e:e]&&c(this,t):(this._events=new r,this._eventsCount=0),this},i.prototype.off=i.prototype.removeListener,i.prototype.addListener=i.prototype.on,i.prefixed=n,i.EventEmitter=i,e.exports=i}(u)),u.exports);const f=i(l);class d extends f{cache=new Map;emit(e,...t){return this.cache.set(e,t[0]),super.emit(e,...t)}setCache(e,t){this.cache.set(e,t)}getCached(e){return this.cache.get(e)}fire(e,...t){return super.emit(e,...t)}}const p=c.createContext(new d);function h(){return c.useContext(p)}function m({children:e}){const n=c.useMemo(()=>new d,[]);return t.jsx(p.Provider,{value:n,children:e})}const y=c.createContext(new Set);function b({children:e}){const n=c.useMemo(()=>new Set,[]);return t.jsx(y.Provider,{value:n,children:e})}const v=c.createContext({current:{}});function g(){const e=c.useContext(v);return c.useMemo(()=>new Proxy({},{get:(t,n)=>Reflect.get(e.current,n),has:(t,n)=>n in e.current,ownKeys:()=>Reflect.ownKeys(e.current),getOwnPropertyDescriptor(t,n){const o=Object.getOwnPropertyDescriptor(e.current,n);if(!r.G.isUndefined(o))return{...o,configurable:!0}},set(){throw new TypeError("Env is read-only outside `context.actions.produce`. Mutate via produce(({ env }) => { env.x = ... }) instead.")}}),[e])}const w=(e="")=>`march-hare.action/${e}`,j=(e="")=>`march-hare.action/broadcast/${e}`,O=(e="")=>`march-hare.action/multicast/${e}`,P=(e="")=>`march-hare.action.lifecycle/${e}`;class x{static Payload=Symbol("march-hare.brand/Payload");static Broadcast=Symbol("march-hare.brand/Broadcast");static Multicast=Symbol("march-hare.brand/Multicast");static Action=Symbol("march-hare.brand/Action");static Channel=Symbol("march-hare.brand/Channel");static Name=Symbol("march-hare.brand/Name");static Lifecycle=Symbol("march-hare.brand/Lifecycle")}function S(e){const t=Symbol(`march-hare.action.lifecycle/${e}`),n=function(n){return{[x.Action]:t,[x.Payload]:void 0,[x.Channel]:n,[x.Name]:e,channel:n}};return Object.defineProperty(n,x.Action,{value:t,enumerable:!1}),Object.defineProperty(n,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(n,x.Name,{value:e,enumerable:!1}),Object.defineProperty(n,x.Lifecycle,{value:e,enumerable:!1}),n}const G=Symbol(j("Fault")),E=Symbol(j("Env"));class k{static Mount(){return S("Mount")}static Paint(){return S("Paint")}static Unmount(){return S("Unmount")}static Error(){return S("Error")}static Update(){return S("Update")}static Fault=(()=>{const e={};return Object.defineProperty(e,x.Action,{value:G,enumerable:!1}),Object.defineProperty(e,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,x.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,x.Name,{value:"Fault",enumerable:!1}),e})();static Env=(()=>{const e={};return Object.defineProperty(e,x.Action,{value:E,enumerable:!1}),Object.defineProperty(e,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,x.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,x.Name,{value:"Env",enumerable:!1}),e})()}var C=(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(C||{}),M=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(M||{});function N({initial:e,children:n}){const o=c.useRef(e),s=h();return r.G.isUndefined(s.getCached(E))&&s.setCache(E,o.current),t.jsx(v.Provider,{value:o,children:n})}const A=c.createContext(new WeakMap);function _({children:e}){const n=c.useMemo(()=>new WeakMap,[]);return t.jsx(A.Provider,{value:n,children:e})}const R=c.createContext(()=>{});function U({tap:e,children:n}){const r=c.useRef(e);c.useLayoutEffect(()=>{r.current=e},[e]);const o=c.useMemo(()=>e=>r.current?.(e),[]);return t.jsx(R.Provider,{value:o,children:n})}const L=c.createContext(new Map);function T({env:e,tap:n,children:r}){const o=c.useMemo(()=>new Map,[]);return t.jsx(m,{children:t.jsx(L.Provider,{value:o,children:t.jsx(N,{initial:e??{},children:t.jsx(b,{children:t.jsx(U,{tap:n,children:t.jsx(_,{children:r})})})})})})}const $=e=>"symbol"==typeof e;function B(e){return r.G.isString(e)||$(e)?e:(r.G.isObject(e)||r.G.isFunction(e))&&x.Action in e?e[x.Action]:e}function W(e){if(r.G.isString(e))return e.startsWith(j());if($(e))return e.description?.startsWith(j())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(x.Broadcast in e&&e[x.Broadcast])return!0;if(x.Action in e){const t=e[x.Action];return t.description?.startsWith(j())??!1}}return!1}function F(e){return r.G.isObject(e)&&x.Channel in e&&"channel"in e}function z(e){const t=B(e),n=$(t)?t.description??"":t;return n.startsWith(P())&&n.slice(P().length)||null}function D(e){if(r.G.isString(e))return e.startsWith(O());if($(e))return e.description?.startsWith(O())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(x.Multicast in e&&e[x.Multicast])return!0;if(x.Action in e){const t=e[x.Action];return t.description?.startsWith(O())??!1}}return!1}const I=Symbol(((e="")=>`march-hare/replay${e}`)());function H(e,t,...n){e instanceof d&&e.setCache(t,n[0]);const r=e.listeners(t);return 0===r.length?Promise.resolve():Promise.all(r.map(e=>Promise.resolve(e(...n)))).then(()=>{})}function J(e,t){for(const n of e.keys())if(z(n)===t)return n;return null}const q=Symbol("march-hare.unset");function K(){const[,e]=c.useReducer(e=>e+1,0);return e}function V(){return{data:q,at:null}}function Q(e,t){return{data:e,at:t}}var X=(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(X||{});class Y extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Z=c.createContext(null);function ee(e){const t=r.G.isUndefined(e)?function(){const e=new Map;return{get:t=>e.get(t)??null,set:(t,n)=>{e.set(t,n)},remove:t=>{e.delete(t)},clear:()=>{e.clear()},keys:()=>e.keys()}}():e,n=e?.key;return{get(e){try{const n=t.get(e);if(r.G.isNull(n))return V();const o=JSON.parse(n);return Q(o.data,Temporal.Instant.from(o.at))}catch{return V()}},set(e,n){if(n.data!==q&&!r.G.isNull(n.at))try{t.set(e,JSON.stringify({data:n.data,at:n.at.toString()}))}catch{return}},remove(e){try{t.remove(e)}catch{return}},clear(){try{t.clear()}catch{return}},keys(){try{return t.keys?.()??[]}catch{return[]}},scope(e){if(r.G.isUndefined(n)||r.G.isNullable(e))return"";try{const t=n({env:e});return r.G.isNullable(t)?"":t}catch{return""}}}}const te=new WeakMap,ne=new Map,re=[];function oe(e,t,n,o){const s=r.G.isNull(n)?"":`${n}:`,c=(e,n)=>{const r=t.scope(e);return`${""===r?"":`${r}:`}${s}${function(e){return JSON.stringify(e)}(n)}`},i=(e,n)=>{const o=t.get(c(n,e));return o.data===q||r.G.isNull(o.at)?{data:q,at:null}:{data:o.data,at:o.at}},a=(n,r,o,s)=>e({env:n,controller:r,params:o,dispatch:s}).then(e=>(t.set(c(n,o),Q(e,Temporal.Now.instant())),e)),u=e=>{const n=o(),r=t.scope(n),c=""===r?s:`${r}:${s}`,i=Object.entries(e);for(const o of[...t.keys()])if(o.startsWith(c))try{const e=JSON.parse(o.slice(c.length));i.every(([t,n])=>e[t]===n)&&t.remove(o)}catch{continue}};function l(e){return{run:a,read:e=>i(e,o()),evict:u,params:e??{}}}return re.push(u),Object.defineProperty(l,"get",{value:function(e){const{data:t}=i(e??{},o());return t===q?null:t},enumerable:!1}),l}function se(e,t,n){const o=e,s=n??(()=>{});return r.G.isUndefined(t)?oe(o,function(e){const t=te.get(e);if(r.G.isNotNullable(t))return t;const n=ee();return te.set(e,n),n}(o),null,s):oe(o,t,function(e){const t=ne.get(e);if(r.G.isNotNullable(t))return t;const n=String(ne.size);return ne.set(e,n),n}(o),s)}const ce=Symbol("march-hare.coalesce/default");function ie(e,t){return new Promise((n,r)=>{if(t.aborted)return void r(t.reason);const o=()=>r(t.reason);t.addEventListener("abort",o,{once:!0}),e.then(e=>{t.removeEventListener("abort",o),n(e)},e=>{t.removeEventListener("abort",o),r(e)})})}let ae=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var ue=(e=>(e[e.Add=1]="Add",e[e.Remove=2]="Remove",e[e.Update=4]="Update",e[e.Move=8]="Move",e[e.Replace=16]="Replace",e[e.Sort=32]="Sort",e[e.Create=64]="Create",e[e.Fetch=128]="Fetch",e[e.Clone=256]="Clone",e[e.Archive=512]="Archive",e[e.Restore=1024]="Restore",e[e.Merge=2048]="Merge",e[e.Reorder=4096]="Reorder",e[e.Sync=8192]="Sync",e[e.Publish=16384]="Publish",e[e.Link=32768]="Link",e[e.Unlink=65536]="Unlink",e[e.Lock=131072]="Lock",e[e.Unlock=262144]="Unlock",e[e.Import=524288]="Import",e[e.Export=1048576]="Export",e[e.Transfer=2097152]="Transfer",e))(ue||{}),le=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(le||{}),fe=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(fe||{});class de{[o.immerable]=!0;static keys=new Set(Object.values(fe));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new de(this.value,this.operation);return n.property=e,n.process=t,n}}class pe{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=ae}const he=Symbol("Box");function me(e,t){const n="string"==typeof t?""===t?[]:t.split("."):t;let r=e;for(const o of n){if(null==r)return;r=r[o]}return r}function ye(e){if(r.G.isNullable(e)||ge(e))return e;if(r.G.isArray(e))return e.map(e=>ye(e));if(r.G.isObject(e)&&ve(e)){const t=Object.entries(e).map(([e,t])=>[e,ye(t)]);return{...Object.fromEntries(t),[pe.tag]:e[pe.tag]??pe.id()}}return e}function be(e){if(Array.isArray(e))return e.filter(e=>pe.tag in e).map(e=>e[pe.tag]??"").join(",");const t=e[pe.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function ve(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function ge(e){return r.G.isNullable(e)||r.G.isString(e)||r.G.isNumber(e)||r.G.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function we(e,t,n,o,s,c){return function i(a,u=t.path){if(a instanceof de){const t=me(n,u.join("."));if(Object.entries(a).filter(([e,t])=>!de.keys.has(e)&&t instanceof de).forEach(([e,t])=>i(t,u.concat(e))),ge(a.value)){if(e===le.Hydrate)return a.value;const i=u.slice(0,-1),l=i.length>0?me(n,i.join(".")):n;return r.G.isNullable(l)||je(l,a,u.at(-1),o,s,c),t??a.value}if(e===le.Hydrate){const e=ye(i(a.value,u));return je(e,a,null,o,s,c),e}const l=t??ye(a.value);return je(l,a,null,o,s,c),r.G.isNullable(t)?l:(i(a.value,u),t)}if(r.G.isArray(a))return a.map((e,t)=>i(e,u.concat(t)));if(r.G.isObject(a)&&!ve(a))return a;if(r.G.isObject(a)){const t=Object.entries(a).map(([e,t])=>[e,i(t,u.concat(e))]),n=Object.fromEntries(t);if(e===le.Hydrate){const e=ye(n);return Object.entries(a).forEach(([t,n])=>{n instanceof de&&ge(n.value)&&je(e,n,t,o,s,c)}),e}return n}return a}(t.value)}function je(e,t,n,r,o,s){const c=s(e),i=o.get(c)??[];o.set(c,[t.assign(n,r),...i])}class Oe{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=be){this.#t=e}static pk(){return ae()}static"κ"=Oe.pk;annotate(e,t){return new de(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,t,n,o,s){function c(o){const s=o.at(-1),c=me(e(),o),i=o.slice(0,-1),a=r.A.isNotEmpty(i)?me(e(),i):e();return[...r.G.isObject(c)||r.G.isArray(c)?t.get(n(c))?.filter(e=>r.G.isNullable(e.property))??[]:[],...r.G.isObject(a)?t.get(n(a))?.filter(e=>e.property===s)??[]:[]]}return function t(n){return new Proxy(()=>{},{get:(i,a)=>"pending"===a?()=>!r.A.isEmpty(c(n)):"remaining"===a?()=>r.A.length(c(n)):"box"===a?()=>({value:me(e(),n),inspect:t(n),[he]:!0}):"is"===a?e=>c(n).some(t=>0!==(t.operation&e)):"draft"===a?()=>r.A.head(c(n))?.value??me(e(),n):"settled"===a?()=>new Promise(t=>{if(r.A.isEmpty(c(n)))return t(me(e(),n));const i=()=>{r.A.isEmpty(c(n))&&(s(i),t(me(e(),n)))};o(i)}):t([...n,String(a)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#s(le.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(le.Produce,e)}#s(e,t){const n=Symbol("process"),[,r]=pe.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>pe.immer.applyPatches(t,[{...r,value:we(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=ye(this.#e),this.#c(),n}prune(e){this.#n.forEach((t,n)=>{const o=t.filter(t=>t.process!==e);r.A.isEmpty(o)?this.#n.delete(n):this.#n.set(n,o)}),this.#c()}#c(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}function Pe({action:e,renderer:t}){const n=h(),o=c.useContext(L),s=K(),i=c.useMemo(()=>{const t=o.get(e);if(t)return t;const s=new Oe,c=n.getCached(e);r.G.isNotNullable(c)&&s.hydrate({value:c});const i={state:s,listeners:new Set};return o.set(e,i),i},[e,n,o]);c.useLayoutEffect(()=>{function t(e){i.state.hydrate({value:e}),i.listeners.forEach(e=>e())}return i.listeners.add(s),n.on(e,t),()=>{i.listeners.delete(s),n.off(e,t)}},[e,n,i]);const a=i.state.model?.value;return r.G.isNullable(a)?null:t(a,i.state.inspect.value)}function xe(e,t){const n=t.split(".");let r=e;for(let o=0;o<n.length-1;o++)r=r[n[o]];return{cursor:r,key:n[n.length-1]}}function Se(e,t,n){const{cursor:r,key:o}=xe(e,t);r[o]=n}function Ge(e){return(t,n)=>{t.actions.produce(t=>{Se(t.model,e,n)})}}function Ee(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=xe(e,t);n[r]=!n[r]}(t.model,e)})}}function ke(e,t){return n=>{n.actions.produce(n=>{Se(n.model,e,t)})}}function Ce(){const e=c.useRef(null);return c.useMemo(()=>({actions:{dispatch:function(t,n){const r=e.current;if(!r)throw new Error("march-hare: useContext handle dispatched before its paired context.useActions(...) ran. Call context.actions.dispatch from event handlers, not synchronously during render.");return r(t,n)}},useActions:function(...t){const n=function(...e){const t=r.G.isUndefined(e[0])||r.G.isFunction(e[0])?{}:e[0],n=r.G.isFunction(e[0])?e[0]:e[1]??(()=>({})),s=h(),i=c.useContext(Z),a=c.useContext(y),u=g(),l=c.useContext(v),d=c.useContext(A),p=c.useContext(R),m=K(),b=c.useRef(!1),j=c.useRef(null),O=c.useRef(new Oe);b.current||(b.current=!0,j.current=O.current.hydrate(t));const[P,x]=c.useState(()=>O.current.model),S=function(e){const t=c.useRef(e);return t.current=e,c.useMemo(()=>{return n=t,Object.keys(e).reduce((e,t)=>(Object.defineProperty(e,t,{get:()=>n.current[t],enumerable:!0}),e),{});var n},[e])}(n()),k=c.useMemo(()=>new f,[]),C=c.useRef({handlers:new Map});C.current.handlers=new Map;const N=function(){const e=c.useRef(new Set),t=c.useRef(new Set);return c.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),_=c.useRef(M.Mounting),U=c.useRef(new Set),L=c.useRef(0),T=c.useCallback((e,t,n)=>{const c=new AbortController,f={controller:c,action:e,payload:t};return a.add(f),U.current.add(f),{model:O.current.model,get phase(){return _.current},task:f,data:S,tasks:a,env:u,actions:{produce(e){if(c.signal.aborted)return;const t=l.current,r=O.current.produce(t=>{l.current=o.produce(l.current,n=>{e({model:t,inspect:O.current.inspect,env:n})})});x(O.current.model),l.current!==t&&s.emit(E,l.current),n.processes.add(r),j.current&&(n.processes.add(j.current),j.current=null)},dispatch(e,t){if(c.signal.aborted)return Promise.resolve();const n=B(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H(W(e)?s:k,n,t,r)},annotate:(e,t=ue.Update)=>O.current.annotate(t,e),get inspect(){return O.current.inspect},resource:Object.assign(function(e){const t=(e,t)=>{if(c.signal.aborted)return Promise.resolve();const n=e,r=B(n);return D(n)?i?H(i.emitter,r,t,void 0):Promise.resolve():W(n)?H(s,r,t,void 0):Promise.resolve()},n={exceedsWindow:null,coalesceToken:void 0},o={then:(o,s)=>(()=>{if(r.G.isNotNullable(n.exceedsWindow)){const{data:t,at:o}=e.read(e.params);if(t!==q&&r.G.isNotNullable(o)){const e=Temporal.Now.instant().since(o),r=Temporal.Duration.from(n.exceedsWindow);if(Temporal.Duration.compare(e,r)<=0)return Promise.resolve(t)}}if(r.G.isUndefined(n.coalesceToken))return e.run(u,c,e.params,t);let o=d.get(e.run);r.G.isUndefined(o)&&(o=new Map,d.set(e.run,o));const s=o,i=`${JSON.stringify(e.params)}|${function(e){switch(typeof e){case"string":return`s:${e}`;case"number":return`n:${e}`;case"bigint":return`i:${e.toString()}`;case"boolean":return`b:${e}`;case"symbol":return`y:${e.description??String(e)}`;default:return`o:${JSON.stringify(e)}`}}(n.coalesceToken)}`,a=s.get(i);if(a)return ie(a,c.signal);const l=new AbortController,f=e.run(u,l,e.params,t).finally(()=>{s.delete(i)});return s.set(i,f),ie(f,c.signal)})().then(o,s),exceeds:e=>(n.exceedsWindow=e,o),coalesce:e=>(n.coalesceToken=e??ce,o),evict(t){e.evict(t??e.params)}};return o},{nuke:e=>function(e){const t=e??{};for(const n of re)n(t)}(e)}),async final(e){if(c.signal.aborted)return null;const t=B(e),n=D(e)?i?.emitter??null:s;if(!n)return null;const o=n.getCached(t);if(r.G.isUndefined(o))return null;const a=O.current.inspect;return a.pending()&&await new Promise((e,t)=>{if(c.signal.aborted)return void t(c.signal.reason);const n=()=>t(c.signal.reason);c.signal.addEventListener("abort",n,{once:!0}),a.settled().then(()=>{c.signal.removeEventListener("abort",n),e()})}),n.getCached(t)??null},peek(e){if(c.signal.aborted)return null;const t=B(e),n=D(e)?i?.emitter??null:s;return n?n.getCached(t)??null:null}}}},[P]);c.useLayoutEffect(()=>{function e(e,t,n){return function(o,c){const u=n();if(c===I&&r.G.isNotNullable(u))return;if(r.G.isNotNullable(c)&&c!==I&&r.G.isNotNullable(u)&&!function(e,t){for(const n of Object.keys(e))if(t[n]!==e[n])return!1;return!0}(c,u))return;const f={processes:new Set},d=Promise.withResolvers(),h=T(e,o,f),y=function(e){const t=B(e),n=r.G.isString(t)?t:t.description??"";return n.startsWith(w())&&n.slice(n.lastIndexOf("/")+1)||"unknown"}(e),b=performance.now(),v=O.current.model,g=l.current;let j,P=!1;function x(){const e=O.current.model,t=l.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function S(){const t=c===I?void 0:c;return D(e)?i?H(i.emitter,e,o,t):Promise.resolve():H(W(e)?s:k,e,o,t)}function E(e){P=!0;const t=J(C.current.handlers,"Error"),n=r.G.isNotNullable(t),c=function(e){return e instanceof Error&&"AbortError"===e.name?X.Aborted:X.Errored}(e),i=function(e){return e instanceof Error?e:new Error(String(e))}(e),u={reason:c,error:i,action:y,handled:n,tasks:a,retry:S};s.fire(G,u),n&&t&&k.emit(t,u),p({stage:"end",result:"error",action:{name:y,payload:o},details:{task:h.task,elapsed:performance.now()-b,mutations:x(),error:i,reason:c}})}function M(){for(const e of a)if(e===h.task){a.delete(e),U.current.delete(e);break}f.processes.forEach(e=>O.current.prune(e)),f.processes.size>0&&m(),P||p({stage:"end",result:"success",action:{name:y,payload:o},details:{task:h.task,elapsed:performance.now()-b,mutations:x()}}),d.resolve()}p({stage:"start",action:{name:y,payload:o},details:{task:h.task}});try{j=t(h,o)}catch(N){return E(N),M(),d.promise}return function(e){if(!e||"object"!=typeof e)return!1;const t=Object.prototype.toString.call(e);return"[object Generator]"===t||"[object AsyncGenerator]"===t}(j)?((async()=>{for await(const e of j);})().catch(E).finally(M),d.promise):(Promise.resolve(j).catch(E).finally(M),d.promise)}}L.current++;const t=new Set;return C.current.handlers.forEach((n,r)=>{for(const{getChannel:o,handler:c}of n){const n=e(r,c,o);if(D(r)){if(i){const e=i.emitter;e.on(r,n),t.add(()=>e.off(r,n))}k.on(r,n),N.multicast.add(r),t.add(()=>k.off(r,n))}else W(r)?(s.on(r,n),k.on(r,n),N.broadcast.add(r),t.add(()=>{s.off(r,n),k.off(r,n)})):(k.on(r,n),t.add(()=>k.off(r,n)))}}),()=>{const e=++L.current,n=new Set(t);queueMicrotask(()=>{if(L.current!==e){for(const e of n)e();return}for(const e of U.current)e.controller.abort(new Y("Component unmounted")),a.delete(e);U.current.clear(),_.current=M.Unmounting;const t=J(C.current.handlers,"Unmount");t&&k.emit(t),_.current=M.Unmounted;for(const e of n)e()})}},[k]),function({unicast:e,broadcast:t,dispatchers:n,scope:o,phase:s,data:i,handlers:a}){const u=c.useRef(null),l=c.useRef(!1);c.useLayoutEffect(()=>{if(s.current===M.Mounted)return;s.current=M.Mounting;const c=J(a,"Mount");c&&e.emit(c),n.broadcast.forEach(n=>{const o=t.getCached(n);r.G.isNullable(o)||e.emit(n,o,I)}),o&&n.multicast.forEach(t=>{const n=o.emitter.getCached(t);r.G.isNullable(n)||e.emit(t,n,I)}),s.current=M.Mounted,l.current=!1},[]),c.useEffect(()=>{if(l.current)return;l.current=!0;const t=J(a,"Paint");t&&e.emit(t)},[]),c.useLayoutEffect(()=>{if(r.G.isNotNullable(u.current)){const t=function(e,t){return Object.keys(t).reduce((n,r)=>e[r]!==t[r]?{...n,[r]:t[r]}:n,{})}(u.current,i);if(r.A.isNotEmpty(Object.keys(t))){const n=J(a,"Update");n&&e.emit(n,t)}}u.current=i},[i,e])}({unicast:k,broadcast:s,dispatchers:N,scope:i,phase:_,data:n(),handlers:C.current.handlers});const $=c.useMemo(()=>({dispatch(e,t){const n=B(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H(W(e)?s:k,n,t,r)},get inspect(){return O.current.inspect},stream:(e,t)=>c.createElement(Pe,{action:B(e),renderer:t})}),[P,k]),z=c.useMemo(()=>[P,$,S],[P,$,S]);return z.useAction=(e,t)=>{!function(e,t,n){const r=c.useRef(n);c.useLayoutEffect(()=>{r.current=n});const o=c.useRef(t);c.useLayoutEffect(()=>{o.current=t});const s=c.useCallback((e,t)=>r.current(e,t),[]),i=c.useCallback(()=>F(o.current)?o.current.channel:void 0,[]),a=B(t),u=e.current.handlers.get(a)??new Set;0===u.size&&e.current.handlers.set(a,u),u.add({getChannel:i,handler:s})}(C,e,t)},z.dispatch=z[1].dispatch,z}(...t);return e.current=n.dispatch,n},with:{update:e=>Ge(e),invert:e=>Ee(e),always:(e,t)=>ke(e,t)}}),[])}function Me(e,n){return{Boundary:function({children:e}){const n=c.useMemo(()=>({id:Symbol("march-hare.scope/instance"),emitter:new d}),[]);return t.jsx(Z.Provider,{value:n,children:e})},useContext:function(){return Ce()},useEnv:function(){return g()},Resource:function(t){return se(t,e,n)}}}function Ne({holder:e}){const t=g();return e.current=t,null}const Ae={Update:e=>Ge(e),Invert:e=>Ee(e),Always:(e,t)=>ke(e,t)},_e=new Oe;function Re(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new Y);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new Y)},{once:!0})})}async function Ue(e,t,n){if(t?.aborted)throw new Y;for(;;){if(await n())return;await Re(e,t)}}function Le(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const Te=Object.freeze(Object.defineProperty({__proto__:null,pk:Le,poll:Ue,sleep:Re,unset:q,"ζ":Re,"κ":Le,"π":Ue},Symbol.toStringTag,{value:"Module"})),$e=Object.freeze(Object.defineProperty({__proto__:null,Resource:function(e){return se(e)},Scope:function(){return Me()},useContext:function(){return Ce()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));e.Aborted=Y,e.Action=(e="",t=C.Unicast)=>{const n=t===C.Broadcast?Symbol(j(e)):t===C.Multicast?Symbol(O(e)):Symbol(w(e)),r=function(t){return{[x.Action]:n,[x.Payload]:void 0,[x.Channel]:t,[x.Name]:e,channel:t}};return Object.defineProperty(r,x.Action,{value:n,enumerable:!1}),Object.defineProperty(r,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,x.Name,{value:e,enumerable:!1}),t===C.Broadcast&&Object.defineProperty(r,x.Broadcast,{value:!0,enumerable:!1}),t===C.Multicast&&Object.defineProperty(r,x.Multicast,{value:!0,enumerable:!1}),r},e.App=function(e){const n={current:void 0};return{Boundary:function({children:r}){return t.jsxs(T,{env:e?.env,tap:e?.tap,children:[t.jsx(Ne,{holder:n}),r]})},useContext:function(){return Ce()},useEnv:function(){return g()},Resource:function(t){return se(t,e?.cache,()=>n.current)},Scope:()=>Me(e?.cache,()=>n.current)}},e.Boundary=T,e.Cache=ee,e.Distribution=C,e.Lifecycle=k,e.Op=ue,e.Operation=ue,e.Reason=X,e.State=Oe,e.With=Ae,e.annotate=function(e,t=ue.Update){return _e.annotate(t,e)},e.isBox=function(e){return r.G.isObject(e)&&he in e},e.shared=$e,e.utils=Te,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})},"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("react/jsx-runtime"),require("react"),require("@mobily/ts-belt"),require("immer")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","react","@mobily/ts-belt","immer"],factory):factory((global="undefined"!=typeof globalThis?globalThis:global||self).MarchHare={},global.jsxRuntime,global.React,global.TsBelt,global.Immer);
2
2
  //# sourceMappingURL=march-hare.umd.cjs.map