march-hare 0.13.8 → 0.13.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/action/utils.d.ts
CHANGED
|
@@ -61,9 +61,9 @@ export declare function isChanneledAction(action: AnyAction): action is Channele
|
|
|
61
61
|
/**
|
|
62
62
|
* Extracts the lifecycle type from an action's symbol description.
|
|
63
63
|
*
|
|
64
|
-
* Returns the lifecycle name (`"Mount"`, `"
|
|
65
|
-
* when the action symbol's description starts with the lifecycle
|
|
66
|
-
* `null` for non-lifecycle actions.
|
|
64
|
+
* Returns the lifecycle name (`"Mount"`, `"Paint"`, `"Unmount"`, `"Error"`,
|
|
65
|
+
* `"Update"`) when the action symbol's description starts with the lifecycle
|
|
66
|
+
* prefix, or `null` for non-lifecycle actions.
|
|
67
67
|
*
|
|
68
68
|
* @param action The action to inspect.
|
|
69
69
|
* @returns The lifecycle name, or `null` if not a lifecycle action.
|
package/dist/actions/utils.d.ts
CHANGED
|
@@ -43,6 +43,10 @@ export declare function emitAsync(emitter: EventEmitter, event: string | symbol,
|
|
|
43
43
|
* mount) and `Unmounted` (re-mount after `<Activity>` show) as entry states
|
|
44
44
|
* so that hidden-then-shown subtrees correctly re-emit Mount.
|
|
45
45
|
*
|
|
46
|
+
* The Paint effect runs as a passive `useEffect` so it fires after the
|
|
47
|
+
* browser has committed the first frame. It tracks its own ref-guard so it
|
|
48
|
+
* fires exactly once per mount cycle (re-firing after `<Activity>` show).
|
|
49
|
+
*
|
|
46
50
|
* Phase transitions:
|
|
47
51
|
* - First mount: Mounting → Mounted
|
|
48
52
|
* - Activity hide / show: Mounted → Unmounting → Unmounted → Mounting → Mounted
|
package/dist/march-hare.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import{jsx as e,jsxs as t}from"react/jsx-runtime";import*as n from"react";import{G as r,A as o}from"@mobily/ts-belt";import{immerable as s,enablePatches as c,Immer as i,produce as a}from"immer";function u(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var l,f={exports:{}};const d=/* @__PURE__ */u((l||(l=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=/* @__PURE__ */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}(f)),f.exports));class p extends d{cache=/* @__PURE__ */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 h=n.createContext(new p);function m(){return n.useContext(h)}function y({children:t}){const r=n.useMemo(()=>new p,[]);/* @__PURE__ */
|
|
2
2
|
return e(h.Provider,{value:r,children:t})}const b=n.createContext(/* @__PURE__ */new Set);function v({children:t}){const r=n.useMemo(()=>/* @__PURE__ */new Set,[]);/* @__PURE__ */
|
|
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 O=(e="")=>`march-hare.action/${e}`,P=(e="")=>`march-hare.action/broadcast/${e}`,S=(e="")=>`march-hare.action/multicast/${e}`,j=(e="")=>`march-hare.action.lifecycle/${e}`;class x{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 E(e){const t=/* @__PURE__ */Symbol(`march-hare.action.lifecycle/${e}`),n=function(n){return{[x.Action]:t,[x.Payload]:void 0,[x.Channel]:n,[x.Name]:e,channel:n}};return Object.defineProperty(n,x.Action,{value:t,enumerable:!1}),Object.defineProperty(n,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(n,x.Name,{value:e,enumerable:!1}),Object.defineProperty(n,x.Lifecycle,{value:e,enumerable:!1}),n}const k=Symbol(P("Fault")),N=Symbol(P("Env"));class C{static Mount(){return E("Mount")}static Unmount(){return E("Unmount")}static Error(){return E("Error")}static Update(){return E("Update")}static Fault=(()=>{const e={};return Object.defineProperty(e,x.Action,{value:k,enumerable:!1}),Object.defineProperty(e,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,x.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,x.Name,{value:"Fault",enumerable:!1}),e})();static Env=(()=>{const e={};return Object.defineProperty(e,x.Action,{value:N,enumerable:!1}),Object.defineProperty(e,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,x.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,x.Name,{value:"Env",enumerable:!1}),e})()}var M=/* @__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
|
|
4
|
-
return e(
|
|
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 O=(e="")=>`march-hare.action/${e}`,P=(e="")=>`march-hare.action/broadcast/${e}`,S=(e="")=>`march-hare.action/multicast/${e}`,j=(e="")=>`march-hare.action.lifecycle/${e}`;class x{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 E(e){const t=/* @__PURE__ */Symbol(`march-hare.action.lifecycle/${e}`),n=function(n){return{[x.Action]:t,[x.Payload]:void 0,[x.Channel]:n,[x.Name]:e,channel:n}};return Object.defineProperty(n,x.Action,{value:t,enumerable:!1}),Object.defineProperty(n,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(n,x.Name,{value:e,enumerable:!1}),Object.defineProperty(n,x.Lifecycle,{value:e,enumerable:!1}),n}const k=Symbol(P("Fault")),N=Symbol(P("Env"));class C{static Mount(){return E("Mount")}static Paint(){return E("Paint")}static Unmount(){return E("Unmount")}static Error(){return E("Error")}static Update(){return E("Update")}static Fault=(()=>{const e={};return Object.defineProperty(e,x.Action,{value:k,enumerable:!1}),Object.defineProperty(e,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,x.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,x.Name,{value:"Fault",enumerable:!1}),e})();static Env=(()=>{const e={};return Object.defineProperty(e,x.Action,{value:N,enumerable:!1}),Object.defineProperty(e,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,x.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,x.Name,{value:"Env",enumerable:!1}),e})()}var M=/* @__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
|
+
return e(R.Provider,{value:r,children:t})}const L=n.createContext(()=>{});function $({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 T=n.createContext(/* @__PURE__ */new Map);function W({env:t,tap:r,children:o}){const s=n.useMemo(()=>/* @__PURE__ */new Map,[]);/* @__PURE__ */
|
|
6
|
-
return e(y,{children:/* @__PURE__ */e(T.Provider,{value:s,children:/* @__PURE__ */e(_,{initial:t??{},children:/* @__PURE__ */e(v,{children:/* @__PURE__ */e($,{tap:r,children:/* @__PURE__ */e(R,{children:o})})})})})})}const B=e=>"symbol"==typeof e;function F(e){return r.isString(e)||B(e)?e:(r.isObject(e)||r.isFunction(e))&&x.Action in e?e[x.Action]:e}function z(e){if(r.isString(e))return e.startsWith(P());if(B(e))return e.description?.startsWith(P())??!1;if(r.isObject(e)||r.isFunction(e)){if(x.Broadcast in e&&e[x.Broadcast])return!0;if(x.Action in e){const t=e[x.Action];return t.description?.startsWith(P())??!1}}return!1}function J(e){return r.isObject(e)&&x.Channel in e&&"channel"in e}function D(e){const t=F(e),n=B(t)?t.description??"":t;return n.startsWith(j())&&n.slice(j().length)||null}function H(e){if(r.isString(e))return e.startsWith(S());if(B(e))return e.description?.startsWith(S())??!1;if(r.isObject(e)||r.isFunction(e)){if(x.Multicast in e&&e[x.Multicast])return!0;if(x.Action in e){const t=e[x.Action];return t.description?.startsWith(S())??!1}}return!1}const I=(e="",t=M.Unicast)=>{const n=t===M.Broadcast?Symbol(P(e)):t===M.Multicast?Symbol(S(e)):Symbol(O(e)),r=function(t){return{[x.Action]:n,[x.Payload]:void 0,[x.Channel]:t,[x.Name]:e,channel:t}};return Object.defineProperty(r,x.Action,{value:n,enumerable:!1}),Object.defineProperty(r,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,x.Name,{value:e,enumerable:!1}),t===M.Broadcast&&Object.defineProperty(r,x.Broadcast,{value:!0,enumerable:!1}),t===M.Multicast&&Object.defineProperty(r,x.Multicast,{value:!0,enumerable:!1}),r},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(D(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)}const ae=/* @__PURE__ */Symbol("march-hare.coalesce/default");function ue(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 le=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var fe=/* @__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))(fe||{}),de=/* @__PURE__ */(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(de||{}),pe=/* @__PURE__ */(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(pe||{});class he{[s]=!0;static keys=new Set(Object.values(pe));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new he(this.value,this.operation);return n.property=e,n.process=t,n}}class me{static immer=(()=>{c();const e=new i;return e.setAutoFreeze(!1),e})();static tag="κ";static id=le}const ye=/* @__PURE__ */Symbol("Box");function be(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 ve(e){if(r.isNullable(e)||Oe(e))return e;if(r.isArray(e))return e.map(e=>ve(e));if(r.isObject(e)&&we(e)){const t=Object.entries(e).map(([e,t])=>[e,ve(t)]);return{...Object.fromEntries(t),[me.tag]:e[me.tag]??me.id()}}return e}function ge(e){if(Array.isArray(e))return e.filter(e=>me.tag in e).map(e=>e[me.tag]??"").join(",");const t=e[me.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function we(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function Oe(e){return r.isNullable(e)||r.isString(e)||r.isNumber(e)||r.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function Pe(e){return r.isObject(e)&&ye in e}function Se(e,t,n,o,s,c){return function i(a,u=t.path){if(a instanceof he){const t=be(n,u.join("."));if(Object.entries(a).filter(([e,t])=>!he.keys.has(e)&&t instanceof he).forEach(([e,t])=>i(t,u.concat(e))),Oe(a.value)){if(e===de.Hydrate)return a.value;const i=u.slice(0,-1),l=i.length>0?be(n,i.join(".")):n;return r.isNullable(l)||je(l,a,u.at(-1),o,s,c),t??a.value}if(e===de.Hydrate){const e=ve(i(a.value,u));return je(e,a,null,o,s,c),e}const l=t??ve(a.value);return je(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)&&!we(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===de.Hydrate){const e=ve(n);return Object.entries(a).forEach(([t,n])=>{n instanceof he&&Oe(n.value)&&je(e,n,t,o,s,c)}),e}return n}return a}(t.value)}function je(e,t,n,r,o,s){const c=s(e),i=o.get(c)??[];o.set(c,[t.assign(n,r),...i])}class xe{#e={};#t;#n=/* @__PURE__ */new Map;#r=/* @__PURE__ */new Set;#o=!1;constructor(e=ge){this.#t=e}static pk(){return le()}static"κ"=xe.pk;annotate(e,t){return new he(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=be(e(),s),a=s.slice(0,-1),u=o.isNotEmpty(a)?be(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:be(e(),n),inspect:t(n),[ye]:!0}):"is"===a?e=>i(n).some(t=>0!==(t.operation&e)):"draft"===a?()=>o.head(i(n))?.value??be(e(),n):"settled"===a?()=>new Promise(t=>{if(o.isEmpty(i(n)))return t(be(e(),n));const r=()=>{o.isEmpty(i(n))&&(c(r),t(be(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(de.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(de.Produce,e)}#s(e,t){const n=/* @__PURE__ */Symbol("process"),[,r]=me.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>me.immer.applyPatches(t,[{...r,value:Se(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=ve(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 Ee({action:e,renderer:t}){const o=m(),s=n.useContext(T),c=Q(),i=n.useMemo(()=>{const t=s.get(e);if(t)return t;const n=new xe,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 ke(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 Ne(e,t,n){const{cursor:r,key:o}=ke(e,t);r[o]=n}function Ce(e){return(t,n)=>{t.actions.produce(t=>{Ne(t.model,e,n)})}}function Me(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=ke(e,t);n[r]=!n[r]}(t.model,e)})}}function Ae(e,t){return n=>{n.actions.produce(n=>{Ne(n.model,e,t)})}}function _e(){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(U),h=n.useContext(L),y=Q(),v=n.useRef(!1),P=n.useRef(null),S=n.useRef(new xe);v.current||(v.current=!0,P.current=S.current.hydrate(t));const[j,x]=n.useState(()=>S.current.model),E=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()),C=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}),[])}(),R=n.useRef(A.Mounting),$=n.useRef(/* @__PURE__ */new Set),T=n.useRef(0),W=n.useCallback((e,t,n)=>{const o=new AbortController,s={controller:o,action:e,payload:t};return u.add(s),$.current.add(s),{model:S.current.model,get phase(){return R.current},task:s,data:E,tasks:u,env:l,actions:{produce(e){if(o.signal.aborted)return;const t=f.current,r=S.current.produce(t=>{f.current=a(f.current,n=>{e({model:t,inspect:S.current.inspect,env:n})})});x(S.current.model),f.current!==t&&c.emit(N,f.current),n.processes.add(r),P.current&&(n.processes.add(P.current),P.current=null)},dispatch(e,t){if(o.signal.aborted)return Promise.resolve();const n=F(e),r=J(e)?e.channel:void 0;return H(e)?i?K(i.emitter,n,t,r):Promise.resolve():K(z(e)?c:C,n,t,r)},annotate:(e,t=fe.Update)=>S.current.annotate(t,e),get inspect(){return S.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 H(n)?i?K(i.emitter,r,t,void 0):Promise.resolve():z(n)?K(c,r,t,void 0):Promise.resolve()},n={exceedsWindow:null,coalesceToken:void 0},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(r.isUndefined(n.coalesceToken))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)}|${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=c.get(i);if(a)return ue(a,o.signal);const u=new AbortController,f=e.run(l,u,e.params,t).finally(()=>{c.delete(i)});return c.set(i,f),ue(f,o.signal)})().then(s,c),exceeds:e=>(n.exceedsWindow=e,s),coalesce:e=>(n.coalesceToken=e??ae,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=H(e)?i?.emitter??null:c;if(!n)return null;const s=n.getCached(t);if(r.isUndefined(s))return null;const a=S.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=H(e)?i?.emitter??null:c;return n?n.getCached(t)??null:null}}}},[j]);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=W(e,o,l),m=function(e){const t=F(e),n=r.isString(t)?t:t.description??"";return n.startsWith(O())&&n.slice(n.lastIndexOf("/")+1)||"unknown"}(e),b=performance.now(),v=S.current.model,g=f.current;let w,P=!1;function j(){const e=S.current.model,t=f.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function x(){const t=s===G?void 0:s;return H(e)?i?K(i.emitter,e,o,t):Promise.resolve():K(z(e)?c:C,e,o,t)}function E(e){P=!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:x};c.fire(k,a),n&&t&&C.emit(t,a),h({stage:"end",result:"error",action:{name:m,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:j(),error:i,reason:s}})}function N(){for(const e of u)if(e===p.task){u.delete(e),$.current.delete(e);break}l.processes.forEach(e=>S.current.prune(e)),l.processes.size>0&&y(),P||h({stage:"end",result:"success",action:{name:m,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:j()}}),d.resolve()}h({stage:"start",action:{name:m,payload:o},details:{task:p.task}});try{w=t(p,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}(w)?((async()=>{for await(const e of w);})().catch(E).finally(N),d.promise):(Promise.resolve(w).catch(E).finally(N),d.promise)}}T.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(H(r)){if(i){const e=i.emitter;e.on(r,n),t.add(()=>e.off(r,n))}C.on(r,n),_.multicast.add(r),t.add(()=>C.off(r,n))}else z(r)?(c.on(r,n),C.on(r,n),_.broadcast.add(r),t.add(()=>{c.off(r,n),C.off(r,n)})):(C.on(r,n),t.add(()=>C.off(r,n)))}}),()=>{const e=++T.current,n=new Set(t);queueMicrotask(()=>{if(T.current!==e){for(const e of n)e();return}for(const e of $.current)e.controller.abort(new ee("Component unmounted")),u.delete(e);$.current.clear(),R.current=A.Unmounting;const t=V(M.current.handlers,"Unmount");t&&C.emit(t),R.current=A.Unmounted;for(const e of n)e()})}},[C]),function({unicast:e,broadcast:t,dispatchers:s,scope:c,phase:i,data:a,handlers:u}){const l=n.useRef(null);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},[]),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:C,broadcast:c,dispatchers:_,scope:i,phase:R,data:s(),handlers:M.current.handlers});const B=n.useMemo(()=>({dispatch(e,t){const n=F(e),r=J(e)?e.channel:void 0;return H(e)?i?K(i.emitter,n,t,r):Promise.resolve():K(z(e)?c:C,n,t,r)},get inspect(){return S.current.inspect},stream:(e,t)=>n.createElement(Ee,{action:F(e),renderer:t})}),[j,C]),D=n.useMemo(()=>[j,B,E],[j,B,E]);return D.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(()=>J(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)},D.dispatch=D[1].dispatch,D}(...t);return e.current=s.dispatch,s},with:{update:e=>Ce(e),invert:e=>Me(e),always:(e,t)=>Ae(e,t)}}),[])}function Ue(t,r){return{Boundary:function({children:t}){const r=n.useMemo(()=>({id:/* @__PURE__ */Symbol("march-hare.scope/instance"),emitter:new p}),[]);/* @__PURE__ */
|
|
7
|
-
return e(te.Provider,{value:r,children:t})},useContext:function(){return _e()},useEnv:function(){return w()},Resource:function(e){return ie(e,t,r)}}}function
|
|
6
|
+
return e(y,{children:/* @__PURE__ */e(T.Provider,{value:s,children:/* @__PURE__ */e(_,{initial:t??{},children:/* @__PURE__ */e(v,{children:/* @__PURE__ */e($,{tap:r,children:/* @__PURE__ */e(U,{children:o})})})})})})}const B=e=>"symbol"==typeof e;function F(e){return r.isString(e)||B(e)?e:(r.isObject(e)||r.isFunction(e))&&x.Action in e?e[x.Action]:e}function z(e){if(r.isString(e))return e.startsWith(P());if(B(e))return e.description?.startsWith(P())??!1;if(r.isObject(e)||r.isFunction(e)){if(x.Broadcast in e&&e[x.Broadcast])return!0;if(x.Action in e){const t=e[x.Action];return t.description?.startsWith(P())??!1}}return!1}function J(e){return r.isObject(e)&&x.Channel in e&&"channel"in e}function D(e){const t=F(e),n=B(t)?t.description??"":t;return n.startsWith(j())&&n.slice(j().length)||null}function H(e){if(r.isString(e))return e.startsWith(S());if(B(e))return e.description?.startsWith(S())??!1;if(r.isObject(e)||r.isFunction(e)){if(x.Multicast in e&&e[x.Multicast])return!0;if(x.Action in e){const t=e[x.Action];return t.description?.startsWith(S())??!1}}return!1}const I=(e="",t=M.Unicast)=>{const n=t===M.Broadcast?Symbol(P(e)):t===M.Multicast?Symbol(S(e)):Symbol(O(e)),r=function(t){return{[x.Action]:n,[x.Payload]:void 0,[x.Channel]:t,[x.Name]:e,channel:t}};return Object.defineProperty(r,x.Action,{value:n,enumerable:!1}),Object.defineProperty(r,x.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,x.Name,{value:e,enumerable:!1}),t===M.Broadcast&&Object.defineProperty(r,x.Broadcast,{value:!0,enumerable:!1}),t===M.Multicast&&Object.defineProperty(r,x.Multicast,{value:!0,enumerable:!1}),r},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(D(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)}const ae=/* @__PURE__ */Symbol("march-hare.coalesce/default");function ue(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 le=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var fe=/* @__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))(fe||{}),de=/* @__PURE__ */(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(de||{}),pe=/* @__PURE__ */(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(pe||{});class he{[s]=!0;static keys=new Set(Object.values(pe));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new he(this.value,this.operation);return n.property=e,n.process=t,n}}class me{static immer=(()=>{c();const e=new i;return e.setAutoFreeze(!1),e})();static tag="κ";static id=le}const ye=/* @__PURE__ */Symbol("Box");function be(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 ve(e){if(r.isNullable(e)||Oe(e))return e;if(r.isArray(e))return e.map(e=>ve(e));if(r.isObject(e)&&we(e)){const t=Object.entries(e).map(([e,t])=>[e,ve(t)]);return{...Object.fromEntries(t),[me.tag]:e[me.tag]??me.id()}}return e}function ge(e){if(Array.isArray(e))return e.filter(e=>me.tag in e).map(e=>e[me.tag]??"").join(",");const t=e[me.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function we(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function Oe(e){return r.isNullable(e)||r.isString(e)||r.isNumber(e)||r.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function Pe(e){return r.isObject(e)&&ye in e}function Se(e,t,n,o,s,c){return function i(a,u=t.path){if(a instanceof he){const t=be(n,u.join("."));if(Object.entries(a).filter(([e,t])=>!he.keys.has(e)&&t instanceof he).forEach(([e,t])=>i(t,u.concat(e))),Oe(a.value)){if(e===de.Hydrate)return a.value;const i=u.slice(0,-1),l=i.length>0?be(n,i.join(".")):n;return r.isNullable(l)||je(l,a,u.at(-1),o,s,c),t??a.value}if(e===de.Hydrate){const e=ve(i(a.value,u));return je(e,a,null,o,s,c),e}const l=t??ve(a.value);return je(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)&&!we(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===de.Hydrate){const e=ve(n);return Object.entries(a).forEach(([t,n])=>{n instanceof he&&Oe(n.value)&&je(e,n,t,o,s,c)}),e}return n}return a}(t.value)}function je(e,t,n,r,o,s){const c=s(e),i=o.get(c)??[];o.set(c,[t.assign(n,r),...i])}class xe{#e={};#t;#n=/* @__PURE__ */new Map;#r=/* @__PURE__ */new Set;#o=!1;constructor(e=ge){this.#t=e}static pk(){return le()}static"κ"=xe.pk;annotate(e,t){return new he(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=be(e(),s),a=s.slice(0,-1),u=o.isNotEmpty(a)?be(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:be(e(),n),inspect:t(n),[ye]:!0}):"is"===a?e=>i(n).some(t=>0!==(t.operation&e)):"draft"===a?()=>o.head(i(n))?.value??be(e(),n):"settled"===a?()=>new Promise(t=>{if(o.isEmpty(i(n)))return t(be(e(),n));const r=()=>{o.isEmpty(i(n))&&(c(r),t(be(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(de.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(de.Produce,e)}#s(e,t){const n=/* @__PURE__ */Symbol("process"),[,r]=me.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>me.immer.applyPatches(t,[{...r,value:Se(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=ve(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 Ee({action:e,renderer:t}){const o=m(),s=n.useContext(T),c=Q(),i=n.useMemo(()=>{const t=s.get(e);if(t)return t;const n=new xe,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 ke(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 Ne(e,t,n){const{cursor:r,key:o}=ke(e,t);r[o]=n}function Ce(e){return(t,n)=>{t.actions.produce(t=>{Ne(t.model,e,n)})}}function Me(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=ke(e,t);n[r]=!n[r]}(t.model,e)})}}function Ae(e,t){return n=>{n.actions.produce(n=>{Ne(n.model,e,t)})}}function _e(){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),P=n.useRef(null),S=n.useRef(new xe);v.current||(v.current=!0,P.current=S.current.hydrate(t));const[j,x]=n.useState(()=>S.current.model),E=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()),C=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),$=n.useRef(/* @__PURE__ */new Set),T=n.useRef(0),W=n.useCallback((e,t,n)=>{const o=new AbortController,s={controller:o,action:e,payload:t};return u.add(s),$.current.add(s),{model:S.current.model,get phase(){return U.current},task:s,data:E,tasks:u,env:l,actions:{produce(e){if(o.signal.aborted)return;const t=f.current,r=S.current.produce(t=>{f.current=a(f.current,n=>{e({model:t,inspect:S.current.inspect,env:n})})});x(S.current.model),f.current!==t&&c.emit(N,f.current),n.processes.add(r),P.current&&(n.processes.add(P.current),P.current=null)},dispatch(e,t){if(o.signal.aborted)return Promise.resolve();const n=F(e),r=J(e)?e.channel:void 0;return H(e)?i?K(i.emitter,n,t,r):Promise.resolve():K(z(e)?c:C,n,t,r)},annotate:(e,t=fe.Update)=>S.current.annotate(t,e),get inspect(){return S.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 H(n)?i?K(i.emitter,r,t,void 0):Promise.resolve():z(n)?K(c,r,t,void 0):Promise.resolve()},n={exceedsWindow:null,coalesceToken:void 0},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(r.isUndefined(n.coalesceToken))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)}|${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=c.get(i);if(a)return ue(a,o.signal);const u=new AbortController,f=e.run(l,u,e.params,t).finally(()=>{c.delete(i)});return c.set(i,f),ue(f,o.signal)})().then(s,c),exceeds:e=>(n.exceedsWindow=e,s),coalesce:e=>(n.coalesceToken=e??ae,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=H(e)?i?.emitter??null:c;if(!n)return null;const s=n.getCached(t);if(r.isUndefined(s))return null;const a=S.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=H(e)?i?.emitter??null:c;return n?n.getCached(t)??null:null}}}},[j]);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=W(e,o,l),m=function(e){const t=F(e),n=r.isString(t)?t:t.description??"";return n.startsWith(O())&&n.slice(n.lastIndexOf("/")+1)||"unknown"}(e),b=performance.now(),v=S.current.model,g=f.current;let w,P=!1;function j(){const e=S.current.model,t=f.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function x(){const t=s===G?void 0:s;return H(e)?i?K(i.emitter,e,o,t):Promise.resolve():K(z(e)?c:C,e,o,t)}function E(e){P=!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:x};c.fire(k,a),n&&t&&C.emit(t,a),h({stage:"end",result:"error",action:{name:m,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:j(),error:i,reason:s}})}function N(){for(const e of u)if(e===p.task){u.delete(e),$.current.delete(e);break}l.processes.forEach(e=>S.current.prune(e)),l.processes.size>0&&y(),P||h({stage:"end",result:"success",action:{name:m,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:j()}}),d.resolve()}h({stage:"start",action:{name:m,payload:o},details:{task:p.task}});try{w=t(p,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}(w)?((async()=>{for await(const e of w);})().catch(E).finally(N),d.promise):(Promise.resolve(w).catch(E).finally(N),d.promise)}}T.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(H(r)){if(i){const e=i.emitter;e.on(r,n),t.add(()=>e.off(r,n))}C.on(r,n),_.multicast.add(r),t.add(()=>C.off(r,n))}else z(r)?(c.on(r,n),C.on(r,n),_.broadcast.add(r),t.add(()=>{c.off(r,n),C.off(r,n)})):(C.on(r,n),t.add(()=>C.off(r,n)))}}),()=>{const e=++T.current,n=new Set(t);queueMicrotask(()=>{if(T.current!==e){for(const e of n)e();return}for(const e of $.current)e.controller.abort(new ee("Component unmounted")),u.delete(e);$.current.clear(),U.current=A.Unmounting;const t=V(M.current.handlers,"Unmount");t&&C.emit(t),U.current=A.Unmounted;for(const e of n)e()})}},[C]),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:C,broadcast:c,dispatchers:_,scope:i,phase:U,data:s(),handlers:M.current.handlers});const B=n.useMemo(()=>({dispatch(e,t){const n=F(e),r=J(e)?e.channel:void 0;return H(e)?i?K(i.emitter,n,t,r):Promise.resolve():K(z(e)?c:C,n,t,r)},get inspect(){return S.current.inspect},stream:(e,t)=>n.createElement(Ee,{action:F(e),renderer:t})}),[j,C]),D=n.useMemo(()=>[j,B,E],[j,B,E]);return D.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(()=>J(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)},D.dispatch=D[1].dispatch,D}(...t);return e.current=s.dispatch,s},with:{update:e=>Ce(e),invert:e=>Me(e),always:(e,t)=>Ae(e,t)}}),[])}function Re(t,r){return{Boundary:function({children:t}){const r=n.useMemo(()=>({id:/* @__PURE__ */Symbol("march-hare.scope/instance"),emitter:new p}),[]);/* @__PURE__ */
|
|
7
|
+
return e(te.Provider,{value:r,children:t})},useContext:function(){return _e()},useEnv:function(){return w()},Resource:function(e){return ie(e,t,r)}}}function Ue(n){const r={current:void 0};return{Boundary:function({children:o}){/* @__PURE__ */
|
|
8
8
|
return t(W,{env:n?.env,tap:n?.tap,children:[
|
|
9
|
-
/* @__PURE__ */e(Le,{holder:r}),o]})},useContext:function(){return _e()},useEnv:function(){return w()},Resource:function(e){return ie(e,n?.cache,()=>r.current)},Scope:()=>
|
|
9
|
+
/* @__PURE__ */e(Le,{holder:r}),o]})},useContext:function(){return _e()},useEnv:function(){return w()},Resource:function(e){return ie(e,n?.cache,()=>r.current)},Scope:()=>Re(n?.cache,()=>r.current)}}function Le({holder:e}){const t=w();return e.current=t,null}const $e={Update:e=>Ce(e),Invert:e=>Me(e),Always:(e,t)=>Ae(e,t)},Te=new xe;function We(e,t=fe.Update){return Te.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 Fe(e,t,n){if(t?.aborted)throw new ee;for(;;){if(await n())return;await Be(e,t)}}function ze(e){return e?Boolean(e&&"symbol"!=typeof e):/* @__PURE__ */Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const Je=/* @__PURE__ */Object.freeze(/* @__PURE__ */Object.defineProperty({__proto__:null,pk:ze,poll:Fe,sleep:Be,unset:q,"ζ":Be,"κ":ze,"π":Fe},Symbol.toStringTag,{value:"Module"})),De=/* @__PURE__ */Object.freeze(/* @__PURE__ */Object.defineProperty({__proto__:null,Resource:function(e){return ie(e)},Scope:function(){return Re()},useContext:function(){return _e()},useEnv:function(){return w()}},Symbol.toStringTag,{value:"Module"}));export{ee as Aborted,I as Action,Ue as App,W as Boundary,ne as Cache,M as Distribution,C as Lifecycle,fe as Op,fe as Operation,Z as Reason,xe as State,$e as With,We as annotate,Pe as isBox,De as shared,Je as utils};
|
|
10
10
|
//# sourceMappingURL=march-hare.js.map
|