march-hare 0.6.1 → 0.7.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 +89 -96
- package/dist/{src/library/action → action}/index.d.ts +19 -15
- package/dist/{src/library/action → action}/utils.d.ts +2 -2
- package/dist/{src/library/boundary → boundary}/components/broadcast/index.d.ts +2 -2
- package/dist/{src/library/boundary → boundary}/components/broadcast/types.d.ts +1 -1
- package/dist/{src/library/boundary → boundary}/components/consumer/components/partition/index.d.ts +1 -1
- package/dist/{src/library/boundary → boundary}/components/consumer/components/partition/types.d.ts +1 -1
- package/dist/{src/library/boundary → boundary}/components/consumer/index.d.ts +5 -5
- package/dist/{src/library/boundary → boundary}/components/consumer/types.d.ts +1 -1
- package/dist/{src/library/boundary → boundary}/components/consumer/utils.d.ts +1 -1
- package/dist/{src/library/boundary → boundary}/components/scope/index.d.ts +3 -3
- package/dist/{src/library/boundary → boundary}/components/scope/types.d.ts +2 -2
- package/dist/{src/library/boundary → boundary}/components/scope/utils.d.ts +2 -2
- package/dist/boundary/components/store/index.d.ts +41 -0
- package/dist/boundary/components/store/types.d.ts +11 -0
- package/dist/boundary/components/store/utils.d.ts +64 -0
- package/dist/{src/library/boundary → boundary}/components/tasks/index.d.ts +2 -2
- package/dist/{src/library/boundary → boundary}/components/tasks/types.d.ts +3 -3
- package/dist/{src/library/boundary → boundary}/components/tasks/utils.d.ts +1 -1
- package/dist/boundary/index.d.ts +21 -0
- package/dist/boundary/types.d.ts +22 -0
- package/dist/cache/index.d.ts +44 -0
- package/dist/cache/types.d.ts +54 -0
- package/dist/{src/library/error → error}/types.d.ts +1 -1
- package/dist/{src/library/error → error}/utils.d.ts +1 -1
- package/dist/{src/library/hooks → hooks}/index.d.ts +3 -3
- package/dist/{src/library/hooks → hooks}/types.d.ts +3 -3
- package/dist/{src/library/hooks → hooks}/utils.d.ts +3 -3
- package/dist/index.d.ts +19 -0
- package/dist/march-hare.js +6 -6
- package/dist/march-hare.umd.cjs +1 -1
- package/dist/resource/index.d.ts +102 -0
- package/dist/resource/types.d.ts +27 -0
- package/dist/resource/utils.d.ts +37 -0
- package/dist/{src/library/types → types}/index.d.ts +162 -21
- package/dist/{src/library/utils → utils}/index.d.ts +3 -43
- package/dist/utils/types.d.ts +18 -0
- package/dist/{src/library/utils → utils}/utils.d.ts +1 -1
- package/package.json +2 -2
- package/dist/src/library/boundary/components/mode/index.d.ts +0 -15
- package/dist/src/library/boundary/components/mode/types.d.ts +0 -7
- package/dist/src/library/boundary/components/mode/utils.d.ts +0 -55
- package/dist/src/library/boundary/index.d.ts +0 -20
- package/dist/src/library/boundary/types.d.ts +0 -4
- package/dist/src/library/index.d.ts +0 -17
- package/dist/src/library/resource/index.d.ts +0 -65
- package/dist/src/library/resource/types.d.ts +0 -150
- package/dist/src/library/resource/utils.d.ts +0 -23
- package/dist/src/library/utils/types.d.ts +0 -101
- /package/dist/{src/library/annotate → annotate}/index.d.ts +0 -0
- /package/dist/{src/library/boundary → boundary}/components/broadcast/utils.d.ts +0 -0
- /package/dist/{src/library/error → error}/index.d.ts +0 -0
- /package/dist/{src/library/utils.d.ts → utils.d.ts} +0 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Cache } from '../cache/index';
|
|
2
|
+
import { unset } from '../utils/index';
|
|
3
|
+
export { Cache } from '../cache/index';
|
|
4
|
+
/**
|
|
5
|
+
* Default in-memory `Cache` used when {@link Resource} is constructed
|
|
6
|
+
* without an explicit one. Each fetcher gets its own slot via the
|
|
7
|
+
* outer `WeakMap` so unrelated Resources don't share a string-key
|
|
8
|
+
* namespace.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare const defaults: WeakMap<object, Cache>;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the {@link Cache} bound to `fetcher`, allocating a fresh
|
|
15
|
+
* in-memory Cache on first access.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function defaultCache(fetcher: object): Cache;
|
|
20
|
+
/**
|
|
21
|
+
* Stable string key derived from the call-site `params`. Two calls with
|
|
22
|
+
* the same logical params (same key order, same primitive values) hit
|
|
23
|
+
* the same slot. JSON.stringify is sufficient for the Chizu params
|
|
24
|
+
* convention (primitive-leaf objects); callers who need order-stable
|
|
25
|
+
* keying should normalise the object before passing it in.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare function key(params: object): string;
|
|
30
|
+
/**
|
|
31
|
+
* Re-export of the shared `unset` sentinel from {@link "../utils/index.ts"}.
|
|
32
|
+
*
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export declare const config: {
|
|
36
|
+
readonly unset: typeof unset;
|
|
37
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Operation, Process, Inspect, Box } from 'immertation';
|
|
2
|
-
import { ActionId, Task, Tasks } from '../boundary/components/tasks/types
|
|
3
|
-
import { Fault } from '../error/types
|
|
2
|
+
import { ActionId, Task, Tasks } from '../boundary/components/tasks/types';
|
|
3
|
+
import { Fault } from '../error/types';
|
|
4
|
+
import { Store } from '../boundary/components/store/index';
|
|
4
5
|
import * as React from "react";
|
|
5
6
|
export type { ActionId, Box, Task, Tasks };
|
|
6
7
|
/**
|
|
@@ -31,6 +32,20 @@ export type BrandedMulticast = {
|
|
|
31
32
|
export type BrandedObject = {
|
|
32
33
|
readonly [x: symbol]: unknown;
|
|
33
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Recursive readonly. Locks every nested property so that read-only
|
|
37
|
+
* projections on `context` (model, data, store) reject direct assignment
|
|
38
|
+
* — mutation must go through `context.actions.produce(...)`.
|
|
39
|
+
*
|
|
40
|
+
* Function types pass through untouched so method calls (e.g.
|
|
41
|
+
* `AbortController#abort`) remain callable. Built-in mutable containers
|
|
42
|
+
* are mapped to their readonly counterparts.
|
|
43
|
+
*
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export type DeepReadonly<T> = T extends (...args: never) => unknown ? T : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepReadonly<U>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends ReadonlySet<infer U> ? ReadonlySet<DeepReadonly<U>> : T extends object ? {
|
|
47
|
+
readonly [K in keyof T]: DeepReadonly<T[K]>;
|
|
48
|
+
} : T;
|
|
34
49
|
/**
|
|
35
50
|
* Union type representing any valid action that can be passed to action utilities.
|
|
36
51
|
* This includes raw ActionIds (symbol/string), and any branded object.
|
|
@@ -53,6 +68,13 @@ export declare class Brand {
|
|
|
53
68
|
static readonly Action: unique symbol;
|
|
54
69
|
/** Identifies channeled actions (result of calling Action(channel)) */
|
|
55
70
|
static readonly Channel: unique symbol;
|
|
71
|
+
/**
|
|
72
|
+
* Phantom brand carrying the action's literal name. Used purely at the
|
|
73
|
+
* type level to make `Action("X")` and `Action("Y")` produce
|
|
74
|
+
* structurally-distinct types so `dispatch`/`useAction` can reject
|
|
75
|
+
* symbols imported from a class outside `AC`.
|
|
76
|
+
*/
|
|
77
|
+
static readonly Name: unique symbol;
|
|
56
78
|
}
|
|
57
79
|
/**
|
|
58
80
|
* Internal symbol for the global `Lifecycle.Fault` broadcast. Exposed so the
|
|
@@ -86,13 +108,13 @@ export declare const FaultSymbol: unique symbol;
|
|
|
86
108
|
*/
|
|
87
109
|
export declare class Lifecycle {
|
|
88
110
|
/** Creates a Mount lifecycle action. Triggered once on component mount (`useLayoutEffect`). */
|
|
89
|
-
static Mount(): HandlerPayload<never>;
|
|
111
|
+
static Mount(): HandlerPayload<never, never, "Mount">;
|
|
90
112
|
/** Creates an Unmount lifecycle action. Triggered when the component unmounts. */
|
|
91
|
-
static Unmount(): HandlerPayload<never>;
|
|
113
|
+
static Unmount(): HandlerPayload<never, never, "Unmount">;
|
|
92
114
|
/** Creates an Error lifecycle action. Triggered when an action throws. Receives `Fault` as payload. */
|
|
93
|
-
static Error(): HandlerPayload<Fault>;
|
|
115
|
+
static Error(): HandlerPayload<Fault, never, "Error">;
|
|
94
116
|
/** Creates an Update lifecycle action. Triggered when `context.data` changes (not on initial mount). */
|
|
95
|
-
static Update(): HandlerPayload<Record<string, unknown
|
|
117
|
+
static Update(): HandlerPayload<Record<string, unknown>, never, "Update">;
|
|
96
118
|
/**
|
|
97
119
|
* Global fault broadcast. Receives a `Fault` whenever any action in the
|
|
98
120
|
* `<Boundary>` errors, times out, or is supplanted. Subscribe via
|
|
@@ -112,7 +134,7 @@ export declare class Lifecycle {
|
|
|
112
134
|
* });
|
|
113
135
|
* ```
|
|
114
136
|
*/
|
|
115
|
-
static Fault: BroadcastPayload<Fault>;
|
|
137
|
+
static Fault: BroadcastPayload<Fault, never, "Fault">;
|
|
116
138
|
}
|
|
117
139
|
/**
|
|
118
140
|
* Distribution modes for actions.
|
|
@@ -209,12 +231,13 @@ export type Model<M = Record<string, unknown>> = M;
|
|
|
209
231
|
* dispatch(UserUpdated({ UserId: 5 }), user); // channeled dispatch
|
|
210
232
|
* ```
|
|
211
233
|
*/
|
|
212
|
-
export type HandlerPayload<P = unknown, C extends Filter = never> = {
|
|
234
|
+
export type HandlerPayload<P = unknown, C extends Filter = never, Name extends string = string> = {
|
|
213
235
|
readonly [Brand.Action]: symbol;
|
|
214
236
|
readonly [Brand.Payload]: P;
|
|
237
|
+
readonly [Brand.Name]: Name;
|
|
215
238
|
readonly [Brand.Broadcast]?: boolean;
|
|
216
239
|
} & ([C] extends [never] ? unknown : {
|
|
217
|
-
(channel: C): ChanneledAction<P, C>;
|
|
240
|
+
(channel: C): ChanneledAction<P, C, Name>;
|
|
218
241
|
});
|
|
219
242
|
/**
|
|
220
243
|
* Result of calling an action with a channel argument.
|
|
@@ -231,10 +254,11 @@ export type HandlerPayload<P = unknown, C extends Filter = never> = {
|
|
|
231
254
|
* dispatch(UserUpdated({ UserId: 5 }), user);
|
|
232
255
|
* ```
|
|
233
256
|
*/
|
|
234
|
-
export type ChanneledAction<P = unknown, C = unknown> = {
|
|
257
|
+
export type ChanneledAction<P = unknown, C = unknown, Name extends string = string> = {
|
|
235
258
|
readonly [Brand.Action]: symbol;
|
|
236
259
|
readonly [Brand.Payload]: P;
|
|
237
260
|
readonly [Brand.Channel]: C;
|
|
261
|
+
readonly [Brand.Name]: Name;
|
|
238
262
|
readonly channel: C;
|
|
239
263
|
};
|
|
240
264
|
/**
|
|
@@ -260,7 +284,7 @@ export type ChanneledAction<P = unknown, C = unknown> = {
|
|
|
260
284
|
* const user = await context.actions.resolution(SignedOut);
|
|
261
285
|
* ```
|
|
262
286
|
*/
|
|
263
|
-
export type BroadcastPayload<P = unknown, C extends Filter = never> = HandlerPayload<P, C> & {
|
|
287
|
+
export type BroadcastPayload<P = unknown, C extends Filter = never, Name extends string = string> = HandlerPayload<P, C, Name> & {
|
|
264
288
|
readonly [Brand.Broadcast]: true;
|
|
265
289
|
};
|
|
266
290
|
/**
|
|
@@ -306,7 +330,7 @@ export type BroadcastPayload<P = unknown, C extends Filter = never> = HandlerPay
|
|
|
306
330
|
* actions.dispatch(Actions.Multicast.Update, 42);
|
|
307
331
|
* ```
|
|
308
332
|
*/
|
|
309
|
-
export type MulticastPayload<P = unknown, C extends Filter = never> = HandlerPayload<P, C> & {
|
|
333
|
+
export type MulticastPayload<P = unknown, C extends Filter = never, Name extends string = string> = HandlerPayload<P, C, Name> & {
|
|
310
334
|
readonly [Brand.Multicast]: true;
|
|
311
335
|
};
|
|
312
336
|
/**
|
|
@@ -401,8 +425,8 @@ export type Actions = object;
|
|
|
401
425
|
export type Result = {
|
|
402
426
|
processes: Set<Process>;
|
|
403
427
|
};
|
|
404
|
-
export type HandlerContext<M extends Model | void,
|
|
405
|
-
readonly model:
|
|
428
|
+
export type HandlerContext<M extends Model | void, AC extends Actions | void, D extends Props = Props> = {
|
|
429
|
+
readonly model: DeepReadonly<M>;
|
|
406
430
|
/**
|
|
407
431
|
* The current lifecycle phase of the component.
|
|
408
432
|
* Useful for determining if the handler was called during mount (e.g., from a cached
|
|
@@ -458,7 +482,7 @@ export type HandlerContext<M extends Model | void, _AC extends Actions | void, D
|
|
|
458
482
|
* });
|
|
459
483
|
* ```
|
|
460
484
|
*/
|
|
461
|
-
readonly data: D
|
|
485
|
+
readonly data: DeepReadonly<D>;
|
|
462
486
|
/**
|
|
463
487
|
* Set of all running tasks across all components in the context.
|
|
464
488
|
* Tasks are ordered by creation time (oldest first).
|
|
@@ -490,13 +514,87 @@ export type HandlerContext<M extends Model | void, _AC extends Actions | void, D
|
|
|
490
514
|
* ```
|
|
491
515
|
*/
|
|
492
516
|
readonly tasks: ReadonlySet<Task>;
|
|
517
|
+
/**
|
|
518
|
+
* Read-only view of the per-`<Boundary>` Store — ambient,
|
|
519
|
+
* cross-cutting state (session, locale, feature flags, etc.) typed
|
|
520
|
+
* via module augmentation on the library's `Store` interface.
|
|
521
|
+
* Identical to the value returned by `useStore()` at the hook level.
|
|
522
|
+
*
|
|
523
|
+
* Reads use plain dot notation and always reflect the latest value,
|
|
524
|
+
* even after `await` boundaries. Writes go through
|
|
525
|
+
* `context.actions.produce(({ store }) => { store.x = ... })`
|
|
526
|
+
* — the same Immer-style recipe used for the model.
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* actions.useAction(Actions.SignIn, async (context, credentials) => {
|
|
531
|
+
* const result = await context.actions.resource(signIn(credentials));
|
|
532
|
+
* context.actions.produce(({ store }) => {
|
|
533
|
+
* store.session = result;
|
|
534
|
+
* });
|
|
535
|
+
* });
|
|
536
|
+
*
|
|
537
|
+
* actions.useAction(Actions.Refresh, async (context) => {
|
|
538
|
+
* if (context.store.session === null) return;
|
|
539
|
+
* // ...
|
|
540
|
+
* });
|
|
541
|
+
* ```
|
|
542
|
+
*/
|
|
543
|
+
readonly store: DeepReadonly<Store>;
|
|
493
544
|
readonly actions: {
|
|
494
545
|
produce<F extends (draft: {
|
|
495
546
|
model: M;
|
|
547
|
+
store: Store;
|
|
496
548
|
readonly inspect: Readonly<Inspect<M>>;
|
|
497
549
|
}) => void>(ƒ: F & AssertSync<F>): void;
|
|
498
|
-
dispatch(action:
|
|
550
|
+
dispatch(action: NoPayloadActions<Dispatchable<AC>>): Promise<void>;
|
|
551
|
+
dispatch<A extends WithPayloadActions<Dispatchable<AC>>>(action: A, payload: Payload<A>): Promise<void>;
|
|
499
552
|
annotate<T>(value: T, operation?: Operation): T;
|
|
553
|
+
/**
|
|
554
|
+
* Fetches a {@link Resource} with the abort controller and Store
|
|
555
|
+
* snapshot auto-threaded from the current handler context. The
|
|
556
|
+
* argument is a resource invocation (`cat({ id: 5 })`) — the
|
|
557
|
+
* call primes a slot with the resource and params, and
|
|
558
|
+
* `.resource(...)` reads it. The return value is a thenable —
|
|
559
|
+
* `await` it to fire the fetch unconditionally, or use
|
|
560
|
+
* `.exceeds(duration)` to short-circuit when the per-params cache
|
|
561
|
+
* slot is still within the supplied freshness window (i.e. fetch
|
|
562
|
+
* only when the cache age *exceeds* the duration).
|
|
563
|
+
*
|
|
564
|
+
* @example
|
|
565
|
+
* ```ts
|
|
566
|
+
* actions.useAction(Actions.Mount, async (context) => {
|
|
567
|
+
* // Always fetch.
|
|
568
|
+
* const fresh = await context.actions.resource(user({ id: 5 }));
|
|
569
|
+
*
|
|
570
|
+
* // Reuse cache when < 5 minutes old.
|
|
571
|
+
* const maybe = await context.actions
|
|
572
|
+
* .resource(user({ id: 5 }))
|
|
573
|
+
* .exceeds({ minutes: 5 });
|
|
574
|
+
*
|
|
575
|
+
* context.actions.produce(({ model }) => void (model.user = fresh));
|
|
576
|
+
* });
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
579
|
+
resource: (<T>(invocation: T | null) => PromiseLike<T> & {
|
|
580
|
+
readonly exceeds: (duration: Temporal.DurationLike) => Promise<T>;
|
|
581
|
+
}) & {
|
|
582
|
+
/**
|
|
583
|
+
* Writes `data` into the per-params cache slot of the resource
|
|
584
|
+
* invocation passed as the first argument, with a fresh timestamp.
|
|
585
|
+
* Use this when payloads arrive out-of-band (SSE, WebSocket,
|
|
586
|
+
* postMessage) and need to be reflected in the Resource cache
|
|
587
|
+
* without a fetcher round-trip.
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```ts
|
|
591
|
+
* actions.useAction(Actions.Broadcast.UserSSE, (context, payload) => {
|
|
592
|
+
* context.actions.resource.set(user({ id: payload.id }), payload);
|
|
593
|
+
* });
|
|
594
|
+
* ```
|
|
595
|
+
*/
|
|
596
|
+
set<T>(invocation: T | null, data: T): void;
|
|
597
|
+
};
|
|
500
598
|
/**
|
|
501
599
|
* Returns the resolved broadcast or multicast value, waiting for any
|
|
502
600
|
* pending annotations to settle before resolving.
|
|
@@ -586,6 +684,50 @@ export type Handler<M extends Model | void, AC extends Actions | void, K extends
|
|
|
586
684
|
* as a handler key and avoids recursion into Function internals.
|
|
587
685
|
*/
|
|
588
686
|
type OwnKeys<AC> = Exclude<keyof AC & string, "prototype">;
|
|
687
|
+
/**
|
|
688
|
+
* Recursively flattens an actions class into the union of its leaf action
|
|
689
|
+
* types. A "leaf" is any property whose own string keys are empty — the
|
|
690
|
+
* branded `HandlerPayload` / `BroadcastPayload` / `MulticastPayload` values
|
|
691
|
+
* produced by `Action(...)` and `Lifecycle.*()`. Nested namespace classes
|
|
692
|
+
* (e.g. `static Broadcast = BroadcastActions`) are descended into.
|
|
693
|
+
*
|
|
694
|
+
* Used to constrain `dispatch` and `useAction` so that only actions owned by
|
|
695
|
+
* the component's `AC` (plus the global `Lifecycle.Fault`) can be referenced.
|
|
696
|
+
*/
|
|
697
|
+
export type LeafActions<AC> = AC extends void ? never : {
|
|
698
|
+
[K in OwnKeys<AC>]: OwnKeys<AC[K]> extends never ? AC[K] : LeafActions<AC[K]>;
|
|
699
|
+
}[OwnKeys<AC>];
|
|
700
|
+
/**
|
|
701
|
+
* Maps each action in a union to its channeled-call variant, when one exists.
|
|
702
|
+
* Distributes over unions so a mixed bag of leaf actions produces the union
|
|
703
|
+
* of their `ChanneledAction<P, C>` results.
|
|
704
|
+
*/
|
|
705
|
+
export type ChanneledOf<A> = A extends HandlerPayload<infer P, infer C> ? [C] extends [never] ? never : ChanneledAction<P, C> : never;
|
|
706
|
+
/**
|
|
707
|
+
* Everything `dispatch` accepts for a given `AC`: leaf actions on the class
|
|
708
|
+
* and their channeled-call variants. The shared `Lifecycle.Fault` broadcast
|
|
709
|
+
* is excluded — it's library-internal and not user-dispatchable.
|
|
710
|
+
*/
|
|
711
|
+
export type Dispatchable<AC> = LeafActions<AC> | ChanneledOf<LeafActions<AC>>;
|
|
712
|
+
/**
|
|
713
|
+
* Everything `useAction` will subscribe to for a given `AC`: same as
|
|
714
|
+
* `Dispatchable<AC>` plus the shared `Lifecycle.Fault` broadcast which lives
|
|
715
|
+
* outside `AC` but is subscribable by any component.
|
|
716
|
+
*/
|
|
717
|
+
export type Subscribable<AC> = Dispatchable<AC> | typeof Lifecycle.Fault;
|
|
718
|
+
/**
|
|
719
|
+
* Subset of a union of actions whose payload type is `never`. Used to split
|
|
720
|
+
* `dispatch`/`useAction` into a no-payload and a with-payload overload so
|
|
721
|
+
* TypeScript reports a clear "no overload matches" error instead of widening
|
|
722
|
+
* the inferred action type when constraints don't match.
|
|
723
|
+
*/
|
|
724
|
+
export type NoPayloadActions<U> = Extract<U, {
|
|
725
|
+
readonly [Brand.Payload]: never;
|
|
726
|
+
}>;
|
|
727
|
+
/** Subset of a union of actions whose payload type is non-`never`. */
|
|
728
|
+
export type WithPayloadActions<U> = Exclude<U, {
|
|
729
|
+
readonly [Brand.Payload]: never;
|
|
730
|
+
}>;
|
|
589
731
|
/**
|
|
590
732
|
* Recursive mapped type for action handlers that mirrors the action class hierarchy.
|
|
591
733
|
*
|
|
@@ -633,10 +775,8 @@ export type UseActions<M extends Model | void, AC extends Actions | void, D exte
|
|
|
633
775
|
* their scope from the action declaration, so no extra options are
|
|
634
776
|
* required at the call site.
|
|
635
777
|
*/
|
|
636
|
-
dispatch
|
|
637
|
-
dispatch<
|
|
638
|
-
dispatch<P>(action: MulticastPayload<P>, payload?: P): Promise<void>;
|
|
639
|
-
dispatch<P, C extends Filter>(action: ChanneledAction<P, C>, payload?: P): Promise<void>;
|
|
778
|
+
dispatch(action: NoPayloadActions<Dispatchable<AC>>): Promise<void>;
|
|
779
|
+
dispatch<A extends WithPayloadActions<Dispatchable<AC>>>(action: A, payload: Payload<A>): Promise<void>;
|
|
640
780
|
inspect: Inspect<M>;
|
|
641
781
|
/**
|
|
642
782
|
* Streams broadcast values declaratively in JSX using a render-prop pattern.
|
|
@@ -689,5 +829,6 @@ export type UseActions<M extends Model | void, AC extends Actions | void, D exte
|
|
|
689
829
|
* });
|
|
690
830
|
* ```
|
|
691
831
|
*/
|
|
692
|
-
useAction
|
|
832
|
+
useAction(action: NoPayloadActions<Subscribable<AC>>, handler: (context: HandlerContext<M, AC, D>) => void | Promise<void> | AsyncGenerator | Generator): void;
|
|
833
|
+
useAction<A extends WithPayloadActions<Subscribable<AC>>>(action: A, handler: (context: HandlerContext<M, AC, D>, payload: Payload<A>) => void | Promise<void> | AsyncGenerator | Generator): void;
|
|
693
834
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Pk } from '../types/index
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
export type { Adapter, Encoded, Store, Stored, Unset } from './types.ts';
|
|
1
|
+
import { Pk } from '../types/index';
|
|
2
|
+
export { unset } from './utils';
|
|
3
|
+
export type { Stored, Unset } from './types';
|
|
5
4
|
/**
|
|
6
5
|
* Returns a promise that resolves after the specified number of
|
|
7
6
|
* milliseconds, or rejects with an {@link AbortError} when the signal is
|
|
@@ -45,48 +44,9 @@ export declare function pk(): symbol;
|
|
|
45
44
|
* @returns `true` if `id` is a non-symbol value, `false` otherwise.
|
|
46
45
|
*/
|
|
47
46
|
export declare function pk<T>(id: Pk<T>): boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Wraps a synchronous {@link Adapter} into a {@link Store} that traffics
|
|
50
|
-
* in {@link Stored} values. Storage entries serialise as
|
|
51
|
-
* {@link Encoded}`<T>` so the `Temporal.Instant` timestamp survives the
|
|
52
|
-
* string round-trip and `BoundResourceHandle.if({ over })` can
|
|
53
|
-
* short-circuit on the persisted timestamp after a reload.
|
|
54
|
-
*
|
|
55
|
-
* @param adapter Backend implementation providing raw string `get`/`set`/
|
|
56
|
-
* `remove`/`clear`. The Store layers JSON encoding and
|
|
57
|
-
* timestamp serialisation on top.
|
|
58
|
-
* @returns A {@link Store} bound to `adapter`. Reads return {@link Stored}
|
|
59
|
-
* envelopes; writes accept Stored envelopes and return `true`
|
|
60
|
-
* when the entry landed in the adapter.
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* const store = utils.store({
|
|
65
|
-
* get: (key) => localStorage.getItem(key),
|
|
66
|
-
* set: (key, value) => localStorage.setItem(key, value),
|
|
67
|
-
* remove: (key) => localStorage.removeItem(key),
|
|
68
|
-
* clear: () => localStorage.clear(),
|
|
69
|
-
* });
|
|
70
|
-
*
|
|
71
|
-
* // Read into a Resource fallback chain.
|
|
72
|
-
* { cat: get.cat.else(store.get(Snapshots.Cat)).else(null) }
|
|
73
|
-
*
|
|
74
|
-
* // Write the latest cached value back to storage.
|
|
75
|
-
* store.set(Snapshots.Cat, get.cat.snapshot());
|
|
76
|
-
*
|
|
77
|
-
* // Drop a snapshot on sign-out, cache invalidation, etc.
|
|
78
|
-
* store.remove(Snapshots.Cat);
|
|
79
|
-
*
|
|
80
|
-
* // Or wipe the whole backing store (scope is the adapter's call).
|
|
81
|
-
* store.clear();
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
export declare function store(adapter: Adapter): Store;
|
|
85
47
|
/** Shorthand alias for {@link sleep}. */
|
|
86
48
|
export declare const ζ: typeof sleep;
|
|
87
49
|
/** Shorthand alias for {@link poll}. */
|
|
88
50
|
export declare const π: typeof poll;
|
|
89
51
|
/** Shorthand alias for {@link pk}. */
|
|
90
52
|
export declare const κ: typeof pk;
|
|
91
|
-
/** Shorthand alias for {@link store}. */
|
|
92
|
-
export declare const σ: typeof store;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { unset } from './utils';
|
|
2
|
+
/** Nominal type of the {@link unset} sentinel. */
|
|
3
|
+
export type Unset = typeof unset;
|
|
4
|
+
/**
|
|
5
|
+
* Common shape for a possibly-present value with a timestamp. Produced by
|
|
6
|
+
* `Cache.get(key)` (from persistent storage or in-memory) and consumed
|
|
7
|
+
* internally by Resource's per-params cache slots.
|
|
8
|
+
*
|
|
9
|
+
* @template T The payload type when present.
|
|
10
|
+
*/
|
|
11
|
+
export type Stored<T> = {
|
|
12
|
+
/** The payload, or {@link unset} when nothing is recorded. */
|
|
13
|
+
readonly data: T | Unset;
|
|
14
|
+
/** When the payload was recorded, or `null` when nothing is recorded. */
|
|
15
|
+
readonly at: Temporal.Instant | null;
|
|
16
|
+
/** Returns {@link data} when present, otherwise the supplied fallback. */
|
|
17
|
+
readonly else: <U>(fallback: U) => T | U;
|
|
18
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "march-hare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"packageManager": "yarn@1.22.22",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"react-flip-numbers": "^3.0.9",
|
|
89
89
|
"react-router-dom": "^7.13.0",
|
|
90
90
|
"react-test-renderer": "^19.2.4",
|
|
91
|
-
"react-wayfinder": "^0.1.
|
|
91
|
+
"react-wayfinder": "^0.1.4",
|
|
92
92
|
"rollup-plugin-visualizer": "^6.0.5",
|
|
93
93
|
"terser": "^5.46.0",
|
|
94
94
|
"traverse": "^0.6.11",
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Props } from './types.ts';
|
|
2
|
-
import * as React from "react";
|
|
3
|
-
export { useMode } from './utils.ts';
|
|
4
|
-
export type { ModeHandle } from './utils.ts';
|
|
5
|
-
/**
|
|
6
|
-
* Provides a single mutable mode handle to every component inside the
|
|
7
|
-
* boundary. Components opt in by calling {@link useMode} and threading the
|
|
8
|
-
* returned handle through {@link useActions}'s `data` callback.
|
|
9
|
-
*
|
|
10
|
-
* Mode is **not** reactive — mutating it does not trigger a re-render.
|
|
11
|
-
* Use it for cross-handler coordination (e.g. flagging an in-progress
|
|
12
|
-
* sign-out) when you do not want the value showing up as render-time UI
|
|
13
|
-
* state.
|
|
14
|
-
*/
|
|
15
|
-
export declare function Mode({ children }: Props): React.ReactNode;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
/**
|
|
3
|
-
* React context exposing the per-Boundary mode handle. The handle itself
|
|
4
|
-
* is stable across renders — readers grab `.current` at call time.
|
|
5
|
-
*
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
export declare const Context: React.Context<React.RefObject<unknown>>;
|
|
9
|
-
/**
|
|
10
|
-
* Handle returned by {@link useMode}. Reads always reflect the latest
|
|
11
|
-
* write, even after `await` boundaries.
|
|
12
|
-
*/
|
|
13
|
-
export type ModeHandle<T> = {
|
|
14
|
-
read(): T | null;
|
|
15
|
-
update(value: T | null): void;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Hook that returns a `{ read, update }` handle to the per-Boundary mode
|
|
19
|
-
* value.
|
|
20
|
-
*
|
|
21
|
-
* Mode is a single mutable value shared across every component inside the
|
|
22
|
-
* surrounding `<Boundary>`. It is **not** reactive — mutating it does
|
|
23
|
-
* not trigger a re-render. Use it for coordinating between async action
|
|
24
|
-
* handlers (e.g. short-circuiting refreshes during sign-out).
|
|
25
|
-
*
|
|
26
|
-
* Pass the handle through the {@link useActions} `data` callback so it
|
|
27
|
-
* shows up under `context.data` inside handlers, fully typed.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* enum Mode {
|
|
32
|
-
* Idle,
|
|
33
|
-
* SigningOut,
|
|
34
|
-
* }
|
|
35
|
-
*
|
|
36
|
-
* function useSignOutActions() {
|
|
37
|
-
* const mode = useMode<Mode>();
|
|
38
|
-
* const actions = useActions<Model, typeof Actions>(model, () => ({ mode }));
|
|
39
|
-
*
|
|
40
|
-
* actions.useAction(Actions.SignOut, async (context) => {
|
|
41
|
-
* context.data.mode.update(Mode.SigningOut);
|
|
42
|
-
* await api.signOut();
|
|
43
|
-
* context.data.mode.update(Mode.Idle);
|
|
44
|
-
* });
|
|
45
|
-
*
|
|
46
|
-
* actions.useAction(Actions.Refresh, async (context) => {
|
|
47
|
-
* if (context.data.mode.read() === Mode.SigningOut) return;
|
|
48
|
-
* // ...
|
|
49
|
-
* });
|
|
50
|
-
*
|
|
51
|
-
* return actions;
|
|
52
|
-
* }
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export declare function useMode<T>(): ModeHandle<T>;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Props } from './types.ts';
|
|
2
|
-
import * as React from "react";
|
|
3
|
-
/**
|
|
4
|
-
* Creates a unified context boundary for all March Hare features.
|
|
5
|
-
* Wraps children with Broadcaster, Mode, and Tasks providers.
|
|
6
|
-
*
|
|
7
|
-
* Use this at the root of your application or to create isolated context boundaries
|
|
8
|
-
* for libraries that need their own March Hare context.
|
|
9
|
-
*
|
|
10
|
-
* @param props.children - The children to render within the boundary.
|
|
11
|
-
* @returns The children wrapped in all required context providers.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```tsx
|
|
15
|
-
* <Boundary>
|
|
16
|
-
* <App />
|
|
17
|
-
* </Boundary>
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export declare function Boundary({ children }: Props): React.ReactNode;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export { Action } from './action/index.ts';
|
|
2
|
-
export { Distribution, Lifecycle } from './types/index.ts';
|
|
3
|
-
export { Reason, AbortError, TimeoutError } from './error/index.ts';
|
|
4
|
-
export { Operation, Op, State } from 'immertation';
|
|
5
|
-
export { annotate } from './annotate/index.ts';
|
|
6
|
-
export { Boundary } from './boundary/index.tsx';
|
|
7
|
-
export { withScope } from './boundary/components/scope/index.tsx';
|
|
8
|
-
export { useMode } from './boundary/components/mode/index.tsx';
|
|
9
|
-
export type { ModeHandle } from './boundary/components/mode/index.tsx';
|
|
10
|
-
export { useActions, With } from './hooks/index.ts';
|
|
11
|
-
export { Resource, useResource } from './resource/index.ts';
|
|
12
|
-
export type { ResourceHandle, ResourceFetcher, BoundResourceHandle, IfOptions, } from './resource/index.ts';
|
|
13
|
-
export * as utils from './utils/index.ts';
|
|
14
|
-
export type { Stored, Unset, Adapter, Store } from './utils/index.ts';
|
|
15
|
-
export type { Box } from 'immertation';
|
|
16
|
-
export type { Fault } from './error/index.ts';
|
|
17
|
-
export type { Pk, Task, Tasks, Handlers } from './types/index.ts';
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { ResourceFetcher, ResourceHandle, BoundResourceHandle } from './types.ts';
|
|
2
|
-
export type { IfOptions, ResourceFetcher, ResourceHandle, BoundResourceHandle, } from './types.ts';
|
|
3
|
-
/**
|
|
4
|
-
* Defines a remote resource — declared at module scope and
|
|
5
|
-
* consumed via {@link useResource}.
|
|
6
|
-
*
|
|
7
|
-
* The fetcher receives the optional `AbortSignal` first and the
|
|
8
|
-
* `params` object second (defaults to `{}`). Resources do **not**
|
|
9
|
-
* carry any callbacks – side-effects (broadcasting, logging,
|
|
10
|
-
* model updates) belong in the `useAction` handler that called
|
|
11
|
-
* `await handle(...)`.
|
|
12
|
-
*
|
|
13
|
-
* Every call fires its own request. The most recent successful
|
|
14
|
-
* payload is cached in a module-level `WeakMap` keyed by the fetcher,
|
|
15
|
-
* so `.if(...)` and `.else(...)` on the bound handle behave
|
|
16
|
-
* consistently across all components that share the same Resource.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* import { Resource } from "march-hare";
|
|
21
|
-
*
|
|
22
|
-
* // `T` is inferred from the fetcher's return type.
|
|
23
|
-
* export const user = Resource((signal) =>
|
|
24
|
-
* ky.get("user", { signal }).json<User>(),
|
|
25
|
-
* );
|
|
26
|
-
*
|
|
27
|
-
* // Annotate `params` when destructuring so `P` is inferred.
|
|
28
|
-
* export const updateUser = Resource(
|
|
29
|
-
* (signal, { id, body }: { id: number; body: { name: string } }) =>
|
|
30
|
-
* ky.patch(`users/${id}`, { json: body, signal }).json<User>(),
|
|
31
|
-
* );
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export declare function Resource<T, P extends object = Record<never, never>>(fetcher: ResourceFetcher<T, P>): ResourceHandle<T, P>;
|
|
35
|
-
/**
|
|
36
|
-
* Binds a module-scope {@link ResourceHandle} to the component, returning
|
|
37
|
-
* the fetch callable with `.if`, `.else`, `.snapshot`, `.data`, and `.at`
|
|
38
|
-
* attached. The hook is standalone – call it *before* `useActions`
|
|
39
|
-
* when you want to seed the initial model from the cache via
|
|
40
|
-
* `.else(fallback)`.
|
|
41
|
-
*
|
|
42
|
-
* Pass `context.task.controller.signal` as the first argument to thread
|
|
43
|
-
* cancellation from the surrounding action handler through to the
|
|
44
|
-
* fetcher. For parameterised resources, pass `null` as the first arg
|
|
45
|
-
* when you have params but no signal.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```ts
|
|
49
|
-
* const cat = useResource(resources.cat);
|
|
50
|
-
* const actions = useActions<Model, typeof Actions, Data>(
|
|
51
|
-
* { cat: cat.else(store.get(Snapshots.Cat)).else(null) },
|
|
52
|
-
* () => ({ index, router }),
|
|
53
|
-
* );
|
|
54
|
-
*
|
|
55
|
-
* actions.useAction(Actions.Mount, async (context) => {
|
|
56
|
-
* const fresh = await cat.if(
|
|
57
|
-
* { over: { minutes: 5 } },
|
|
58
|
-
* context.task.controller.signal,
|
|
59
|
-
* );
|
|
60
|
-
* store.set(Snapshots.Cat, cat.snapshot());
|
|
61
|
-
* context.actions.produce(({ model }) => void (model.cat = fresh));
|
|
62
|
-
* });
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
export declare function useResource<T, P extends object>(resource: ResourceHandle<T, P>): BoundResourceHandle<T, P>;
|