@directive-run/react 1.1.1 → 1.3.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/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -1
- package/dist/index.d.ts +24 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +8 -2
package/dist/index.d.ts
CHANGED
|
@@ -57,6 +57,27 @@ declare function useDispatch<S extends ModuleSchema>(system: SingleModuleSystem<
|
|
|
57
57
|
declare function useWatch<S extends ModuleSchema, K extends keyof InferDerivations<S> & string>(system: SingleModuleSystem<S>, key: K, callback: (newValue: InferDerivations<S>[K], prevValue: InferDerivations<S>[K] | undefined) => void): void;
|
|
58
58
|
/** Watch a fact key with auto-detection. */
|
|
59
59
|
declare function useWatch<S extends ModuleSchema, K extends keyof InferFacts<S> & string>(system: SingleModuleSystem<S>, key: K, callback: (newValue: InferFacts<S>[K] | undefined, prevValue: InferFacts<S>[K] | undefined) => void): void;
|
|
60
|
+
/**
|
|
61
|
+
* Fires a system event on a recurring interval while a predicate returns true.
|
|
62
|
+
*
|
|
63
|
+
* Replaces the verbatim `setInterval(() => sys.events.TICK(), ms)` /
|
|
64
|
+
* `clearInterval` pattern that appears across components driving status-gated
|
|
65
|
+
* polling, animation, or timer flows. The hook subscribes to all fact and
|
|
66
|
+
* settled-state changes so the predicate is re-evaluated whenever the system
|
|
67
|
+
* mutates; when it flips to true, an interval starts; when it flips to false,
|
|
68
|
+
* the interval is cleared. Cleans up on unmount.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```tsx
|
|
72
|
+
* useTickWhile(
|
|
73
|
+
* system,
|
|
74
|
+
* (sys) => sys.facts.status === "running",
|
|
75
|
+
* "TICK",
|
|
76
|
+
* 1000,
|
|
77
|
+
* );
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
declare function useTickWhile<S extends ModuleSchema>(system: SingleModuleSystem<S>, predicate: (system: SingleModuleSystem<S>) => boolean, eventName: keyof S["events"] & string, intervalMs: number): void;
|
|
60
81
|
/** Options for useInspect */
|
|
61
82
|
interface UseInspectOptions {
|
|
62
83
|
/** Throttle updates to this interval (ms). When set, uses useState instead of useSyncExternalStore. */
|
|
@@ -419,6 +440,8 @@ declare function createDirectiveContext<M extends ModuleSchema>(system: SingleMo
|
|
|
419
440
|
useSelector: <R>(selector: (state: InferSelectorState<M>) => R, equalityFn?: (a: R, b: R) => boolean) => R;
|
|
420
441
|
/** Watch a key and call a callback on change. */
|
|
421
442
|
useWatch: <K extends string>(key: K, callback: (newValue: unknown, previousValue: unknown) => void) => void;
|
|
443
|
+
/** Fire a system event on an interval while a predicate returns true. */
|
|
444
|
+
useTickWhile: (predicate: (system: SingleModuleSystem<M>) => boolean, eventName: keyof M["events"] & string, intervalMs: number) => void;
|
|
422
445
|
/** Get the current inspection state. */
|
|
423
446
|
useInspect: () => InspectState;
|
|
424
447
|
/** Explain a requirement. */
|
|
@@ -427,4 +450,4 @@ declare function createDirectiveContext<M extends ModuleSchema>(system: SingleMo
|
|
|
427
450
|
useHistory: () => HistoryState | null;
|
|
428
451
|
};
|
|
429
452
|
|
|
430
|
-
export { DirectiveHydrator, type HydratorProps, type OptimisticUpdateResult, type StatusPlugin, type UseDirectiveOptions, type UseDirectiveRefNamespacedOptions, type UseDirectiveRefOptions, type UseDirectiveReturn, type UseDirectiveReturnWithStatus, type UseInspectOptions, createDirectiveContext, createTypedHooks, useConstraintStatus, useDerived, useDirective, useDirectiveRef, useDispatch, useEvents, useExplain, useFact, useHistory, useHydratedSystem, useInspect, useNamespacedSelector, useOptimisticUpdate, useQuerySystem, useRequirementStatus, useSelector, useSuspenseQuery, useSuspenseRequirement, useWatch };
|
|
453
|
+
export { DirectiveHydrator, type HydratorProps, type OptimisticUpdateResult, type StatusPlugin, type UseDirectiveOptions, type UseDirectiveRefNamespacedOptions, type UseDirectiveRefOptions, type UseDirectiveReturn, type UseDirectiveReturnWithStatus, type UseInspectOptions, createDirectiveContext, createTypedHooks, useConstraintStatus, useDerived, useDirective, useDirectiveRef, useDispatch, useEvents, useExplain, useFact, useHistory, useHydratedSystem, useInspect, useNamespacedSelector, useOptimisticUpdate, useQuerySystem, useRequirementStatus, useSelector, useSuspenseQuery, useSuspenseRequirement, useTickWhile, useWatch };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {createSystem,createRequirementStatusPlugin}from'@directive-run/core';import {assertSystem,defaultEquality,runTrackedSelector,createThrottle,buildHistoryState,mergeHydrationFacts,depsChanged,computeInspectState}from'@directive-run/core/adapter-utils';export{shallowEqual}from'@directive-run/core/adapter-utils';import {createContext,useCallback,useSyncExternalStore,useRef,useMemo,useEffect,useState,useContext}from'react';import {jsx}from'react/jsx-runtime';var oe=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var b=Symbol("directive.uninitialized");function Z(e,t){return assertSystem("useFact",e),process.env.NODE_ENV!=="production"&&typeof t=="function"&&console.error("[Directive] useFact() received a function. Did you mean useSelector()? useFact() takes a string key or array of keys, not a selector function."),Array.isArray(t)?Se(e,t):fe(e,t)}function fe(e,t){process.env.NODE_ENV!=="production"&&(t in e.facts.$store.toObject()||console.warn(`[Directive] useFact("${t}") \u2014 fact not found in store. Check that "${t}" is defined in your module's schema.`));let r=useCallback(n=>e.facts.$store.subscribe([t],n),[e,t]),o=useCallback(()=>e.facts.$store.get(t),[e,t]);return useSyncExternalStore(r,o,o)}function Se(e,t){let r=useRef(b),o=useCallback(u=>e.facts.$store.subscribe(t,u),[e,...t]),n=useCallback(()=>{let u={};for(let s of t)u[s]=e.facts.$store.get(s);if(r.current!==b){let s=true;for(let i of t)if(!Object.is(r.current[i],u[i])){s=false;break}if(s)return r.current}return r.current=u,u},[e,...t]);return useSyncExternalStore(o,n,n)}function G(e,t){return assertSystem("useDerived",e),process.env.NODE_ENV!=="production"&&typeof t=="function"&&console.error("[Directive] useDerived() received a function. Did you mean useSelector()? useDerived() takes a string key or array of keys, not a selector function."),Array.isArray(t)?ge(e,t):pe(e,t)}function pe(e,t){process.env.NODE_ENV!=="production"&&(t in e.derive||console.warn(`[Directive] useDerived("${t}") \u2014 derivation not found. Check that "${t}" is defined in your module's derive property.`));let r=useCallback(n=>e.subscribe([t],n),[e,t]),o=useCallback(()=>e.read(t),[e,t]);return useSyncExternalStore(r,o,o)}function ge(e,t){let r=useRef(b),o=useCallback(u=>e.subscribe(t,u),[e,...t]),n=useCallback(()=>{let u={};for(let s of t)u[s]=e.read(s);if(r.current!==b){let s=true;for(let i of t)if(!Object.is(r.current[i],u[i])){s=false;break}if(s)return r.current}return r.current=u,u},[e,...t]);return useSyncExternalStore(o,n,n)}function ye(e,t,r,o){if(e&&e._mode==="namespaced")return ve(e,t,r,o);let n=e,u,s=false,i=o??defaultEquality;r!==void 0&&(u=r,s=true),process.env.NODE_ENV!=="production"&&!n&&!s&&console.error("[Directive] useSelector() received a null/undefined system without a default value. Provide a default value as the 3rd parameter: useSelector(system, selector, defaultValue)");let f=useRef(t),l=useRef(i),c=useRef(u),g=useRef(t),y=useRef(0);g.current!==t&&(g.current=t,y.current++),f.current=t,l.current=i,c.current=u;let K=y.current,v=useRef([]),x=useRef([]),k=useRef(b),m=useRef([]),P=useMemo(()=>n?new Set(Object.keys(n.derive)):new Set,[n]),F=useCallback(()=>n?runTrackedSelector(n,P,f.current):{value:c.current,factKeys:[],deriveKeys:[]},[n,P]),I=useCallback(S=>{if(!n)return ()=>{};let p=()=>{for(let M of m.current)M();m.current=[];let{factKeys:R,deriveKeys:w}=F();v.current=R,x.current=w,R.length>0?m.current.push(n.facts.$store.subscribe(R,()=>{let M=F();depsChanged(v.current,M.factKeys,x.current,M.deriveKeys)&&p(),S();})):w.length===0&&m.current.push(n.facts.$store.subscribeAll(S)),w.length>0&&m.current.push(n.subscribe(w,()=>{let M=F();depsChanged(v.current,M.factKeys,x.current,M.deriveKeys)&&p(),S();}));};return p(),()=>{for(let R of m.current)R();m.current=[];}},[n,F,K]),C=useCallback(()=>{let S;if(!n)S=c.current;else {let{value:p}=F();S=p===void 0&&s?c.current:p;}return k.current!==b&&l.current(k.current,S)?k.current:(k.current=S,S)},[F,n,s]);return useSyncExternalStore(I,C,C)}function ve(e,t,r,o){let n=r!==void 0,u=o??defaultEquality,s=useRef(t),i=useRef(u),f=useRef(r);s.current=t,i.current=u,f.current=r;let l=useRef(b),c=useMemo(()=>Object.keys(e.facts),[e]),g=useCallback(K=>{let v=c.map(x=>`${x}.*`);return e.subscribe(v,K)},[e,c]),y=useCallback(()=>{let K=s.current(e),v=K===void 0&&n?f.current:K;return l.current!==b&&i.current(l.current,v)?l.current:(l.current=v,v)},[e,n]);return useSyncExternalStore(g,y,y)}function J(e){return assertSystem("useDispatch",e),useCallback(t=>{e.dispatch(t);},[e])}function X(e,t,r){assertSystem("useWatch",e);let o=useRef(r);o.current=r,useEffect(()=>e.watch(t,(n,u)=>{o.current(n,u);}),[e,t]);}function Y(e,t){assertSystem("useInspect",e);let r=me(e),o=t?.throttleMs,[n,u]=useState(r),s=useRef(null);return useEffect(()=>{if(!o||o<=0){s.current?.cleanup(),s.current=null;return}return s.current?.cleanup(),s.current=createThrottle((...i)=>{u(i[0]);},o),()=>{s.current?.cleanup(),s.current=null;}},[o]),useEffect(()=>{s.current&&s.current.throttled(r);},[r]),!o||o<=0?r:n}function Me(e){return computeInspectState(e)}function me(e){let t=useRef(null),r=useRef([]),o=useRef([]),n=useRef(null),u=useCallback(i=>{let f=e.facts.$store.subscribeAll(i),l=e.onSettledChange(i);return ()=>{f(),l();}},[e]),s=useCallback(()=>{let i=Me(e),f=i.unmet.length===r.current.length&&i.unmet.every((g,y)=>g.id===r.current[y]),l=i.inflight.length===o.current.length&&i.inflight.every((g,y)=>g.id===o.current[y]),c=i.isSettled===n.current;return f&&l&&c&&t.current?t.current:(t.current=i,r.current=i.unmet.map(g=>g.id),o.current=i.inflight.map(g=>g.id),n.current=i.isSettled,i)},[e]);return useSyncExternalStore(u,s,s)}function Re(e){assertSystem("useHistory",e);let t=useRef(null),r=useCallback(n=>e.onHistoryChange(n),[e]),o=useCallback(()=>{let n=buildHistoryState(e);return n?(t.current&&t.current.canGoBack===n.canGoBack&&t.current.canGoForward===n.canGoForward&&t.current.currentIndex===n.currentIndex&&t.current.totalSnapshots===n.totalSnapshots&&t.current.isPaused===n.isPaused||(t.current=n),t.current):null},[e]);return useSyncExternalStore(r,o,o)}function Ce(e,t){return Array.isArray(t)?be(e,t):he(e,t)}function he(e,t){let r=useRef(b),o=useCallback(u=>e.subscribe(u),[e]),n=useCallback(()=>{let u=e.getStatus(t);if(r.current!==b){let s=r.current;if(s.pending===u.pending&&s.inflight===u.inflight&&s.failed===u.failed&&s.isLoading===u.isLoading&&s.hasError===u.hasError&&s.lastError===u.lastError)return r.current}return r.current=u,u},[e,t]);return useSyncExternalStore(o,n,n)}function be(e,t){let r=useRef(null),o=useRef(""),n=useCallback(s=>e.subscribe(s),[e]),u=useCallback(()=>{let s={},i=[];for(let l of t){let c=e.getStatus(l);s[l]=c,i.push(`${l}:${c.pending}:${c.inflight}:${c.failed}:${c.hasError}:${c.lastError?.message??""}`);}let f=i.join("|");return f!==o.current&&(o.current=f,r.current=s),r.current??s},[e,...t]);return useSyncExternalStore(n,u,u)}var j=new WeakMap;function ee(e){let t=j.get(e);return t||(t=new Map,j.set(e,t)),t}function Ve(e,t){return Array.isArray(t)?xe(e,t):De(e,t)}function De(e,t){let r=e.getStatus(t);if(r.hasError&&r.lastError)throw r.lastError;if(r.isLoading){let o=ee(e),n=o.get(t);throw n||(n=new Promise(u=>{let s=e.subscribe(()=>{e.getStatus(t).isLoading||(o.delete(t),s(),u());});}),o.set(t,n)),n}return r}function xe(e,t){let r={},o=false,n=null;for(let u of t){let s=e.getStatus(u);r[u]=s,s.hasError&&s.lastError&&!n&&(n=s.lastError),s.isLoading&&(o=true);}if(n)throw n;if(o){let u=ee(e),s=t.slice().sort().join(","),i=u.get(s);throw i||(i=new Promise(f=>{let l=e.subscribe(()=>{t.every(g=>!e.getStatus(g).isLoading)&&(u.delete(s),l(),f());});}),u.set(s,i)),i}return r}function H(e,t){let r=useRef(null),o=useRef(null),n=useRef(null),u=t?.status===true,s="modules"in e;return r.current||(n.current=()=>{if(s){let{modules:F,...I}=e,C=t?.plugins??I.plugins??[],S=t?.history??I.history,p=t?.trace??I.trace,R=t?.errorBoundary??I.errorBoundary,w=t?.tickMs??I.tickMs,M=t?.zeroConfig??I.zeroConfig,V=t?.initialFacts??I.initialFacts,U=createSystem({modules:F,plugins:C.length>0?C:void 0,history:S,trace:p,errorBoundary:R,tickMs:w,zeroConfig:M,initialFacts:V});return U.initialize(),typeof window<"u"&&U.start(),U}let i="id"in e&&"schema"in e,f=i?e:e.module,l=i?{}:e,c=t?.plugins??l.plugins??[],g=t?.history??l.history,y=t?.trace??l.trace,K=t?.errorBoundary??l.errorBoundary,v=t?.tickMs??l.tickMs,x=t?.zeroConfig??l.zeroConfig,k=t?.initialFacts??l.initialFacts,m=[...c];u&&(o.current=createRequirementStatusPlugin(),m=[...m,o.current.plugin]);let P=createSystem({module:f,plugins:m.length>0?m:void 0,history:g,trace:y,errorBoundary:K,tickMs:v,zeroConfig:x,initialFacts:k});return P.initialize(),typeof window<"u"&&P.start(),P},r.current=n.current()),useEffect(()=>(!r.current&&n.current&&(r.current=n.current()),()=>{r.current?.destroy(),r.current=null,o.current=null;}),[]),u&&!s?{system:r.current,statusPlugin:o.current}:r.current}function Te(e,t,r){let o=useRef(t);o.current=t;let n=useRef(r);n.current=r;let u=useCallback(i=>e.subscribe(o.current,i),[e]),s=useCallback(()=>n.current(e),[e]);return useSyncExternalStore(u,s,s)}function Ne(e,t={}){let{facts:r,derived:o,status:n,...u}=t,s=r??[],i=o??[],f=s.length===0&&i.length===0,l=n?H(e,{status:true,...u}):H(e,u),c=n?l.system:l,g=n?l.statusPlugin:void 0,y=useMemo(()=>f?Object.keys(c.derive):[],[c,f]),K=useCallback(S=>{let p=[];return f?(p.push(c.facts.$store.subscribeAll(S)),y.length>0&&p.push(c.subscribe(y,S))):(s.length>0&&p.push(c.facts.$store.subscribe(s,S)),i.length>0&&p.push(c.subscribe(i,S))),()=>{for(let R of p)R();}},[c,f,...s,...i,...y]),v=useRef(b),x=useRef(b),k=useRef(null),m=useCallback(()=>{let S,p,R,w;if(f){S=c.facts.$store.toObject(),R=Object.keys(S),p={};for(let h of y)p[h]=c.read(h);w=y;}else {S={};for(let h of s)S[h]=c.facts[h];R=s,p={};for(let h of i)p[h]=c.read(h);w=i;}let M=v.current!==b;if(M){let h=v.current;if(Object.keys(h).length!==R.length)M=false;else for(let O of R)if(!Object.is(h[O],S[O])){M=false;break}}let V=x.current!==b;if(V){let h=x.current;if(Object.keys(h).length!==w.length)V=false;else for(let O of w)if(!Object.is(h[O],p[O])){V=false;break}}let U=M?v.current:S,re=V?x.current:p;return M||(v.current=S),V||(x.current=p),M&&V&&k.current||(k.current={facts:U,derived:re}),k.current},[c,f,...s,...i,...y]),P=useSyncExternalStore(K,m,m),F=useCallback(S=>c.dispatch(S),[c]),I=_(c),C={system:c,dispatch:F,events:I,facts:P.facts,derived:P.derived};return n&&g?{...C,statusPlugin:g}:C}function _(e){return assertSystem("useEvents",e),useMemo(()=>e.events,[e])}function ke(e,t){assertSystem("useExplain",e);let r=useCallback(n=>{let u=e.facts.$store.subscribeAll(n),s=e.onSettledChange(n);return ()=>{u(),s();}},[e]),o=useCallback(()=>e.explain(t),[e,t]);return useSyncExternalStore(r,o,o)}function Ue(e,t){assertSystem("useConstraintStatus",e);let r=Y(e);return useMemo(()=>{let o=e.inspect();return t?o.constraints.find(n=>n.id===t)??null:o.constraints},[e,t,r])}function Oe(e,t,r){assertSystem("useOptimisticUpdate",e);let[o,n]=useState(false),[u,s]=useState(null),i=useRef(null),f=useCallback(()=>{i.current&&(e.restore(i.current),i.current=null),n(false),s(null);},[e]),l=useCallback(c=>{i.current=e.getSnapshot(),n(true),s(null),e.batch(c);},[e]);return useEffect(()=>{if(!(!t||!r||!o))return t.subscribe(()=>{let c=t.getStatus(r);!c.isLoading&&!c.hasError?(i.current=null,n(false)):c.hasError&&(s(c.lastError),f());})},[t,r,o,f]),{mutate:l,isPending:o,error:u,rollback:f}}var te=createContext(null);function qe({snapshot:e,children:t}){return jsx(te.Provider,{value:e,children:t})}function $e(e,t){let r=useContext(te),o=useMemo(()=>{let n=mergeHydrationFacts(r,t?.initialFacts);return {...t??{},initialFacts:n}},[r,t]);return H(e,o)}function Be(){return {useFact:(e,t)=>Z(e,t),useDerived:(e,t)=>G(e,t),useDispatch:e=>J(e),useEvents:e=>_(e),useWatch:(e,t,r)=>X(e,t,r)}}var q=null;function Ie(){if(!q)try{q=oe("@directive-run/query").createQuerySystem;}catch{throw new Error("[Directive] @directive-run/query is not installed. Install it with: pnpm add @directive-run/query")}return q}function He(e){let t=useRef(e),r=useMemo(()=>{let u=Ie();return ()=>u(t.current)},[]),o=useRef(null),n=useRef(r);return n.current=r,o.current||(o.current=n.current()),useEffect(()=>(!o.current&&n.current&&(o.current=n.current()),typeof window<"u"&&o.current&&!o.current.isRunning&&o.current.start(),()=>{o.current?.destroy(),o.current=null;}),[]),o.current}var $=new WeakMap;function _e(e,t){let r=useCallback(s=>e.subscribe([t],s),[e,t]),o=useCallback(()=>e.read(t),[e,t]),n=useSyncExternalStore(r,o,o);$.has(e)||$.set(e,new Map);let u=$.get(e);if(n?.status==="success"&&n?.data!==null)return u.has(t)&&(u.get(t).resolve(),u.delete(t)),n.data;if(n?.status==="error"&&n?.error)throw u.has(t)&&(u.get(t).resolve(),u.delete(t)),n.error;if(!u.has(t)){let s,i=new Promise(l=>{s=l;});u.set(t,{promise:i,resolve:s});let f=e.subscribe([t],()=>{let l=e.read(t);if(l?.status==="success"||l?.status==="error"){let c=u.get(t);c&&(c.resolve(),u.delete(t)),f();}});}throw u.get(t).promise}function We(e){let t=createContext(null);function r(){let n=useContext(t);if(!n)throw new Error("[Directive] useSystem called outside of <Provider>. Wrap your component tree with the Provider from createDirectiveContext().");return n}function o({children:n,system:u}){return jsx(t.Provider,{value:u??e,children:n})}return o.displayName="DirectiveProvider",{Provider:o,useSystem:r,useFact:n=>Z(r(),n),useDerived:n=>G(r(),n),useEvents:()=>_(r()),useDispatch:()=>J(r()),useSelector:(n,u)=>ye(r(),n,u),useWatch:(n,u)=>X(r(),n,u),useInspect:()=>Y(r()),useExplain:n=>ke(r(),n),useHistory:()=>Re(r())}}export{qe as DirectiveHydrator,We as createDirectiveContext,Be as createTypedHooks,Ue as useConstraintStatus,G as useDerived,Ne as useDirective,H as useDirectiveRef,J as useDispatch,_ as useEvents,ke as useExplain,Z as useFact,Re as useHistory,$e as useHydratedSystem,Y as useInspect,Te as useNamespacedSelector,Oe as useOptimisticUpdate,He as useQuerySystem,Ce as useRequirementStatus,ye as useSelector,_e as useSuspenseQuery,Ve as useSuspenseRequirement,X as useWatch};//# sourceMappingURL=index.js.map
|
|
1
|
+
import {createSystem,createRequirementStatusPlugin}from'@directive-run/core';import {assertSystem,defaultEquality,runTrackedSelector,createThrottle,buildHistoryState,mergeHydrationFacts,depsChanged,computeInspectState}from'@directive-run/core/adapter-utils';export{shallowEqual}from'@directive-run/core/adapter-utils';import {createContext,useCallback,useSyncExternalStore,useRef,useMemo,useEffect,useState,useContext}from'react';import {jsx}from'react/jsx-runtime';var ue=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var x=Symbol("directive.uninitialized");function G(e,t){return assertSystem("useFact",e),typeof t=="function"&&console.error("[Directive] useFact() received a function. Did you mean useSelector()? useFact() takes a string key or array of keys, not a selector function."),Array.isArray(t)?pe(e,t):Se(e,t)}function Se(e,t){(t in e.facts.$store.toObject()||console.warn(`[Directive] useFact("${t}") \u2014 fact not found in store. Check that "${t}" is defined in your module's schema.`));let r=useCallback(n=>e.facts.$store.subscribe([t],n),[e,t]),s=useCallback(()=>e.facts.$store.get(t),[e,t]);return useSyncExternalStore(r,s,s)}function pe(e,t){let r=useRef(x),s=useCallback(u=>e.facts.$store.subscribe(t,u),[e,...t]),n=useCallback(()=>{let u={};for(let o of t)u[o]=e.facts.$store.get(o);if(r.current!==x){let o=true;for(let i of t)if(!Object.is(r.current[i],u[i])){o=false;break}if(o)return r.current}return r.current=u,u},[e,...t]);return useSyncExternalStore(s,n,n)}function J(e,t){return assertSystem("useDerived",e),typeof t=="function"&&console.error("[Directive] useDerived() received a function. Did you mean useSelector()? useDerived() takes a string key or array of keys, not a selector function."),Array.isArray(t)?ye(e,t):ge(e,t)}function ge(e,t){(t in e.derive||console.warn(`[Directive] useDerived("${t}") \u2014 derivation not found. Check that "${t}" is defined in your module's derive property.`));let r=useCallback(n=>e.subscribe([t],n),[e,t]),s=useCallback(()=>e.read(t),[e,t]);return useSyncExternalStore(r,s,s)}function ye(e,t){let r=useRef(x),s=useCallback(u=>e.subscribe(t,u),[e,...t]),n=useCallback(()=>{let u={};for(let o of t)u[o]=e.read(o);if(r.current!==x){let o=true;for(let i of t)if(!Object.is(r.current[i],u[i])){o=false;break}if(o)return r.current}return r.current=u,u},[e,...t]);return useSyncExternalStore(s,n,n)}function ve(e,t,r,s){if(e&&e._mode==="namespaced")return Me(e,t,r,s);let n=e,u,o=false,i=s??defaultEquality;r!==void 0&&(u=r,o=true),!n&&!o&&console.error("[Directive] useSelector() received a null/undefined system without a default value. Provide a default value as the 3rd parameter: useSelector(system, selector, defaultValue)");let f=useRef(t),a=useRef(i),c=useRef(u),g=useRef(t),y=useRef(0);g.current!==t&&(g.current=t,y.current++),f.current=t,a.current=i,c.current=u;let F=y.current,v=useRef([]),k=useRef([]),D=useRef(x),m=useRef([]),P=useMemo(()=>n?new Set(Object.keys(n.derive)):new Set,[n]),E=useCallback(()=>n?runTrackedSelector(n,P,f.current):{value:c.current,factKeys:[],deriveKeys:[]},[n,P]),I=useCallback(S=>{if(!n)return ()=>{};let p=()=>{for(let M of m.current)M();m.current=[];let{factKeys:R,deriveKeys:w}=E();v.current=R,k.current=w,R.length>0?m.current.push(n.facts.$store.subscribe(R,()=>{let M=E();depsChanged(v.current,M.factKeys,k.current,M.deriveKeys)&&p(),S();})):w.length===0&&m.current.push(n.facts.$store.subscribeAll(S)),w.length>0&&m.current.push(n.subscribe(w,()=>{let M=E();depsChanged(v.current,M.factKeys,k.current,M.deriveKeys)&&p(),S();}));};return p(),()=>{for(let R of m.current)R();m.current=[];}},[n,E,F]),C=useCallback(()=>{let S;if(!n)S=c.current;else {let{value:p}=E();S=p===void 0&&o?c.current:p;}return D.current!==x&&a.current(D.current,S)?D.current:(D.current=S,S)},[E,n,o]);return useSyncExternalStore(I,C,C)}function Me(e,t,r,s){let n=r!==void 0,u=s??defaultEquality,o=useRef(t),i=useRef(u),f=useRef(r);o.current=t,i.current=u,f.current=r;let a=useRef(x),c=useMemo(()=>Object.keys(e.facts),[e]),g=useCallback(F=>{let v=c.map(k=>`${k}.*`);return e.subscribe(v,F)},[e,c]),y=useCallback(()=>{let F=o.current(e),v=F===void 0&&n?f.current:F;return a.current!==x&&i.current(a.current,v)?a.current:(a.current=v,v)},[e,n]);return useSyncExternalStore(g,y,y)}function X(e){return assertSystem("useDispatch",e),useCallback(t=>{e.dispatch(t);},[e])}function Y(e,t,r){assertSystem("useWatch",e);let s=useRef(r);s.current=r,useEffect(()=>e.watch(t,(n,u)=>{s.current(n,u);}),[e,t]);}function me(e,t,r,s){assertSystem("useTickWhile",e);let n=useRef(t);n.current=t;let u=useCallback(f=>{let a=e.facts.$store.subscribeAll(f),c=e.onSettledChange(f);return ()=>{a(),c();}},[e]),o=useCallback(()=>n.current(e),[e]),i=useSyncExternalStore(u,o,o);useEffect(()=>{if(!i)return;if(!Number.isFinite(s)||s<=0){console.warn(`[Directive] useTickWhile() received non-positive intervalMs: ${s}. Interval will not fire.`);return}let f=setInterval(()=>{let c=e.events[r];typeof c=="function"?c():console.warn(`[Directive] useTickWhile() \u2014 event "${String(r)}" not found on system.events.`);},s);return ()=>clearInterval(f)},[i,e,r,s]);}function ee(e,t){assertSystem("useInspect",e);let r=Re(e),s=t?.throttleMs,[n,u]=useState(r),o=useRef(null);return useEffect(()=>{if(!s||s<=0){o.current?.cleanup(),o.current=null;return}return o.current?.cleanup(),o.current=createThrottle((...i)=>{u(i[0]);},s),()=>{o.current?.cleanup(),o.current=null;}},[s]),useEffect(()=>{o.current&&o.current.throttled(r);},[r]),!s||s<=0?r:n}function he(e){return computeInspectState(e)}function Re(e){let t=useRef(null),r=useRef([]),s=useRef([]),n=useRef(null),u=useCallback(i=>{let f=e.facts.$store.subscribeAll(i),a=e.onSettledChange(i);return ()=>{f(),a();}},[e]),o=useCallback(()=>{let i=he(e),f=i.unmet.length===r.current.length&&i.unmet.every((g,y)=>g.id===r.current[y]),a=i.inflight.length===s.current.length&&i.inflight.every((g,y)=>g.id===s.current[y]),c=i.isSettled===n.current;return f&&a&&c&&t.current?t.current:(t.current=i,r.current=i.unmet.map(g=>g.id),s.current=i.inflight.map(g=>g.id),n.current=i.isSettled,i)},[e]);return useSyncExternalStore(u,o,o)}function be(e){assertSystem("useHistory",e);let t=useRef(null),r=useCallback(n=>e.onHistoryChange(n),[e]),s=useCallback(()=>{let n=buildHistoryState(e);return n?(t.current&&t.current.canGoBack===n.canGoBack&&t.current.canGoForward===n.canGoForward&&t.current.currentIndex===n.currentIndex&&t.current.totalSnapshots===n.totalSnapshots&&t.current.isPaused===n.isPaused||(t.current=n),t.current):null},[e]);return useSyncExternalStore(r,s,s)}function $e(e,t){return Array.isArray(t)?ke(e,t):xe(e,t)}function xe(e,t){let r=useRef(x),s=useCallback(u=>e.subscribe(u),[e]),n=useCallback(()=>{let u=e.getStatus(t);if(r.current!==x){let o=r.current;if(o.pending===u.pending&&o.inflight===u.inflight&&o.failed===u.failed&&o.isLoading===u.isLoading&&o.hasError===u.hasError&&o.lastError===u.lastError)return r.current}return r.current=u,u},[e,t]);return useSyncExternalStore(s,n,n)}function ke(e,t){let r=useRef(null),s=useRef(""),n=useCallback(o=>e.subscribe(o),[e]),u=useCallback(()=>{let o={},i=[];for(let a of t){let c=e.getStatus(a);o[a]=c,i.push(`${a}:${c.pending}:${c.inflight}:${c.failed}:${c.hasError}:${c.lastError?.message??""}`);}let f=i.join("|");return f!==s.current&&(s.current=f,r.current=o),r.current??o},[e,...t]);return useSyncExternalStore(n,u,u)}var _=new WeakMap;function te(e){let t=_.get(e);return t||(t=new Map,_.set(e,t)),t}function qe(e,t){return Array.isArray(t)?Ie(e,t):De(e,t)}function De(e,t){let r=e.getStatus(t);if(r.hasError&&r.lastError)throw r.lastError;if(r.isLoading){let s=te(e),n=s.get(t);throw n||(n=new Promise(u=>{let o=e.subscribe(()=>{e.getStatus(t).isLoading||(s.delete(t),o(),u());});}),s.set(t,n)),n}return r}function Ie(e,t){let r={},s=false,n=null;for(let u of t){let o=e.getStatus(u);r[u]=o,o.hasError&&o.lastError&&!n&&(n=o.lastError),o.isLoading&&(s=true);}if(n)throw n;if(s){let u=te(e),o=t.slice().sort().join(","),i=u.get(o);throw i||(i=new Promise(f=>{let a=e.subscribe(()=>{t.every(g=>!e.getStatus(g).isLoading)&&(u.delete(o),a(),f());});}),u.set(o,i)),i}return r}function H(e,t){let r=useRef(null),s=useRef(null),n=useRef(null),u=t?.status===true,o="modules"in e;return r.current||(n.current=()=>{if(o){let{modules:E,...I}=e,C=t?.plugins??I.plugins??[],S=t?.history??I.history,p=t?.trace??I.trace,R=t?.errorBoundary??I.errorBoundary,w=t?.tickMs??I.tickMs,M=t?.zeroConfig??I.zeroConfig,T=t?.initialFacts??I.initialFacts,q=createSystem({modules:E,plugins:C.length>0?C:void 0,history:S,trace:p,errorBoundary:R,tickMs:w,zeroConfig:M,initialFacts:T});return q.initialize(),typeof window<"u"&&q.start(),q}let i="id"in e&&"schema"in e,f=i?e:e.module,a=i?{}:e,c=t?.plugins??a.plugins??[],g=t?.history??a.history,y=t?.trace??a.trace,F=t?.errorBoundary??a.errorBoundary,v=t?.tickMs??a.tickMs,k=t?.zeroConfig??a.zeroConfig,D=t?.initialFacts??a.initialFacts,m=[...c];u&&(s.current=createRequirementStatusPlugin(),m=[...m,s.current.plugin]);let P=createSystem({module:f,plugins:m.length>0?m:void 0,history:g,trace:y,errorBoundary:F,tickMs:v,zeroConfig:k,initialFacts:D});return P.initialize(),typeof window<"u"&&P.start(),P},r.current=n.current()),useEffect(()=>(!r.current&&n.current&&(r.current=n.current()),()=>{r.current?.destroy(),r.current=null,s.current=null;}),[]),u&&!o?{system:r.current,statusPlugin:s.current}:r.current}function Oe(e,t,r){let s=useRef(t);s.current=t;let n=useRef(r);n.current=r;let u=useCallback(i=>e.subscribe(s.current,i),[e]),o=useCallback(()=>n.current(e),[e]);return useSyncExternalStore(u,o,o)}function Be(e,t={}){let{facts:r,derived:s,status:n,...u}=t,o=r??[],i=s??[],f=o.length===0&&i.length===0,a=n?H(e,{status:true,...u}):H(e,u),c=n?a.system:a,g=n?a.statusPlugin:void 0,y=useMemo(()=>f?Object.keys(c.derive):[],[c,f]),F=useCallback(S=>{let p=[];return f?(p.push(c.facts.$store.subscribeAll(S)),y.length>0&&p.push(c.subscribe(y,S))):(o.length>0&&p.push(c.facts.$store.subscribe(o,S)),i.length>0&&p.push(c.subscribe(i,S))),()=>{for(let R of p)R();}},[c,f,...o,...i,...y]),v=useRef(x),k=useRef(x),D=useRef(null),m=useCallback(()=>{let S,p,R,w;if(f){S=c.facts.$store.toObject(),R=Object.keys(S),p={};for(let b of y)p[b]=c.read(b);w=y;}else {S={};for(let b of o)S[b]=c.facts[b];R=o,p={};for(let b of i)p[b]=c.read(b);w=i;}let M=v.current!==x;if(M){let b=v.current;if(Object.keys(b).length!==R.length)M=false;else for(let O of R)if(!Object.is(b[O],S[O])){M=false;break}}let T=k.current!==x;if(T){let b=k.current;if(Object.keys(b).length!==w.length)T=false;else for(let O of w)if(!Object.is(b[O],p[O])){T=false;break}}let q=M?v.current:S,se=T?k.current:p;return M||(v.current=S),T||(k.current=p),M&&T&&D.current||(D.current={facts:q,derived:se}),D.current},[c,f,...o,...i,...y]),P=useSyncExternalStore(F,m,m),E=useCallback(S=>c.dispatch(S),[c]),I=A(c),C={system:c,dispatch:E,events:I,facts:P.facts,derived:P.derived};return n&&g?{...C,statusPlugin:g}:C}function A(e){return assertSystem("useEvents",e),useMemo(()=>e.events,[e])}function we(e,t){assertSystem("useExplain",e);let r=useCallback(n=>{let u=e.facts.$store.subscribeAll(n),o=e.onSettledChange(n);return ()=>{u(),o();}},[e]),s=useCallback(()=>e.explain(t),[e,t]);return useSyncExternalStore(r,s,s)}function Ne(e,t){assertSystem("useConstraintStatus",e);let r=ee(e);return useMemo(()=>{let s=e.inspect();return t?s.constraints.find(n=>n.id===t)??null:s.constraints},[e,t,r])}function We(e,t,r){assertSystem("useOptimisticUpdate",e);let[s,n]=useState(false),[u,o]=useState(null),i=useRef(null),f=useCallback(()=>{i.current&&(e.restore(i.current),i.current=null),n(false),o(null);},[e]),a=useCallback(c=>{i.current=e.getSnapshot(),n(true),o(null),e.batch(c);},[e]);return useEffect(()=>{if(!(!t||!r||!s))return t.subscribe(()=>{let c=t.getStatus(r);!c.isLoading&&!c.hasError?(i.current=null,n(false)):c.hasError&&(o(c.lastError),f());})},[t,r,s,f]),{mutate:a,isPending:s,error:u,rollback:f}}var ne=createContext(null);function He({snapshot:e,children:t}){return jsx(ne.Provider,{value:e,children:t})}function Ae(e,t){let r=useContext(ne),s=useMemo(()=>{let n=mergeHydrationFacts(r,t?.initialFacts);return {...t??{},initialFacts:n}},[r,t]);return H(e,s)}function Le(){return {useFact:(e,t)=>G(e,t),useDerived:(e,t)=>J(e,t),useDispatch:e=>X(e),useEvents:e=>A(e),useWatch:(e,t,r)=>Y(e,t,r)}}var B=null;function Ke(){if(!B)try{B=ue("@directive-run/query").createQuerySystem;}catch{throw new Error("[Directive] @directive-run/query is not installed. Install it with: pnpm add @directive-run/query")}return B}function je(e){let t=useRef(e),r=useMemo(()=>{let u=Ke();return ()=>u(t.current)},[]),s=useRef(null),n=useRef(r);return n.current=r,s.current||(s.current=n.current()),useEffect(()=>(!s.current&&n.current&&(s.current=n.current()),typeof window<"u"&&s.current&&!s.current.isRunning&&s.current.start(),()=>{s.current?.destroy(),s.current=null;}),[]),s.current}var N=new WeakMap;function _e(e,t){let r=useCallback(o=>e.subscribe([t],o),[e,t]),s=useCallback(()=>e.read(t),[e,t]),n=useSyncExternalStore(r,s,s);N.has(e)||N.set(e,new Map);let u=N.get(e);if(n?.status==="success"&&n?.data!==null)return u.has(t)&&(u.get(t).resolve(),u.delete(t)),n.data;if(n?.status==="error"&&n?.error)throw u.has(t)&&(u.get(t).resolve(),u.delete(t)),n.error;if(!u.has(t)){let o,i=new Promise(a=>{o=a;});u.set(t,{promise:i,resolve:o});let f=e.subscribe([t],()=>{let a=e.read(t);if(a?.status==="success"||a?.status==="error"){let c=u.get(t);c&&(c.resolve(),u.delete(t)),f();}});}throw u.get(t).promise}function ze(e){let t=createContext(null);function r(){let n=useContext(t);if(!n)throw new Error("[Directive] useSystem called outside of <Provider>. Wrap your component tree with the Provider from createDirectiveContext().");return n}function s({children:n,system:u}){return jsx(t.Provider,{value:u??e,children:n})}return s.displayName="DirectiveProvider",{Provider:s,useSystem:r,useFact:n=>G(r(),n),useDerived:n=>J(r(),n),useEvents:()=>A(r()),useDispatch:()=>X(r()),useSelector:(n,u)=>ve(r(),n,u),useWatch:(n,u)=>Y(r(),n,u),useTickWhile:(n,u,o)=>me(r(),n,u,o),useInspect:()=>ee(r()),useExplain:n=>we(r(),n),useHistory:()=>be(r())}}export{He as DirectiveHydrator,ze as createDirectiveContext,Le as createTypedHooks,Ne as useConstraintStatus,J as useDerived,Be as useDirective,H as useDirectiveRef,X as useDispatch,A as useEvents,we as useExplain,G as useFact,be as useHistory,Ae as useHydratedSystem,ee as useInspect,Oe as useNamespacedSelector,We as useOptimisticUpdate,je as useQuerySystem,$e as useRequirementStatus,ve as useSelector,_e as useSuspenseQuery,qe as useSuspenseRequirement,me as useTickWhile,Y as useWatch};//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|