march-hare 0.16.0 → 0.17.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.
@@ -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 b=c.createContext(new Set);function y({children:e}){const n=c.useMemo(()=>new Set,[]);return t.jsx(b.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 E=Symbol(j("Fault")),G=Symbol(j("Env"));class C{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: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:"Fault",enumerable:!1}),e})();static Env=(()=>{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:"Env",enumerable:!1}),e})()}var M=(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(M||{}),N=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(N||{});function k({initial:e,children:n}){const o=c.useRef(e),s=h();return r.G.isUndefined(s.getCached(G))&&s.setCache(G,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(k,{initial:e??{},children:t.jsx(y,{children:t.jsx(U,{tap:n,children:t.jsx(_,{children:r})})})})})})}const B=e=>"symbol"==typeof e;function W(e){return r.G.isString(e)||B(e)?e:(r.G.isObject(e)||r.G.isFunction(e))&&x.Action in e?e[x.Action]:e}function $(e){if(r.G.isString(e))return e.startsWith(j());if(B(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=W(e),n=B(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(B(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)}let ce=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var ie=(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))(ie||{}),ae=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(ae||{}),ue=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(ue||{});class le{[o.immerable]=!0;static keys=new Set(Object.values(ue));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new le(this.value,this.operation);return n.property=e,n.process=t,n}}class fe{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=ce}const de=Symbol("Box");function pe(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 he(e){if(r.G.isNullable(e)||ye(e))return e;if(r.G.isArray(e))return e.map(e=>he(e));if(r.G.isObject(e)&&be(e)){const t=Object.entries(e).map(([e,t])=>[e,he(t)]);return{...Object.fromEntries(t),[fe.tag]:e[fe.tag]??fe.id()}}return e}function me(e){if(Array.isArray(e))return e.filter(e=>fe.tag in e).map(e=>e[fe.tag]??"").join(",");const t=e[fe.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function be(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function ye(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 ve(e,t,n,o,s,c){return function i(a,u=t.path){if(a instanceof le){const t=pe(n,u.join("."));if(Object.entries(a).filter(([e,t])=>!le.keys.has(e)&&t instanceof le).forEach(([e,t])=>i(t,u.concat(e))),ye(a.value)){if(e===ae.Hydrate)return a.value;const i=u.slice(0,-1),l=i.length>0?pe(n,i.join(".")):n;return r.G.isNullable(l)||ge(l,a,u.at(-1),o,s,c),t??a.value}if(e===ae.Hydrate){const e=he(i(a.value,u));return ge(e,a,null,o,s,c),e}const l=t??he(a.value);return ge(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)&&!be(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===ae.Hydrate){const e=he(n);return Object.entries(a).forEach(([t,n])=>{n instanceof le&&ye(n.value)&&ge(e,n,t,o,s,c)}),e}return n}return a}(t.value)}function ge(e,t,n,r,o,s){const c=s(e),i=o.get(c)??[];o.set(c,[t.assign(n,r),...i])}class we{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=me){this.#t=e}static pk(){return ce()}static"κ"=we.pk;annotate(e,t){return new le(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=pe(e(),o),i=o.slice(0,-1),a=r.A.isNotEmpty(i)?pe(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:pe(e(),n),inspect:t(n),[de]:!0}):"is"===a?e=>c(n).some(t=>0!==(t.operation&e)):"draft"===a?()=>r.A.head(c(n))?.value??pe(e(),n):"settled"===a?()=>new Promise(t=>{if(r.A.isEmpty(c(n)))return t(pe(e(),n));const i=()=>{r.A.isEmpty(c(n))&&(s(i),t(pe(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(ae.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(ae.Produce,e)}#s(e,t){const n=Symbol("process"),[,r]=fe.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>fe.immer.applyPatches(t,[{...r,value:ve(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=he(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 je({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 we,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 Oe(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 Pe(e,t,n){const{cursor:r,key:o}=Oe(e,t);r[o]=n}function xe(e){return(t,n)=>{t.actions.produce(t=>{Pe(t.model,e,n)})}}function Se(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=Oe(e,t);n[r]=!n[r]}(t.model,e)})}}function Ee(e,t){return n=>{n.actions.produce(n=>{Pe(n.model,e,t)})}}function Ge(){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(b),u=g(),l=c.useContext(v),d=c.useContext(A),p=c.useContext(R),m=K(),y=c.useRef(!1),j=c.useRef(null),O=c.useRef(new we);y.current||(y.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()),C=c.useMemo(()=>new f,[]),M=c.useRef({handlers:new Map});M.current.handlers=new Map;const k=function(){const e=c.useRef(new Set),t=c.useRef(new Set);return c.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),_=c.useRef(N.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),{get model(){return 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(G,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=W(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H($(e)?s:C,n,t,r)},annotate:(e,t=ie.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=W(n);return D(n)?i?H(i.emitter,r,t,void 0):Promise.resolve():$(n)?H(s,r,t,void 0):Promise.resolve()},n={exceedsWindow:null,isolated:!1},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(n.isolated)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);let a=s.get(i);if(r.G.isUndefined(a)){const n=new AbortController,r={controller:n,refs:0};r.promise=e.run(u,n,e.params,t).finally(()=>{s.delete(i)}),s.set(i,r),a=r}const l=a;l.refs+=1;const f=()=>{l.refs-=1,0===l.refs&&(s.delete(i),l.controller.abort(c.signal.reason))};if(c.signal.aborted)f();else{c.signal.addEventListener("abort",f,{once:!0});const e=()=>c.signal.removeEventListener("abort",f);l.promise.then(e,e)}return p=l.promise,h=c.signal,new Promise((e,t)=>{if(h.aborted)return void t(h.reason);const n=()=>t(h.reason);h.addEventListener("abort",n,{once:!0}),p.then(t=>{h.removeEventListener("abort",n),e(t)},e=>{h.removeEventListener("abort",n),t(e)})});var p,h})().then(o,s),exceeds:e=>(n.exceedsWindow=e,o),isolated:()=>(n.isolated=!0,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=W(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=W(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),b=function(e){const t=W(e),n=r.G.isString(t)?t:t.description??"";return n.startsWith(w())&&n.slice(n.lastIndexOf("/")+1)||"unknown"}(e),y=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($(e)?s:C,e,o,t)}function G(e){P=!0;const t=J(M.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:b,handled:n,tasks:a,retry:S};s.fire(E,u),n&&t&&C.emit(t,u),p({stage:"end",result:"error",action:{name:b,payload:o},details:{task:h.task,elapsed:performance.now()-y,mutations:x(),error:i,reason:c}})}function N(){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:b,payload:o},details:{task:h.task,elapsed:performance.now()-y,mutations:x()}}),d.resolve()}p({stage:"start",action:{name:b,payload:o},details:{task:h.task}});try{j=t(h,o)}catch(k){return G(k),N(),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(G).finally(N),d.promise):(Promise.resolve(j).catch(G).finally(N),d.promise)}}L.current++;const t=new Set;return M.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))}C.on(r,n),k.multicast.add(r),t.add(()=>C.off(r,n))}else $(r)?(s.on(r,n),C.on(r,n),k.broadcast.add(r),t.add(()=>{s.off(r,n),C.off(r,n)})):(C.on(r,n),t.add(()=>C.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=N.Unmounting;const t=J(M.current.handlers,"Unmount");t&&C.emit(t),_.current=N.Unmounted;for(const e of n)e()})}},[C]),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===N.Mounted)return;s.current=N.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=N.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:C,broadcast:s,dispatchers:k,scope:i,phase:_,data:n(),handlers:M.current.handlers});const B=c.useMemo(()=>({dispatch(e,t){const n=W(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H($(e)?s:C,n,t,r)},get inspect(){return O.current.inspect},stream:(e,t)=>c.createElement(je,{action:W(e),renderer:t})}),[P,C]),z=c.useMemo(()=>[P,B,S],[P,B,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=W(t),u=e.current.handlers.get(a)??new Set;0===u.size&&e.current.handlers.set(a,u),u.add({getChannel:i,handler:s})}(M,e,t)},z.dispatch=z[1].dispatch,z}(...t);return e.current=n.dispatch,n},with:{update:e=>xe(e),invert:e=>Se(e),always:(e,t)=>Ee(e,t)}}),[])}function Ce(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 Ge()},useEnv:function(){return g()},Resource:function(t){return se(t,e,n)}}}function Me({holder:e}){const t=g();return e.current=t,null}const Ne={Update:e=>xe(e),Invert:e=>Se(e),Always:(e,t)=>Ee(e,t)},ke=new we;function Ae(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 _e(e,t,n){if(t?.aborted)throw new Y;for(;;){if(await n())return;await Ae(e,t)}}function Re(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const Ue=Object.freeze(Object.defineProperty({__proto__:null,pk:Re,poll:_e,sleep:Ae,unset:q,"ζ":Ae,"κ":Re,"π":_e},Symbol.toStringTag,{value:"Module"})),Le=Object.freeze(Object.defineProperty({__proto__:null,Resource:function(e){return se(e)},Scope:function(){return Ce()},useContext:function(){return Ge()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));e.Aborted=Y,e.Action=(e="",t=M.Unicast)=>{const n=t===M.Broadcast?Symbol(j(e)):t===M.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===M.Broadcast&&Object.defineProperty(r,x.Broadcast,{value:!0,enumerable:!1}),t===M.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(Me,{holder:n}),r]})},useContext:function(){return Ge()},useEnv:function(){return g()},Resource:function(t){return se(t,e?.cache,()=>n.current)},Scope:()=>Ce(e?.cache,()=>n.current)}},e.Boundary=T,e.Cache=ee,e.Distribution=M,e.Lifecycle=C,e.Op=ie,e.Operation=ie,e.Reason=X,e.State=we,e.With=Ne,e.annotate=function(e,t=ie.Update){return ke.annotate(t,e)},e.isBox=function(e){return r.G.isObject(e)&&de in e},e.shared=Le,e.utils=Ue,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,h=l.length;for(u=0;u<h;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;latest=new Map;emit(e,...t){return this.setCache(e,t[0],void 0),super.emit(e,...t)}setCache(e,t,n){const r=this.cache.get(e)??new Map,o={channel:n,value:t};r.set(function(e){return void 0===e?"":JSON.stringify(e)}(n),o),this.cache.set(e,r),this.latest.set(e,o)}getCached(e){return this.latest.get(e)?.value}getCachedAll(e){return this.cache.get(e)?.values()??[]}fire(e,...t){return super.emit(e,...t)}}const h=c.createContext(new d);function p(){return c.useContext(h)}function m({children:e}){const n=c.useMemo(()=>new d,[]);return t.jsx(h.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 E=Symbol(j("Fault")),G=Symbol(j("Env"));class A{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: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:"Fault",enumerable:!1}),e})();static Env=(()=>{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:"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 k({initial:e,children:n}){const o=c.useRef(e),s=p();return r.G.isUndefined(s.getCached(G))&&s.setCache(G,o.current,void 0),t.jsx(v.Provider,{value:o,children:n})}const N=c.createContext(new WeakMap);function _({children:e}){const n=c.useMemo(()=>new WeakMap,[]);return t.jsx(N.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 B({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(k,{initial:e??{},children:t.jsx(b,{children:t.jsx(U,{tap:n,children:t.jsx(_,{children:r})})})})})})}const T=e=>"symbol"==typeof e;function W(e){return r.G.isString(e)||T(e)?e:(r.G.isObject(e)||r.G.isFunction(e))&&x.Action in e?e[x.Action]:e}function $(e){if(r.G.isString(e))return e.startsWith(j());if(T(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=W(e),n=T(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(T(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=(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};function H(e,t,...n){e instanceof d&&e.setCache(t,n[0],n[1]);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"),K="mh:";function V(){const[,e]=c.useReducer(e=>e+1,0);return e}function Q(){return{data:q,at:null}}function X(e,t){return{data:e,at:t}}var Y=(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(Y||{});class Z extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const ee=c.createContext(null);function te(e){const t=function(e){return!r.G.isUndefined(e)&&"get"in e&&"function"==typeof e.get}(e)?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)},keys:()=>e.keys()}}(),n=e?.key;function o(){try{return Array.from(t.keys()).reduce((e,t)=>t.startsWith(K)?[...e,t]:e,[])}catch{return[]}}return{get(e){try{const n=t.get(K+e);if(r.G.isNull(n))return Q();const o=JSON.parse(n);return X(o.data,Temporal.Instant.from(o.at))}catch{return Q()}},set(e,n){if(n.data!==q&&!r.G.isNull(n.at))try{t.set(K+e,JSON.stringify({data:n.data,at:n.at.toString()}))}catch{return}},remove(e){try{t.remove(K+e)}catch{return}},clear(){for(const e of o())try{t.remove(e)}catch{continue}},*keys(){for(const e of o())yield e.slice(3)},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 ne=new WeakMap,re=new Map,oe=[];function se(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=I(`resource:${e.name||"anonymous"}`,C.Broadcast),a=(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}},u=(n,r,o,s)=>e({env:n,controller:r,params:o,dispatch:s}).then(e=>(t.set(c(n,o),X(e,Temporal.Now.instant())),s(i(o),e),e)),l=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 f(e){return{run:u,read:e=>a(e,o()),evict:l,params:e??{}}}return oe.push(l),Object.defineProperty(f,"get",{value:function(e){const{data:t}=a(e??{},o());return t===q?null:t},enumerable:!1}),Object.defineProperty(f,"action",{value:function(e){return i(e??{})},enumerable:!1}),f}function ce(e,t,n){const o=e,s=n??(()=>{});return r.G.isUndefined(t)?se(o,function(e){const t=ne.get(e);if(r.G.isNotNullable(t))return t;const n=te();return ne.set(e,n),n}(o),null,s):se(o,t,function(e){const t=re.get(e);if(r.G.isNotNullable(t))return t;const n=String(re.size);return re.set(e,n),n}(o),s)}let ie=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var ae=(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))(ae||{}),ue=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(ue||{}),le=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(le||{});class fe{[o.immerable]=!0;static keys=new Set(Object.values(le));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new fe(this.value,this.operation);return n.property=e,n.process=t,n}}class de{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=ie}const he=Symbol("Box");function pe(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 me(e){if(r.G.isNullable(e)||ve(e))return e;if(r.G.isArray(e))return e.map(e=>me(e));if(r.G.isObject(e)&&be(e)){const t=Object.entries(e).map(([e,t])=>[e,me(t)]);return{...Object.fromEntries(t),[de.tag]:e[de.tag]??de.id()}}return e}function ye(e){if(Array.isArray(e))return e.filter(e=>de.tag in e).map(e=>e[de.tag]??"").join(",");const t=e[de.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function be(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function ve(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 ge(e,t,n,o,s,c){return function i(a,u=t.path){if(a instanceof fe){const t=pe(n,u.join("."));if(Object.entries(a).filter(([e,t])=>!fe.keys.has(e)&&t instanceof fe).forEach(([e,t])=>i(t,u.concat(e))),ve(a.value)){if(e===ue.Hydrate)return a.value;const i=u.slice(0,-1),l=i.length>0?pe(n,i.join(".")):n;return r.G.isNullable(l)||we(l,a,u.at(-1),o,s,c),t??a.value}if(e===ue.Hydrate){const e=me(i(a.value,u));return we(e,a,null,o,s,c),e}const l=t??me(a.value);return we(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)&&!be(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===ue.Hydrate){const e=me(n);return Object.entries(a).forEach(([t,n])=>{n instanceof fe&&ve(n.value)&&we(e,n,t,o,s,c)}),e}return n}return a}(t.value)}function we(e,t,n,r,o,s){const c=s(e),i=o.get(c)??[];o.set(c,[t.assign(n,r),...i])}class je{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=ye){this.#t=e}static pk(){return ie()}static"κ"=je.pk;annotate(e,t){return new fe(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=pe(e(),o),i=o.slice(0,-1),a=r.A.isNotEmpty(i)?pe(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:pe(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??pe(e(),n):"settled"===a?()=>new Promise(t=>{if(r.A.isEmpty(c(n)))return t(pe(e(),n));const i=()=>{r.A.isEmpty(c(n))&&(s(i),t(pe(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(ue.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(ue.Produce,e)}#s(e,t){const n=Symbol("process"),[,r]=de.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>de.immer.applyPatches(t,[{...r,value:ge(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=me(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 Oe({action:e,renderer:t}){const n=p(),o=c.useContext(L),s=V(),i=c.useMemo(()=>{const t=o.get(e);if(t)return t;const s=new je,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 xe(e,t,n){const{cursor:r,key:o}=Pe(e,t);r[o]=n}function Se(e){return(t,n)=>{t.actions.produce(t=>{xe(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 Ge(e,t){return n=>{n.actions.produce(n=>{xe(n.model,e,t)})}}function Ae(){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=p(),i=c.useContext(ee),a=c.useContext(y),u=g(),l=c.useContext(v),d=c.useContext(N),h=c.useContext(R),m=V(),b=c.useRef(!1),j=c.useRef(null),O=c.useRef(new je);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()),A=c.useMemo(()=>new f,[]),C=c.useRef({handlers:new Map});C.current.handlers=new Map;const k=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),B=c.useCallback((e,t,n)=>{const c=new AbortController,f={controller:c,action:e,payload:t};return a.add(f),U.current.add(f),{get model(){return 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(G,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=W(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H($(e)?s:A,n,t,r)},annotate:(e,t=ae.Update)=>O.current.annotate(t,e),get inspect(){return O.current.inspect},resource:Object.assign(function(e){const t=(e,t)=>{const n=e,r=W(n),o=F(n)?n.channel:void 0;return D(n)?i?H(i.emitter,r,t,o):Promise.resolve():$(n)?H(s,r,t,o):Promise.resolve()},n={exceedsWindow:null,isolated:!1},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(n.isolated)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);let a=s.get(i);if(r.G.isUndefined(a)){const n=new AbortController,r={controller:n,refs:0};r.promise=e.run(u,n,e.params,t).finally(()=>{s.delete(i)}),s.set(i,r),a=r}const l=a;l.refs+=1;const f=()=>{l.refs-=1,0===l.refs&&(s.delete(i),l.controller.abort(c.signal.reason))};if(c.signal.aborted)f();else{c.signal.addEventListener("abort",f,{once:!0});const e=()=>c.signal.removeEventListener("abort",f);l.promise.then(e,e)}return h=l.promise,p=c.signal,new Promise((e,t)=>{if(p.aborted)return void t(p.reason);const n=()=>t(p.reason);p.addEventListener("abort",n,{once:!0}),h.then(t=>{p.removeEventListener("abort",n),e(t)},e=>{p.removeEventListener("abort",n),t(e)})});var h,p})().then(o,s),exceeds:e=>(n.exceedsWindow=e,o),isolated:()=>(n.isolated=!0,o),evict(t){e.evict(t??e.params)}};return o},{nuke:e=>function(e){const t=e??{};for(const n of oe)n(t)}(e)}),async final(e){if(c.signal.aborted)return null;const t=W(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=W(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(r.G.isNotNullable(c)&&r.G.isNotNullable(u)&&!function(e,t){for(const n of Object.keys(t))if(e[n]!==t[n])return!1;return!0}(c,u))return;const f={processes:new Set},d=Promise.withResolvers(),p=B(e,o,f),y=function(e){const t=W(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(){return D(e)?i?H(i.emitter,e,o,c):Promise.resolve():H($(e)?s:A,e,o,c)}function G(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?Y.Aborted:Y.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(E,u),n&&t&&A.emit(t,u),h({stage:"end",result:"error",action:{name:y,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:x(),error:i,reason:c}})}function M(){for(const e of a)if(e===p.task){a.delete(e),U.current.delete(e);break}f.processes.forEach(e=>O.current.prune(e)),f.processes.size>0&&m(),P||h({stage:"end",result:"success",action:{name:y,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:x()}}),d.resolve()}h({stage:"start",action:{name:y,payload:o},details:{task:p.task}});try{j=t(p,o)}catch(k){return G(k),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(G).finally(M),d.promise):(Promise.resolve(j).catch(G).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))}A.on(r,n),k.multicast.add(r),t.add(()=>A.off(r,n))}else $(r)?(s.on(r,n),A.on(r,n),k.broadcast.add(r),t.add(()=>{s.off(r,n),A.off(r,n)})):(A.on(r,n),t.add(()=>A.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 Z("Component unmounted")),a.delete(e);U.current.clear(),_.current=M.Unmounting;const t=J(C.current.handlers,"Unmount");t&&A.emit(t),_.current=M.Unmounted;for(const e of n)e()})}},[A]),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=>{for(const{channel:o,value:s}of t.getCachedAll(n))r.G.isNullable(s)||e.emit(n,s,o)}),o&&n.multicast.forEach(t=>{for(const{channel:n,value:s}of o.emitter.getCachedAll(t))r.G.isNullable(s)||e.emit(t,s,n)}),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:A,broadcast:s,dispatchers:k,scope:i,phase:_,data:n(),handlers:C.current.handlers});const T=c.useMemo(()=>({dispatch(e,t){const n=W(e),r=F(e)?e.channel:void 0;return D(e)?i?H(i.emitter,n,t,r):Promise.resolve():H($(e)?s:A,n,t,r)},get inspect(){return O.current.inspect},stream:(e,t)=>c.createElement(Oe,{action:W(e),renderer:t})}),[P,A]),z=c.useMemo(()=>[P,T,S],[P,T,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=W(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=>Se(e),invert:e=>Ee(e),always:(e,t)=>Ge(e,t)}}),[])}function Ce(e,n){return{Boundary:function({children:e}){const n=c.useMemo(()=>({id:Symbol("march-hare.scope/instance"),emitter:new d}),[]);return t.jsx(ee.Provider,{value:n,children:e})},useContext:function(){return Ae()},useEnv:function(){return g()},Resource:function(t){return ce(t,e,n)}}}function Me({holder:e}){const t=g();return e.current=t,null}const ke={Update:e=>Se(e),Invert:e=>Ee(e),Always:(e,t)=>Ge(e,t)},Ne=new je;function _e(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new Z);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new Z)},{once:!0})})}async function Re(e,t,n){if(t?.aborted)throw new Z;for(;;){if(await n())return;await _e(e,t)}}function Ue(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const Le=Object.freeze(Object.defineProperty({__proto__:null,pk:Ue,poll:Re,sleep:_e,unset:q,"ζ":_e,"κ":Ue,"π":Re},Symbol.toStringTag,{value:"Module"})),Be=Object.freeze(Object.defineProperty({__proto__:null,Resource:function(e){return ce(e)},Scope:function(){return Ce()},useContext:function(){return Ae()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));e.Aborted=Z,e.Action=I,e.App=function(e){const n={current:void 0};return{Boundary:function({children:r}){return t.jsxs(B,{env:e?.env,tap:e?.tap,children:[t.jsx(Me,{holder:n}),r]})},useContext:function(){return Ae()},useEnv:function(){return g()},Resource:function(t){return ce(t,e?.cache,()=>n.current)},Scope:()=>Ce(e?.cache,()=>n.current)}},e.Boundary=B,e.Cache=te,e.Distribution=C,e.Lifecycle=A,e.Op=ae,e.Operation=ae,e.Reason=Y,e.State=je,e.With=ke,e.annotate=function(e,t=ae.Update){return Ne.annotate(t,e)},e.isBox=function(e){return r.G.isObject(e)&&he in e},e.shared=Be,e.utils=Le,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