coaction 2.1.0 → 3.1.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 +28 -4
- package/adapter.d.ts +1 -0
- package/adapter.js +3 -0
- package/dist/adapter.d.mts +512 -0
- package/dist/adapter.d.ts +512 -0
- package/dist/adapter.js +686 -0
- package/dist/adapter.mjs +640 -0
- package/dist/index.d.mts +36 -216
- package/dist/index.d.ts +36 -216
- package/dist/index.js +1350 -1025
- package/dist/index.mjs +1353 -991
- package/dist/local.d.mts +344 -0
- package/dist/local.d.ts +344 -0
- package/dist/local.js +1421 -0
- package/dist/local.mjs +1352 -0
- package/dist/shared.d.mts +446 -0
- package/dist/shared.d.ts +446 -0
- package/dist/shared.js +2190 -0
- package/dist/shared.mjs +2114 -0
- package/local.d.ts +1 -0
- package/local.js +3 -0
- package/package.json +41 -2
- package/shared.d.ts +1 -0
- package/shared.js +3 -0
package/dist/local.d.mts
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { Transport } from "data-transport";
|
|
2
|
+
import { Draft, Patches } from "mutative";
|
|
3
|
+
import { computed, effect, effectScope, endBatch, isComputed, isEffect, isEffectScope, isSignal, signal, startBatch, trigger } from "alien-signals";
|
|
4
|
+
|
|
5
|
+
//#region packages/core/src/jsonTypes.d.ts
|
|
6
|
+
type JsonPrimitive = null | boolean | number | string;
|
|
7
|
+
type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
8
|
+
[key: string]: JsonValue;
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region packages/core/src/interface.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* Generic object shape used by stores and slices.
|
|
14
|
+
*/
|
|
15
|
+
type ISlices<T = any> = Record<PropertyKey, T>;
|
|
16
|
+
/**
|
|
17
|
+
* Recursive partial object accepted by {@link Store.setState} when merging a
|
|
18
|
+
* plain object payload into the current state tree.
|
|
19
|
+
*/
|
|
20
|
+
type DeepPartial<T> = { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K] };
|
|
21
|
+
/**
|
|
22
|
+
* Subscription callback invoked after the store publishes a state change.
|
|
23
|
+
*/
|
|
24
|
+
type Listener = () => void;
|
|
25
|
+
/**
|
|
26
|
+
* Patch pair exposed to middleware compatibility hooks.
|
|
27
|
+
*/
|
|
28
|
+
interface PatchTransform {
|
|
29
|
+
patches: Patches;
|
|
30
|
+
inversePatches: Patches;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Trace envelope emitted before and after a store method executes.
|
|
34
|
+
*/
|
|
35
|
+
interface StoreTraceEvent {
|
|
36
|
+
/**
|
|
37
|
+
* The id of the method.
|
|
38
|
+
*/
|
|
39
|
+
id: string;
|
|
40
|
+
/**
|
|
41
|
+
* The method name.
|
|
42
|
+
*/
|
|
43
|
+
method: string;
|
|
44
|
+
/**
|
|
45
|
+
* The slice key.
|
|
46
|
+
*/
|
|
47
|
+
sliceKey?: PropertyKey;
|
|
48
|
+
/**
|
|
49
|
+
* The parameters of the method.
|
|
50
|
+
*/
|
|
51
|
+
parameters?: any[];
|
|
52
|
+
/**
|
|
53
|
+
* The result of the method.
|
|
54
|
+
*/
|
|
55
|
+
result?: any;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Runtime store contract returned by {@link create} before framework-specific
|
|
59
|
+
* wrappers add selectors or reactivity helpers.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* `getState()` returns methods and getters alongside plain data. Methods
|
|
63
|
+
* extracted from the returned object keep the correct `this` binding when they
|
|
64
|
+
* are later invoked.
|
|
65
|
+
*/
|
|
66
|
+
interface Store<T extends ISlices = ISlices> {
|
|
67
|
+
/**
|
|
68
|
+
* The name of the store.
|
|
69
|
+
*/
|
|
70
|
+
name: string;
|
|
71
|
+
/**
|
|
72
|
+
* Mutate the current state.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* Pass a deep-partial object to merge fields, or pass an updater to edit a
|
|
76
|
+
* Mutative draft. Passing `null` is a no-op. Client-side shared stores intentionally reject direct
|
|
77
|
+
* `setState()` calls; trigger a store method instead.
|
|
78
|
+
*/
|
|
79
|
+
setState: (
|
|
80
|
+
/**
|
|
81
|
+
* The next partial state, or an updater that mutates a draft.
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
next: DeepPartial<T> | ((draft: Draft<T>) => any) | null,
|
|
85
|
+
/**
|
|
86
|
+
* Low-level updater hook used by transports and middleware integrations.
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
updater?: (next: DeepPartial<T> | ((draft: Draft<T>) => any) | null) => [] | [T, Patches, Patches]) => void;
|
|
90
|
+
/**
|
|
91
|
+
* Read the current state object.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* The returned object includes methods and getters. Methods destructured from
|
|
95
|
+
* this object continue to execute against the latest store state.
|
|
96
|
+
*/
|
|
97
|
+
getState: () => T;
|
|
98
|
+
/**
|
|
99
|
+
* Subscribe to state changes.
|
|
100
|
+
*
|
|
101
|
+
* @returns A function that removes the listener.
|
|
102
|
+
*/
|
|
103
|
+
subscribe: (listener: Listener) => () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Tear down the store.
|
|
106
|
+
*
|
|
107
|
+
* @remarks
|
|
108
|
+
* `destroy()` is idempotent. It clears subscriptions and disposes any
|
|
109
|
+
* attached transport.
|
|
110
|
+
*/
|
|
111
|
+
destroy: () => void;
|
|
112
|
+
/**
|
|
113
|
+
* Indicates whether the store is local, the main shared store, or a client
|
|
114
|
+
* mirror of a shared store.
|
|
115
|
+
*/
|
|
116
|
+
share?: 'main' | 'client' | false;
|
|
117
|
+
/**
|
|
118
|
+
* Transport used to synchronize a shared store between processes or threads.
|
|
119
|
+
*/
|
|
120
|
+
transport?: Transport;
|
|
121
|
+
/**
|
|
122
|
+
* Whether `createState` was interpreted as a slices object.
|
|
123
|
+
*/
|
|
124
|
+
isSliceStore: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Apply patches to the current state.
|
|
127
|
+
*
|
|
128
|
+
* @remarks
|
|
129
|
+
* This is a low-level hook used by transports and middleware. Application
|
|
130
|
+
* code should generally prefer store methods or `setState()`. Client-side
|
|
131
|
+
* shared-store mirrors reject direct `apply()` calls.
|
|
132
|
+
*/
|
|
133
|
+
apply: (state?: T, patches?: Patches) => void;
|
|
134
|
+
/**
|
|
135
|
+
* Return the current state without methods or getters.
|
|
136
|
+
*
|
|
137
|
+
* @remarks
|
|
138
|
+
* Useful for serialization, inspection, or tests that only care about raw
|
|
139
|
+
* data.
|
|
140
|
+
*/
|
|
141
|
+
getPureState: () => T;
|
|
142
|
+
/**
|
|
143
|
+
* Return the state produced during initialization before later mutations.
|
|
144
|
+
*/
|
|
145
|
+
getInitialState: () => T;
|
|
146
|
+
/**
|
|
147
|
+
* @deprecated Middleware compatibility hook. Prefer typing middleware stores
|
|
148
|
+
* with `MiddlewareStore`.
|
|
149
|
+
*/
|
|
150
|
+
patch?: (option: PatchTransform) => PatchTransform;
|
|
151
|
+
/**
|
|
152
|
+
* @deprecated Middleware compatibility hook. Prefer typing middleware stores
|
|
153
|
+
* with `MiddlewareStore`.
|
|
154
|
+
*/
|
|
155
|
+
trace?: (options: StoreTraceEvent) => void;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Semantic alias for middleware-facing stores.
|
|
159
|
+
*
|
|
160
|
+
* @remarks
|
|
161
|
+
* Middleware implementations should type their `store` parameter as
|
|
162
|
+
* `MiddlewareStore` instead of relying on deprecated `patch` or `trace` hooks.
|
|
163
|
+
*/
|
|
164
|
+
interface MiddlewareStore<T extends ISlices = ISlices> extends Store<T> {}
|
|
165
|
+
type TransportPolicyRequest = {
|
|
166
|
+
action: readonly string[];
|
|
167
|
+
args: readonly JsonValue[];
|
|
168
|
+
type: 'execute';
|
|
169
|
+
} | {
|
|
170
|
+
type: 'fullSync';
|
|
171
|
+
};
|
|
172
|
+
type TransportPolicy = {
|
|
173
|
+
/** Further restrict action paths declared by the authoritative store. */allowedActions?: readonly (readonly string[])[]; /** Authorize a decoded JSON request before serving it. */
|
|
174
|
+
authorize?: (request: TransportPolicyRequest) => boolean | Promise<boolean>;
|
|
175
|
+
/**
|
|
176
|
+
* Map a caught execute error to an explicitly client-visible message.
|
|
177
|
+
* Returning `undefined` keeps the generic redacted message.
|
|
178
|
+
*/
|
|
179
|
+
mapError?: (error: unknown, request: TransportPolicyRequest) => string | undefined | Promise<string | undefined>;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Helper passed into {@link Slice} and {@link Slices} factories.
|
|
183
|
+
*
|
|
184
|
+
* @remarks
|
|
185
|
+
* Call it with no arguments to read the current store state. Call it with a
|
|
186
|
+
* dependency selector pair to define a computed value.
|
|
187
|
+
*/
|
|
188
|
+
interface Getter<T extends ISlices> {
|
|
189
|
+
<P extends any[], R>(getDeps: (store: T) => readonly [...P] | [...P], selector: (...args: P) => R): R;
|
|
190
|
+
(): T;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Factory for a single store object.
|
|
194
|
+
*
|
|
195
|
+
* @remarks
|
|
196
|
+
* Return a plain object containing state, getters, and methods. Methods and
|
|
197
|
+
* getters may use `this` to access the live store state.
|
|
198
|
+
*/
|
|
199
|
+
type Slice<T extends ISlices> = (
|
|
200
|
+
/**
|
|
201
|
+
* The setState is used to update the state.
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
set: Store<T>['setState'],
|
|
205
|
+
/**
|
|
206
|
+
* The getState is used to get the state.
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
get: Getter<T>,
|
|
210
|
+
/**
|
|
211
|
+
* The store is used to store the state.
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
store: Store<T>) => T;
|
|
215
|
+
/**
|
|
216
|
+
* Factory for a named slice inside a slices store.
|
|
217
|
+
*
|
|
218
|
+
* @remarks
|
|
219
|
+
* The returned object becomes the value stored under the slice key. When an
|
|
220
|
+
* object input only contains functions, prefer explicit `sliceMode` to avoid
|
|
221
|
+
* ambiguity between slices and a plain method-only store.
|
|
222
|
+
*/
|
|
223
|
+
type Slices<T extends ISlices, K extends keyof T> = (
|
|
224
|
+
/**
|
|
225
|
+
* The setState is used to update the state.
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
set: Store<T>['setState'],
|
|
229
|
+
/**
|
|
230
|
+
* The getState is used to get the state.
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
get: Getter<T>,
|
|
234
|
+
/**
|
|
235
|
+
* The store is used to store the state.
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
store: Store<T>) => T[K];
|
|
239
|
+
/**
|
|
240
|
+
* Store enhancer invoked during store creation.
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* Middleware may mutate the received store in place or return a replacement
|
|
244
|
+
* store object, but it must preserve the {@link Store} contract.
|
|
245
|
+
*/
|
|
246
|
+
type Middleware<T extends CreateState> = (store: MiddlewareStore<T>) => MiddlewareStore<T>;
|
|
247
|
+
/**
|
|
248
|
+
* Derived state object produced by mapping slice factories to their return
|
|
249
|
+
* types.
|
|
250
|
+
*/
|
|
251
|
+
type SliceState<T extends Record<PropertyKey, Slice<any>>> = { [K in keyof T]: ReturnType<T[K]> };
|
|
252
|
+
/**
|
|
253
|
+
* Options for creating a local store or the main side of a shared store.
|
|
254
|
+
*/
|
|
255
|
+
type StoreOptions<T extends CreateState> = {
|
|
256
|
+
/**
|
|
257
|
+
* The name of the store.
|
|
258
|
+
*/
|
|
259
|
+
name?: string;
|
|
260
|
+
/**
|
|
261
|
+
* @deprecated Internal worker-mode override retained for compatibility.
|
|
262
|
+
* Prefer passing `transport` or letting the runtime infer the environment.
|
|
263
|
+
*/
|
|
264
|
+
workerType?: 'SharedWorkerInternal' | 'WebWorkerInternal';
|
|
265
|
+
/**
|
|
266
|
+
* Inject a pre-built transport for advanced shared-store setups.
|
|
267
|
+
*/
|
|
268
|
+
transport?: Transport; /** Restrict requests accepted by a shared-main store. */
|
|
269
|
+
transportPolicy?: TransportPolicy;
|
|
270
|
+
/**
|
|
271
|
+
* Middleware chain applied before the initial state is finalized.
|
|
272
|
+
*/
|
|
273
|
+
middlewares?: Middleware<T>[];
|
|
274
|
+
/**
|
|
275
|
+
* Enable patch generation.
|
|
276
|
+
*
|
|
277
|
+
* @remarks
|
|
278
|
+
* Required for async client stores and useful for middleware or mutable
|
|
279
|
+
* integrations that depend on patch streams.
|
|
280
|
+
*/
|
|
281
|
+
enablePatches?: boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Control how `createState` should be interpreted.
|
|
284
|
+
*
|
|
285
|
+
* @remarks
|
|
286
|
+
* - auto: infer from createState shape. Object maps whose values are all
|
|
287
|
+
* functions are ambiguous, so prefer setting `sliceMode` explicitly.
|
|
288
|
+
* - slices: force slices mode.
|
|
289
|
+
* - single: force single-store mode.
|
|
290
|
+
*/
|
|
291
|
+
sliceMode?: 'auto' | 'slices' | 'single';
|
|
292
|
+
};
|
|
293
|
+
/** Options accepted by the statically isolated local-store entry point. */
|
|
294
|
+
type LocalStoreOptions<T extends CreateState> = Omit<StoreOptions<T>, 'transport' | 'transportPolicy' | 'workerType'>;
|
|
295
|
+
/**
|
|
296
|
+
* Callable store returned by {@link create} in local or main/shared mode.
|
|
297
|
+
*/
|
|
298
|
+
type StoreReturn<T extends object> = Store<T> & ((...args: any[]) => T);
|
|
299
|
+
/**
|
|
300
|
+
* Accepted `create()` input shape.
|
|
301
|
+
*
|
|
302
|
+
* @remarks
|
|
303
|
+
* This can be either a single store factory/object or a map of slice
|
|
304
|
+
* factories.
|
|
305
|
+
*/
|
|
306
|
+
type CreateState = ISlices | Record<PropertyKey, Slice<any>>;
|
|
307
|
+
type SingleLocalStoreOptions<T extends CreateState> = LocalStoreOptions<T> & {
|
|
308
|
+
sliceMode: 'single';
|
|
309
|
+
};
|
|
310
|
+
/** Overload set for the transport-free `coaction/local` create function. */
|
|
311
|
+
type LocalCreator = {
|
|
312
|
+
<T extends ISlices>(createState: T, options: SingleLocalStoreOptions<T>): StoreReturn<T>;
|
|
313
|
+
<T extends Record<PropertyKey, Slice<any>>>(createState: T, options?: LocalStoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
314
|
+
<T extends ISlices>(createState: Slice<T> | T, options?: LocalStoreOptions<T>): StoreReturn<T>;
|
|
315
|
+
};
|
|
316
|
+
//#endregion
|
|
317
|
+
//#region packages/core/src/createLocal.d.ts
|
|
318
|
+
/**
|
|
319
|
+
* Create a store without linking the shared transport runtime.
|
|
320
|
+
*
|
|
321
|
+
* @remarks
|
|
322
|
+
* The public `coaction/local` entry exports this implementation as `create`.
|
|
323
|
+
* `createLocal` is only its internal and documentation name; it is not
|
|
324
|
+
* exported by the root `coaction` entry.
|
|
325
|
+
*/
|
|
326
|
+
declare const createLocal: LocalCreator;
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region packages/core/src/lifecycle.d.ts
|
|
329
|
+
declare const onStoreReady: <T extends CreateState>(store: Store<T>, callback: () => void) => () => void;
|
|
330
|
+
//#endregion
|
|
331
|
+
//#region packages/core/src/wrapStore.d.ts
|
|
332
|
+
/**
|
|
333
|
+
* Convert a store object into Coaction's callable store shape.
|
|
334
|
+
*
|
|
335
|
+
* @remarks
|
|
336
|
+
* Framework bindings use this to attach selector-aware readers while
|
|
337
|
+
* preserving the underlying store API on the returned function object. Most
|
|
338
|
+
* applications should use a public `create` entry instead of calling
|
|
339
|
+
* `wrapStore()` directly. Framework authors import this helper from
|
|
340
|
+
* `coaction/local` or `coaction/adapter`.
|
|
341
|
+
*/
|
|
342
|
+
declare const wrapStore: <T extends object>(store: Store<T>, getState?: (...args: unknown[]) => T) => StoreReturn<T>;
|
|
343
|
+
//#endregion
|
|
344
|
+
export { type CreateState, type DeepPartial, type Getter, type ISlices, type Listener, type LocalCreator, type LocalStoreOptions, type Middleware, type MiddlewareStore, type PatchTransform, type Slice, type SliceState, type Slices, type Store, type StoreReturn, type StoreTraceEvent, computed, createLocal as create, effect, effectScope, endBatch, isComputed, isEffect, isEffectScope, isSignal, onStoreReady, signal, startBatch, trigger, wrapStore };
|
package/dist/local.d.ts
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { Transport } from "data-transport";
|
|
2
|
+
import { Draft, Patches } from "mutative";
|
|
3
|
+
import { computed, effect, effectScope, endBatch, isComputed, isEffect, isEffectScope, isSignal, signal, startBatch, trigger } from "alien-signals";
|
|
4
|
+
|
|
5
|
+
//#region packages/core/src/jsonTypes.d.ts
|
|
6
|
+
type JsonPrimitive = null | boolean | number | string;
|
|
7
|
+
type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
8
|
+
[key: string]: JsonValue;
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region packages/core/src/interface.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* Generic object shape used by stores and slices.
|
|
14
|
+
*/
|
|
15
|
+
type ISlices<T = any> = Record<PropertyKey, T>;
|
|
16
|
+
/**
|
|
17
|
+
* Recursive partial object accepted by {@link Store.setState} when merging a
|
|
18
|
+
* plain object payload into the current state tree.
|
|
19
|
+
*/
|
|
20
|
+
type DeepPartial<T> = { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K] };
|
|
21
|
+
/**
|
|
22
|
+
* Subscription callback invoked after the store publishes a state change.
|
|
23
|
+
*/
|
|
24
|
+
type Listener = () => void;
|
|
25
|
+
/**
|
|
26
|
+
* Patch pair exposed to middleware compatibility hooks.
|
|
27
|
+
*/
|
|
28
|
+
interface PatchTransform {
|
|
29
|
+
patches: Patches;
|
|
30
|
+
inversePatches: Patches;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Trace envelope emitted before and after a store method executes.
|
|
34
|
+
*/
|
|
35
|
+
interface StoreTraceEvent {
|
|
36
|
+
/**
|
|
37
|
+
* The id of the method.
|
|
38
|
+
*/
|
|
39
|
+
id: string;
|
|
40
|
+
/**
|
|
41
|
+
* The method name.
|
|
42
|
+
*/
|
|
43
|
+
method: string;
|
|
44
|
+
/**
|
|
45
|
+
* The slice key.
|
|
46
|
+
*/
|
|
47
|
+
sliceKey?: PropertyKey;
|
|
48
|
+
/**
|
|
49
|
+
* The parameters of the method.
|
|
50
|
+
*/
|
|
51
|
+
parameters?: any[];
|
|
52
|
+
/**
|
|
53
|
+
* The result of the method.
|
|
54
|
+
*/
|
|
55
|
+
result?: any;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Runtime store contract returned by {@link create} before framework-specific
|
|
59
|
+
* wrappers add selectors or reactivity helpers.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* `getState()` returns methods and getters alongside plain data. Methods
|
|
63
|
+
* extracted from the returned object keep the correct `this` binding when they
|
|
64
|
+
* are later invoked.
|
|
65
|
+
*/
|
|
66
|
+
interface Store<T extends ISlices = ISlices> {
|
|
67
|
+
/**
|
|
68
|
+
* The name of the store.
|
|
69
|
+
*/
|
|
70
|
+
name: string;
|
|
71
|
+
/**
|
|
72
|
+
* Mutate the current state.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* Pass a deep-partial object to merge fields, or pass an updater to edit a
|
|
76
|
+
* Mutative draft. Passing `null` is a no-op. Client-side shared stores intentionally reject direct
|
|
77
|
+
* `setState()` calls; trigger a store method instead.
|
|
78
|
+
*/
|
|
79
|
+
setState: (
|
|
80
|
+
/**
|
|
81
|
+
* The next partial state, or an updater that mutates a draft.
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
next: DeepPartial<T> | ((draft: Draft<T>) => any) | null,
|
|
85
|
+
/**
|
|
86
|
+
* Low-level updater hook used by transports and middleware integrations.
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
updater?: (next: DeepPartial<T> | ((draft: Draft<T>) => any) | null) => [] | [T, Patches, Patches]) => void;
|
|
90
|
+
/**
|
|
91
|
+
* Read the current state object.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* The returned object includes methods and getters. Methods destructured from
|
|
95
|
+
* this object continue to execute against the latest store state.
|
|
96
|
+
*/
|
|
97
|
+
getState: () => T;
|
|
98
|
+
/**
|
|
99
|
+
* Subscribe to state changes.
|
|
100
|
+
*
|
|
101
|
+
* @returns A function that removes the listener.
|
|
102
|
+
*/
|
|
103
|
+
subscribe: (listener: Listener) => () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Tear down the store.
|
|
106
|
+
*
|
|
107
|
+
* @remarks
|
|
108
|
+
* `destroy()` is idempotent. It clears subscriptions and disposes any
|
|
109
|
+
* attached transport.
|
|
110
|
+
*/
|
|
111
|
+
destroy: () => void;
|
|
112
|
+
/**
|
|
113
|
+
* Indicates whether the store is local, the main shared store, or a client
|
|
114
|
+
* mirror of a shared store.
|
|
115
|
+
*/
|
|
116
|
+
share?: 'main' | 'client' | false;
|
|
117
|
+
/**
|
|
118
|
+
* Transport used to synchronize a shared store between processes or threads.
|
|
119
|
+
*/
|
|
120
|
+
transport?: Transport;
|
|
121
|
+
/**
|
|
122
|
+
* Whether `createState` was interpreted as a slices object.
|
|
123
|
+
*/
|
|
124
|
+
isSliceStore: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Apply patches to the current state.
|
|
127
|
+
*
|
|
128
|
+
* @remarks
|
|
129
|
+
* This is a low-level hook used by transports and middleware. Application
|
|
130
|
+
* code should generally prefer store methods or `setState()`. Client-side
|
|
131
|
+
* shared-store mirrors reject direct `apply()` calls.
|
|
132
|
+
*/
|
|
133
|
+
apply: (state?: T, patches?: Patches) => void;
|
|
134
|
+
/**
|
|
135
|
+
* Return the current state without methods or getters.
|
|
136
|
+
*
|
|
137
|
+
* @remarks
|
|
138
|
+
* Useful for serialization, inspection, or tests that only care about raw
|
|
139
|
+
* data.
|
|
140
|
+
*/
|
|
141
|
+
getPureState: () => T;
|
|
142
|
+
/**
|
|
143
|
+
* Return the state produced during initialization before later mutations.
|
|
144
|
+
*/
|
|
145
|
+
getInitialState: () => T;
|
|
146
|
+
/**
|
|
147
|
+
* @deprecated Middleware compatibility hook. Prefer typing middleware stores
|
|
148
|
+
* with `MiddlewareStore`.
|
|
149
|
+
*/
|
|
150
|
+
patch?: (option: PatchTransform) => PatchTransform;
|
|
151
|
+
/**
|
|
152
|
+
* @deprecated Middleware compatibility hook. Prefer typing middleware stores
|
|
153
|
+
* with `MiddlewareStore`.
|
|
154
|
+
*/
|
|
155
|
+
trace?: (options: StoreTraceEvent) => void;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Semantic alias for middleware-facing stores.
|
|
159
|
+
*
|
|
160
|
+
* @remarks
|
|
161
|
+
* Middleware implementations should type their `store` parameter as
|
|
162
|
+
* `MiddlewareStore` instead of relying on deprecated `patch` or `trace` hooks.
|
|
163
|
+
*/
|
|
164
|
+
interface MiddlewareStore<T extends ISlices = ISlices> extends Store<T> {}
|
|
165
|
+
type TransportPolicyRequest = {
|
|
166
|
+
action: readonly string[];
|
|
167
|
+
args: readonly JsonValue[];
|
|
168
|
+
type: 'execute';
|
|
169
|
+
} | {
|
|
170
|
+
type: 'fullSync';
|
|
171
|
+
};
|
|
172
|
+
type TransportPolicy = {
|
|
173
|
+
/** Further restrict action paths declared by the authoritative store. */allowedActions?: readonly (readonly string[])[]; /** Authorize a decoded JSON request before serving it. */
|
|
174
|
+
authorize?: (request: TransportPolicyRequest) => boolean | Promise<boolean>;
|
|
175
|
+
/**
|
|
176
|
+
* Map a caught execute error to an explicitly client-visible message.
|
|
177
|
+
* Returning `undefined` keeps the generic redacted message.
|
|
178
|
+
*/
|
|
179
|
+
mapError?: (error: unknown, request: TransportPolicyRequest) => string | undefined | Promise<string | undefined>;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Helper passed into {@link Slice} and {@link Slices} factories.
|
|
183
|
+
*
|
|
184
|
+
* @remarks
|
|
185
|
+
* Call it with no arguments to read the current store state. Call it with a
|
|
186
|
+
* dependency selector pair to define a computed value.
|
|
187
|
+
*/
|
|
188
|
+
interface Getter<T extends ISlices> {
|
|
189
|
+
<P extends any[], R>(getDeps: (store: T) => readonly [...P] | [...P], selector: (...args: P) => R): R;
|
|
190
|
+
(): T;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Factory for a single store object.
|
|
194
|
+
*
|
|
195
|
+
* @remarks
|
|
196
|
+
* Return a plain object containing state, getters, and methods. Methods and
|
|
197
|
+
* getters may use `this` to access the live store state.
|
|
198
|
+
*/
|
|
199
|
+
type Slice<T extends ISlices> = (
|
|
200
|
+
/**
|
|
201
|
+
* The setState is used to update the state.
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
set: Store<T>['setState'],
|
|
205
|
+
/**
|
|
206
|
+
* The getState is used to get the state.
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
get: Getter<T>,
|
|
210
|
+
/**
|
|
211
|
+
* The store is used to store the state.
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
store: Store<T>) => T;
|
|
215
|
+
/**
|
|
216
|
+
* Factory for a named slice inside a slices store.
|
|
217
|
+
*
|
|
218
|
+
* @remarks
|
|
219
|
+
* The returned object becomes the value stored under the slice key. When an
|
|
220
|
+
* object input only contains functions, prefer explicit `sliceMode` to avoid
|
|
221
|
+
* ambiguity between slices and a plain method-only store.
|
|
222
|
+
*/
|
|
223
|
+
type Slices<T extends ISlices, K extends keyof T> = (
|
|
224
|
+
/**
|
|
225
|
+
* The setState is used to update the state.
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
set: Store<T>['setState'],
|
|
229
|
+
/**
|
|
230
|
+
* The getState is used to get the state.
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
get: Getter<T>,
|
|
234
|
+
/**
|
|
235
|
+
* The store is used to store the state.
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
store: Store<T>) => T[K];
|
|
239
|
+
/**
|
|
240
|
+
* Store enhancer invoked during store creation.
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* Middleware may mutate the received store in place or return a replacement
|
|
244
|
+
* store object, but it must preserve the {@link Store} contract.
|
|
245
|
+
*/
|
|
246
|
+
type Middleware<T extends CreateState> = (store: MiddlewareStore<T>) => MiddlewareStore<T>;
|
|
247
|
+
/**
|
|
248
|
+
* Derived state object produced by mapping slice factories to their return
|
|
249
|
+
* types.
|
|
250
|
+
*/
|
|
251
|
+
type SliceState<T extends Record<PropertyKey, Slice<any>>> = { [K in keyof T]: ReturnType<T[K]> };
|
|
252
|
+
/**
|
|
253
|
+
* Options for creating a local store or the main side of a shared store.
|
|
254
|
+
*/
|
|
255
|
+
type StoreOptions<T extends CreateState> = {
|
|
256
|
+
/**
|
|
257
|
+
* The name of the store.
|
|
258
|
+
*/
|
|
259
|
+
name?: string;
|
|
260
|
+
/**
|
|
261
|
+
* @deprecated Internal worker-mode override retained for compatibility.
|
|
262
|
+
* Prefer passing `transport` or letting the runtime infer the environment.
|
|
263
|
+
*/
|
|
264
|
+
workerType?: 'SharedWorkerInternal' | 'WebWorkerInternal';
|
|
265
|
+
/**
|
|
266
|
+
* Inject a pre-built transport for advanced shared-store setups.
|
|
267
|
+
*/
|
|
268
|
+
transport?: Transport; /** Restrict requests accepted by a shared-main store. */
|
|
269
|
+
transportPolicy?: TransportPolicy;
|
|
270
|
+
/**
|
|
271
|
+
* Middleware chain applied before the initial state is finalized.
|
|
272
|
+
*/
|
|
273
|
+
middlewares?: Middleware<T>[];
|
|
274
|
+
/**
|
|
275
|
+
* Enable patch generation.
|
|
276
|
+
*
|
|
277
|
+
* @remarks
|
|
278
|
+
* Required for async client stores and useful for middleware or mutable
|
|
279
|
+
* integrations that depend on patch streams.
|
|
280
|
+
*/
|
|
281
|
+
enablePatches?: boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Control how `createState` should be interpreted.
|
|
284
|
+
*
|
|
285
|
+
* @remarks
|
|
286
|
+
* - auto: infer from createState shape. Object maps whose values are all
|
|
287
|
+
* functions are ambiguous, so prefer setting `sliceMode` explicitly.
|
|
288
|
+
* - slices: force slices mode.
|
|
289
|
+
* - single: force single-store mode.
|
|
290
|
+
*/
|
|
291
|
+
sliceMode?: 'auto' | 'slices' | 'single';
|
|
292
|
+
};
|
|
293
|
+
/** Options accepted by the statically isolated local-store entry point. */
|
|
294
|
+
type LocalStoreOptions<T extends CreateState> = Omit<StoreOptions<T>, 'transport' | 'transportPolicy' | 'workerType'>;
|
|
295
|
+
/**
|
|
296
|
+
* Callable store returned by {@link create} in local or main/shared mode.
|
|
297
|
+
*/
|
|
298
|
+
type StoreReturn<T extends object> = Store<T> & ((...args: any[]) => T);
|
|
299
|
+
/**
|
|
300
|
+
* Accepted `create()` input shape.
|
|
301
|
+
*
|
|
302
|
+
* @remarks
|
|
303
|
+
* This can be either a single store factory/object or a map of slice
|
|
304
|
+
* factories.
|
|
305
|
+
*/
|
|
306
|
+
type CreateState = ISlices | Record<PropertyKey, Slice<any>>;
|
|
307
|
+
type SingleLocalStoreOptions<T extends CreateState> = LocalStoreOptions<T> & {
|
|
308
|
+
sliceMode: 'single';
|
|
309
|
+
};
|
|
310
|
+
/** Overload set for the transport-free `coaction/local` create function. */
|
|
311
|
+
type LocalCreator = {
|
|
312
|
+
<T extends ISlices>(createState: T, options: SingleLocalStoreOptions<T>): StoreReturn<T>;
|
|
313
|
+
<T extends Record<PropertyKey, Slice<any>>>(createState: T, options?: LocalStoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
314
|
+
<T extends ISlices>(createState: Slice<T> | T, options?: LocalStoreOptions<T>): StoreReturn<T>;
|
|
315
|
+
};
|
|
316
|
+
//#endregion
|
|
317
|
+
//#region packages/core/src/createLocal.d.ts
|
|
318
|
+
/**
|
|
319
|
+
* Create a store without linking the shared transport runtime.
|
|
320
|
+
*
|
|
321
|
+
* @remarks
|
|
322
|
+
* The public `coaction/local` entry exports this implementation as `create`.
|
|
323
|
+
* `createLocal` is only its internal and documentation name; it is not
|
|
324
|
+
* exported by the root `coaction` entry.
|
|
325
|
+
*/
|
|
326
|
+
declare const createLocal: LocalCreator;
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region packages/core/src/lifecycle.d.ts
|
|
329
|
+
declare const onStoreReady: <T extends CreateState>(store: Store<T>, callback: () => void) => () => void;
|
|
330
|
+
//#endregion
|
|
331
|
+
//#region packages/core/src/wrapStore.d.ts
|
|
332
|
+
/**
|
|
333
|
+
* Convert a store object into Coaction's callable store shape.
|
|
334
|
+
*
|
|
335
|
+
* @remarks
|
|
336
|
+
* Framework bindings use this to attach selector-aware readers while
|
|
337
|
+
* preserving the underlying store API on the returned function object. Most
|
|
338
|
+
* applications should use a public `create` entry instead of calling
|
|
339
|
+
* `wrapStore()` directly. Framework authors import this helper from
|
|
340
|
+
* `coaction/local` or `coaction/adapter`.
|
|
341
|
+
*/
|
|
342
|
+
declare const wrapStore: <T extends object>(store: Store<T>, getState?: (...args: unknown[]) => T) => StoreReturn<T>;
|
|
343
|
+
//#endregion
|
|
344
|
+
export { type CreateState, type DeepPartial, type Getter, type ISlices, type Listener, type LocalCreator, type LocalStoreOptions, type Middleware, type MiddlewareStore, type PatchTransform, type Slice, type SliceState, type Slices, type Store, type StoreReturn, type StoreTraceEvent, computed, createLocal as create, effect, effectScope, endBatch, isComputed, isEffect, isEffectScope, isSignal, onStoreReady, signal, startBatch, trigger, wrapStore };
|