march-hare 0.13.2 → 0.13.5

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||{}),N=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(N||{});function A({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 M=c.createContext(new WeakMap);function _({children:e}){const n=c.useMemo(()=>new WeakMap,[]);return t.jsx(M.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})}function L({env:e,tap:n,children:r}){return t.jsx(m,{children:t.jsx(A,{initial:e??{},children:t.jsx(b,{children:t.jsx(U,{tap:n,children:t.jsx(_,{children:r})})})})})}const T=e=>"symbol"==typeof e;function $(e){return r.G.isString(e)||T(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(T(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 B(e){return r.G.isObject(e)&&P.Channel in e&&"channel"in e}function F(e){const t=$(e),n=T(t)?t.description??"":t;return n.startsWith(x())&&n.slice(x().length)||null}function z(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(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 D=Symbol(((e="")=>`march-hare/replay${e}`)());function I(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 H(e,t){for(const n of e.keys())if(F(n)===t)return n;return null}const J=Symbol("march-hare.unset");function q(){const[,e]=c.useReducer(e=>e+1,0);return e}function K(){return{data:J,at:null}}function V(e,t){return{data:e,at:t}}var Q=(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(Q||{});class X extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Y=c.createContext(null);function Z(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 K();const o=JSON.parse(n);return V(o.data,Temporal.Instant.from(o.at))}catch{return K()}},set(e,n){if(n.data!==J&&!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 ee=new WeakMap,te=new Map,ne=[];function re(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===J||r.G.isNull(o.at)?{data:J,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),V(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 ne.push(u),Object.defineProperty(l,"get",{value:function(e){const{data:t}=i(e??{},o());return t===J?null:t},enumerable:!1}),l}function oe(e,t,n){const o=e,s=n??(()=>{});return r.G.isUndefined(t)?re(o,function(e){const t=ee.get(e);if(r.G.isNotNullable(t))return t;const n=Z();return ee.set(e,n),n}(o),null,s):re(o,t,function(e){const t=te.get(e);if(r.G.isNotNullable(t))return t;const n=String(te.size);return te.set(e,n),n}(o),s)}const se=Symbol("march-hare.coalesce/default");function ce(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 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}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)||be(e))return e;if(r.G.isArray(e))return e.map(e=>he(e));if(r.G.isObject(e)&&ye(e)){const t=Object.entries(e).map(([e,t])=>[e,he(t)]);return{...Object.fromEntries(t),[de.tag]:e[de.tag]??de.id()}}return e}function me(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 ye(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function be(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 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))),be(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)||ge(l,a,u.at(-1),o,s,c),t??a.value}if(e===ue.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)&&!ye(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=he(n);return Object.entries(a).forEach(([t,n])=>{n instanceof fe&&be(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 ie()}static"κ"=we.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)}):"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: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)}}const je=c.createContext(new Map);function Oe({action:e,renderer:t}){const n=h(),o=c.useContext(je),s=q(),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 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 Pe(e,t,n){const{cursor:r,key:o}=xe(e,t);r[o]=n}function Se(e){return(t,n)=>{t.actions.produce(t=>{Pe(t.model,e,n)})}}function Ge(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 Ee(e,t){return n=>{n.actions.produce(n=>{Pe(n.model,e,t)})}}function ke(){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(Y),a=c.useContext(y),u=g(),l=c.useContext(v),d=c.useContext(M),p=c.useContext(R),m=q(),b=c.useRef(!1),j=c.useRef(null),O=c.useRef(new we);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 A=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),{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=$(e),r=B(e)?e.channel:void 0;return z(e)?i?I(i.emitter,n,t,r):Promise.resolve():I(W(e)?s:k,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)=>{if(c.signal.aborted)return Promise.resolve();const n=e,r=$(n);return z(n)?i?I(i.emitter,r,t,void 0):Promise.resolve():W(n)?I(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!==J&&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 ce(a,c.signal);const l=new AbortController,f=e.run(u,l,e.params,t).finally(()=>{s.delete(i)});return s.set(i,f),ce(f,c.signal)})().then(o,s),exceeds:e=>(n.exceedsWindow=e,o),coalesce:e=>(n.coalesceToken=e??se,o),evict(t){e.evict(t??e.params)}};return o},{nuke:e=>function(e){const t=e??{};for(const n of ne)n(t)}(e)}),async final(e){if(c.signal.aborted)return null;const t=$(e),n=z(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=$(e),n=z(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===D&&r.G.isNotNullable(u))return;if(r.G.isNotNullable(c)&&c!==D&&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=$(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===D?void 0:c;return z(e)?i?I(i.emitter,e,o,t):Promise.resolve():I(W(e)?s:k,e,o,t)}function E(e){x=!0;const t=H(C.current.handlers,"Error"),n=r.G.isNotNullable(t),c=function(e){return e instanceof Error&&"AbortError"===e.name?Q.Aborted:Q.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 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(),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(A){return E(A),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(E).finally(N),d.promise):(Promise.resolve(j).catch(E).finally(N),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(z(r)){if(i){const e=i.emitter;e.on(r,n),t.add(()=>e.off(r,n))}k.on(r,n),A.multicast.add(r),t.add(()=>k.off(r,n))}else W(r)?(s.on(r,n),k.on(r,n),A.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 X("Component unmounted")),a.delete(e);U.current.clear(),_.current=N.Unmounting;const t=H(C.current.handlers,"Unmount");t&&k.emit(t),_.current=N.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===N.Mounted)return;s.current=N.Mounting;const c=H(a,"Mount");c&&e.emit(c),n.broadcast.forEach(n=>{const o=t.getCached(n);r.G.isNullable(o)||e.emit(n,o,D)}),o&&n.multicast.forEach(t=>{const n=o.emitter.getCached(t);r.G.isNullable(n)||e.emit(t,n,D)}),s.current=N.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=H(a,"Update");n&&e.emit(n,t)}}u.current=i},[i,e])}({unicast:k,broadcast:s,dispatchers:A,scope:i,phase:_,data:n(),handlers:C.current.handlers});const F=c.useMemo(()=>({dispatch(e,t){const n=$(e),r=B(e)?e.channel:void 0;return z(e)?i?I(i.emitter,n,t,r):Promise.resolve():I(W(e)?s:k,n,t,r)},get inspect(){return O.current.inspect},stream:(e,t)=>c.createElement(Oe,{action:$(e),renderer:t})}),[x,k]),K=c.useMemo(()=>[x,F,S],[x,F,S]);return K.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(()=>B(o.current)?o.current.channel:void 0,[]),a=$(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)},K.dispatch=K[1].dispatch,K}(...t);return e.current=n.dispatch,n},with:{update:e=>Se(e),invert:e=>Ge(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(Y.Provider,{value:n,children:e})},useContext:function(){return ke()},useEnv:function(){return g()},Resource:function(t){return oe(t,e,n)}}}function Ne({holder:e}){const t=g();return e.current=t,null}const Ae={Update:e=>Se(e),Invert:e=>Ge(e),Always:(e,t)=>Ee(e,t)},Me=new we;function _e(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new X);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new X)},{once:!0})})}async function Re(e,t,n){if(t?.aborted)throw new X;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:J,"ζ":_e,"κ":Ue,"π":Re},Symbol.toStringTag,{value:"Module"})),Te=Object.freeze(Object.defineProperty({__proto__:null,Resource:function(e){return oe(e)},Scope:function(){return Ce()},useContext:function(){return ke()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));e.Aborted=X,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(L,{env:e?.env,tap:e?.tap,children:[t.jsx(Ne,{holder:n}),r]})},useContext:function(){return ke()},useEnv:function(){return g()},Resource:function(t){return oe(t,e?.cache,()=>n.current)},Scope:()=>Ce(e?.cache,()=>n.current)}},e.Boundary=L,e.Cache=Z,e.Distribution=C,e.Lifecycle=k,e.Op=ae,e.Operation=ae,e.Reason=Q,e.State=we,e.With=Ae,e.annotate=function(e,t=ae.Update){return Me.annotate(t,e)},e.isBox=function(e){return r.G.isObject(e)&&"value"in e&&"inspect"in e},e.shared=Te,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);
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||{}),N=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(N||{});function A({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 M=c.createContext(new WeakMap);function _({children:e}){const n=c.useMemo(()=>new WeakMap,[]);return t.jsx(M.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})}function L({env:e,tap:n,children:r}){return t.jsx(m,{children:t.jsx(A,{initial:e??{},children:t.jsx(b,{children:t.jsx(U,{tap:n,children:t.jsx(_,{children:r})})})})})}const T=e=>"symbol"==typeof e;function $(e){return r.G.isString(e)||T(e)?e:(r.G.isObject(e)||r.G.isFunction(e))&&P.Action in e?e[P.Action]:e}function B(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(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 W(e){return r.G.isObject(e)&&P.Channel in e&&"channel"in e}function F(e){const t=$(e),n=T(t)?t.description??"":t;return n.startsWith(x())&&n.slice(x().length)||null}function z(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(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 D=Symbol(((e="")=>`march-hare/replay${e}`)());function I(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 H(e,t){for(const n of e.keys())if(F(n)===t)return n;return null}const J=Symbol("march-hare.unset");function q(){const[,e]=c.useReducer(e=>e+1,0);return e}function K(){return{data:J,at:null}}function V(e,t){return{data:e,at:t}}var Q=(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(Q||{});class X extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Y=c.createContext(null);function Z(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 K();const o=JSON.parse(n);return V(o.data,Temporal.Instant.from(o.at))}catch{return K()}},set(e,n){if(n.data!==J&&!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 ee=new WeakMap,te=new Map,ne=[];function re(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===J||r.G.isNull(o.at)?{data:J,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),V(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 ne.push(u),Object.defineProperty(l,"get",{value:function(e){const{data:t}=i(e??{},o());return t===J?null:t},enumerable:!1}),l}function oe(e,t,n){const o=e,s=n??(()=>{});return r.G.isUndefined(t)?re(o,function(e){const t=ee.get(e);if(r.G.isNotNullable(t))return t;const n=Z();return ee.set(e,n),n}(o),null,s):re(o,t,function(e){const t=te.get(e);if(r.G.isNotNullable(t))return t;const n=String(te.size);return te.set(e,n),n}(o),s)}const se=Symbol("march-hare.coalesce/default");function ce(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 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 pe=Symbol("Box");function he(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=he(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?he(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=he(e(),o),i=o.slice(0,-1),a=r.A.isNotEmpty(i)?he(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:he(e(),n),inspect:t(n),[pe]:!0}):"is"===a?e=>c(n).some(t=>0!==(t.operation&e)):"draft"===a?()=>r.A.head(c(n))?.value??he(e(),n):"settled"===a?()=>new Promise(t=>{if(r.A.isEmpty(c(n)))return t(he(e(),n));const i=()=>{r.A.isEmpty(c(n))&&(s(i),t(he(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)}}const Oe=c.createContext(new Map);function xe({action:e,renderer:t}){const n=h(),o=c.useContext(Oe),s=q(),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 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(Y),a=c.useContext(y),u=g(),l=c.useContext(v),d=c.useContext(M),p=c.useContext(R),m=q(),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[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 A=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),{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=$(e),r=W(e)?e.channel:void 0;return z(e)?i?I(i.emitter,n,t,r):Promise.resolve():I(B(e)?s:k,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)=>{if(c.signal.aborted)return Promise.resolve();const n=e,r=$(n);return z(n)?i?I(i.emitter,r,t,void 0):Promise.resolve():B(n)?I(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!==J&&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 ce(a,c.signal);const l=new AbortController,f=e.run(u,l,e.params,t).finally(()=>{s.delete(i)});return s.set(i,f),ce(f,c.signal)})().then(o,s),exceeds:e=>(n.exceedsWindow=e,o),coalesce:e=>(n.coalesceToken=e??se,o),evict(t){e.evict(t??e.params)}};return o},{nuke:e=>function(e){const t=e??{};for(const n of ne)n(t)}(e)}),async final(e){if(c.signal.aborted)return null;const t=$(e),n=z(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=$(e),n=z(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===D&&r.G.isNotNullable(u))return;if(r.G.isNotNullable(c)&&c!==D&&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=$(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===D?void 0:c;return z(e)?i?I(i.emitter,e,o,t):Promise.resolve():I(B(e)?s:k,e,o,t)}function E(e){x=!0;const t=H(C.current.handlers,"Error"),n=r.G.isNotNullable(t),c=function(e){return e instanceof Error&&"AbortError"===e.name?Q.Aborted:Q.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 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(),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(A){return E(A),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(E).finally(N),d.promise):(Promise.resolve(j).catch(E).finally(N),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(z(r)){if(i){const e=i.emitter;e.on(r,n),t.add(()=>e.off(r,n))}k.on(r,n),A.multicast.add(r),t.add(()=>k.off(r,n))}else B(r)?(s.on(r,n),k.on(r,n),A.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 X("Component unmounted")),a.delete(e);U.current.clear(),_.current=N.Unmounting;const t=H(C.current.handlers,"Unmount");t&&k.emit(t),_.current=N.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===N.Mounted)return;s.current=N.Mounting;const c=H(a,"Mount");c&&e.emit(c),n.broadcast.forEach(n=>{const o=t.getCached(n);r.G.isNullable(o)||e.emit(n,o,D)}),o&&n.multicast.forEach(t=>{const n=o.emitter.getCached(t);r.G.isNullable(n)||e.emit(t,n,D)}),s.current=N.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=H(a,"Update");n&&e.emit(n,t)}}u.current=i},[i,e])}({unicast:k,broadcast:s,dispatchers:A,scope:i,phase:_,data:n(),handlers:C.current.handlers});const F=c.useMemo(()=>({dispatch(e,t){const n=$(e),r=W(e)?e.channel:void 0;return z(e)?i?I(i.emitter,n,t,r):Promise.resolve():I(B(e)?s:k,n,t,r)},get inspect(){return O.current.inspect},stream:(e,t)=>c.createElement(xe,{action:$(e),renderer:t})}),[x,k]),K=c.useMemo(()=>[x,F,S],[x,F,S]);return K.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(()=>W(o.current)?o.current.channel:void 0,[]),a=$(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)},K.dispatch=K[1].dispatch,K}(...t);return e.current=n.dispatch,n},with:{update:e=>Ge(e),invert:e=>Ee(e),always:(e,t)=>ke(e,t)}}),[])}function Ne(e,n){return{Boundary:function({children:e}){const n=c.useMemo(()=>({id:Symbol("march-hare.scope/instance"),emitter:new d}),[]);return t.jsx(Y.Provider,{value:n,children:e})},useContext:function(){return Ce()},useEnv:function(){return g()},Resource:function(t){return oe(t,e,n)}}}function Ae({holder:e}){const t=g();return e.current=t,null}const Me={Update:e=>Ge(e),Invert:e=>Ee(e),Always:(e,t)=>ke(e,t)},_e=new je;function Re(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new X);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new X)},{once:!0})})}async function Ue(e,t,n){if(t?.aborted)throw new X;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:J,"ζ":Re,"κ":Le,"π":Ue},Symbol.toStringTag,{value:"Module"})),$e=Object.freeze(Object.defineProperty({__proto__:null,Resource:function(e){return oe(e)},Scope:function(){return Ne()},useContext:function(){return Ce()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));e.Aborted=X,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(L,{env:e?.env,tap:e?.tap,children:[t.jsx(Ae,{holder:n}),r]})},useContext:function(){return Ce()},useEnv:function(){return g()},Resource:function(t){return oe(t,e?.cache,()=>n.current)},Scope:()=>Ne(e?.cache,()=>n.current)}},e.Boundary=L,e.Cache=Z,e.Distribution=C,e.Lifecycle=k,e.Op=ae,e.Operation=ae,e.Reason=Q,e.State=je,e.With=Me,e.annotate=function(e,t=ae.Update){return _e.annotate(t,e)},e.isBox=function(e){return r.G.isObject(e)&&pe 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