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