march-hare 0.11.1 → 0.12.1
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/README.md +26 -11
- package/dist/app/index.d.ts +55 -34
- package/dist/app/types.d.ts +0 -25
- package/dist/index.d.ts +2 -2
- package/dist/march-hare.js +6 -6
- package/dist/march-hare.umd.cjs +1 -1
- package/dist/resource/index.d.ts +48 -21
- package/dist/scope/index.d.ts +35 -0
- package/dist/shared/index.d.ts +24 -0
- package/dist/types/index.d.ts +5 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -252,7 +252,7 @@ function useActions() {
|
|
|
252
252
|
}
|
|
253
253
|
```
|
|
254
254
|
|
|
255
|
-
See the [`useContext` recipe](./recipes/use-
|
|
255
|
+
See the [`app.useContext` recipe](./recipes/use-controller.md) for the full pattern.
|
|
256
256
|
|
|
257
257
|
The model defaults to `void`, so a component that only coordinates via events — forwarding broadcasts, triggering side-effects, bridging external systems — can call `context.useActions()` with no initial model:
|
|
258
258
|
|
|
@@ -800,10 +800,24 @@ export const app = App();
|
|
|
800
800
|
|
|
801
801
|
## Reusable components
|
|
802
802
|
|
|
803
|
-
Importing `app` from a single location is fine inside a feature, but it breaks when a component needs to run under **more than one** `App` — a shared `<Profile />` used by both a web app and a mobile shell, for example.
|
|
803
|
+
Importing `app` from a single location is fine inside a feature, but it breaks when a component needs to run under **more than one** `App` — a shared `<Profile />` used by both a web app and a mobile shell, for example. For that case, every `app.X` factory has a **standalone counterpart** on the `shared` namespace that takes the Env shape `E` as its mandatory first generic:
|
|
804
|
+
|
|
805
|
+
| Bound to an App | Standalone (`shared.X`) |
|
|
806
|
+
| ---------------------------- | ---------------------------------------- |
|
|
807
|
+
| `app.useContext<M, A, D>()` | `shared.useContext<E, M, A, D>()` |
|
|
808
|
+
| `app.useEnv()` | `shared.useEnv<E>()` |
|
|
809
|
+
| `app.Resource<T, P>(...)` | `shared.Resource<E, T, P>(...)` |
|
|
810
|
+
| `app.Resource.Cachable(...)` | `shared.Resource.Cachable<E, T, P>(...)` |
|
|
811
|
+
| `app.Scope<A>()` | `shared.Scope<E, A>()` |
|
|
812
|
+
|
|
813
|
+
The standalone forms take the same runtime path as the App-bound ones — `E` is purely a type-level binding the caller supplies so reusable code stays App-agnostic.
|
|
804
814
|
|
|
805
815
|
```tsx
|
|
806
|
-
import {
|
|
816
|
+
import { Action, shared } from "march-hare";
|
|
817
|
+
|
|
818
|
+
type WebEnv = { session: Session | null; locale: string };
|
|
819
|
+
type MobileEnv = { session: Session | null; platform: "ios" | "android" };
|
|
820
|
+
type Envs = WebEnv | MobileEnv;
|
|
807
821
|
|
|
808
822
|
type Model = { name: string | null };
|
|
809
823
|
const model: Model = { name: null };
|
|
@@ -813,8 +827,7 @@ class Actions {
|
|
|
813
827
|
}
|
|
814
828
|
|
|
815
829
|
function useProfileActions() {
|
|
816
|
-
const
|
|
817
|
-
const context = app.useContext<Model, typeof Actions>();
|
|
830
|
+
const context = shared.useContext<Envs, Model, typeof Actions>();
|
|
818
831
|
const actions = context.useActions(model);
|
|
819
832
|
|
|
820
833
|
actions.useAction(Actions.Sign, (context, name) =>
|
|
@@ -837,22 +850,24 @@ export default function Profile(): React.ReactElement {
|
|
|
837
850
|
|
|
838
851
|
Drop `<Profile />` inside `<web.Boundary>` and it reads the web app's env; drop it inside `<mobile.Boundary>` and it reads the mobile app's env. The component never references either `App()` handle.
|
|
839
852
|
|
|
840
|
-
When more than one `App` lives in your repo, declare a union of every Env once and parameterise every reusable component against it.
|
|
853
|
+
When more than one `App` lives in your repo, declare a union of every Env once and parameterise every reusable component against it. Keys present on every member resolve directly; keys on a subset need an `in` / `typeof` guard:
|
|
841
854
|
|
|
842
855
|
```ts
|
|
843
|
-
// shared/
|
|
856
|
+
// shared/envs.ts
|
|
844
857
|
export type WebEnv = { session: Session | null; locale: string };
|
|
845
858
|
export type MobileEnv = {
|
|
846
859
|
session: Session | null;
|
|
847
860
|
platform: "ios" | "android";
|
|
848
861
|
};
|
|
849
|
-
export type
|
|
862
|
+
export type Envs = WebEnv | MobileEnv;
|
|
850
863
|
```
|
|
851
864
|
|
|
852
865
|
```tsx
|
|
866
|
+
import { shared } from "march-hare";
|
|
867
|
+
import type { Envs } from "../shared/envs";
|
|
868
|
+
|
|
853
869
|
function Where(): React.ReactElement {
|
|
854
|
-
const
|
|
855
|
-
const env = app.useEnv();
|
|
870
|
+
const env = shared.useEnv<Envs>();
|
|
856
871
|
|
|
857
872
|
const signedIn = env.session !== null;
|
|
858
873
|
const where = "locale" in env ? env.locale : env.platform;
|
|
@@ -861,6 +876,6 @@ function Where(): React.ReactElement {
|
|
|
861
876
|
}
|
|
862
877
|
```
|
|
863
878
|
|
|
864
|
-
|
|
879
|
+
`shared.Resource<E, T, P>` and `shared.Resource.Cachable<E, T, P>` are the same story for shared resources — declare them at module scope, pass the Env union as the first generic, and the fetcher's `context.env` is typed against it. `shared.Scope<E, A>()` opens a multicast scope without going through an App handle. See the [reusable components recipe](./recipes/reusable-components.md) for the full pattern including discriminator-keyed switches and the `App()`-with-no-env case.
|
|
865
880
|
|
|
866
881
|
For one-line handler binding — flipping a boolean, assigning a payload to a leaf, pinning a field to a fixed value — reach for `context.with.{update,invert,always}`. See the [`With` helpers recipe](./recipes/with-helpers.md) for the full surface.
|
package/dist/app/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Env } from '../boundary/components/env/index';
|
|
2
2
|
import { Actions, Model, Props } from '../types/index';
|
|
3
3
|
import { Scope } from '../scope/index';
|
|
4
|
-
import { AppContextHandle, AppResource
|
|
4
|
+
import { AppContextHandle, AppResource } from './types';
|
|
5
5
|
import { Tap } from '../boundary/components/tap/types';
|
|
6
6
|
import * as React from "react";
|
|
7
|
-
export type { AppArgs, AppContextHandle, AppFetcher, AppResource,
|
|
7
|
+
export type { AppArgs, AppContextHandle, AppFetcher, AppResource, } from './types';
|
|
8
8
|
/**
|
|
9
9
|
* Returned from {@link App}. Bundles the Boundary, hooks, and Resource
|
|
10
10
|
* factory bound to a single typed Env shape `S`.
|
|
@@ -132,32 +132,34 @@ export declare function App<S extends object = Env>(config?: {
|
|
|
132
132
|
tap?: Tap;
|
|
133
133
|
}): App<S>;
|
|
134
134
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* component
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
135
|
+
* Standalone counterpart to `app.useContext`, exported as
|
|
136
|
+
* `shared.useContext` — same call shape, but takes the **Env
|
|
137
|
+
* shape `E` as a mandatory first generic** so the caller can be a
|
|
138
|
+
* reusable component that isn't tied to a single `App` import.
|
|
139
|
+
*
|
|
140
|
+
* `E` is the Env type your component expects to see — usually
|
|
141
|
+
* a union of every App's Env shape it might run under. Inside the
|
|
142
|
+
* handler, `context.env` is typed as `E`; reach for `in` / `typeof`
|
|
143
|
+
* narrowing for keys present on only a subset.
|
|
144
|
+
*
|
|
145
|
+
* Pass `app` directly if you only need to talk to one App —
|
|
146
|
+
* `app.useContext<Model, typeof Actions>()` is shorter and infers the
|
|
147
|
+
* Env from the value. Reach for the standalone form only when a
|
|
148
|
+
* component must support more than one App.
|
|
149
|
+
*
|
|
150
|
+
* @template E The Env shape (or union) the component supports.
|
|
151
|
+
* @template M The model type, or `void`.
|
|
152
|
+
* @template AC The Actions class, or `void`.
|
|
153
|
+
* @template D The reactive data type returned from the `useActions`
|
|
154
|
+
* data callback.
|
|
153
155
|
*
|
|
154
156
|
* @example
|
|
155
157
|
* ```tsx
|
|
156
|
-
* import {
|
|
158
|
+
* import { Action, shared } from "march-hare";
|
|
157
159
|
*
|
|
158
160
|
* type WebEnv = { session: Session | null; locale: string };
|
|
159
161
|
* type MobileEnv = { session: Session | null; platform: "ios" | "android" };
|
|
160
|
-
* type
|
|
162
|
+
* type Envs = WebEnv | MobileEnv;
|
|
161
163
|
*
|
|
162
164
|
* type Model = { name: string | null };
|
|
163
165
|
* const model: Model = { name: null };
|
|
@@ -166,20 +168,39 @@ export declare function App<S extends object = Env>(config?: {
|
|
|
166
168
|
* static Sign = Action<string>("Sign");
|
|
167
169
|
* }
|
|
168
170
|
*
|
|
169
|
-
*
|
|
170
|
-
* const
|
|
171
|
-
* const
|
|
172
|
-
* const context = app.useContext<Model, typeof Actions>();
|
|
173
|
-
* const [view, actions] = context.useActions(model);
|
|
171
|
+
* function useProfileActions() {
|
|
172
|
+
* const context = shared.useContext<Envs, Model, typeof Actions>();
|
|
173
|
+
* const actions = context.useActions(model);
|
|
174
174
|
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* return (
|
|
178
|
-
* <button onClick={() => actions.dispatch(Actions.Sign, "Adam")}>
|
|
179
|
-
* Hey {view.name} ({where})
|
|
180
|
-
* </button>
|
|
175
|
+
* actions.useAction(Actions.Sign, (context, name) =>
|
|
176
|
+
* context.actions.produce(({ model }) => void (model.name = name)),
|
|
181
177
|
* );
|
|
178
|
+
*
|
|
179
|
+
* return actions;
|
|
180
|
+
* }
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
export declare function useContext<E extends object, M extends Model | void = void, A extends Actions | void = void, D extends Props = Props>(): AppContextHandle<M, A, D, E>;
|
|
184
|
+
/**
|
|
185
|
+
* Standalone counterpart to `app.useEnv`, exported as `shared.useEnv`
|
|
186
|
+
* — reads the nearest `<app.Boundary>`'s Env, typed against the
|
|
187
|
+
* Env shape `E` supplied at the call site. For reusable components
|
|
188
|
+
* that need an Env read outside any action handler (e.g. to hand a
|
|
189
|
+
* closure to an external library at module bridge time).
|
|
190
|
+
*
|
|
191
|
+
* @template E The Env shape (or union) the component supports.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```tsx
|
|
195
|
+
* import { shared } from "march-hare";
|
|
196
|
+
*
|
|
197
|
+
* type WebEnv = { session: Session | null };
|
|
198
|
+
* type MobileEnv = { session: Session | null };
|
|
199
|
+
*
|
|
200
|
+
* function SessionBadge() {
|
|
201
|
+
* const env = shared.useEnv<WebEnv | MobileEnv>();
|
|
202
|
+
* return <span>{env.session ? env.session.user.name : "Signed out"}</span>;
|
|
182
203
|
* }
|
|
183
204
|
* ```
|
|
184
205
|
*/
|
|
185
|
-
export declare function
|
|
206
|
+
export declare function useEnv<E extends object>(): Readonly<E>;
|
package/dist/app/types.d.ts
CHANGED
|
@@ -39,31 +39,6 @@ type AppActionsResult<M, AC, D, S> = UseActions<M extends Model | void ? M : voi
|
|
|
39
39
|
* callback as the second.
|
|
40
40
|
*/
|
|
41
41
|
type AppUseActions<M, AC, D, S> = M extends void ? (getData?: Data<D & Props>) => AppActionsResult<M, AC, D, S> : (model: M, getData?: Data<D & Props>) => AppActionsResult<M, AC, D, S>;
|
|
42
|
-
/**
|
|
43
|
-
* Handle returned by {@link useApp}. Same surface as {@link App} minus
|
|
44
|
-
* `Boundary` (which can only be rendered from the App declaration site)
|
|
45
|
-
* and `Scope` (which is module-level, not per-render). Use this inside
|
|
46
|
-
* reusable components that need to run under more than one App.
|
|
47
|
-
*
|
|
48
|
-
* The generic `S` is the Env shape (or union of shapes) the component
|
|
49
|
-
* expects. Pass a union of every App's Env type in your monorepo and
|
|
50
|
-
* read keys that exist on every member directly; reach for `in` /
|
|
51
|
-
* `typeof` type-guards when accessing keys that only exist on a subset.
|
|
52
|
-
*
|
|
53
|
-
* @template S The Env shape (or union) the caller expects to be in scope.
|
|
54
|
-
*/
|
|
55
|
-
export type UseAppHandle<S extends object> = {
|
|
56
|
-
/**
|
|
57
|
-
* Same as `app.useContext` — reads the nearest `<app.Boundary>`'s
|
|
58
|
-
* dispatch surface, with `context.env` typed against `S`.
|
|
59
|
-
*/
|
|
60
|
-
readonly useContext: <M extends Model | void = void, AC extends Actions | void = void, D extends Props = Props>() => AppContextHandle<M, AC, D, S>;
|
|
61
|
-
/**
|
|
62
|
-
* Same as `app.useEnv` — read-only Proxy over the nearest
|
|
63
|
-
* `<app.Boundary>`'s Env, typed as `S`.
|
|
64
|
-
*/
|
|
65
|
-
readonly useEnv: () => Readonly<S>;
|
|
66
|
-
};
|
|
67
42
|
/**
|
|
68
43
|
* `Context` handle returned by `app.useContext()`. Mirrors the base
|
|
69
44
|
* {@link Context} but threads the App's Env shape `S` through every
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export { App
|
|
1
|
+
export { App } from './app/index';
|
|
2
2
|
export { Action } from './action/index';
|
|
3
3
|
export { Distribution, Lifecycle } from './types/index';
|
|
4
4
|
export { With } from './with/index';
|
|
5
5
|
export { Boundary } from './boundary/index';
|
|
6
|
-
export { Resource } from './resource/index';
|
|
7
6
|
export { Cache } from './cache/index';
|
|
8
7
|
export { Reason, Aborted } from './error/index';
|
|
9
8
|
export { annotate } from './annotate/index';
|
|
10
9
|
export { Operation, Op, State } from 'immertation';
|
|
11
10
|
export * as utils from './utils/index';
|
|
11
|
+
export * as shared from './shared/index';
|
|
12
12
|
export type { Fault } from './error/index';
|
|
13
13
|
export type { Adapter } from './cache/index';
|
|
14
14
|
export type { Box } from 'immertation';
|
package/dist/march-hare.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{jsx as e}from"react/jsx-runtime";import*as t from"react";import{G as n,A as r}from"@mobily/ts-belt";import{immerable as o,enablePatches as s,Immer as c,produce as a}from"immer";function i(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var u,l={exports:{}};const f=/* @__PURE__ */i((u||(u=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 a=new o(r,s||e,c),i=n?n+t:t;return e._events[i]?e._events[i].fn?e._events[i]=[e._events[i],a]:e._events[i].push(a):(e._events[i]=a,e._eventsCount++),e}function c(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function a(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=/* @__PURE__ */Object.create(null),(new r).__proto__||(n=!1)),a.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},a.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},a.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},a.prototype.emit=function(e,t,r,o,s,c){var a=n?n+e:e;if(!this._events[a])return!1;var i,u,l=this._events[a],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,i=new Array(f-1);u<f;u++)i[u-1]=arguments[u];l.fn.apply(l.context,i)}else{var d,
|
|
2
|
-
return e(
|
|
3
|
-
return e(b.Provider,{value:r,children:n})}const v=t.createContext({current:{}});function g(){const e=t.useContext(v);return t.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,r){const o=Object.getOwnPropertyDescriptor(e.current,r);if(!n.isUndefined(o))return{...o,configurable:!0}},set(){throw new TypeError("Env is read-only outside `context.actions.produce`. Mutate via produce(({ env }) => { env.x = ... }) instead.")}}),[e])}const w=(e="")=>`march-hare.action/${e}`,O=(e="")=>`march-hare.action/broadcast/${e}`,P=(e="")=>`march-hare.action/multicast/${e}`,
|
|
1
|
+
import{jsx as e}from"react/jsx-runtime";import*as t from"react";import{G as n,A as r}from"@mobily/ts-belt";import{immerable as o,enablePatches as s,Immer as c,produce as a}from"immer";function i(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var u,l={exports:{}};const f=/* @__PURE__ */i((u||(u=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 a=new o(r,s||e,c),i=n?n+t:t;return e._events[i]?e._events[i].fn?e._events[i]=[e._events[i],a]:e._events[i].push(a):(e._events[i]=a,e._eventsCount++),e}function c(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function a(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=/* @__PURE__ */Object.create(null),(new r).__proto__||(n=!1)),a.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},a.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},a.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},a.prototype.emit=function(e,t,r,o,s,c){var a=n?n+e:e;if(!this._events[a])return!1;var i,u,l=this._events[a],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,i=new Array(f-1);u<f;u++)i[u-1]=arguments[u];l.fn.apply(l.context,i)}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(!i)for(d=1,i=new Array(f-1);d<f;d++)i[d-1]=arguments[d];l[u].fn.apply(l[u].context,i)}}return!0},a.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},a.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},a.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 a=this._events[s];if(a.fn)a.fn!==t||o&&!a.once||r&&a.context!==r||c(this,s);else{for(var i=0,u=[],l=a.length;i<l;i++)(a[i].fn!==t||o&&!a[i].once||r&&a[i].context!==r)&&u.push(a[i]);u.length?this._events[s]=1===u.length?u[0]:u:c(this,s)}return this},a.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},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=n,a.EventEmitter=a,e.exports=a}(l)),l.exports));class d extends f{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 p=t.createContext(new d);function h(){return t.useContext(p)}function m({children:n}){const r=t.useMemo(()=>new d,[]);/* @__PURE__ */
|
|
2
|
+
return e(p.Provider,{value:r,children:n})}const b=t.createContext(/* @__PURE__ */new Set);function y({children:n}){const r=t.useMemo(()=>/* @__PURE__ */new Set,[]);/* @__PURE__ */
|
|
3
|
+
return e(b.Provider,{value:r,children:n})}const v=t.createContext({current:{}});function g(){const e=t.useContext(v);return t.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,r){const o=Object.getOwnPropertyDescriptor(e.current,r);if(!n.isUndefined(o))return{...o,configurable:!0}},set(){throw new TypeError("Env is read-only outside `context.actions.produce`. Mutate via produce(({ env }) => { env.x = ... }) instead.")}}),[e])}const w=(e="")=>`march-hare.action/${e}`,O=(e="")=>`march-hare.action/broadcast/${e}`,P=(e="")=>`march-hare.action/multicast/${e}`,S=(e="")=>`march-hare.action.lifecycle/${e}`;class j{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")}function x(e){const t=/* @__PURE__ */Symbol(`march-hare.action.lifecycle/${e}`),n=function(n){return{[j.Action]:t,[j.Payload]:void 0,[j.Channel]:n,[j.Name]:e,channel:n}};return Object.defineProperty(n,j.Action,{value:t,enumerable:!1}),Object.defineProperty(n,j.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(n,j.Name,{value:e,enumerable:!1}),n}const C=Symbol(O("Fault")),E=Symbol(O("Env"));class k{static Mount(){return x("Mount")}static Unmount(){return x("Unmount")}static Error(){return x("Error")}static Update(){return x("Update")}static Fault=(()=>{const e={};return Object.defineProperty(e,j.Action,{value:C,enumerable:!1}),Object.defineProperty(e,j.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,j.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,j.Name,{value:"Fault",enumerable:!1}),e})();static Env=(()=>{const e={};return Object.defineProperty(e,j.Action,{value:E,enumerable:!1}),Object.defineProperty(e,j.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,j.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,j.Name,{value:"Env",enumerable:!1}),e})()}var N=/* @__PURE__ */(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(N||{}),M=/* @__PURE__ */(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(M||{});function A({initial:r,children:o}){const s=t.useRef(r),c=h();return n.isUndefined(c.getCached(E))&&c.setCache(E,s.current),/* @__PURE__ */e(v.Provider,{value:s,children:o})}const _=t.createContext(/* @__PURE__ */new WeakMap);function R({children:n}){const r=t.useMemo(()=>/* @__PURE__ */new WeakMap,[]);/* @__PURE__ */
|
|
4
4
|
return e(_.Provider,{value:r,children:n})}const U=t.createContext(()=>{});function L({tap:n,children:r}){const o=t.useRef(n);t.useLayoutEffect(()=>{o.current=n},[n]);const s=t.useMemo(()=>e=>o.current?.(e),[]);/* @__PURE__ */
|
|
5
5
|
return e(U.Provider,{value:s,children:r})}function T({env:t,tap:n,children:r}){/* @__PURE__ */
|
|
6
|
-
return e(m,{children:/* @__PURE__ */e(A,{initial:t??{},children:/* @__PURE__ */e(y,{children:/* @__PURE__ */e(L,{tap:n,children:/* @__PURE__ */e(R,{children:r})})})})})}const W=e=>"symbol"==typeof e;function $(e){return n.isString(e)||W(e)?e:(n.isObject(e)||n.isFunction(e))&&j.Action in e?e[j.Action]:e}function B(e){if(n.isString(e))return e.startsWith(O());if(W(e))return e.description?.startsWith(O())??!1;if(n.isObject(e)||n.isFunction(e)){if(j.Broadcast in e&&e[j.Broadcast])return!0;if(j.Action in e){const t=e[j.Action];return t.description?.startsWith(O())??!1}}return!1}function F(e){return n.isObject(e)&&j.Channel in e&&"channel"in e}function D(e){const t=$(e),n=W(t)?t.description??"":t;return n.startsWith(x())&&n.slice(x().length)||null}function H(e){if(n.isString(e))return e.startsWith(P());if(W(e))return e.description?.startsWith(P())??!1;if(n.isObject(e)||n.isFunction(e)){if(j.Multicast in e&&e[j.Multicast])return!0;if(j.Action in e){const t=e[j.Action];return t.description?.startsWith(P())??!1}}return!1}const I=(e="",t=N.Unicast)=>{const n=t===N.Broadcast?Symbol(O(e)):t===N.Multicast?Symbol(P(e)):Symbol(w(e)),r=function(t){return{[j.Action]:n,[j.Payload]:void 0,[j.Channel]:t,[j.Name]:e,channel:t}};return Object.defineProperty(r,j.Action,{value:n,enumerable:!1}),Object.defineProperty(r,j.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,j.Name,{value:e,enumerable:!1}),t===N.Broadcast&&Object.defineProperty(r,j.Broadcast,{value:!0,enumerable:!1}),t===N.Multicast&&Object.defineProperty(r,j.Multicast,{value:!0,enumerable:!1}),r},J=Symbol(((e="")=>`march-hare/replay${e}`)());function z(e,t,...n){e instanceof d&&e.setCache(t,n[0]);const r=e.listeners(t);return 0===r.length?Promise.resolve():Promise.all(r.map(e=>Promise.resolve(e(...n)))).then(()=>{})}function q(e,t){for(const n of e.keys())if(D(n)===t)return n;return null}const G=/* @__PURE__ */Symbol("march-hare.unset");function K(){const[,e]=t.useReducer(e=>e+1,0);return e}function V(){return{data:G,at:null,else:e=>e}}function Q(e,t){return{data:e,at:t,else:t=>e}}var X=/* @__PURE__ */(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(X||{});class Y extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Z=t.createContext(null);function ee(e){const t=/* @__PURE__ */new Map,r=e??{get:e=>t.get(e)??null,set:(e,n)=>{t.set(e,n)},remove:e=>{t.delete(e)},clear:()=>{t.clear()}};return{get(e){try{const t=r.get(e);if(n.isNull(t))return V();const o=JSON.parse(t);return Q(o.data,Temporal.Instant.from(o.at))}catch{return V()}},set(e,t){if(t.data===G||n.isNull(t.at))return!1;try{return r.set(e,JSON.stringify({data:t.data,at:t.at.toString()})),!0}catch{return!1}},remove(e){r.remove(e)},clear(){r.clear()}}}function te(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new Y);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new Y)},{once:!0})})}async function ne(e,t,n){if(t?.aborted)throw new Y;for(;;){if(await n())return;await te(e,t)}}function re(e){return e?Boolean(e&&"symbol"!=typeof e):/* @__PURE__ */Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const oe=/* @__PURE__ */Object.freeze(/* @__PURE__ */Object.defineProperty({__proto__:null,pk:re,poll:ne,sleep:te,unset:G,"ζ":te,"κ":re,"π":ne},Symbol.toStringTag,{value:"Module"})),se=/* @__PURE__ */new WeakMap;function ce(e){return JSON.stringify(e)}let ae=null;function ie(){if(n.isNull(ae))throw new Error("context.actions.resource(...) and context.actions.resource.set(...) must be called with a fresh resource invocation, e.g. context.actions.resource(resource.cat({ id: 5 })).");const e=ae;return ae=null,e}function ue(e,t){const r=e=>{const r=t.get(ce(e));return r.data===G||n.isNull(r.at)?{data:G,at:null}:{data:r.data,at:r.at}},o=(n,r,o,s)=>e({env:n,controller:r,params:o,dispatch:s}).then(e=>(t.set(ce(o),Q(e,Temporal.Now.instant())),e)),s=(e,n,r)=>{t.set(ce(e),Q(n,r))};return function(e){const t=e??{};ae={run:o,read:r,seed:s,params:t},queueMicrotask(()=>{n.isNotNullable(ae)&&ae.params===t&&(ae=null)});const{data:c}=r(t);return c===G?null:c}}function le(e){return ue(e,function(e){let t=se.get(e);return n.isUndefined(t)&&(t=ee(),se.set(e,t)),t}(e))}(le||(le={})).Cachable=function(e,t){return ue(t,e)};const fe=/* @__PURE__ */Symbol("march-hare.coalesce/default");function de(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 he=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var pe=/* @__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))(pe||{}),me=/* @__PURE__ */(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(me||{}),be=/* @__PURE__ */(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(be||{});class ye{[o]=!0;static keys=new Set(Object.values(be));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new ye(this.value,this.operation);return n.property=e,n.process=t,n}}class ve{static immer=(()=>{s();const e=new c;return e.setAutoFreeze(!1),e})();static tag="κ";static id=he}function ge(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 we(e){if(n.isNullable(e)||xe(e))return e;if(n.isArray(e))return e.map(e=>we(e));if(n.isObject(e)&&Pe(e)){const t=Object.entries(e).map(([e,t])=>[e,we(t)]);return{...Object.fromEntries(t),[ve.tag]:e[ve.tag]??ve.id()}}return e}function Oe(e){if(Array.isArray(e))return e.filter(e=>ve.tag in e).map(e=>e[ve.tag]??"").join(",");const t=e[ve.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function Pe(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function xe(e){return n.isNullable(e)||n.isString(e)||n.isNumber(e)||n.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function je(e,t,r,o,s,c){return function a(i,u=t.path){if(i instanceof ye){const t=ge(r,u.join("."));if(Object.entries(i).filter(([e,t])=>!ye.keys.has(e)&&t instanceof ye).forEach(([e,t])=>a(t,u.concat(e))),xe(i.value)){if(e===me.Hydrate)return i.value;const a=u.slice(0,-1),l=a.length>0?ge(r,a.join(".")):r;return n.isNullable(l)||Ce(l,i,u.at(-1),o,s,c),t??i.value}if(e===me.Hydrate){const e=we(a(i.value,u));return Ce(e,i,null,o,s,c),e}const l=t??we(i.value);return Ce(l,i,null,o,s,c),n.isNullable(t)?l:(a(i.value,u),t)}if(n.isArray(i))return i.map((e,t)=>a(e,u.concat(t)));if(n.isObject(i)&&!Pe(i))return i;if(n.isObject(i)){const t=Object.entries(i).map(([e,t])=>[e,a(t,u.concat(e))]),n=Object.fromEntries(t);if(e===me.Hydrate){const e=we(n);return Object.entries(i).forEach(([t,n])=>{n instanceof ye&&xe(n.value)&&Ce(e,n,t,o,s,c)}),e}return n}return i}(t.value)}function Ce(e,t,n,r,o,s){const c=s(e),a=o.get(c)??[];o.set(c,[t.assign(n,r),...a])}class Ee{#e={};#t;#n=/* @__PURE__ */new Map;#r=/* @__PURE__ */new Set;#o=!1;constructor(e=Oe){this.#t=e}static pk(){return he()}static"κ"=Ee.pk;annotate(e,t){return new ye(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,t,o,s,c){function a(s){const c=s.at(-1),a=ge(e(),s),i=s.slice(0,-1),u=r.isNotEmpty(i)?ge(e(),i):e();return[...n.isObject(a)||n.isArray(a)?t.get(o(a))?.filter(e=>n.isNullable(e.property))??[]:[],...n.isObject(u)?t.get(o(u))?.filter(e=>e.property===c)??[]:[]]}return function t(n){return new Proxy(()=>{},{get:(o,i)=>"pending"===i?()=>!r.isEmpty(a(n)):"remaining"===i?()=>r.length(a(n)):"box"===i?()=>({value:ge(e(),n),inspect:t(n)}):"is"===i?e=>a(n).some(t=>0!==(t.operation&e)):"draft"===i?()=>r.head(a(n))?.value??ge(e(),n):"settled"===i?()=>new Promise(t=>{if(r.isEmpty(a(n)))return t(ge(e(),n));const o=()=>{r.isEmpty(a(n))&&(c(o),t(ge(e(),n)))};s(o)}):t([...n,String(i)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#s(me.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(me.Produce,e)}#s(e,t){const n=/* @__PURE__ */Symbol("process"),[,r]=ve.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>ve.immer.applyPatches(t,[{...r,value:je(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=we(this.#e),this.#c(),n}prune(e){this.#n.forEach((t,n)=>{const o=t.filter(t=>t.process!==e);r.isEmpty(o)?this.#n.delete(n):this.#n.set(n,o)}),this.#c()}#c(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}const Se=t.createContext(/* @__PURE__ */new Map);function ke({action:e,renderer:r}){const o=p(),s=t.useContext(Se),c=K(),a=t.useMemo(()=>{const t=s.get(e);if(t)return t;const r=new Ee,c=o.getCached(e);n.isNotNullable(c)&&r.hydrate({value:c});const a={state:r,listeners:/* @__PURE__ */new Set};return s.set(e,a),a},[e,o,s]);t.useLayoutEffect(()=>{function t(e){a.state.hydrate({value:e}),a.listeners.forEach(e=>e())}return a.listeners.add(c),o.on(e,t),()=>{a.listeners.delete(c),o.off(e,t)}},[e,o,a]);const i=a.state.model?.value;return n.isNullable(i)?null:r(i,a.state.inspect.value)}function Ne(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 Me(e,t,n){const{cursor:r,key:o}=Ne(e,t);r[o]=n}function Ae(e){return(t,n)=>{t.actions.produce(t=>{Me(t.model,e,n)})}}function _e(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=Ne(e,t);n[r]=!n[r]}(t.model,e)})}}function Re(e,t){return n=>{n.actions.produce(n=>{Me(n.model,e,t)})}}const Ue={Update:e=>Ae(e),Invert:e=>_e(e),Always:(e,t)=>Re(e,t)};function Le(){const e=t.useRef(null);return t.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(...o){const s=function(...e){const o=n.isUndefined(e[0])||n.isFunction(e[0])?{}:e[0],s=n.isFunction(e[0])?e[0]:e[1]??(()=>({})),c=p(),i=t.useContext(Z),u=t.useContext(b),l=g(),d=t.useContext(v),h=t.useContext(_),m=t.useContext(U),y=K(),O=t.useRef(!1),P=t.useRef(null),x=t.useRef(new Ee);O.current||(O.current=!0,P.current=x.current.hydrate(o));const[j,C]=t.useState(()=>x.current.model),k=function(e){const n=t.useRef(e);return n.current=e,t.useMemo(()=>{return t=n,Object.keys(e).reduce((e,n)=>(Object.defineProperty(e,n,{get:()=>t.current[n],enumerable:!0}),e),{});var t},[e])}(s()),N=t.useMemo(()=>new f,[]),A=t.useRef({handlers:/* @__PURE__ */new Map});A.current.handlers=/* @__PURE__ */new Map;const R=function(){const e=t.useRef(/* @__PURE__ */new Set),n=t.useRef(/* @__PURE__ */new Set);return t.useMemo(()=>({broadcast:e.current,multicast:n.current}),[])}(),L=t.useRef(M.Mounting),T=t.useRef(/* @__PURE__ */new Set),W=t.useRef(0),D=t.useCallback((e,t,r)=>{const o=new AbortController,s={controller:o,action:e,payload:t};return u.add(s),T.current.add(s),{model:x.current.model,get phase(){return L.current},task:s,data:k,tasks:u,env:l,actions:{produce(e){if(o.signal.aborted)return;const t=d.current,n=x.current.produce(t=>{d.current=a(d.current,n=>{e({model:t,inspect:x.current.inspect,env:n})})});C(x.current.model),d.current!==t&&c.emit(S,d.current),r.processes.add(n),P.current&&(r.processes.add(P.current),P.current=null)},dispatch(e,t){if(o.signal.aborted)return Promise.resolve();const n=$(e),r=F(e)?e.channel:void 0;return H(e)?i?z(i.emitter,n,t,r):Promise.resolve():z(B(e)?c:N,n,t,r)},annotate:(e,t=pe.Update)=>x.current.annotate(t,e),get inspect(){return x.current.inspect},resource:Object.assign(function(e){const t=ie(),r=(e,t)=>{if(o.signal.aborted)return Promise.resolve();const n=e,r=$(n);return H(n)?i?z(i.emitter,r,t,void 0):Promise.resolve():B(n)?z(c,r,t,void 0):Promise.resolve()},s={exceedsWindow:null,coalesceToken:void 0},a={then:(e,c)=>(()=>{if(n.isNotNullable(s.exceedsWindow)){const{data:e,at:r}=t.read(t.params);if(e!==G&&n.isNotNullable(r)){const t=Temporal.Now.instant().since(r),n=Temporal.Duration.from(s.exceedsWindow);if(Temporal.Duration.compare(t,n)<=0)return Promise.resolve(e)}}if(n.isUndefined(s.coalesceToken))return t.run(l,o,t.params,r);let e=h.get(t.run);n.isUndefined(e)&&(e=/* @__PURE__ */new Map,h.set(t.run,e));const c=e,a=`${JSON.stringify(t.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)}`}}(s.coalesceToken)}`,i=c.get(a);if(i)return de(i,o.signal);const u=new AbortController,f=t.run(l,u,t.params,r).finally(()=>{c.delete(a)});return c.set(a,f),de(f,o.signal)})().then(e,c),exceeds:e=>(s.exceedsWindow=e,a),coalesce:e=>(s.coalesceToken=e??fe,a)};return a},{set:(e,t)=>{const n=ie();n.seed(n.params,t,Temporal.Now.instant())}}),async final(e){if(o.signal.aborted)return null;const t=$(e),r=H(e)?i?.emitter??null:c;if(!r)return null;const s=r.getCached(t);if(n.isUndefined(s))return null;const a=x.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()})}),r.getCached(t)??null},peek(e){if(o.signal.aborted)return null;const t=$(e),n=H(e)?i?.emitter??null:c;return n?n.getCached(t)??null:null}}}},[j]);t.useLayoutEffect(()=>{function e(e,t,r){return function(o,s){const a=r();if(s===J&&n.isNotNullable(a))return;if(n.isNotNullable(s)&&s!==J&&n.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},f=Promise.withResolvers(),h=D(e,o,l),p=function(e){const t=$(e),r=n.isString(t)?t:t.description??"";return r.startsWith(w())&&r.slice(r.lastIndexOf("/")+1)||"unknown"}(e),b=performance.now(),v=x.current.model,g=d.current;let O,P=!1;function j(){const e=x.current.model,t=d.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function C(){const t=s===J?void 0:s;return H(e)?i?z(i.emitter,e,o,t):Promise.resolve():z(B(e)?c:N,e,o,t)}function S(e){P=!0;const t=q(A.current.handlers,"Error"),r=n.isNotNullable(t),s=function(e){return e instanceof Error&&"AbortError"===e.name?X.Aborted:X.Errored}(e),a=function(e){return e instanceof Error?e:new Error(String(e))}(e),i={reason:s,error:a,action:p,handled:r,tasks:u,retry:C};c.fire(E,i),r&&t&&N.emit(t,i),m({stage:"end",result:"error",action:{name:p,payload:o},details:{task:h.task,elapsed:performance.now()-b,mutations:j(),error:a,reason:s}})}function k(){for(const e of u)if(e===h.task){u.delete(e),T.current.delete(e);break}l.processes.forEach(e=>x.current.prune(e)),l.processes.size>0&&y(),P||m({stage:"end",result:"success",action:{name:p,payload:o},details:{task:h.task,elapsed:performance.now()-b,mutations:j()}}),f.resolve()}m({stage:"start",action:{name:p,payload:o},details:{task:h.task}});try{O=t(h,o)}catch(M){return S(M),k(),f.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}(O)?((async()=>{for await(const e of O);})().catch(S).finally(k),f.promise):(Promise.resolve(O).catch(S).finally(k),f.promise)}}W.current++;const t=/* @__PURE__ */new Set;return A.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))}N.on(r,n),R.multicast.add(r),t.add(()=>N.off(r,n))}else B(r)?(c.on(r,n),N.on(r,n),R.broadcast.add(r),t.add(()=>{c.off(r,n),N.off(r,n)})):(N.on(r,n),t.add(()=>N.off(r,n)))}}),()=>{const e=++W.current,n=new Set(t);queueMicrotask(()=>{if(W.current!==e){for(const e of n)e();return}for(const e of T.current)e.controller.abort(new Y("Component unmounted")),u.delete(e);T.current.clear(),L.current=M.Unmounting;const t=q(A.current.handlers,"Unmount");t&&N.emit(t),L.current=M.Unmounted;for(const e of n)e()})}},[N]),function({unicast:e,broadcast:o,dispatchers:s,scope:c,phase:a,data:i,handlers:u}){const l=t.useRef(null);t.useLayoutEffect(()=>{if(a.current===M.Mounted)return;a.current=M.Mounting;const t=q(u,"Mount");t&&e.emit(t),s.broadcast.forEach(t=>{const r=o.getCached(t);n.isNullable(r)||e.emit(t,r,J)}),c&&s.multicast.forEach(t=>{const r=c.emitter.getCached(t);n.isNullable(r)||e.emit(t,r,J)}),a.current=M.Mounted},[]),t.useLayoutEffect(()=>{if(n.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,i);if(r.isNotEmpty(Object.keys(t))){const n=q(u,"Update");n&&e.emit(n,t)}}l.current=i},[i,e])}({unicast:N,broadcast:c,dispatchers:R,scope:i,phase:L,data:s(),handlers:A.current.handlers});const I=t.useMemo(()=>({dispatch(e,t){const n=$(e),r=F(e)?e.channel:void 0;return H(e)?i?z(i.emitter,n,t,r):Promise.resolve():z(B(e)?c:N,n,t,r)},get inspect(){return x.current.inspect},stream:(e,n)=>t.createElement(ke,{action:$(e),renderer:n})}),[j,N]),V=t.useMemo(()=>[j,I,k],[j,I,k]);return V.useAction=(e,n)=>{!function(e,n,r){const o=t.useRef(r);t.useLayoutEffect(()=>{o.current=r});const s=t.useRef(n);t.useLayoutEffect(()=>{s.current=n});const c=t.useCallback((e,t)=>o.current(e,t),[]),a=t.useCallback(()=>F(s.current)?s.current.channel:void 0,[]),i=$(n),u=e.current.handlers.get(i)??/* @__PURE__ */new Set;0===u.size&&e.current.handlers.set(i,u),u.add({getChannel:a,handler:c})}(A,e,n)},V.dispatch=V[1].dispatch,V}(...o);return e.current=s.dispatch,s},with:{update:e=>Ae(e),invert:e=>_e(e),always:(e,t)=>Re(e,t)}}),[])}function Te(n){return{Boundary:function({children:t}){/* @__PURE__ */
|
|
7
|
-
return e(
|
|
8
|
-
return e(
|
|
6
|
+
return e(m,{children:/* @__PURE__ */e(A,{initial:t??{},children:/* @__PURE__ */e(y,{children:/* @__PURE__ */e(L,{tap:n,children:/* @__PURE__ */e(R,{children:r})})})})})}const W=e=>"symbol"==typeof e;function $(e){return n.isString(e)||W(e)?e:(n.isObject(e)||n.isFunction(e))&&j.Action in e?e[j.Action]:e}function B(e){if(n.isString(e))return e.startsWith(O());if(W(e))return e.description?.startsWith(O())??!1;if(n.isObject(e)||n.isFunction(e)){if(j.Broadcast in e&&e[j.Broadcast])return!0;if(j.Action in e){const t=e[j.Action];return t.description?.startsWith(O())??!1}}return!1}function F(e){return n.isObject(e)&&j.Channel in e&&"channel"in e}function z(e){const t=$(e),n=W(t)?t.description??"":t;return n.startsWith(S())&&n.slice(S().length)||null}function D(e){if(n.isString(e))return e.startsWith(P());if(W(e))return e.description?.startsWith(P())??!1;if(n.isObject(e)||n.isFunction(e)){if(j.Multicast in e&&e[j.Multicast])return!0;if(j.Action in e){const t=e[j.Action];return t.description?.startsWith(P())??!1}}return!1}const H=(e="",t=N.Unicast)=>{const n=t===N.Broadcast?Symbol(O(e)):t===N.Multicast?Symbol(P(e)):Symbol(w(e)),r=function(t){return{[j.Action]:n,[j.Payload]:void 0,[j.Channel]:t,[j.Name]:e,channel:t}};return Object.defineProperty(r,j.Action,{value:n,enumerable:!1}),Object.defineProperty(r,j.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,j.Name,{value:e,enumerable:!1}),t===N.Broadcast&&Object.defineProperty(r,j.Broadcast,{value:!0,enumerable:!1}),t===N.Multicast&&Object.defineProperty(r,j.Multicast,{value:!0,enumerable:!1}),r},I=Symbol(((e="")=>`march-hare/replay${e}`)());function J(e,t,...n){e instanceof d&&e.setCache(t,n[0]);const r=e.listeners(t);return 0===r.length?Promise.resolve():Promise.all(r.map(e=>Promise.resolve(e(...n)))).then(()=>{})}function q(e,t){for(const n of e.keys())if(z(n)===t)return n;return null}const G=/* @__PURE__ */Symbol("march-hare.unset");function K(){const[,e]=t.useReducer(e=>e+1,0);return e}function V(){return{data:G,at:null,else:e=>e}}function Q(e,t){return{data:e,at:t,else:t=>e}}var X=/* @__PURE__ */(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(X||{});class Y extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Z=t.createContext(null);function ee(e){const t=/* @__PURE__ */new Map,r=e??{get:e=>t.get(e)??null,set:(e,n)=>{t.set(e,n)},remove:e=>{t.delete(e)},clear:()=>{t.clear()}};return{get(e){try{const t=r.get(e);if(n.isNull(t))return V();const o=JSON.parse(t);return Q(o.data,Temporal.Instant.from(o.at))}catch{return V()}},set(e,t){if(t.data===G||n.isNull(t.at))return!1;try{return r.set(e,JSON.stringify({data:t.data,at:t.at.toString()})),!0}catch{return!1}},remove(e){r.remove(e)},clear(){r.clear()}}}function te(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new Y);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new Y)},{once:!0})})}async function ne(e,t,n){if(t?.aborted)throw new Y;for(;;){if(await n())return;await te(e,t)}}function re(e){return e?Boolean(e&&"symbol"!=typeof e):/* @__PURE__ */Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const oe=/* @__PURE__ */Object.freeze(/* @__PURE__ */Object.defineProperty({__proto__:null,pk:re,poll:ne,sleep:te,unset:G,"ζ":te,"κ":re,"π":ne},Symbol.toStringTag,{value:"Module"})),se=/* @__PURE__ */new WeakMap;function ce(e){return JSON.stringify(e)}let ae=null;function ie(){if(n.isNull(ae))throw new Error("context.actions.resource(...) and context.actions.resource.set(...) must be called with a fresh resource invocation, e.g. context.actions.resource(resource.cat({ id: 5 })).");const e=ae;return ae=null,e}function ue(e,t){const r=e=>{const r=t.get(ce(e));return r.data===G||n.isNull(r.at)?{data:G,at:null}:{data:r.data,at:r.at}},o=(n,r,o,s)=>e({env:n,controller:r,params:o,dispatch:s}).then(e=>(t.set(ce(o),Q(e,Temporal.Now.instant())),e)),s=(e,n,r)=>{t.set(ce(e),Q(n,r))};return function(e){const t=e??{};ae={run:o,read:r,seed:s,params:t},queueMicrotask(()=>{n.isNotNullable(ae)&&ae.params===t&&(ae=null)});const{data:c}=r(t);return c===G?null:c}}function le(e){return ue(e,function(e){let t=se.get(e);return n.isUndefined(t)&&(t=ee(),se.set(e,t)),t}(e))}(le||(le={})).Cachable=function(e,t){return ue(t,e)};const fe=/* @__PURE__ */Symbol("march-hare.coalesce/default");function de(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 pe=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var he=/* @__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))(he||{}),me=/* @__PURE__ */(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(me||{}),be=/* @__PURE__ */(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(be||{});class ye{[o]=!0;static keys=new Set(Object.values(be));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new ye(this.value,this.operation);return n.property=e,n.process=t,n}}class ve{static immer=(()=>{s();const e=new c;return e.setAutoFreeze(!1),e})();static tag="κ";static id=pe}function ge(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 we(e){if(n.isNullable(e)||Se(e))return e;if(n.isArray(e))return e.map(e=>we(e));if(n.isObject(e)&&Pe(e)){const t=Object.entries(e).map(([e,t])=>[e,we(t)]);return{...Object.fromEntries(t),[ve.tag]:e[ve.tag]??ve.id()}}return e}function Oe(e){if(Array.isArray(e))return e.filter(e=>ve.tag in e).map(e=>e[ve.tag]??"").join(",");const t=e[ve.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function Pe(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function Se(e){return n.isNullable(e)||n.isString(e)||n.isNumber(e)||n.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function je(e,t,r,o,s,c){return function a(i,u=t.path){if(i instanceof ye){const t=ge(r,u.join("."));if(Object.entries(i).filter(([e,t])=>!ye.keys.has(e)&&t instanceof ye).forEach(([e,t])=>a(t,u.concat(e))),Se(i.value)){if(e===me.Hydrate)return i.value;const a=u.slice(0,-1),l=a.length>0?ge(r,a.join(".")):r;return n.isNullable(l)||xe(l,i,u.at(-1),o,s,c),t??i.value}if(e===me.Hydrate){const e=we(a(i.value,u));return xe(e,i,null,o,s,c),e}const l=t??we(i.value);return xe(l,i,null,o,s,c),n.isNullable(t)?l:(a(i.value,u),t)}if(n.isArray(i))return i.map((e,t)=>a(e,u.concat(t)));if(n.isObject(i)&&!Pe(i))return i;if(n.isObject(i)){const t=Object.entries(i).map(([e,t])=>[e,a(t,u.concat(e))]),n=Object.fromEntries(t);if(e===me.Hydrate){const e=we(n);return Object.entries(i).forEach(([t,n])=>{n instanceof ye&&Se(n.value)&&xe(e,n,t,o,s,c)}),e}return n}return i}(t.value)}function xe(e,t,n,r,o,s){const c=s(e),a=o.get(c)??[];o.set(c,[t.assign(n,r),...a])}class Ce{#e={};#t;#n=/* @__PURE__ */new Map;#r=/* @__PURE__ */new Set;#o=!1;constructor(e=Oe){this.#t=e}static pk(){return pe()}static"κ"=Ce.pk;annotate(e,t){return new ye(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,t,o,s,c){function a(s){const c=s.at(-1),a=ge(e(),s),i=s.slice(0,-1),u=r.isNotEmpty(i)?ge(e(),i):e();return[...n.isObject(a)||n.isArray(a)?t.get(o(a))?.filter(e=>n.isNullable(e.property))??[]:[],...n.isObject(u)?t.get(o(u))?.filter(e=>e.property===c)??[]:[]]}return function t(n){return new Proxy(()=>{},{get:(o,i)=>"pending"===i?()=>!r.isEmpty(a(n)):"remaining"===i?()=>r.length(a(n)):"box"===i?()=>({value:ge(e(),n),inspect:t(n)}):"is"===i?e=>a(n).some(t=>0!==(t.operation&e)):"draft"===i?()=>r.head(a(n))?.value??ge(e(),n):"settled"===i?()=>new Promise(t=>{if(r.isEmpty(a(n)))return t(ge(e(),n));const o=()=>{r.isEmpty(a(n))&&(c(o),t(ge(e(),n)))};s(o)}):t([...n,String(i)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#s(me.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(me.Produce,e)}#s(e,t){const n=/* @__PURE__ */Symbol("process"),[,r]=ve.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>ve.immer.applyPatches(t,[{...r,value:je(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=we(this.#e),this.#c(),n}prune(e){this.#n.forEach((t,n)=>{const o=t.filter(t=>t.process!==e);r.isEmpty(o)?this.#n.delete(n):this.#n.set(n,o)}),this.#c()}#c(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}const Ee=t.createContext(/* @__PURE__ */new Map);function ke({action:e,renderer:r}){const o=h(),s=t.useContext(Ee),c=K(),a=t.useMemo(()=>{const t=s.get(e);if(t)return t;const r=new Ce,c=o.getCached(e);n.isNotNullable(c)&&r.hydrate({value:c});const a={state:r,listeners:/* @__PURE__ */new Set};return s.set(e,a),a},[e,o,s]);t.useLayoutEffect(()=>{function t(e){a.state.hydrate({value:e}),a.listeners.forEach(e=>e())}return a.listeners.add(c),o.on(e,t),()=>{a.listeners.delete(c),o.off(e,t)}},[e,o,a]);const i=a.state.model?.value;return n.isNullable(i)?null:r(i,a.state.inspect.value)}function Ne(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 Me(e,t,n){const{cursor:r,key:o}=Ne(e,t);r[o]=n}function Ae(e){return(t,n)=>{t.actions.produce(t=>{Me(t.model,e,n)})}}function _e(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=Ne(e,t);n[r]=!n[r]}(t.model,e)})}}function Re(e,t){return n=>{n.actions.produce(n=>{Me(n.model,e,t)})}}const Ue={Update:e=>Ae(e),Invert:e=>_e(e),Always:(e,t)=>Re(e,t)};function Le(){const e=t.useRef(null);return t.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(...o){const s=function(...e){const o=n.isUndefined(e[0])||n.isFunction(e[0])?{}:e[0],s=n.isFunction(e[0])?e[0]:e[1]??(()=>({})),c=h(),i=t.useContext(Z),u=t.useContext(b),l=g(),d=t.useContext(v),p=t.useContext(_),m=t.useContext(U),y=K(),O=t.useRef(!1),P=t.useRef(null),S=t.useRef(new Ce);O.current||(O.current=!0,P.current=S.current.hydrate(o));const[j,x]=t.useState(()=>S.current.model),k=function(e){const n=t.useRef(e);return n.current=e,t.useMemo(()=>{return t=n,Object.keys(e).reduce((e,n)=>(Object.defineProperty(e,n,{get:()=>t.current[n],enumerable:!0}),e),{});var t},[e])}(s()),N=t.useMemo(()=>new f,[]),A=t.useRef({handlers:/* @__PURE__ */new Map});A.current.handlers=/* @__PURE__ */new Map;const R=function(){const e=t.useRef(/* @__PURE__ */new Set),n=t.useRef(/* @__PURE__ */new Set);return t.useMemo(()=>({broadcast:e.current,multicast:n.current}),[])}(),L=t.useRef(M.Mounting),T=t.useRef(/* @__PURE__ */new Set),W=t.useRef(0),z=t.useCallback((e,t,r)=>{const o=new AbortController,s={controller:o,action:e,payload:t};return u.add(s),T.current.add(s),{model:S.current.model,get phase(){return L.current},task:s,data:k,tasks:u,env:l,actions:{produce(e){if(o.signal.aborted)return;const t=d.current,n=S.current.produce(t=>{d.current=a(d.current,n=>{e({model:t,inspect:S.current.inspect,env:n})})});x(S.current.model),d.current!==t&&c.emit(E,d.current),r.processes.add(n),P.current&&(r.processes.add(P.current),P.current=null)},dispatch(e,t){if(o.signal.aborted)return Promise.resolve();const n=$(e),r=F(e)?e.channel:void 0;return D(e)?i?J(i.emitter,n,t,r):Promise.resolve():J(B(e)?c:N,n,t,r)},annotate:(e,t=he.Update)=>S.current.annotate(t,e),get inspect(){return S.current.inspect},resource:Object.assign(function(e){const t=ie(),r=(e,t)=>{if(o.signal.aborted)return Promise.resolve();const n=e,r=$(n);return D(n)?i?J(i.emitter,r,t,void 0):Promise.resolve():B(n)?J(c,r,t,void 0):Promise.resolve()},s={exceedsWindow:null,coalesceToken:void 0},a={then:(e,c)=>(()=>{if(n.isNotNullable(s.exceedsWindow)){const{data:e,at:r}=t.read(t.params);if(e!==G&&n.isNotNullable(r)){const t=Temporal.Now.instant().since(r),n=Temporal.Duration.from(s.exceedsWindow);if(Temporal.Duration.compare(t,n)<=0)return Promise.resolve(e)}}if(n.isUndefined(s.coalesceToken))return t.run(l,o,t.params,r);let e=p.get(t.run);n.isUndefined(e)&&(e=/* @__PURE__ */new Map,p.set(t.run,e));const c=e,a=`${JSON.stringify(t.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)}`}}(s.coalesceToken)}`,i=c.get(a);if(i)return de(i,o.signal);const u=new AbortController,f=t.run(l,u,t.params,r).finally(()=>{c.delete(a)});return c.set(a,f),de(f,o.signal)})().then(e,c),exceeds:e=>(s.exceedsWindow=e,a),coalesce:e=>(s.coalesceToken=e??fe,a)};return a},{set:(e,t)=>{const n=ie();n.seed(n.params,t,Temporal.Now.instant())}}),async final(e){if(o.signal.aborted)return null;const t=$(e),r=D(e)?i?.emitter??null:c;if(!r)return null;const s=r.getCached(t);if(n.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()})}),r.getCached(t)??null},peek(e){if(o.signal.aborted)return null;const t=$(e),n=D(e)?i?.emitter??null:c;return n?n.getCached(t)??null:null}}}},[j]);t.useLayoutEffect(()=>{function e(e,t,r){return function(o,s){const a=r();if(s===I&&n.isNotNullable(a))return;if(n.isNotNullable(s)&&s!==I&&n.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},f=Promise.withResolvers(),p=z(e,o,l),h=function(e){const t=$(e),r=n.isString(t)?t:t.description??"";return r.startsWith(w())&&r.slice(r.lastIndexOf("/")+1)||"unknown"}(e),b=performance.now(),v=S.current.model,g=d.current;let O,P=!1;function j(){const e=S.current.model,t=d.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function x(){const t=s===I?void 0:s;return D(e)?i?J(i.emitter,e,o,t):Promise.resolve():J(B(e)?c:N,e,o,t)}function E(e){P=!0;const t=q(A.current.handlers,"Error"),r=n.isNotNullable(t),s=function(e){return e instanceof Error&&"AbortError"===e.name?X.Aborted:X.Errored}(e),a=function(e){return e instanceof Error?e:new Error(String(e))}(e),i={reason:s,error:a,action:h,handled:r,tasks:u,retry:x};c.fire(C,i),r&&t&&N.emit(t,i),m({stage:"end",result:"error",action:{name:h,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:j(),error:a,reason:s}})}function k(){for(const e of u)if(e===p.task){u.delete(e),T.current.delete(e);break}l.processes.forEach(e=>S.current.prune(e)),l.processes.size>0&&y(),P||m({stage:"end",result:"success",action:{name:h,payload:o},details:{task:p.task,elapsed:performance.now()-b,mutations:j()}}),f.resolve()}m({stage:"start",action:{name:h,payload:o},details:{task:p.task}});try{O=t(p,o)}catch(M){return E(M),k(),f.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}(O)?((async()=>{for await(const e of O);})().catch(E).finally(k),f.promise):(Promise.resolve(O).catch(E).finally(k),f.promise)}}W.current++;const t=/* @__PURE__ */new Set;return A.current.handlers.forEach((n,r)=>{for(const{getChannel:o,handler:s}of n){const n=e(r,s,o);if(D(r)){if(i){const e=i.emitter;e.on(r,n),t.add(()=>e.off(r,n))}N.on(r,n),R.multicast.add(r),t.add(()=>N.off(r,n))}else B(r)?(c.on(r,n),N.on(r,n),R.broadcast.add(r),t.add(()=>{c.off(r,n),N.off(r,n)})):(N.on(r,n),t.add(()=>N.off(r,n)))}}),()=>{const e=++W.current,n=new Set(t);queueMicrotask(()=>{if(W.current!==e){for(const e of n)e();return}for(const e of T.current)e.controller.abort(new Y("Component unmounted")),u.delete(e);T.current.clear(),L.current=M.Unmounting;const t=q(A.current.handlers,"Unmount");t&&N.emit(t),L.current=M.Unmounted;for(const e of n)e()})}},[N]),function({unicast:e,broadcast:o,dispatchers:s,scope:c,phase:a,data:i,handlers:u}){const l=t.useRef(null);t.useLayoutEffect(()=>{if(a.current===M.Mounted)return;a.current=M.Mounting;const t=q(u,"Mount");t&&e.emit(t),s.broadcast.forEach(t=>{const r=o.getCached(t);n.isNullable(r)||e.emit(t,r,I)}),c&&s.multicast.forEach(t=>{const r=c.emitter.getCached(t);n.isNullable(r)||e.emit(t,r,I)}),a.current=M.Mounted},[]),t.useLayoutEffect(()=>{if(n.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,i);if(r.isNotEmpty(Object.keys(t))){const n=q(u,"Update");n&&e.emit(n,t)}}l.current=i},[i,e])}({unicast:N,broadcast:c,dispatchers:R,scope:i,phase:L,data:s(),handlers:A.current.handlers});const H=t.useMemo(()=>({dispatch(e,t){const n=$(e),r=F(e)?e.channel:void 0;return D(e)?i?J(i.emitter,n,t,r):Promise.resolve():J(B(e)?c:N,n,t,r)},get inspect(){return S.current.inspect},stream:(e,n)=>t.createElement(ke,{action:$(e),renderer:n})}),[j,N]),V=t.useMemo(()=>[j,H,k],[j,H,k]);return V.useAction=(e,n)=>{!function(e,n,r){const o=t.useRef(r);t.useLayoutEffect(()=>{o.current=r});const s=t.useRef(n);t.useLayoutEffect(()=>{s.current=n});const c=t.useCallback((e,t)=>o.current(e,t),[]),a=t.useCallback(()=>F(s.current)?s.current.channel:void 0,[]),i=$(n),u=e.current.handlers.get(i)??/* @__PURE__ */new Set;0===u.size&&e.current.handlers.set(i,u),u.add({getChannel:a,handler:c})}(A,e,n)},V.dispatch=V[1].dispatch,V}(...o);return e.current=s.dispatch,s},with:{update:e=>Ae(e),invert:e=>_e(e),always:(e,t)=>Re(e,t)}}),[])}function Te(){return{Boundary:function({children:n}){const r=t.useMemo(()=>({id:/* @__PURE__ */Symbol("march-hare.scope/instance"),emitter:new d}),[]);/* @__PURE__ */
|
|
7
|
+
return e(Z.Provider,{value:r,children:n})},useContext:function(){return Le()},useEnv:function(){return g()},Resource:Object.assign(function(e){return le(e)},{Cachable:(e,t)=>le.Cachable(e,t)})}}function We(t){return{Boundary:function({children:n}){/* @__PURE__ */
|
|
8
|
+
return e(T,{env:t?.env,tap:t?.tap,children:n})},useContext:function(){return Le()},useEnv:function(){return g()},Resource:Object.assign(function(e){return le(e)},{Cachable:(e,t)=>le.Cachable(e,t)}),Scope:()=>Te()}}const $e=new Ce;function Be(e,t=he.Update){return $e.annotate(t,e)}const Fe=/* @__PURE__ */Object.freeze(/* @__PURE__ */Object.defineProperty({__proto__:null,get Resource(){return le},Scope:function(){return Te()},useContext:function(){return Le()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));export{Y as Aborted,H as Action,We as App,T as Boundary,ee as Cache,N as Distribution,k as Lifecycle,he as Op,he as Operation,X as Reason,Ce as State,Ue as With,Be as annotate,Fe as shared,oe as utils};
|
package/dist/march-hare.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var global,factory;global=this,factory=function(e,t,n,r,o){"use strict";function s(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e)for(const n in e)if("default"!==n){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}return t.default=e,Object.freeze(t)}const c=s(n);function a(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var i,u={exports:{}},l=(i||(i=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 a=new o(r,s||e,c),i=n?n+t:t;return e._events[i]?e._events[i].fn?e._events[i]=[e._events[i],a]:e._events[i].push(a):(e._events[i]=a,e._eventsCount++),e}function c(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function a(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),a.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},a.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},a.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},a.prototype.emit=function(e,t,r,o,s,c){var a=n?n+e:e;if(!this._events[a])return!1;var i,u,l=this._events[a],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,i=new Array(f-1);u<f;u++)i[u-1]=arguments[u];l.fn.apply(l.context,i)}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(!i)for(d=1,i=new Array(f-1);d<f;d++)i[d-1]=arguments[d];l[u].fn.apply(l[u].context,i)}}return!0},a.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},a.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},a.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 a=this._events[s];if(a.fn)a.fn!==t||o&&!a.once||r&&a.context!==r||c(this,s);else{for(var i=0,u=[],l=a.length;i<l;i++)(a[i].fn!==t||o&&!a[i].once||r&&a[i].context!==r)&&u.push(a[i]);u.length?this._events[s]=1===u.length?u[0]:u:c(this,s)}return this},a.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},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=n,a.EventEmitter=a,e.exports=a}(u)),u.exports);const f=a(l);class d extends f{cache=new Map;emit(e,...t){return this.cache.set(e,t[0]),super.emit(e,...t)}setCache(e,t){this.cache.set(e,t)}getCached(e){return this.cache.get(e)}fire(e,...t){return super.emit(e,...t)}}const p=c.createContext(new d);function h(){return c.useContext(p)}function m({children:e}){const n=c.useMemo(()=>new d,[]);return t.jsx(p.Provider,{value:n,children:e})}const b=c.createContext(new Set);function y({children:e}){const n=c.useMemo(()=>new Set,[]);return t.jsx(b.Provider,{value:n,children:e})}const v=c.createContext({current:{}});function g(){const e=c.useContext(v);return c.useMemo(()=>new Proxy({},{get:(t,n)=>Reflect.get(e.current,n),has:(t,n)=>n in e.current,ownKeys:()=>Reflect.ownKeys(e.current),getOwnPropertyDescriptor(t,n){const o=Object.getOwnPropertyDescriptor(e.current,n);if(!r.G.isUndefined(o))return{...o,configurable:!0}},set(){throw new TypeError("Env is read-only outside `context.actions.produce`. Mutate via produce(({ env }) => { env.x = ... }) instead.")}}),[e])}const w=(e="")=>`march-hare.action/${e}`,j=(e="")=>`march-hare.action/broadcast/${e}`,x=(e="")=>`march-hare.action/multicast/${e}`,O=(e="")=>`march-hare.action.lifecycle/${e}`;class P{static Payload=Symbol("march-hare.brand/Payload");static Broadcast=Symbol("march-hare.brand/Broadcast");static Multicast=Symbol("march-hare.brand/Multicast");static Action=Symbol("march-hare.brand/Action");static Channel=Symbol("march-hare.brand/Channel");static Name=Symbol("march-hare.brand/Name")}function S(e){const t=Symbol(`march-hare.action.lifecycle/${e}`),n=function(n){return{[P.Action]:t,[P.Payload]:void 0,[P.Channel]:n,[P.Name]:e,channel:n}};return Object.defineProperty(n,P.Action,{value:t,enumerable:!1}),Object.defineProperty(n,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(n,P.Name,{value:e,enumerable:!1}),n}const C=Symbol(j("Fault")),E=Symbol(j("Env"));class A{static Mount(){return S("Mount")}static Unmount(){return S("Unmount")}static Error(){return S("Error")}static Update(){return S("Update")}static Fault=(()=>{const e={};return Object.defineProperty(e,P.Action,{value:C,enumerable:!1}),Object.defineProperty(e,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,P.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,P.Name,{value:"Fault",enumerable:!1}),e})();static Env=(()=>{const e={};return Object.defineProperty(e,P.Action,{value:E,enumerable:!1}),Object.defineProperty(e,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,P.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,P.Name,{value:"Env",enumerable:!1}),e})()}var G=(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(G||{}),M=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(M||{});function k({initial:e,children:n}){const o=c.useRef(e),s=h();return r.G.isUndefined(s.getCached(E))&&s.setCache(E,o.current),t.jsx(v.Provider,{value:o,children:n})}const N=c.createContext(new WeakMap);function R({children:e}){const n=c.useMemo(()=>new WeakMap,[]);return t.jsx(N.Provider,{value:n,children:e})}const _=c.createContext(()=>{});function U({tap:e,children:n}){const r=c.useRef(e);c.useLayoutEffect(()=>{r.current=e},[e]);const o=c.useMemo(()=>e=>r.current?.(e),[]);return t.jsx(_.Provider,{value:o,children:n})}function L({env:e,tap:n,children:r}){return t.jsx(m,{children:t.jsx(k,{initial:e??{},children:t.jsx(y,{children:t.jsx(U,{tap:n,children:t.jsx(R,{children:r})})})})})}const T=e=>"symbol"==typeof e;function W(e){return r.G.isString(e)||T(e)?e:(r.G.isObject(e)||r.G.isFunction(e))&&P.Action in e?e[P.Action]:e}function B(e){if(r.G.isString(e))return e.startsWith(j());if(T(e))return e.description?.startsWith(j())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(P.Broadcast in e&&e[P.Broadcast])return!0;if(P.Action in e){const t=e[P.Action];return t.description?.startsWith(j())??!1}}return!1}function $(e){return r.G.isObject(e)&&P.Channel in e&&"channel"in e}function F(e){const t=W(e),n=T(t)?t.description??"":t;return n.startsWith(O())&&n.slice(O().length)||null}function D(e){if(r.G.isString(e))return e.startsWith(x());if(T(e))return e.description?.startsWith(x())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(P.Multicast in e&&e[P.Multicast])return!0;if(P.Action in e){const t=e[P.Action];return t.description?.startsWith(x())??!1}}return!1}const I=Symbol(((e="")=>`march-hare/replay${e}`)());function H(e,t,...n){e instanceof d&&e.setCache(t,n[0]);const r=e.listeners(t);return 0===r.length?Promise.resolve():Promise.all(r.map(e=>Promise.resolve(e(...n)))).then(()=>{})}function q(e,t){for(const n of e.keys())if(F(n)===t)return n;return null}const z=Symbol("march-hare.unset");function J(){const[,e]=c.useReducer(e=>e+1,0);return e}function K(){return{data:z,at:null,else:e=>e}}function V(e,t){return{data:e,at:t,else:t=>e}}var Q=(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(Q||{});class X extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Y=c.createContext(null);function Z(e){const t=new Map,n=e??{get:e=>t.get(e)??null,set:(e,n)=>{t.set(e,n)},remove:e=>{t.delete(e)},clear:()=>{t.clear()}};return{get(e){try{const t=n.get(e);if(r.G.isNull(t))return K();const o=JSON.parse(t);return V(o.data,Temporal.Instant.from(o.at))}catch{return K()}},set(e,t){if(t.data===z||r.G.isNull(t.at))return!1;try{return n.set(e,JSON.stringify({data:t.data,at:t.at.toString()})),!0}catch{return!1}},remove(e){n.remove(e)},clear(){n.clear()}}}function ee(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new X);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new X)},{once:!0})})}async function te(e,t,n){if(t?.aborted)throw new X;for(;;){if(await n())return;await ee(e,t)}}function ne(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const re=Object.freeze(Object.defineProperty({__proto__:null,pk:ne,poll:te,sleep:ee,unset:z,"ζ":ee,"κ":ne,"π":te},Symbol.toStringTag,{value:"Module"})),oe=new WeakMap;function se(e){return JSON.stringify(e)}let ce=null;function ae(){if(r.G.isNull(ce))throw new Error("context.actions.resource(...) and context.actions.resource.set(...) must be called with a fresh resource invocation, e.g. context.actions.resource(resource.cat({ id: 5 })).");const e=ce;return ce=null,e}function ie(e,t){const n=e=>{const n=t.get(se(e));return n.data===z||r.G.isNull(n.at)?{data:z,at:null}:{data:n.data,at:n.at}},o=(n,r,o,s)=>e({env:n,controller:r,params:o,dispatch:s}).then(e=>(t.set(se(o),V(e,Temporal.Now.instant())),e)),s=(e,n,r)=>{t.set(se(e),V(n,r))};return function(e){const t=e??{};ce={run:o,read:n,seed:s,params:t},queueMicrotask(()=>{r.G.isNotNullable(ce)&&ce.params===t&&(ce=null)});const{data:c}=n(t);return c===z?null:c}}function ue(e){return ie(e,function(e){let t=oe.get(e);return r.G.isUndefined(t)&&(t=Z(),oe.set(e,t)),t}(e))}(ue||(ue={})).Cachable=function(e,t){return ie(t,e)};const le=Symbol("march-hare.coalesce/default");function fe(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 de=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var pe=(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))(pe||{}),he=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(he||{}),me=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(me||{});class be{[o.immerable]=!0;static keys=new Set(Object.values(me));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new be(this.value,this.operation);return n.property=e,n.process=t,n}}class ye{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=de}function ve(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 ge(e){if(r.G.isNullable(e)||xe(e))return e;if(r.G.isArray(e))return e.map(e=>ge(e));if(r.G.isObject(e)&&je(e)){const t=Object.entries(e).map(([e,t])=>[e,ge(t)]);return{...Object.fromEntries(t),[ye.tag]:e[ye.tag]??ye.id()}}return e}function we(e){if(Array.isArray(e))return e.filter(e=>ye.tag in e).map(e=>e[ye.tag]??"").join(",");const t=e[ye.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function je(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function xe(e){return r.G.isNullable(e)||r.G.isString(e)||r.G.isNumber(e)||r.G.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function Oe(e,t,n,o,s,c){return function a(i,u=t.path){if(i instanceof be){const t=ve(n,u.join("."));if(Object.entries(i).filter(([e,t])=>!be.keys.has(e)&&t instanceof be).forEach(([e,t])=>a(t,u.concat(e))),xe(i.value)){if(e===he.Hydrate)return i.value;const a=u.slice(0,-1),l=a.length>0?ve(n,a.join(".")):n;return r.G.isNullable(l)||Pe(l,i,u.at(-1),o,s,c),t??i.value}if(e===he.Hydrate){const e=ge(a(i.value,u));return Pe(e,i,null,o,s,c),e}const l=t??ge(i.value);return Pe(l,i,null,o,s,c),r.G.isNullable(t)?l:(a(i.value,u),t)}if(r.G.isArray(i))return i.map((e,t)=>a(e,u.concat(t)));if(r.G.isObject(i)&&!je(i))return i;if(r.G.isObject(i)){const t=Object.entries(i).map(([e,t])=>[e,a(t,u.concat(e))]),n=Object.fromEntries(t);if(e===he.Hydrate){const e=ge(n);return Object.entries(i).forEach(([t,n])=>{n instanceof be&&xe(n.value)&&Pe(e,n,t,o,s,c)}),e}return n}return i}(t.value)}function Pe(e,t,n,r,o,s){const c=s(e),a=o.get(c)??[];o.set(c,[t.assign(n,r),...a])}class Se{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=we){this.#t=e}static pk(){return de()}static"κ"=Se.pk;annotate(e,t){return new be(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,t,n,o,s){function c(o){const s=o.at(-1),c=ve(e(),o),a=o.slice(0,-1),i=r.A.isNotEmpty(a)?ve(e(),a):e();return[...r.G.isObject(c)||r.G.isArray(c)?t.get(n(c))?.filter(e=>r.G.isNullable(e.property))??[]:[],...r.G.isObject(i)?t.get(n(i))?.filter(e=>e.property===s)??[]:[]]}return function t(n){return new Proxy(()=>{},{get:(a,i)=>"pending"===i?()=>!r.A.isEmpty(c(n)):"remaining"===i?()=>r.A.length(c(n)):"box"===i?()=>({value:ve(e(),n),inspect:t(n)}):"is"===i?e=>c(n).some(t=>0!==(t.operation&e)):"draft"===i?()=>r.A.head(c(n))?.value??ve(e(),n):"settled"===i?()=>new Promise(t=>{if(r.A.isEmpty(c(n)))return t(ve(e(),n));const a=()=>{r.A.isEmpty(c(n))&&(s(a),t(ve(e(),n)))};o(a)}):t([...n,String(i)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#s(he.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(he.Produce,e)}#s(e,t){const n=Symbol("process"),[,r]=ye.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>ye.immer.applyPatches(t,[{...r,value:Oe(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=ge(this.#e),this.#c(),n}prune(e){this.#n.forEach((t,n)=>{const o=t.filter(t=>t.process!==e);r.A.isEmpty(o)?this.#n.delete(n):this.#n.set(n,o)}),this.#c()}#c(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}const Ce=c.createContext(new Map);function Ee({action:e,renderer:t}){const n=h(),o=c.useContext(Ce),s=J(),a=c.useMemo(()=>{const t=o.get(e);if(t)return t;const s=new Se,c=n.getCached(e);r.G.isNotNullable(c)&&s.hydrate({value:c});const a={state:s,listeners:new Set};return o.set(e,a),a},[e,n,o]);c.useLayoutEffect(()=>{function t(e){a.state.hydrate({value:e}),a.listeners.forEach(e=>e())}return a.listeners.add(s),n.on(e,t),()=>{a.listeners.delete(s),n.off(e,t)}},[e,n,a]);const i=a.state.model?.value;return r.G.isNullable(i)?null:t(i,a.state.inspect.value)}function Ae(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 Ge(e,t,n){const{cursor:r,key:o}=Ae(e,t);r[o]=n}function Me(e){return(t,n)=>{t.actions.produce(t=>{Ge(t.model,e,n)})}}function ke(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=Ae(e,t);n[r]=!n[r]}(t.model,e)})}}function Ne(e,t){return n=>{n.actions.produce(n=>{Ge(n.model,e,t)})}}const Re={Update:e=>Me(e),Invert:e=>ke(e),Always:(e,t)=>Ne(e,t)};function _e(){const e=c.useRef(null);return c.useMemo(()=>({actions:{dispatch:function(t,n){const r=e.current;if(!r)throw new Error("march-hare: useContext handle dispatched before its paired context.useActions(...) ran. Call context.actions.dispatch from event handlers, not synchronously during render.");return r(t,n)}},useActions:function(...t){const n=function(...e){const t=r.G.isUndefined(e[0])||r.G.isFunction(e[0])?{}:e[0],n=r.G.isFunction(e[0])?e[0]:e[1]??(()=>({})),s=h(),a=c.useContext(Y),i=c.useContext(b),u=g(),l=c.useContext(v),d=c.useContext(N),p=c.useContext(_),m=J(),y=c.useRef(!1),j=c.useRef(null),x=c.useRef(new Se);y.current||(y.current=!0,j.current=x.current.hydrate(t));const[O,P]=c.useState(()=>x.current.model),S=function(e){const t=c.useRef(e);return t.current=e,c.useMemo(()=>{return n=t,Object.keys(e).reduce((e,t)=>(Object.defineProperty(e,t,{get:()=>n.current[t],enumerable:!0}),e),{});var n},[e])}(n()),A=c.useMemo(()=>new f,[]),G=c.useRef({handlers:new Map});G.current.handlers=new Map;const k=function(){const e=c.useRef(new Set),t=c.useRef(new Set);return c.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),R=c.useRef(M.Mounting),U=c.useRef(new Set),L=c.useRef(0),T=c.useCallback((e,t,n)=>{const c=new AbortController,f={controller:c,action:e,payload:t};return i.add(f),U.current.add(f),{model:x.current.model,get phase(){return R.current},task:f,data:S,tasks:i,env:u,actions:{produce(e){if(c.signal.aborted)return;const t=l.current,r=x.current.produce(t=>{l.current=o.produce(l.current,n=>{e({model:t,inspect:x.current.inspect,env:n})})});P(x.current.model),l.current!==t&&s.emit(E,l.current),n.processes.add(r),j.current&&(n.processes.add(j.current),j.current=null)},dispatch(e,t){if(c.signal.aborted)return Promise.resolve();const n=W(e),r=$(e)?e.channel:void 0;return D(e)?a?H(a.emitter,n,t,r):Promise.resolve():H(B(e)?s:A,n,t,r)},annotate:(e,t=pe.Update)=>x.current.annotate(t,e),get inspect(){return x.current.inspect},resource:Object.assign(function(e){const t=ae(),n=(e,t)=>{if(c.signal.aborted)return Promise.resolve();const n=e,r=W(n);return D(n)?a?H(a.emitter,r,t,void 0):Promise.resolve():B(n)?H(s,r,t,void 0):Promise.resolve()},o={exceedsWindow:null,coalesceToken:void 0},i={then:(e,s)=>(()=>{if(r.G.isNotNullable(o.exceedsWindow)){const{data:e,at:n}=t.read(t.params);if(e!==z&&r.G.isNotNullable(n)){const t=Temporal.Now.instant().since(n),r=Temporal.Duration.from(o.exceedsWindow);if(Temporal.Duration.compare(t,r)<=0)return Promise.resolve(e)}}if(r.G.isUndefined(o.coalesceToken))return t.run(u,c,t.params,n);let e=d.get(t.run);r.G.isUndefined(e)&&(e=new Map,d.set(t.run,e));const s=e,a=`${JSON.stringify(t.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)}`}}(o.coalesceToken)}`,i=s.get(a);if(i)return fe(i,c.signal);const l=new AbortController,f=t.run(u,l,t.params,n).finally(()=>{s.delete(a)});return s.set(a,f),fe(f,c.signal)})().then(e,s),exceeds:e=>(o.exceedsWindow=e,i),coalesce:e=>(o.coalesceToken=e??le,i)};return i},{set:(e,t)=>{const n=ae();n.seed(n.params,t,Temporal.Now.instant())}}),async final(e){if(c.signal.aborted)return null;const t=W(e),n=D(e)?a?.emitter??null:s;if(!n)return null;const o=n.getCached(t);if(r.G.isUndefined(o))return null;const i=x.current.inspect;return i.pending()&&await new Promise((e,t)=>{if(c.signal.aborted)return void t(c.signal.reason);const n=()=>t(c.signal.reason);c.signal.addEventListener("abort",n,{once:!0}),i.settled().then(()=>{c.signal.removeEventListener("abort",n),e()})}),n.getCached(t)??null},peek(e){if(c.signal.aborted)return null;const t=W(e),n=D(e)?a?.emitter??null:s;return n?n.getCached(t)??null:null}}}},[O]);c.useLayoutEffect(()=>{function e(e,t,n){return function(o,c){const u=n();if(c===I&&r.G.isNotNullable(u))return;if(r.G.isNotNullable(c)&&c!==I&&r.G.isNotNullable(u)&&!function(e,t){for(const n of Object.keys(e))if(t[n]!==e[n])return!1;return!0}(c,u))return;const f={processes:new Set},d=Promise.withResolvers(),h=T(e,o,f),b=function(e){const t=W(e),n=r.G.isString(t)?t:t.description??"";return n.startsWith(w())&&n.slice(n.lastIndexOf("/")+1)||"unknown"}(e),y=performance.now(),v=x.current.model,g=l.current;let j,O=!1;function P(){const e=x.current.model,t=l.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function S(){const t=c===I?void 0:c;return D(e)?a?H(a.emitter,e,o,t):Promise.resolve():H(B(e)?s:A,e,o,t)}function E(e){O=!0;const t=q(G.current.handlers,"Error"),n=r.G.isNotNullable(t),c=function(e){return e instanceof Error&&"AbortError"===e.name?Q.Aborted:Q.Errored}(e),a=function(e){return e instanceof Error?e:new Error(String(e))}(e),u={reason:c,error:a,action:b,handled:n,tasks:i,retry:S};s.fire(C,u),n&&t&&A.emit(t,u),p({stage:"end",result:"error",action:{name:b,payload:o},details:{task:h.task,elapsed:performance.now()-y,mutations:P(),error:a,reason:c}})}function M(){for(const e of i)if(e===h.task){i.delete(e),U.current.delete(e);break}f.processes.forEach(e=>x.current.prune(e)),f.processes.size>0&&m(),O||p({stage:"end",result:"success",action:{name:b,payload:o},details:{task:h.task,elapsed:performance.now()-y,mutations:P()}}),d.resolve()}p({stage:"start",action:{name:b,payload:o},details:{task:h.task}});try{j=t(h,o)}catch(k){return E(k),M(),d.promise}return function(e){if(!e||"object"!=typeof e)return!1;const t=Object.prototype.toString.call(e);return"[object Generator]"===t||"[object AsyncGenerator]"===t}(j)?((async()=>{for await(const e of j);})().catch(E).finally(M),d.promise):(Promise.resolve(j).catch(E).finally(M),d.promise)}}L.current++;const t=new Set;return G.current.handlers.forEach((n,r)=>{for(const{getChannel:o,handler:c}of n){const n=e(r,c,o);if(D(r)){if(a){const e=a.emitter;e.on(r,n),t.add(()=>e.off(r,n))}A.on(r,n),k.multicast.add(r),t.add(()=>A.off(r,n))}else B(r)?(s.on(r,n),A.on(r,n),k.broadcast.add(r),t.add(()=>{s.off(r,n),A.off(r,n)})):(A.on(r,n),t.add(()=>A.off(r,n)))}}),()=>{const e=++L.current,n=new Set(t);queueMicrotask(()=>{if(L.current!==e){for(const e of n)e();return}for(const e of U.current)e.controller.abort(new X("Component unmounted")),i.delete(e);U.current.clear(),R.current=M.Unmounting;const t=q(G.current.handlers,"Unmount");t&&A.emit(t),R.current=M.Unmounted;for(const e of n)e()})}},[A]),function({unicast:e,broadcast:t,dispatchers:n,scope:o,phase:s,data:a,handlers:i}){const u=c.useRef(null);c.useLayoutEffect(()=>{if(s.current===M.Mounted)return;s.current=M.Mounting;const c=q(i,"Mount");c&&e.emit(c),n.broadcast.forEach(n=>{const o=t.getCached(n);r.G.isNullable(o)||e.emit(n,o,I)}),o&&n.multicast.forEach(t=>{const n=o.emitter.getCached(t);r.G.isNullable(n)||e.emit(t,n,I)}),s.current=M.Mounted},[]),c.useLayoutEffect(()=>{if(r.G.isNotNullable(u.current)){const t=function(e,t){return Object.keys(t).reduce((n,r)=>e[r]!==t[r]?{...n,[r]:t[r]}:n,{})}(u.current,a);if(r.A.isNotEmpty(Object.keys(t))){const n=q(i,"Update");n&&e.emit(n,t)}}u.current=a},[a,e])}({unicast:A,broadcast:s,dispatchers:k,scope:a,phase:R,data:n(),handlers:G.current.handlers});const F=c.useMemo(()=>({dispatch(e,t){const n=W(e),r=$(e)?e.channel:void 0;return D(e)?a?H(a.emitter,n,t,r):Promise.resolve():H(B(e)?s:A,n,t,r)},get inspect(){return x.current.inspect},stream:(e,t)=>c.createElement(Ee,{action:W(e),renderer:t})}),[O,A]),K=c.useMemo(()=>[O,F,S],[O,F,S]);return K.useAction=(e,t)=>{!function(e,t,n){const r=c.useRef(n);c.useLayoutEffect(()=>{r.current=n});const o=c.useRef(t);c.useLayoutEffect(()=>{o.current=t});const s=c.useCallback((e,t)=>r.current(e,t),[]),a=c.useCallback(()=>$(o.current)?o.current.channel:void 0,[]),i=W(t),u=e.current.handlers.get(i)??new Set;0===u.size&&e.current.handlers.set(i,u),u.add({getChannel:a,handler:s})}(G,e,t)},K.dispatch=K[1].dispatch,K}(...t);return e.current=n.dispatch,n},with:{update:e=>Me(e),invert:e=>ke(e),always:(e,t)=>Ne(e,t)}}),[])}const Ue=new Se;e.Aborted=X,e.Action=(e="",t=G.Unicast)=>{const n=t===G.Broadcast?Symbol(j(e)):t===G.Multicast?Symbol(x(e)):Symbol(w(e)),r=function(t){return{[P.Action]:n,[P.Payload]:void 0,[P.Channel]:t,[P.Name]:e,channel:t}};return Object.defineProperty(r,P.Action,{value:n,enumerable:!1}),Object.defineProperty(r,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,P.Name,{value:e,enumerable:!1}),t===G.Broadcast&&Object.defineProperty(r,P.Broadcast,{value:!0,enumerable:!1}),t===G.Multicast&&Object.defineProperty(r,P.Multicast,{value:!0,enumerable:!1}),r},e.App=function(e){return{Boundary:function({children:n}){return t.jsx(L,{env:e?.env,tap:e?.tap,children:n})},useContext:function(){return _e()},useEnv:function(){return g()},Resource:Object.assign(function(e){return ue(e)},{Cachable:(e,t)=>ue.Cachable(e,t)}),Scope:()=>({Boundary:function({children:e}){const n=c.useMemo(()=>({id:Symbol("march-hare.scope/instance"),emitter:new d}),[]);return t.jsx(Y.Provider,{value:n,children:e})},useContext:function(){return _e()},useEnv:function(){return g()},Resource:Object.assign(function(e){return ue(e)},{Cachable:(e,t)=>ue.Cachable(e,t)})})}},e.Boundary=L,e.Cache=Z,e.Distribution=G,e.Lifecycle=A,e.Op=pe,e.Operation=pe,e.Reason=Q,e.Resource=ue,e.State=Se,e.With=Re,e.annotate=function(e,t=pe.Update){return Ue.annotate(t,e)},e.useApp=function(){return c.useMemo(()=>({useContext:()=>_e(),useEnv:()=>g()}),[])},e.utils=re,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})},"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("react/jsx-runtime"),require("react"),require("@mobily/ts-belt"),require("immer")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","react","@mobily/ts-belt","immer"],factory):factory((global="undefined"!=typeof globalThis?globalThis:global||self).MarchHare={},global.jsxRuntime,global.React,global.TsBelt,global.Immer);
|
|
1
|
+
var global,factory;global=this,factory=function(e,t,n,r,o){"use strict";function s(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e)for(const n in e)if("default"!==n){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}return t.default=e,Object.freeze(t)}const c=s(n);function a(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var i,u={exports:{}},l=(i||(i=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 a=new o(r,s||e,c),i=n?n+t:t;return e._events[i]?e._events[i].fn?e._events[i]=[e._events[i],a]:e._events[i].push(a):(e._events[i]=a,e._eventsCount++),e}function c(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function a(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),a.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},a.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},a.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},a.prototype.emit=function(e,t,r,o,s,c){var a=n?n+e:e;if(!this._events[a])return!1;var i,u,l=this._events[a],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,i=new Array(f-1);u<f;u++)i[u-1]=arguments[u];l.fn.apply(l.context,i)}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(!i)for(d=1,i=new Array(f-1);d<f;d++)i[d-1]=arguments[d];l[u].fn.apply(l[u].context,i)}}return!0},a.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},a.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},a.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 a=this._events[s];if(a.fn)a.fn!==t||o&&!a.once||r&&a.context!==r||c(this,s);else{for(var i=0,u=[],l=a.length;i<l;i++)(a[i].fn!==t||o&&!a[i].once||r&&a[i].context!==r)&&u.push(a[i]);u.length?this._events[s]=1===u.length?u[0]:u:c(this,s)}return this},a.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},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=n,a.EventEmitter=a,e.exports=a}(u)),u.exports);const f=a(l);class d extends f{cache=new Map;emit(e,...t){return this.cache.set(e,t[0]),super.emit(e,...t)}setCache(e,t){this.cache.set(e,t)}getCached(e){return this.cache.get(e)}fire(e,...t){return super.emit(e,...t)}}const p=c.createContext(new d);function h(){return c.useContext(p)}function m({children:e}){const n=c.useMemo(()=>new d,[]);return t.jsx(p.Provider,{value:n,children:e})}const b=c.createContext(new Set);function y({children:e}){const n=c.useMemo(()=>new Set,[]);return t.jsx(b.Provider,{value:n,children:e})}const v=c.createContext({current:{}});function g(){const e=c.useContext(v);return c.useMemo(()=>new Proxy({},{get:(t,n)=>Reflect.get(e.current,n),has:(t,n)=>n in e.current,ownKeys:()=>Reflect.ownKeys(e.current),getOwnPropertyDescriptor(t,n){const o=Object.getOwnPropertyDescriptor(e.current,n);if(!r.G.isUndefined(o))return{...o,configurable:!0}},set(){throw new TypeError("Env is read-only outside `context.actions.produce`. Mutate via produce(({ env }) => { env.x = ... }) instead.")}}),[e])}const w=(e="")=>`march-hare.action/${e}`,j=(e="")=>`march-hare.action/broadcast/${e}`,O=(e="")=>`march-hare.action/multicast/${e}`,x=(e="")=>`march-hare.action.lifecycle/${e}`;class P{static Payload=Symbol("march-hare.brand/Payload");static Broadcast=Symbol("march-hare.brand/Broadcast");static Multicast=Symbol("march-hare.brand/Multicast");static Action=Symbol("march-hare.brand/Action");static Channel=Symbol("march-hare.brand/Channel");static Name=Symbol("march-hare.brand/Name")}function S(e){const t=Symbol(`march-hare.action.lifecycle/${e}`),n=function(n){return{[P.Action]:t,[P.Payload]:void 0,[P.Channel]:n,[P.Name]:e,channel:n}};return Object.defineProperty(n,P.Action,{value:t,enumerable:!1}),Object.defineProperty(n,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(n,P.Name,{value:e,enumerable:!1}),n}const C=Symbol(j("Fault")),E=Symbol(j("Env"));class G{static Mount(){return S("Mount")}static Unmount(){return S("Unmount")}static Error(){return S("Error")}static Update(){return S("Update")}static Fault=(()=>{const e={};return Object.defineProperty(e,P.Action,{value:C,enumerable:!1}),Object.defineProperty(e,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,P.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,P.Name,{value:"Fault",enumerable:!1}),e})();static Env=(()=>{const e={};return Object.defineProperty(e,P.Action,{value:E,enumerable:!1}),Object.defineProperty(e,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(e,P.Broadcast,{value:!0,enumerable:!1}),Object.defineProperty(e,P.Name,{value:"Env",enumerable:!1}),e})()}var A=(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(A||{}),M=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(M||{});function k({initial:e,children:n}){const o=c.useRef(e),s=h();return r.G.isUndefined(s.getCached(E))&&s.setCache(E,o.current),t.jsx(v.Provider,{value:o,children:n})}const N=c.createContext(new WeakMap);function _({children:e}){const n=c.useMemo(()=>new WeakMap,[]);return t.jsx(N.Provider,{value:n,children:e})}const R=c.createContext(()=>{});function U({tap:e,children:n}){const r=c.useRef(e);c.useLayoutEffect(()=>{r.current=e},[e]);const o=c.useMemo(()=>e=>r.current?.(e),[]);return t.jsx(R.Provider,{value:o,children:n})}function L({env:e,tap:n,children:r}){return t.jsx(m,{children:t.jsx(k,{initial:e??{},children:t.jsx(y,{children:t.jsx(U,{tap:n,children:t.jsx(_,{children:r})})})})})}const T=e=>"symbol"==typeof e;function W(e){return r.G.isString(e)||T(e)?e:(r.G.isObject(e)||r.G.isFunction(e))&&P.Action in e?e[P.Action]:e}function B(e){if(r.G.isString(e))return e.startsWith(j());if(T(e))return e.description?.startsWith(j())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(P.Broadcast in e&&e[P.Broadcast])return!0;if(P.Action in e){const t=e[P.Action];return t.description?.startsWith(j())??!1}}return!1}function $(e){return r.G.isObject(e)&&P.Channel in e&&"channel"in e}function F(e){const t=W(e),n=T(t)?t.description??"":t;return n.startsWith(x())&&n.slice(x().length)||null}function D(e){if(r.G.isString(e))return e.startsWith(O());if(T(e))return e.description?.startsWith(O())??!1;if(r.G.isObject(e)||r.G.isFunction(e)){if(P.Multicast in e&&e[P.Multicast])return!0;if(P.Action in e){const t=e[P.Action];return t.description?.startsWith(O())??!1}}return!1}const I=Symbol(((e="")=>`march-hare/replay${e}`)());function z(e,t,...n){e instanceof d&&e.setCache(t,n[0]);const r=e.listeners(t);return 0===r.length?Promise.resolve():Promise.all(r.map(e=>Promise.resolve(e(...n)))).then(()=>{})}function H(e,t){for(const n of e.keys())if(F(n)===t)return n;return null}const q=Symbol("march-hare.unset");function J(){const[,e]=c.useReducer(e=>e+1,0);return e}function K(){return{data:q,at:null,else:e=>e}}function V(e,t){return{data:e,at:t,else:t=>e}}var Q=(e=>(e[e.Aborted=0]="Aborted",e[e.Errored=1]="Errored",e))(Q||{});class X extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const Y=c.createContext(null);function Z(e){const t=new Map,n=e??{get:e=>t.get(e)??null,set:(e,n)=>{t.set(e,n)},remove:e=>{t.delete(e)},clear:()=>{t.clear()}};return{get(e){try{const t=n.get(e);if(r.G.isNull(t))return K();const o=JSON.parse(t);return V(o.data,Temporal.Instant.from(o.at))}catch{return K()}},set(e,t){if(t.data===q||r.G.isNull(t.at))return!1;try{return n.set(e,JSON.stringify({data:t.data,at:t.at.toString()})),!0}catch{return!1}},remove(e){n.remove(e)},clear(){n.clear()}}}function ee(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new X);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new X)},{once:!0})})}async function te(e,t,n){if(t?.aborted)throw new X;for(;;){if(await n())return;await ee(e,t)}}function ne(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const re=Object.freeze(Object.defineProperty({__proto__:null,pk:ne,poll:te,sleep:ee,unset:q,"ζ":ee,"κ":ne,"π":te},Symbol.toStringTag,{value:"Module"})),oe=new WeakMap;function se(e){return JSON.stringify(e)}let ce=null;function ae(){if(r.G.isNull(ce))throw new Error("context.actions.resource(...) and context.actions.resource.set(...) must be called with a fresh resource invocation, e.g. context.actions.resource(resource.cat({ id: 5 })).");const e=ce;return ce=null,e}function ie(e,t){const n=e=>{const n=t.get(se(e));return n.data===q||r.G.isNull(n.at)?{data:q,at:null}:{data:n.data,at:n.at}},o=(n,r,o,s)=>e({env:n,controller:r,params:o,dispatch:s}).then(e=>(t.set(se(o),V(e,Temporal.Now.instant())),e)),s=(e,n,r)=>{t.set(se(e),V(n,r))};return function(e){const t=e??{};ce={run:o,read:n,seed:s,params:t},queueMicrotask(()=>{r.G.isNotNullable(ce)&&ce.params===t&&(ce=null)});const{data:c}=n(t);return c===q?null:c}}function ue(e){return ie(e,function(e){let t=oe.get(e);return r.G.isUndefined(t)&&(t=Z(),oe.set(e,t)),t}(e))}(ue||(ue={})).Cachable=function(e,t){return ie(t,e)};const le=Symbol("march-hare.coalesce/default");function fe(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 de=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var pe=(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))(pe||{}),he=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(he||{}),me=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(me||{});class be{[o.immerable]=!0;static keys=new Set(Object.values(me));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new be(this.value,this.operation);return n.property=e,n.process=t,n}}class ye{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=de}function ve(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 ge(e){if(r.G.isNullable(e)||Oe(e))return e;if(r.G.isArray(e))return e.map(e=>ge(e));if(r.G.isObject(e)&&je(e)){const t=Object.entries(e).map(([e,t])=>[e,ge(t)]);return{...Object.fromEntries(t),[ye.tag]:e[ye.tag]??ye.id()}}return e}function we(e){if(Array.isArray(e))return e.filter(e=>ye.tag in e).map(e=>e[ye.tag]??"").join(",");const t=e[ye.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function je(e){const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function Oe(e){return r.G.isNullable(e)||r.G.isString(e)||r.G.isNumber(e)||r.G.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function xe(e,t,n,o,s,c){return function a(i,u=t.path){if(i instanceof be){const t=ve(n,u.join("."));if(Object.entries(i).filter(([e,t])=>!be.keys.has(e)&&t instanceof be).forEach(([e,t])=>a(t,u.concat(e))),Oe(i.value)){if(e===he.Hydrate)return i.value;const a=u.slice(0,-1),l=a.length>0?ve(n,a.join(".")):n;return r.G.isNullable(l)||Pe(l,i,u.at(-1),o,s,c),t??i.value}if(e===he.Hydrate){const e=ge(a(i.value,u));return Pe(e,i,null,o,s,c),e}const l=t??ge(i.value);return Pe(l,i,null,o,s,c),r.G.isNullable(t)?l:(a(i.value,u),t)}if(r.G.isArray(i))return i.map((e,t)=>a(e,u.concat(t)));if(r.G.isObject(i)&&!je(i))return i;if(r.G.isObject(i)){const t=Object.entries(i).map(([e,t])=>[e,a(t,u.concat(e))]),n=Object.fromEntries(t);if(e===he.Hydrate){const e=ge(n);return Object.entries(i).forEach(([t,n])=>{n instanceof be&&Oe(n.value)&&Pe(e,n,t,o,s,c)}),e}return n}return i}(t.value)}function Pe(e,t,n,r,o,s){const c=s(e),a=o.get(c)??[];o.set(c,[t.assign(n,r),...a])}class Se{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=we){this.#t=e}static pk(){return de()}static"κ"=Se.pk;annotate(e,t){return new be(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,t,n,o,s){function c(o){const s=o.at(-1),c=ve(e(),o),a=o.slice(0,-1),i=r.A.isNotEmpty(a)?ve(e(),a):e();return[...r.G.isObject(c)||r.G.isArray(c)?t.get(n(c))?.filter(e=>r.G.isNullable(e.property))??[]:[],...r.G.isObject(i)?t.get(n(i))?.filter(e=>e.property===s)??[]:[]]}return function t(n){return new Proxy(()=>{},{get:(a,i)=>"pending"===i?()=>!r.A.isEmpty(c(n)):"remaining"===i?()=>r.A.length(c(n)):"box"===i?()=>({value:ve(e(),n),inspect:t(n)}):"is"===i?e=>c(n).some(t=>0!==(t.operation&e)):"draft"===i?()=>r.A.head(c(n))?.value??ve(e(),n):"settled"===i?()=>new Promise(t=>{if(r.A.isEmpty(c(n)))return t(ve(e(),n));const a=()=>{r.A.isEmpty(c(n))&&(s(a),t(ve(e(),n)))};o(a)}):t([...n,String(i)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#s(he.Hydrate,()=>e)}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#s(he.Produce,e)}#s(e,t){const n=Symbol("process"),[,r]=ye.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>ye.immer.applyPatches(t,[{...r,value:xe(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=ge(this.#e),this.#c(),n}prune(e){this.#n.forEach((t,n)=>{const o=t.filter(t=>t.process!==e);r.A.isEmpty(o)?this.#n.delete(n):this.#n.set(n,o)}),this.#c()}#c(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}const Ce=c.createContext(new Map);function Ee({action:e,renderer:t}){const n=h(),o=c.useContext(Ce),s=J(),a=c.useMemo(()=>{const t=o.get(e);if(t)return t;const s=new Se,c=n.getCached(e);r.G.isNotNullable(c)&&s.hydrate({value:c});const a={state:s,listeners:new Set};return o.set(e,a),a},[e,n,o]);c.useLayoutEffect(()=>{function t(e){a.state.hydrate({value:e}),a.listeners.forEach(e=>e())}return a.listeners.add(s),n.on(e,t),()=>{a.listeners.delete(s),n.off(e,t)}},[e,n,a]);const i=a.state.model?.value;return r.G.isNullable(i)?null:t(i,a.state.inspect.value)}function Ge(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 Ae(e,t,n){const{cursor:r,key:o}=Ge(e,t);r[o]=n}function Me(e){return(t,n)=>{t.actions.produce(t=>{Ae(t.model,e,n)})}}function ke(e){return t=>{t.actions.produce(t=>{!function(e,t){const{cursor:n,key:r}=Ge(e,t);n[r]=!n[r]}(t.model,e)})}}function Ne(e,t){return n=>{n.actions.produce(n=>{Ae(n.model,e,t)})}}const _e={Update:e=>Me(e),Invert:e=>ke(e),Always:(e,t)=>Ne(e,t)};function Re(){const e=c.useRef(null);return c.useMemo(()=>({actions:{dispatch:function(t,n){const r=e.current;if(!r)throw new Error("march-hare: useContext handle dispatched before its paired context.useActions(...) ran. Call context.actions.dispatch from event handlers, not synchronously during render.");return r(t,n)}},useActions:function(...t){const n=function(...e){const t=r.G.isUndefined(e[0])||r.G.isFunction(e[0])?{}:e[0],n=r.G.isFunction(e[0])?e[0]:e[1]??(()=>({})),s=h(),a=c.useContext(Y),i=c.useContext(b),u=g(),l=c.useContext(v),d=c.useContext(N),p=c.useContext(R),m=J(),y=c.useRef(!1),j=c.useRef(null),O=c.useRef(new Se);y.current||(y.current=!0,j.current=O.current.hydrate(t));const[x,P]=c.useState(()=>O.current.model),S=function(e){const t=c.useRef(e);return t.current=e,c.useMemo(()=>{return n=t,Object.keys(e).reduce((e,t)=>(Object.defineProperty(e,t,{get:()=>n.current[t],enumerable:!0}),e),{});var n},[e])}(n()),G=c.useMemo(()=>new f,[]),A=c.useRef({handlers:new Map});A.current.handlers=new Map;const k=function(){const e=c.useRef(new Set),t=c.useRef(new Set);return c.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),_=c.useRef(M.Mounting),U=c.useRef(new Set),L=c.useRef(0),T=c.useCallback((e,t,n)=>{const c=new AbortController,f={controller:c,action:e,payload:t};return i.add(f),U.current.add(f),{model:O.current.model,get phase(){return _.current},task:f,data:S,tasks:i,env:u,actions:{produce(e){if(c.signal.aborted)return;const t=l.current,r=O.current.produce(t=>{l.current=o.produce(l.current,n=>{e({model:t,inspect:O.current.inspect,env:n})})});P(O.current.model),l.current!==t&&s.emit(E,l.current),n.processes.add(r),j.current&&(n.processes.add(j.current),j.current=null)},dispatch(e,t){if(c.signal.aborted)return Promise.resolve();const n=W(e),r=$(e)?e.channel:void 0;return D(e)?a?z(a.emitter,n,t,r):Promise.resolve():z(B(e)?s:G,n,t,r)},annotate:(e,t=pe.Update)=>O.current.annotate(t,e),get inspect(){return O.current.inspect},resource:Object.assign(function(e){const t=ae(),n=(e,t)=>{if(c.signal.aborted)return Promise.resolve();const n=e,r=W(n);return D(n)?a?z(a.emitter,r,t,void 0):Promise.resolve():B(n)?z(s,r,t,void 0):Promise.resolve()},o={exceedsWindow:null,coalesceToken:void 0},i={then:(e,s)=>(()=>{if(r.G.isNotNullable(o.exceedsWindow)){const{data:e,at:n}=t.read(t.params);if(e!==q&&r.G.isNotNullable(n)){const t=Temporal.Now.instant().since(n),r=Temporal.Duration.from(o.exceedsWindow);if(Temporal.Duration.compare(t,r)<=0)return Promise.resolve(e)}}if(r.G.isUndefined(o.coalesceToken))return t.run(u,c,t.params,n);let e=d.get(t.run);r.G.isUndefined(e)&&(e=new Map,d.set(t.run,e));const s=e,a=`${JSON.stringify(t.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)}`}}(o.coalesceToken)}`,i=s.get(a);if(i)return fe(i,c.signal);const l=new AbortController,f=t.run(u,l,t.params,n).finally(()=>{s.delete(a)});return s.set(a,f),fe(f,c.signal)})().then(e,s),exceeds:e=>(o.exceedsWindow=e,i),coalesce:e=>(o.coalesceToken=e??le,i)};return i},{set:(e,t)=>{const n=ae();n.seed(n.params,t,Temporal.Now.instant())}}),async final(e){if(c.signal.aborted)return null;const t=W(e),n=D(e)?a?.emitter??null:s;if(!n)return null;const o=n.getCached(t);if(r.G.isUndefined(o))return null;const i=O.current.inspect;return i.pending()&&await new Promise((e,t)=>{if(c.signal.aborted)return void t(c.signal.reason);const n=()=>t(c.signal.reason);c.signal.addEventListener("abort",n,{once:!0}),i.settled().then(()=>{c.signal.removeEventListener("abort",n),e()})}),n.getCached(t)??null},peek(e){if(c.signal.aborted)return null;const t=W(e),n=D(e)?a?.emitter??null:s;return n?n.getCached(t)??null:null}}}},[x]);c.useLayoutEffect(()=>{function e(e,t,n){return function(o,c){const u=n();if(c===I&&r.G.isNotNullable(u))return;if(r.G.isNotNullable(c)&&c!==I&&r.G.isNotNullable(u)&&!function(e,t){for(const n of Object.keys(e))if(t[n]!==e[n])return!1;return!0}(c,u))return;const f={processes:new Set},d=Promise.withResolvers(),h=T(e,o,f),b=function(e){const t=W(e),n=r.G.isString(t)?t:t.description??"";return n.startsWith(w())&&n.slice(n.lastIndexOf("/")+1)||"unknown"}(e),y=performance.now(),v=O.current.model,g=l.current;let j,x=!1;function P(){const e=O.current.model,t=l.current;return{model:v===e?null:{before:v,after:e},env:g===t?null:{before:g,after:t}}}function S(){const t=c===I?void 0:c;return D(e)?a?z(a.emitter,e,o,t):Promise.resolve():z(B(e)?s:G,e,o,t)}function E(e){x=!0;const t=H(A.current.handlers,"Error"),n=r.G.isNotNullable(t),c=function(e){return e instanceof Error&&"AbortError"===e.name?Q.Aborted:Q.Errored}(e),a=function(e){return e instanceof Error?e:new Error(String(e))}(e),u={reason:c,error:a,action:b,handled:n,tasks:i,retry:S};s.fire(C,u),n&&t&&G.emit(t,u),p({stage:"end",result:"error",action:{name:b,payload:o},details:{task:h.task,elapsed:performance.now()-y,mutations:P(),error:a,reason:c}})}function M(){for(const e of i)if(e===h.task){i.delete(e),U.current.delete(e);break}f.processes.forEach(e=>O.current.prune(e)),f.processes.size>0&&m(),x||p({stage:"end",result:"success",action:{name:b,payload:o},details:{task:h.task,elapsed:performance.now()-y,mutations:P()}}),d.resolve()}p({stage:"start",action:{name:b,payload:o},details:{task:h.task}});try{j=t(h,o)}catch(k){return E(k),M(),d.promise}return function(e){if(!e||"object"!=typeof e)return!1;const t=Object.prototype.toString.call(e);return"[object Generator]"===t||"[object AsyncGenerator]"===t}(j)?((async()=>{for await(const e of j);})().catch(E).finally(M),d.promise):(Promise.resolve(j).catch(E).finally(M),d.promise)}}L.current++;const t=new Set;return A.current.handlers.forEach((n,r)=>{for(const{getChannel:o,handler:c}of n){const n=e(r,c,o);if(D(r)){if(a){const e=a.emitter;e.on(r,n),t.add(()=>e.off(r,n))}G.on(r,n),k.multicast.add(r),t.add(()=>G.off(r,n))}else B(r)?(s.on(r,n),G.on(r,n),k.broadcast.add(r),t.add(()=>{s.off(r,n),G.off(r,n)})):(G.on(r,n),t.add(()=>G.off(r,n)))}}),()=>{const e=++L.current,n=new Set(t);queueMicrotask(()=>{if(L.current!==e){for(const e of n)e();return}for(const e of U.current)e.controller.abort(new X("Component unmounted")),i.delete(e);U.current.clear(),_.current=M.Unmounting;const t=H(A.current.handlers,"Unmount");t&&G.emit(t),_.current=M.Unmounted;for(const e of n)e()})}},[G]),function({unicast:e,broadcast:t,dispatchers:n,scope:o,phase:s,data:a,handlers:i}){const u=c.useRef(null);c.useLayoutEffect(()=>{if(s.current===M.Mounted)return;s.current=M.Mounting;const c=H(i,"Mount");c&&e.emit(c),n.broadcast.forEach(n=>{const o=t.getCached(n);r.G.isNullable(o)||e.emit(n,o,I)}),o&&n.multicast.forEach(t=>{const n=o.emitter.getCached(t);r.G.isNullable(n)||e.emit(t,n,I)}),s.current=M.Mounted},[]),c.useLayoutEffect(()=>{if(r.G.isNotNullable(u.current)){const t=function(e,t){return Object.keys(t).reduce((n,r)=>e[r]!==t[r]?{...n,[r]:t[r]}:n,{})}(u.current,a);if(r.A.isNotEmpty(Object.keys(t))){const n=H(i,"Update");n&&e.emit(n,t)}}u.current=a},[a,e])}({unicast:G,broadcast:s,dispatchers:k,scope:a,phase:_,data:n(),handlers:A.current.handlers});const F=c.useMemo(()=>({dispatch(e,t){const n=W(e),r=$(e)?e.channel:void 0;return D(e)?a?z(a.emitter,n,t,r):Promise.resolve():z(B(e)?s:G,n,t,r)},get inspect(){return O.current.inspect},stream:(e,t)=>c.createElement(Ee,{action:W(e),renderer:t})}),[x,G]),K=c.useMemo(()=>[x,F,S],[x,F,S]);return K.useAction=(e,t)=>{!function(e,t,n){const r=c.useRef(n);c.useLayoutEffect(()=>{r.current=n});const o=c.useRef(t);c.useLayoutEffect(()=>{o.current=t});const s=c.useCallback((e,t)=>r.current(e,t),[]),a=c.useCallback(()=>$(o.current)?o.current.channel:void 0,[]),i=W(t),u=e.current.handlers.get(i)??new Set;0===u.size&&e.current.handlers.set(i,u),u.add({getChannel:a,handler:s})}(A,e,t)},K.dispatch=K[1].dispatch,K}(...t);return e.current=n.dispatch,n},with:{update:e=>Me(e),invert:e=>ke(e),always:(e,t)=>Ne(e,t)}}),[])}function Ue(){return{Boundary:function({children:e}){const n=c.useMemo(()=>({id:Symbol("march-hare.scope/instance"),emitter:new d}),[]);return t.jsx(Y.Provider,{value:n,children:e})},useContext:function(){return Re()},useEnv:function(){return g()},Resource:Object.assign(function(e){return ue(e)},{Cachable:(e,t)=>ue.Cachable(e,t)})}}const Le=new Se,Te=Object.freeze(Object.defineProperty({__proto__:null,get Resource(){return ue},Scope:function(){return Ue()},useContext:function(){return Re()},useEnv:function(){return g()}},Symbol.toStringTag,{value:"Module"}));e.Aborted=X,e.Action=(e="",t=A.Unicast)=>{const n=t===A.Broadcast?Symbol(j(e)):t===A.Multicast?Symbol(O(e)):Symbol(w(e)),r=function(t){return{[P.Action]:n,[P.Payload]:void 0,[P.Channel]:t,[P.Name]:e,channel:t}};return Object.defineProperty(r,P.Action,{value:n,enumerable:!1}),Object.defineProperty(r,P.Payload,{value:void 0,enumerable:!1}),Object.defineProperty(r,P.Name,{value:e,enumerable:!1}),t===A.Broadcast&&Object.defineProperty(r,P.Broadcast,{value:!0,enumerable:!1}),t===A.Multicast&&Object.defineProperty(r,P.Multicast,{value:!0,enumerable:!1}),r},e.App=function(e){return{Boundary:function({children:n}){return t.jsx(L,{env:e?.env,tap:e?.tap,children:n})},useContext:function(){return Re()},useEnv:function(){return g()},Resource:Object.assign(function(e){return ue(e)},{Cachable:(e,t)=>ue.Cachable(e,t)}),Scope:()=>Ue()}},e.Boundary=L,e.Cache=Z,e.Distribution=A,e.Lifecycle=G,e.Op=pe,e.Operation=pe,e.Reason=Q,e.State=Se,e.With=_e,e.annotate=function(e,t=pe.Update){return Le.annotate(t,e)},e.shared=Te,e.utils=re,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})},"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("react/jsx-runtime"),require("react"),require("@mobily/ts-belt"),require("immer")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","react","@mobily/ts-belt","immer"],factory):factory((global="undefined"!=typeof globalThis?globalThis:global||self).MarchHare={},global.jsxRuntime,global.React,global.TsBelt,global.Immer);
|
package/dist/resource/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PendingCall, ResourceHandle } from './types';
|
|
2
2
|
import { Cache } from './utils';
|
|
3
|
+
import { AppFetcher } from '../app/types';
|
|
3
4
|
export type { Coalesce, Fetcher, PendingCall, ResourceHandle, } from './types';
|
|
4
5
|
/**
|
|
5
6
|
* Reads and clears the slot populated by the most recent resource
|
|
@@ -12,28 +13,41 @@ export type { Coalesce, Fetcher, PendingCall, ResourceHandle, } from './types';
|
|
|
12
13
|
export declare function consumePending(): PendingCall;
|
|
13
14
|
/**
|
|
14
15
|
* Defines a remote resource — declared at module scope and used
|
|
15
|
-
* directly. Calling the returned handle
|
|
16
|
-
* cache value (`T | null`) and primes
|
|
17
|
-
* `context.actions.resource(...)` / `.set(...)`
|
|
18
|
-
* paths.
|
|
16
|
+
* directly. Exported as `shared.Resource`. Calling the returned handle
|
|
17
|
+
* with `params` returns the sync cache value (`T | null`) and primes
|
|
18
|
+
* the slot consumed by `context.actions.resource(...)` / `.set(...)`
|
|
19
|
+
* for fetch and write paths.
|
|
20
|
+
*
|
|
21
|
+
* Takes the **Env shape `E` as a mandatory first generic** —
|
|
22
|
+
* `context.env` inside the fetcher is typed as `E`. Pass a union of
|
|
23
|
+
* every App's Env if the resource is shared across reusable
|
|
24
|
+
* components. For single-app resources, prefer `app.Resource` —
|
|
25
|
+
* the Env is captured from `app` automatically and you only need the
|
|
26
|
+
* payload generic.
|
|
19
27
|
*
|
|
20
28
|
* The fetcher receives a single `context` argument carrying `env`,
|
|
21
29
|
* `controller`, `params`, and a broadcast/multicast-only `dispatch`.
|
|
22
|
-
* `env` is a live handle — dot reads inside the fetcher
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* across reloads.
|
|
30
|
+
* `env` is a live handle — dot reads inside the fetcher always
|
|
31
|
+
* see the latest per-`<Boundary>` Env, even after `await` boundaries.
|
|
32
|
+
* Every successful fetch writes through to a per-resource in-memory
|
|
33
|
+
* cache; pair with {@link Resource.Cachable} to persist across reloads.
|
|
27
34
|
*
|
|
28
35
|
* Concurrent calls fire fresh requests by default. Opt in to in-flight
|
|
29
36
|
* sharing per call via `.coalesce(key)` on the thenable returned from
|
|
30
37
|
* `context.actions.resource(...)`.
|
|
31
38
|
*
|
|
39
|
+
* @template E The Env shape (or union) the fetcher's `context.env` is
|
|
40
|
+
* typed against.
|
|
41
|
+
* @template T The payload type the fetcher resolves to.
|
|
42
|
+
* @template P The call-time params type.
|
|
43
|
+
*
|
|
32
44
|
* @example
|
|
33
45
|
* ```ts
|
|
34
|
-
* import {
|
|
46
|
+
* import { shared } from "march-hare";
|
|
47
|
+
*
|
|
48
|
+
* type WebEnv = { session: Session | null };
|
|
35
49
|
*
|
|
36
|
-
* export const user = Resource<User, { id: number }>((context) =>
|
|
50
|
+
* export const user = shared.Resource<WebEnv, User, { id: number }>((context) =>
|
|
37
51
|
* ky
|
|
38
52
|
* .get(`users/${context.params.id}`, {
|
|
39
53
|
* headers: context.env.session
|
|
@@ -45,18 +59,31 @@ export declare function consumePending(): PendingCall;
|
|
|
45
59
|
* );
|
|
46
60
|
* ```
|
|
47
61
|
*/
|
|
48
|
-
export declare function Resource<T, P extends object = Record<never, never>>(ƒ:
|
|
62
|
+
export declare function Resource<E extends object, T, P extends object = Record<never, never>>(ƒ: AppFetcher<E, T, P>): ResourceHandle<T, P>;
|
|
49
63
|
export declare namespace Resource {
|
|
50
64
|
/**
|
|
51
|
-
* Cache-aware variant of {@link Resource}
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
65
|
+
* Cache-aware variant of {@link Resource}, exported as
|
|
66
|
+
* `shared.Resource.Cachable`. The supplied {@link Cache} is the
|
|
67
|
+
* **second** argument (after the Env generic) — persistence is
|
|
68
|
+
* the headline of this form, the fetcher is the operation. Every
|
|
69
|
+
* successful fetch writes through to the cache; first reads via the
|
|
70
|
+
* call form auto-seed from the cache's adapter.
|
|
71
|
+
*
|
|
72
|
+
* Takes the same **Env shape `E` as a mandatory first generic** as
|
|
73
|
+
* {@link Resource}. For single-app resources, prefer
|
|
74
|
+
* `app.Resource.Cachable(cache, fetcher)` — the Env is captured
|
|
75
|
+
* from `app` automatically.
|
|
76
|
+
*
|
|
77
|
+
* @template E The Env shape (or union) the fetcher's `context.env` is
|
|
78
|
+
* typed against.
|
|
79
|
+
* @template T The payload type the fetcher resolves to.
|
|
80
|
+
* @template P The call-time params type.
|
|
56
81
|
*
|
|
57
82
|
* @example
|
|
58
83
|
* ```ts
|
|
59
|
-
* import { Cache,
|
|
84
|
+
* import { Cache, shared } from "march-hare";
|
|
85
|
+
*
|
|
86
|
+
* type WebEnv = { session: Session | null };
|
|
60
87
|
*
|
|
61
88
|
* const cache = Cache({
|
|
62
89
|
* get: (key) => localStorage.getItem(key),
|
|
@@ -65,7 +92,7 @@ export declare namespace Resource {
|
|
|
65
92
|
* clear: () => localStorage.clear(),
|
|
66
93
|
* });
|
|
67
94
|
*
|
|
68
|
-
* export const cat = Resource.Cachable(cache, async (context) =>
|
|
95
|
+
* export const cat = shared.Resource.Cachable<WebEnv, Cat>(cache, async (context) =>
|
|
69
96
|
* ky
|
|
70
97
|
* .get("https://api.thecatapi.com/v1/images/search", {
|
|
71
98
|
* signal: context.controller.signal,
|
|
@@ -75,5 +102,5 @@ export declare namespace Resource {
|
|
|
75
102
|
* );
|
|
76
103
|
* ```
|
|
77
104
|
*/
|
|
78
|
-
function Cachable<T, P extends object = Record<never, never>>(cache: Cache, ƒ:
|
|
105
|
+
function Cachable<E extends object, T, P extends object = Record<never, never>>(cache: Cache, ƒ: AppFetcher<E, T, P>): ResourceHandle<T, P>;
|
|
79
106
|
}
|
package/dist/scope/index.d.ts
CHANGED
|
@@ -61,3 +61,38 @@ export type Scope<S extends object, MulticastActions> = {
|
|
|
61
61
|
* @internal
|
|
62
62
|
*/
|
|
63
63
|
export declare function createScope<S extends object, MulticastActions>(): Scope<S, MulticastActions>;
|
|
64
|
+
/**
|
|
65
|
+
* Standalone counterpart to `app.Scope<MulticastActions>()`, exported
|
|
66
|
+
* as `shared.Scope` — opens a typed multicast scope without
|
|
67
|
+
* going through an `App` handle. Takes the **Env shape `E` as a
|
|
68
|
+
* mandatory first generic**, mirroring the other standalone exports
|
|
69
|
+
* (`shared.useContext`, `shared.useEnv`, `shared.Resource`). The Env
|
|
70
|
+
* carried by `scope.useContext()` is typed as `E`.
|
|
71
|
+
*
|
|
72
|
+
* Use this in reusable feature modules that need to open their own
|
|
73
|
+
* multicast scope without binding to one App's `app.Scope` factory.
|
|
74
|
+
* For single-app code, prefer `app.Scope<MulticastActions>()` —
|
|
75
|
+
* the Env is captured from `app` automatically.
|
|
76
|
+
*
|
|
77
|
+
* @template E The Env shape (or union) the scope's `useContext` types
|
|
78
|
+
* `context.env` against.
|
|
79
|
+
* @template A The multicast Actions class (or union of classes) the
|
|
80
|
+
* scope's dispatch surface is widened to include.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```tsx
|
|
84
|
+
* import { Action, Distribution, shared } from "march-hare";
|
|
85
|
+
*
|
|
86
|
+
* class MulticastActions {
|
|
87
|
+
* static Mood = Action<"happy" | "sad">(
|
|
88
|
+
* "Mood",
|
|
89
|
+
* Distribution.Multicast,
|
|
90
|
+
* );
|
|
91
|
+
* }
|
|
92
|
+
*
|
|
93
|
+
* type MoodEnv = { tracker: string };
|
|
94
|
+
*
|
|
95
|
+
* export const scope = shared.Scope<MoodEnv, typeof MulticastActions>();
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function Scope<E extends object, A>(): Scope<E, A>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `shared` namespace — standalone counterparts to the `app.X`
|
|
3
|
+
* factories returned by `App<E>()`. Each export takes the Env shape
|
|
4
|
+
* `E` as a mandatory first generic so reusable components can run
|
|
5
|
+
* under more than one App without binding to a single `app` import.
|
|
6
|
+
*
|
|
7
|
+
* Reach for `shared.X` only when a component must support more than
|
|
8
|
+
* one App. Single-app code should keep using `app.X` — the Env
|
|
9
|
+
* is captured from `app` automatically and the call site is one
|
|
10
|
+
* generic shorter.
|
|
11
|
+
*
|
|
12
|
+
* | Bound to an App | Standalone (`shared.X`) |
|
|
13
|
+
* | -------------------------------- | ---------------------------------------- |
|
|
14
|
+
* | `app.useContext<M, A, D>()` | `shared.useContext<E, M, A, D>()` |
|
|
15
|
+
* | `app.useEnv()` | `shared.useEnv<E>()` |
|
|
16
|
+
* | `app.Resource<T, P>(...)` | `shared.Resource<E, T, P>(...)` |
|
|
17
|
+
* | `app.Resource.Cachable(...)` | `shared.Resource.Cachable<E, T, P>(...)` |
|
|
18
|
+
* | `app.Scope<A>()` | `shared.Scope<E, A>()` |
|
|
19
|
+
*
|
|
20
|
+
* @see {@link ./app/index.tsx App}
|
|
21
|
+
*/
|
|
22
|
+
export { useContext, useEnv } from '../app/index';
|
|
23
|
+
export { Scope } from '../scope/index';
|
|
24
|
+
export { Resource } from '../resource/index';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -650,11 +650,14 @@ export type WithPayloadActions<U> = Exclude<U, {
|
|
|
650
650
|
*
|
|
651
651
|
* @example
|
|
652
652
|
* ```ts
|
|
653
|
-
* import { Action, Distribution, Handlers } from "march-hare";
|
|
653
|
+
* import { Action, Distribution, type Handlers } from "march-hare";
|
|
654
654
|
*
|
|
655
655
|
* class BroadcastActions {
|
|
656
656
|
* static PaymentSent = Action("PaymentSent", Distribution.Broadcast);
|
|
657
|
-
* static PaymentLink = Action<PaymentLinkData>(
|
|
657
|
+
* static PaymentLink = Action<PaymentLinkData>(
|
|
658
|
+
* "PaymentLink",
|
|
659
|
+
* Distribution.Broadcast,
|
|
660
|
+
* );
|
|
658
661
|
* }
|
|
659
662
|
*
|
|
660
663
|
* class Actions {
|