preact-sigma 4.0.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -21
- package/dist/index.d.mts +41 -325
- package/dist/index.mjs +52 -712
- package/dist/persist.d.mts +126 -0
- package/dist/persist.mjs +240 -0
- package/dist/sigma-CJibGQ6g.mjs +383 -0
- package/dist/sigma-DD7HfTvw.d.mts +162 -0
- package/docs/context.md +81 -55
- package/docs/migrations/v5-to-v6.md +273 -0
- package/docs/persist.md +67 -0
- package/examples/async-commit.ts +38 -31
- package/examples/basic-counter.ts +21 -16
- package/examples/command-palette.tsx +114 -104
- package/examples/observe-and-restore.ts +25 -17
- package/examples/persist-search-draft.ts +70 -0
- package/examples/setup-act.ts +17 -9
- package/examples/sigma-target.ts +17 -8
- package/package.json +5 -1
- package/examples/signal-access.ts +0 -28
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Purpose
|
|
4
4
|
|
|
5
|
-
`preact-sigma` is a typed state-model
|
|
5
|
+
`preact-sigma` is a typed state-model layer for Preact and TypeScript. It keeps top-level public state reactive, derived reads local to the model, writes explicit through class actions, and side effects owned by explicit setup.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -13,22 +13,27 @@ npm install preact-sigma @preact/signals immer preact
|
|
|
13
13
|
## Quick Example
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
16
|
+
import { Sigma } from "preact-sigma";
|
|
17
|
+
|
|
18
|
+
type CounterState = { count: number };
|
|
19
|
+
|
|
20
|
+
class Counter extends Sigma<CounterState> {
|
|
21
|
+
constructor() {
|
|
22
|
+
super({
|
|
23
|
+
count: 0,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get doubled() {
|
|
28
|
+
return this.count * 2;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
increment() {
|
|
32
|
+
this.count += 1;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Counter extends CounterState {}
|
|
32
37
|
|
|
33
38
|
const counter = new Counter();
|
|
34
39
|
|
|
@@ -41,7 +46,7 @@ console.log(counter.doubled); // 2
|
|
|
41
46
|
## Documentation Map
|
|
42
47
|
|
|
43
48
|
- Concepts, lifecycle, invariants, and API selection live in [`docs/context.md`](./docs/context.md).
|
|
44
|
-
-
|
|
45
|
-
-
|
|
46
|
-
-
|
|
47
|
-
- Exact exported signatures live in `dist/index.d.mts` after `pnpm build`.
|
|
49
|
+
- Persistence-specific guidance lives in [`docs/persist.md`](./docs/persist.md).
|
|
50
|
+
- Migration guidance from v5 lives in [`docs/migrations/v5-to-v6.md`](./docs/migrations/v5-to-v6.md).
|
|
51
|
+
- Runnable usage patterns live in [`examples/`](./examples/), starting with [`examples/basic-counter.ts`](./examples/basic-counter.ts) and [`examples/command-palette.tsx`](./examples/command-palette.tsx).
|
|
52
|
+
- Exact exported signatures and public API comments live in `dist/index.d.mts` and `dist/persist.d.mts` after `pnpm build`.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,345 +1,61 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Patch } from "immer";
|
|
1
|
+
import { a as SigmaState, c as query, d as Draft, f as Immutable, i as SigmaRef, l as setAutoFreeze, m as Cleanup, n as Sigma, o as SigmaTarget, p as typeSymbol, r as SigmaDefinition, s as castProtected, t as Protected, u as sigma } from "./sigma-DD7HfTvw.mjs";
|
|
3
2
|
|
|
4
|
-
//#region src/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
declare
|
|
3
|
+
//#region src/defaults.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Shallowly applies defined initial values over a defaults object.
|
|
6
|
+
*
|
|
7
|
+
* `undefined` initial values leave their matching default value in place.
|
|
8
|
+
*/
|
|
9
|
+
declare function mergeDefaults<T extends object>(initial: Partial<T> | undefined, defaults: T): T;
|
|
11
10
|
//#endregion
|
|
12
11
|
//#region src/listener.d.ts
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
12
|
+
/** Sigma targets and protected sigma target views that carry typed event metadata. */
|
|
13
|
+
type SigmaListenable<TEvents extends object = any> = {
|
|
14
|
+
readonly [typeSymbol]: {
|
|
15
|
+
readonly events: TEvents;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
/** Target types supported by `listen(...)` and `useListener(...)`. */
|
|
19
|
+
type Listenable = SigmaListenable | EventTarget;
|
|
20
|
+
type InferEventMap<TTarget extends Listenable> = TTarget extends SigmaListenable<infer TEvents> ? TEvents : TTarget extends Window ? WindowEventMap : TTarget extends Document ? DocumentEventMap : TTarget extends HTMLBodyElement ? HTMLBodyElementEventMap : TTarget extends HTMLMediaElement ? HTMLMediaElementEventMap : TTarget extends HTMLElement ? HTMLElementEventMap : TTarget extends SVGSVGElement ? SVGSVGElementEventMap : TTarget extends SVGElement ? SVGElementEventMap : TTarget extends EventTarget ? Record<string, Event> : never;
|
|
21
|
+
type InferListenerArgs<TEvents extends object, TTarget extends Listenable, TEvent extends string> = [(TEvent extends keyof TEvents ? TEvents[TEvent] : never) extends infer TPayload ? TTarget extends SigmaListenable<any> ? [TPayload] extends [never] ? never : [TPayload] extends [void] ? undefined : TPayload : ([TPayload] extends [never] ? CustomEvent : Extract<TPayload, Event>) & {
|
|
19
22
|
readonly currentTarget: TTarget;
|
|
20
23
|
} : never];
|
|
21
|
-
/** Infers the listener callback shape for a target and event name. Sigma
|
|
22
|
-
type InferListener<TTarget extends
|
|
24
|
+
/** Infers the listener callback shape for a target and event name. Sigma targets receive payloads directly, while DOM targets receive typed events. */
|
|
25
|
+
type InferListener<TTarget extends Listenable, TEvent extends string = string> = InferEventMap<TTarget> extends infer TEvents extends object ? ((...args: InferListenerArgs<TEvents, TTarget, TEvent>) => void) & {
|
|
23
26
|
__eventType?: TEvent;
|
|
24
27
|
} : never;
|
|
25
28
|
/** Infers the event names accepted by `listen(...)` or `useListener(...)` for a target. */
|
|
26
|
-
type InferEventType<TTarget extends
|
|
29
|
+
type InferEventType<TTarget extends Listenable> = (InferListener<TTarget> extends {
|
|
27
30
|
__eventType?: infer TEvent;
|
|
28
31
|
} ? string & TEvent : never) | (string & {});
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* A standalone typed event hub with `emit(...)` and `on(...)` methods and full
|
|
33
|
-
* `EventTarget`, `listen(...)`, and `useListener(...)` compatibility.
|
|
34
|
-
*/
|
|
35
|
-
declare class SigmaTarget<TEvents extends AnyEvents = {}> extends EventTarget {
|
|
36
|
-
readonly [sigmaEventsBrand]: TEvents;
|
|
37
|
-
/**
|
|
38
|
-
* Emits a typed event from the hub.
|
|
39
|
-
*
|
|
40
|
-
* Void events dispatch a plain `Event`. Payload events dispatch a
|
|
41
|
-
* `CustomEvent` whose `detail` holds the payload.
|
|
42
|
-
*/
|
|
43
|
-
emit<TEvent extends string & keyof TEvents>(name: TEvent, ...[detail]: EventParameters<TEvents[TEvent]>): void;
|
|
44
|
-
/**
|
|
45
|
-
* Registers a typed event listener and returns an unsubscribe function.
|
|
46
|
-
*
|
|
47
|
-
* Payload events pass their payload directly to the listener. Void events
|
|
48
|
-
* call the listener with no arguments.
|
|
49
|
-
*/
|
|
50
|
-
on<TEvent extends string & keyof TEvents>(name: TEvent, listener: (...args: InferListenerArgs<TEvents, this, TEvent>) => void): () => void;
|
|
51
|
-
}
|
|
52
|
-
/** Adds a listener to a sigma state or DOM target and returns a cleanup function that removes it. */
|
|
53
|
-
declare function listen<TTarget extends EventTarget, TEvent extends InferEventType<TTarget>>(target: TTarget, name: TEvent, listener: InferListener<TTarget, TEvent>): () => void;
|
|
32
|
+
/** Adds a listener to a sigma target or DOM target and returns a cleanup function that removes it. */
|
|
33
|
+
declare function listen<TTarget extends Listenable, TEvent extends InferEventType<TTarget>>(target: TTarget, name: TEvent, listener: InferListener<TTarget, TEvent>): Cleanup;
|
|
54
34
|
//#endregion
|
|
55
|
-
//#region src/
|
|
56
|
-
type PrimitiveType = number | string | boolean;
|
|
57
|
-
/** Object types that should never be mapped */
|
|
58
|
-
type AtomicObject = Function | Promise<any> | Date | RegExp | EventTarget | AnySigmaState;
|
|
59
|
-
/**
|
|
60
|
-
* If the lib "ES2015.Collection" is not included in tsconfig.json,
|
|
61
|
-
* types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)
|
|
62
|
-
* or `{}` (from the node types), in both cases entering an infinite recursion in
|
|
63
|
-
* pattern matching type mappings
|
|
64
|
-
* This type can be used to cast these types to `void` in these cases.
|
|
65
|
-
*/
|
|
66
|
-
type IfAvailable<T, Fallback = void> = true | false extends (T extends never ? true : false) ? Fallback : keyof T extends never ? Fallback : T;
|
|
67
|
-
/**
|
|
68
|
-
* These should also never be mapped but must be tested after regular Map and
|
|
69
|
-
* Set
|
|
70
|
-
*/
|
|
71
|
-
type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>;
|
|
72
|
-
type HasSigmaRefBrand<T> = [T] extends [object] ? typeof sigmaRefBrand extends keyof T ? true : false : false;
|
|
73
|
-
type WritableDraft<T> = T extends any[] ? number extends T["length"] ? Draft<T[number]>[] : WritableNonArrayDraft<T> : WritableNonArrayDraft<T>;
|
|
74
|
-
type WritableNonArrayDraft<T> = { -readonly [K in keyof T]: T[K] extends infer V ? (V extends object ? Draft<V> : V) : never };
|
|
75
|
-
/**
|
|
76
|
-
* Convert a readonly type into a mutable type, if possible.
|
|
77
|
-
*
|
|
78
|
-
* Use this instead of `immer.Draft`
|
|
79
|
-
*/
|
|
80
|
-
type Draft<T> = T extends PrimitiveType ? T : T extends AtomicObject ? T : HasSigmaRefBrand<T> extends true ? T : T extends ReadonlyMap<infer K, infer V> ? Map<Draft<K>, Draft<V>> : T extends ReadonlySet<infer V> ? Set<Draft<V>> : T extends WeakReferences ? T : T extends object ? WritableDraft<T> : T;
|
|
35
|
+
//#region src/hooks/useListener.d.ts
|
|
81
36
|
/**
|
|
82
|
-
*
|
|
37
|
+
* Attaches an event listener in a component and cleans it up automatically.
|
|
83
38
|
*
|
|
84
|
-
*
|
|
39
|
+
* Passing `null` disables the listener. The latest callback is used without
|
|
40
|
+
* forcing the effect to resubscribe on every render.
|
|
85
41
|
*/
|
|
86
|
-
|
|
42
|
+
declare function useListener<TTarget extends Listenable, TEvent extends InferEventType<TTarget>>(target: TTarget | null, name: TEvent, listener: InferListener<TTarget, TEvent>): void;
|
|
87
43
|
//#endregion
|
|
88
|
-
//#region src/
|
|
89
|
-
|
|
90
|
-
type
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
type DefaultStateValue<TValue> = TValue | DefaultStateInitializer<TValue>;
|
|
94
|
-
type Disposable = {
|
|
95
|
-
[Symbol.dispose](): void;
|
|
96
|
-
};
|
|
97
|
-
type DisposableLike = {
|
|
98
|
-
dispose(): void;
|
|
99
|
-
};
|
|
100
|
-
interface SigmaRefBrand {
|
|
101
|
-
[sigmaRefBrand]?: true;
|
|
102
|
-
}
|
|
103
|
-
/** A type brand that keeps a value by reference in sigma's `Draft` and `Immutable` helpers. */
|
|
104
|
-
type SigmaRef<T = unknown> = T & SigmaRefBrand;
|
|
105
|
-
/** The event map shape used by sigma types. */
|
|
106
|
-
type AnyEvents = Record<string, object | void>;
|
|
107
|
-
/** The top-level state object shape used by sigma types. */
|
|
108
|
-
type AnyState = Record<string, unknown>;
|
|
109
|
-
/** The object accepted by `.defaultState(...)`, where each property may be a value or a zero-argument initializer. */
|
|
110
|
-
type AnyDefaultState<TState extends object> = { [K in keyof TState]?: DefaultStateValue<TState[K]> };
|
|
111
|
-
/** A cleanup resource supported by `.setup(...)`, including function, `dispose()`, and `Symbol.dispose` cleanup. */
|
|
112
|
-
type AnyResource = Cleanup | Disposable | DisposableLike | AbortController;
|
|
113
|
-
type ComputedValues<TComputeds> = { readonly [K in keyof TComputeds]: TComputeds[K] extends AnyFunction ? Immutable<ReturnType<TComputeds[K]>> : never };
|
|
114
|
-
type QueryMethods<TQueries> = { [K in keyof TQueries]: TQueries[K] extends AnyFunction ? (...args: Parameters<TQueries[K]>) => ReturnType<TQueries[K]> : never };
|
|
115
|
-
type ActionMethods<TActions> = { [K in keyof TActions]: TActions[K] extends AnyFunction ? (...args: Parameters<TActions[K]>) => ReturnType<TActions[K]> : never };
|
|
116
|
-
type MergeObjects<TLeft, TRight, TConstraint extends object = object> = TRight extends TConstraint ? TLeft extends TConstraint ? Simplify<Omit<TLeft, keyof TRight> & TRight> : TRight : TLeft extends TConstraint ? TLeft : {};
|
|
117
|
-
type Simplify<T> = {} & { [K in keyof T]: T[K] };
|
|
118
|
-
type ComputedContext<T extends AnySigmaType, TOverrides extends Partial<SigmaDefinition> = {}> = Simplify<Immutable<MergeObjects<T[Def]["state"], TOverrides["state"]>> & ComputedValues<MergeObjects<T[Def]["computeds"], TOverrides["computeds"]>>>;
|
|
119
|
-
type ReadonlyContext<T extends AnySigmaType, TOverrides extends Partial<SigmaDefinition> = {}> = Simplify<Immutable<MergeObjects<T[Def]["state"], TOverrides["state"]>> & ComputedValues<MergeObjects<T[Def]["computeds"], TOverrides["computeds"]>> & QueryMethods<MergeObjects<T[Def]["queries"], TOverrides["queries"]>>>;
|
|
120
|
-
type Emit<T extends AnySigmaType, TOverrides extends Partial<SigmaDefinition> = {}> = MergeObjects<T[Def]["events"], TOverrides["events"], AnyEvents> extends infer TEvents ? [TEvents] extends [AnyEvents] ? <TEvent extends string & keyof TEvents>(name: TEvent, ...[detail]: EventParameters<TEvents[TEvent]>) => void : never : never;
|
|
121
|
-
type ActionContext<T extends AnySigmaType, TOverrides extends Partial<SigmaDefinition> = {}> = Simplify<Draft<MergeObjects<T[Def]["state"], TOverrides["state"]>> & ComputedValues<MergeObjects<T[Def]["computeds"], TOverrides["computeds"]>> & QueryMethods<MergeObjects<T[Def]["queries"], TOverrides["queries"]>> & ActionMethods<MergeObjects<T[Def]["actions"], TOverrides["actions"]>> & {
|
|
122
|
-
/** Publishes the current action draft immediately so later boundaries use committed state. */commit(): void; /** Emits a typed event from the current action. */
|
|
123
|
-
emit: Emit<T, TOverrides>;
|
|
124
|
-
}>;
|
|
125
|
-
type SetupContext<T extends AnySigmaType, TOverrides extends Partial<SigmaDefinition> = {}> = Simplify<SigmaState<T[Def] & TOverrides> & {
|
|
126
|
-
/** Runs a synchronous anonymous action from setup so reads and writes use normal action semantics. */act<TResult>(fn: (this: ActionContext<T>) => TResult): TResult; /** Emits a typed event from setup. */
|
|
127
|
-
emit: Emit<T, TOverrides>;
|
|
128
|
-
}>;
|
|
129
|
-
type AnySigmaType = SigmaType<any, any, any> & {
|
|
130
|
-
readonly [sigmaTypeBrand]: {
|
|
131
|
-
state: object;
|
|
132
|
-
events?: object;
|
|
133
|
-
computeds?: object;
|
|
134
|
-
queries?: object;
|
|
135
|
-
actions?: object;
|
|
136
|
-
setupArgs?: any[];
|
|
137
|
-
};
|
|
138
|
-
};
|
|
139
|
-
/** The public shape shared by all sigma-state instances. */
|
|
140
|
-
interface AnySigmaState extends EventTarget {
|
|
141
|
-
readonly [sigmaStateBrand]: true;
|
|
142
|
-
}
|
|
143
|
-
/** A sigma-state instance with a typed event map. */
|
|
144
|
-
type AnySigmaStateWithEvents<TEvents extends AnyEvents> = AnySigmaState & {
|
|
145
|
-
readonly [sigmaEventsBrand]: TEvents;
|
|
146
|
-
};
|
|
147
|
-
/** Options accepted by `.observe(...)`. */
|
|
148
|
-
type SigmaObserveOptions = {
|
|
149
|
-
/** Includes Immer patches and inverse patches on the delivered change object. */patches?: boolean;
|
|
150
|
-
};
|
|
151
|
-
/** The change object delivered to `.observe(...)` listeners. */
|
|
152
|
-
type SigmaObserveChange<TState extends AnyState, TWithPatches extends boolean = false> = {
|
|
153
|
-
readonly newState: Immutable<TState>;
|
|
154
|
-
readonly oldState: Immutable<TState>;
|
|
155
|
-
} & (TWithPatches extends true ? {
|
|
156
|
-
readonly inversePatches: readonly Patch[];
|
|
157
|
-
readonly patches: readonly Patch[];
|
|
158
|
-
} : {});
|
|
159
|
-
type SigmaDefinition = {
|
|
160
|
-
state: AnyState;
|
|
161
|
-
events?: AnyEvents;
|
|
162
|
-
computeds?: object;
|
|
163
|
-
queries?: object;
|
|
164
|
-
actions?: object;
|
|
165
|
-
setupArgs?: any[];
|
|
166
|
-
};
|
|
167
|
-
interface SignalAccessors<T extends object> {
|
|
168
|
-
/** Returns the underlying signal for a top-level state property or computed. */
|
|
169
|
-
get<K extends keyof T>(key: K): ReadonlySignal<T[K]>;
|
|
170
|
-
}
|
|
171
|
-
type EventMethods<TEvents extends AnyEvents | undefined> = [undefined] extends [TEvents] ? never : {
|
|
172
|
-
readonly [sigmaEventsBrand]: TEvents; /** Registers a typed event listener and returns an unsubscribe function. */
|
|
173
|
-
on<TEvent extends string & keyof TEvents>(name: TEvent, listener: (...[detail]: EventParameters<TEvents[TEvent]>) => void): Cleanup;
|
|
44
|
+
//#region src/hooks/useSigma.d.ts
|
|
45
|
+
/** Setup arguments and recreation dependencies for `useSigma(...)`. */
|
|
46
|
+
type UseSigmaOptions<TSetup extends readonly any[] = any[]> = {
|
|
47
|
+
/** Arguments passed to the sigma instance's `onSetup(...)` method. */setup: TSetup | (() => TSetup); /** Dependencies that recreate the sigma instance when they change. */
|
|
48
|
+
deps?: readonly any[];
|
|
174
49
|
};
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
180
|
-
/** The public instance shape produced by a configured sigma type, including signal access inferred from the definition. */
|
|
181
|
-
type SigmaState<T extends SigmaDefinition = SigmaDefinition> = AnySigmaState & Simplify<UnionToIntersection<MapSigmaDefinition<T>>>;
|
|
182
|
-
type RequiredKeys<TObject extends object> = { [K in keyof TObject]-?: {} extends Pick<TObject, K> ? never : K }[keyof TObject];
|
|
183
|
-
type MissingInitialKeys<TState extends AnyState, TDefaults extends object> = Exclude<RequiredKeys<TState>, keyof TDefaults>;
|
|
184
|
-
type InitialStateInput<TState extends AnyState, TDefaults extends object> = [MissingInitialKeys<TState, TDefaults>] extends [never] ? [initialState?: Partial<TState>] : [initialState: Pick<TState, MissingInitialKeys<TState, TDefaults>> & Partial<TState>];
|
|
185
|
-
type ExtendSigmaType<T extends SigmaType<any, any, any>, TExtension extends Partial<SigmaDefinition>> = T & {
|
|
186
|
-
readonly [sigmaTypeBrand]: T[Def] & TExtension;
|
|
187
|
-
};
|
|
188
|
-
type OmitEmpty<T extends object> = Omit<T, { [K in keyof T]: [undefined] extends [T[K]] ? K : [{}] extends [T[K]] ? K : never }[keyof T]>;
|
|
189
|
-
type InferSigmaDefinition<T extends SigmaType<any, any, any>> = Extract<Simplify<OmitEmpty<T[Def]>>, SigmaDefinition>;
|
|
190
|
-
/** Infers the `setup(...)` argument list for a sigma-state instance. */
|
|
191
|
-
type InferSetupArgs<T extends AnySigmaState> = T extends {
|
|
192
|
-
setup(...args: infer TArgs extends any[]): Cleanup;
|
|
193
|
-
} ? TArgs : never;
|
|
194
|
-
//#endregion
|
|
195
|
-
//#region src/internal/runtime.d.ts
|
|
196
|
-
/** Controls whether sigma deep-freezes published public state. Auto-freezing starts enabled and the setting is shared across instances. */
|
|
197
|
-
declare function setAutoFreeze(autoFreeze: boolean): void;
|
|
198
|
-
/**
|
|
199
|
-
* Returns a shallow snapshot of an instance's committed public state.
|
|
200
|
-
*
|
|
201
|
-
* The snapshot includes one own property for each top-level state key and reads
|
|
202
|
-
* the current committed value for that key. Nested sigma states remain as
|
|
203
|
-
* referenced values. Its type is inferred from the instance's sigma-state
|
|
204
|
-
* definition.
|
|
205
|
-
*/
|
|
206
|
-
declare function snapshot<T extends AnySigmaState>(publicInstance: T): T extends SigmaState<infer TDefinition> ? Immutable<TDefinition["state"]> : never;
|
|
207
|
-
/**
|
|
208
|
-
* Replaces an instance's committed public state from a snapshot object.
|
|
209
|
-
*
|
|
210
|
-
* The replacement snapshot must be a plain object with exactly the instance's
|
|
211
|
-
* top-level state keys. It updates committed state outside action semantics and
|
|
212
|
-
* notifies observers when the committed state changes. Its type is inferred
|
|
213
|
-
* from the instance's sigma-state definition.
|
|
214
|
-
*/
|
|
215
|
-
declare function replaceState<T extends AnySigmaState>(publicInstance: T, nextState: T extends SigmaState<infer TDefinition> ? Immutable<TDefinition["state"]> : never): void;
|
|
216
|
-
//#endregion
|
|
217
|
-
//#region src/framework.d.ts
|
|
218
|
-
/** Checks whether a value is an instance created by a configured sigma type. */
|
|
219
|
-
declare function isSigmaState(value: object): value is AnySigmaState;
|
|
220
|
-
/**
|
|
221
|
-
* Creates a standalone tracked query helper with the same signature as `fn`.
|
|
222
|
-
*
|
|
223
|
-
* Each call is reactive at the call site and does not memoize results across
|
|
224
|
-
* invocations, which makes `query(fn)` a good fit for local tracked helpers
|
|
225
|
-
* that do not need to live on the sigma-state instance.
|
|
226
|
-
*/
|
|
227
|
-
declare function query<TArgs extends any[], TResult>(fn: (this: void, ...args: TArgs) => TResult): typeof fn;
|
|
228
|
-
/**
|
|
229
|
-
* Builds sigma-state constructors by accumulating default state, computeds,
|
|
230
|
-
* queries, observers, actions, and setup handlers.
|
|
231
|
-
*
|
|
232
|
-
* State and event inference starts from `new SigmaType<TState, TEvents>()`.
|
|
233
|
-
* Later builder methods infer names and types from the objects you pass to them.
|
|
234
|
-
*/
|
|
235
|
-
declare class SigmaType<TState extends AnyState, TEvents extends AnyEvents = {}, TDefaults extends AnyDefaultState<TState> = {}> extends Function {
|
|
236
|
-
readonly [sigmaTypeBrand]: {
|
|
237
|
-
state: TState;
|
|
238
|
-
events: TEvents;
|
|
239
|
-
};
|
|
240
|
-
constructor(name?: string);
|
|
241
|
-
/**
|
|
242
|
-
* Adds top-level public state and default values to the builder.
|
|
243
|
-
*
|
|
244
|
-
* Each property becomes a reactive public state property on instances. Use a
|
|
245
|
-
* zero-argument function when each instance needs a fresh object or array.
|
|
246
|
-
*/
|
|
247
|
-
defaultState<TDefaults extends AnyDefaultState<TState>>(defaultState: TDefaults): SigmaType<TState, TEvents, TDefaults>;
|
|
248
|
-
/**
|
|
249
|
-
* Adds reactive getter properties for derived values that take no arguments.
|
|
250
|
-
*
|
|
251
|
-
* Computed names and return types are inferred from the object you pass.
|
|
252
|
-
* `this` exposes readonly state plus computeds that are already on the builder.
|
|
253
|
-
*/
|
|
254
|
-
computed<TComputeds extends object>(computeds: TComputeds & ThisType<ComputedContext<this, {
|
|
255
|
-
computeds: TComputeds;
|
|
256
|
-
}>>): ExtendSigmaType<this, {
|
|
257
|
-
computeds: TComputeds;
|
|
258
|
-
}>;
|
|
259
|
-
/**
|
|
260
|
-
* Adds reactive read methods that accept arguments.
|
|
261
|
-
*
|
|
262
|
-
* Query names, parameters, and return types are inferred from the object you
|
|
263
|
-
* pass. Each call tracks reactively at the call site and does not memoize
|
|
264
|
-
* results across invocations.
|
|
265
|
-
*/
|
|
266
|
-
queries<TQueries extends object>(queries: TQueries & ThisType<ReadonlyContext<this, {
|
|
267
|
-
queries: TQueries;
|
|
268
|
-
}>>): ExtendSigmaType<this, {
|
|
269
|
-
queries: TQueries;
|
|
270
|
-
}>;
|
|
271
|
-
/**
|
|
272
|
-
* Adds a committed-state observer.
|
|
273
|
-
*
|
|
274
|
-
* Observers run after successful publishes and can opt into Immer patches
|
|
275
|
-
* with `{ patches: true }`.
|
|
276
|
-
*/
|
|
277
|
-
observe(listener: (this: ReadonlyContext<this>, change: SigmaObserveChange<TState>) => void, options?: SigmaObserveOptions & {
|
|
278
|
-
patches?: false | undefined;
|
|
279
|
-
}): this;
|
|
280
|
-
observe(listener: (this: ReadonlyContext<this>, change: SigmaObserveChange<TState, true>) => void, options: SigmaObserveOptions & {
|
|
281
|
-
patches: true;
|
|
282
|
-
}): this;
|
|
283
|
-
/**
|
|
284
|
-
* Adds action methods whose `this` receives draft state, typed events, `commit()`,
|
|
285
|
-
* and the computeds, queries, and actions already defined on the builder.
|
|
286
|
-
*
|
|
287
|
-
* Actions create drafts lazily as they need them. Sync actions on the same
|
|
288
|
-
* instance reuse the current draft, so they can compose and publish once when
|
|
289
|
-
* the outer action returns. Declared async actions publish their initial
|
|
290
|
-
* synchronous work on return, then require `this.commit()` to publish later
|
|
291
|
-
* writes made after `await`. Non-async actions stay synchronous; if one
|
|
292
|
-
* returns a promise, sigma throws so async boundaries stay explicit.
|
|
293
|
-
*/
|
|
294
|
-
actions<TActions extends object>(actions: TActions & ThisType<ActionContext<this, {
|
|
295
|
-
actions: TActions;
|
|
296
|
-
}>>): ExtendSigmaType<this, {
|
|
297
|
-
actions: TActions;
|
|
298
|
-
}>;
|
|
299
|
-
/**
|
|
300
|
-
* Adds an explicit setup handler for side effects and owned resources.
|
|
301
|
-
*
|
|
302
|
-
* Every registered handler runs when `instance.setup(...)` is called, and the
|
|
303
|
-
* setup argument list is inferred from the first handler you add.
|
|
304
|
-
*/
|
|
305
|
-
setup<TSetupArgs extends any[]>(setup: (this: SetupContext<this, {
|
|
306
|
-
setupArgs: TSetupArgs;
|
|
307
|
-
}>, ...args: TSetupArgs) => readonly AnyResource[]): ExtendSigmaType<this, {
|
|
308
|
-
setupArgs: TSetupArgs;
|
|
309
|
-
}>;
|
|
310
|
-
}
|
|
311
|
-
interface SigmaType<TState extends AnyState, TEvents extends AnyEvents, TDefaults extends AnyDefaultState<TState>> {
|
|
312
|
-
/**
|
|
313
|
-
* Type-only access to the configured instance shape.
|
|
314
|
-
*
|
|
315
|
-
* This property does not exist at runtime. Its type is inferred from the
|
|
316
|
-
* generics on `new SigmaType<TState, TEvents>()` plus the later builder inputs.
|
|
317
|
-
*/
|
|
318
|
-
get Instance(): SigmaState<InferSigmaDefinition<this>>;
|
|
319
|
-
/**
|
|
320
|
-
* Creates a sigma-state instance.
|
|
321
|
-
*
|
|
322
|
-
* Constructor input shallowly overrides `defaultState(...)`. Required keys are
|
|
323
|
-
* inferred from whichever state properties still do not have defaults.
|
|
324
|
-
*/
|
|
325
|
-
new (...args: InitialStateInput<TState, TDefaults>): this["Instance"];
|
|
326
|
-
}
|
|
327
|
-
//#endregion
|
|
328
|
-
//#region src/hooks.d.ts
|
|
50
|
+
/** Infers the accepted `useSigma(...)` call shape from a sigma class and its setup parameters. */
|
|
51
|
+
type UseSigmaArgs<T extends Sigma<any>> = T extends {
|
|
52
|
+
onSetup: (...params: infer TParams) => any;
|
|
53
|
+
} ? [] extends TParams ? [create: () => T, options?: Partial<UseSigmaOptions<TParams>>] : [create: () => T, options: UseSigmaOptions<TParams>] : [create: () => T, deps?: readonly any[]];
|
|
329
54
|
/**
|
|
330
|
-
* Creates
|
|
55
|
+
* Creates or reuses a sigma instance for a component and returns its protected consumer view.
|
|
331
56
|
*
|
|
332
|
-
* `
|
|
333
|
-
* defines setup, `useSigma(...)` also runs `setup(...setupArgs)` in an effect
|
|
334
|
-
* and cleans it up when the setup arguments change or the component unmounts.
|
|
335
|
-
*/
|
|
336
|
-
declare function useSigma<T extends AnySigmaState>(create: () => T, setupArgs?: InferSetupArgs<T>): T;
|
|
337
|
-
/**
|
|
338
|
-
* Attaches an event listener in a component and cleans it up automatically.
|
|
339
|
-
*
|
|
340
|
-
* Passing `null` disables the listener. The latest callback is used without
|
|
341
|
-
* forcing the effect to resubscribe on every render.
|
|
57
|
+
* Classes with `onSetup(...)` run setup in an effect and clean it up on unmount.
|
|
342
58
|
*/
|
|
343
|
-
declare function
|
|
59
|
+
declare function useSigma<T extends Sigma<any>>(...args: UseSigmaArgs<T>): Protected<T>;
|
|
344
60
|
//#endregion
|
|
345
|
-
export { type
|
|
61
|
+
export { type Draft, type Immutable, InferEventType, InferListener, Listenable, Protected, Sigma, SigmaDefinition, SigmaListenable, SigmaRef, SigmaState, SigmaTarget, UseSigmaArgs, UseSigmaOptions, castProtected, listen, mergeDefaults, query, setAutoFreeze, sigma, useListener, useSigma };
|