@effuse/store 1.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.
@@ -0,0 +1,698 @@
1
+ import { Schema, Effect, Option } from 'effect';
2
+ import { Signal } from '@effuse/core';
3
+ import * as effect_Cause from 'effect/Cause';
4
+ import * as effect_Types from 'effect/Types';
5
+
6
+ /**
7
+ * MIT License
8
+ *
9
+ * Copyright (c) 2025 Chris M. Perez
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ * of this software and associated documentation files (the "Software"), to deal
13
+ * in the Software without restriction, including without limitation the rights
14
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ * copies of the Software, and to permit persons to whom the Software is
16
+ * furnished to do so, subject to the following conditions:
17
+ *
18
+ * The above copyright notice and this permission notice shall be included in all
19
+ * copies or substantial portions of the Software.
20
+ *
21
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ * SOFTWARE.
28
+ */
29
+
30
+ declare const StoreOptionsSchema: Schema.Struct<{
31
+ persist: Schema.optional<typeof Schema.Boolean>;
32
+ storageKey: Schema.optional<typeof Schema.String>;
33
+ devtools: Schema.optional<typeof Schema.Boolean>;
34
+ }>;
35
+ type StoreOptions = Schema.Schema.Type<typeof StoreOptionsSchema>;
36
+ type StoreState<T> = {
37
+ [K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => R : Signal<T[K]>;
38
+ };
39
+ type StoreContext<T> = {
40
+ [K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? T[K] : Signal<T[K]>;
41
+ };
42
+ type StoreDefinition<T> = {
43
+ [K in keyof T]: T[K] extends (...args: infer A) => infer R ? (this: StoreContext<T>, ...args: A) => R : T[K];
44
+ };
45
+ type ActionContext<T> = StoreContext<T>;
46
+ type Middleware<T> = (state: T, action: string, args: unknown[]) => T | undefined;
47
+ interface Store<T> {
48
+ readonly name: string;
49
+ readonly state: StoreState<T>;
50
+ subscribe: (callback: () => void) => () => void;
51
+ subscribeToKey: <K extends keyof T>(key: K, callback: (value: T[K]) => void) => () => void;
52
+ getSnapshot: () => {
53
+ [K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? never : T[K];
54
+ };
55
+ computed: <R>(selector: (snapshot: Record<string, unknown>) => R) => Signal<R>;
56
+ batch: (updates: () => void) => void;
57
+ reset: () => void;
58
+ use: (middleware: Middleware<Record<string, unknown>>) => () => void;
59
+ toJSON: () => {
60
+ [K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? never : T[K];
61
+ };
62
+ update: (updater: (draft: {
63
+ [K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? never : T[K];
64
+ }) => void) => void;
65
+ select: <R>(selector: (snapshot: Record<string, unknown>) => R) => Signal<R>;
66
+ }
67
+ type InferStoreState<S> = S extends Store<infer T> ? {
68
+ [K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? never : T[K];
69
+ } : never;
70
+
71
+ /**
72
+ * MIT License
73
+ *
74
+ * Copyright (c) 2025 Chris M. Perez
75
+ *
76
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
77
+ * of this software and associated documentation files (the "Software"), to deal
78
+ * in the Software without restriction, including without limitation the rights
79
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
80
+ * copies of the Software, and to permit persons to whom the Software is
81
+ * furnished to do so, subject to the following conditions:
82
+ *
83
+ * The above copyright notice and this permission notice shall be included in all
84
+ * copies or substantial portions of the Software.
85
+ *
86
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
87
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
88
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
89
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
90
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
91
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
92
+ * SOFTWARE.
93
+ */
94
+
95
+ interface StorageAdapter {
96
+ getItem: (key: string) => Effect.Effect<Option.Option<string>>;
97
+ setItem: (key: string, value: string) => Effect.Effect<void>;
98
+ removeItem: (key: string) => Effect.Effect<void>;
99
+ }
100
+
101
+ /**
102
+ * MIT License
103
+ *
104
+ * Copyright (c) 2025 Chris M. Perez
105
+ *
106
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
107
+ * of this software and associated documentation files (the "Software"), to deal
108
+ * in the Software without restriction, including without limitation the rights
109
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
110
+ * copies of the Software, and to permit persons to whom the Software is
111
+ * furnished to do so, subject to the following conditions:
112
+ *
113
+ * The above copyright notice and this permission notice shall be included in all
114
+ * copies or substantial portions of the Software.
115
+ *
116
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
117
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
118
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
119
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
120
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
121
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
122
+ * SOFTWARE.
123
+ */
124
+
125
+ interface CreateStoreOptions extends StoreOptions {
126
+ storage?: StorageAdapter;
127
+ }
128
+ declare const createStore: <T extends object>(name: string, definition: StoreDefinition<T>, options?: CreateStoreOptions) => Store<T> & StoreState<T>;
129
+
130
+ /**
131
+ * MIT License
132
+ *
133
+ * Copyright (c) 2025 Chris M. Perez
134
+ *
135
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
136
+ * of this software and associated documentation files (the "Software"), to deal
137
+ * in the Software without restriction, including without limitation the rights
138
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
139
+ * copies of the Software, and to permit persons to whom the Software is
140
+ * furnished to do so, subject to the following conditions:
141
+ *
142
+ * The above copyright notice and this permission notice shall be included in all
143
+ * copies or substantial portions of the Software.
144
+ *
145
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
146
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
147
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
148
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
149
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
150
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
151
+ * SOFTWARE.
152
+ */
153
+ interface AtomicState<T extends Record<string, unknown>> {
154
+ get: () => T;
155
+ set: (value: T) => void;
156
+ update: (fn: (state: T) => T) => void;
157
+ }
158
+ declare const createAtomicState: <T extends Record<string, unknown>>(initial: T) => AtomicState<T>;
159
+
160
+ /**
161
+ * MIT License
162
+ *
163
+ * Copyright (c) 2025 Chris M. Perez
164
+ *
165
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
166
+ * of this software and associated documentation files (the "Software"), to deal
167
+ * in the Software without restriction, including without limitation the rights
168
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
169
+ * copies of the Software, and to permit persons to whom the Software is
170
+ * furnished to do so, subject to the following conditions:
171
+ *
172
+ * The above copyright notice and this permission notice shall be included in all
173
+ * copies or substantial portions of the Software.
174
+ *
175
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
176
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
178
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
179
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
180
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
181
+ * SOFTWARE.
182
+ */
183
+
184
+ declare const STORAGE_PREFIX = "effuse-store:";
185
+ declare const ROOT_SCOPE_ID = "__root__";
186
+ declare const SCOPE_PREFIX = "scope_";
187
+ declare const DEFAULT_TIMEOUT_MS = 5000;
188
+ declare const DEFAULT_RETRY_INITIAL_DELAY_MS = 100;
189
+ declare const DEFAULT_RETRY_MAX_DELAY_MS = 5000;
190
+ declare const DEFAULT_RETRY_BACKOFF_FACTOR = 2;
191
+ declare const StoreConstants: {
192
+ readonly STORAGE_PREFIX: "effuse-store:";
193
+ readonly ROOT_SCOPE_ID: "__root__";
194
+ readonly SCOPE_PREFIX: "scope_";
195
+ readonly DEBUG_PREFIX: "[store]";
196
+ readonly DEVTOOLS_PREFIX: "Effuse:";
197
+ readonly DEFAULT_TIMEOUT_MS: 5000;
198
+ };
199
+ interface StoreConfigValues {
200
+ persistByDefault: boolean;
201
+ storagePrefix: string;
202
+ debug: boolean;
203
+ devtools: boolean;
204
+ }
205
+ declare const getStoreConfig: () => StoreConfigValues;
206
+ declare const resetStoreConfigCache: () => void;
207
+
208
+ /**
209
+ * MIT License
210
+ *
211
+ * Copyright (c) 2025 Chris M. Perez
212
+ *
213
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
214
+ * of this software and associated documentation files (the "Software"), to deal
215
+ * in the Software without restriction, including without limitation the rights
216
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
217
+ * copies of the Software, and to permit persons to whom the Software is
218
+ * furnished to do so, subject to the following conditions:
219
+ *
220
+ * The above copyright notice and this permission notice shall be included in all
221
+ * copies or substantial portions of the Software.
222
+ *
223
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
224
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
225
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
226
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
227
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
228
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
229
+ * SOFTWARE.
230
+ */
231
+
232
+ type ScopeId = string;
233
+ interface ScopeNode {
234
+ id: ScopeId;
235
+ parent: ScopeNode | null;
236
+ stores: Map<string, Store<unknown>>;
237
+ }
238
+ declare const createScope: (parentScope?: ScopeNode) => ScopeNode;
239
+ declare const disposeScope: (scope: ScopeNode) => void;
240
+ declare const enterScope: (scope: ScopeNode) => void;
241
+ declare const exitScope: () => void;
242
+ declare const getCurrentScope: () => ScopeNode;
243
+ declare const getRootScope: () => ScopeNode;
244
+ declare const registerScopedStore: <T>(name: string, store: Store<T>, scope?: ScopeNode) => void;
245
+ declare const getScopedStore: <T>(name: string, scope?: ScopeNode) => Store<T> | null;
246
+ declare const hasScopedStore: (name: string, scope?: ScopeNode) => boolean;
247
+ declare const runInScope: <R>(scope: ScopeNode, fn: () => R) => R;
248
+ declare const withScope: <R>(fn: (scope: ScopeNode) => R) => R;
249
+
250
+ /**
251
+ * MIT License
252
+ *
253
+ * Copyright (c) 2025 Chris M. Perez
254
+ *
255
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
256
+ * of this software and associated documentation files (the "Software"), to deal
257
+ * in the Software without restriction, including without limitation the rights
258
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
259
+ * copies of the Software, and to permit persons to whom the Software is
260
+ * furnished to do so, subject to the following conditions:
261
+ *
262
+ * The above copyright notice and this permission notice shall be included in all
263
+ * copies or substantial portions of the Software.
264
+ *
265
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
266
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
267
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
268
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
269
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
270
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
271
+ * SOFTWARE.
272
+ */
273
+
274
+ interface ActionResult<T> {
275
+ data: T | null;
276
+ error: Error | null;
277
+ loading: boolean;
278
+ }
279
+ interface AsyncAction<A extends unknown[], R> {
280
+ (...args: A): Promise<R>;
281
+ pending: boolean;
282
+ }
283
+ interface CancellableAction<A extends unknown[], R> {
284
+ (...args: A): Promise<R>;
285
+ pending: boolean;
286
+ cancel: () => void;
287
+ }
288
+ type ActionFn<A extends unknown[], R> = (...args: A) => Promise<R> | R;
289
+ declare const createAsyncAction: <A extends unknown[], R>(fn: ActionFn<A, R>) => AsyncAction<A, R>;
290
+ declare const createCancellableAction: <A extends unknown[], R>(fn: ActionFn<A, R>) => CancellableAction<A, R>;
291
+ declare const withTimeout: <A extends unknown[], R>(fn: ActionFn<A, R>, timeoutMs: number) => ((...args: A) => Promise<R>);
292
+ interface RetryConfig {
293
+ maxRetries: number;
294
+ initialDelayMs?: number;
295
+ maxDelayMs?: number;
296
+ backoffFactor?: number;
297
+ }
298
+ declare const withRetry: <A extends unknown[], R>(fn: ActionFn<A, R>, config: RetryConfig) => ((...args: A) => Promise<R>);
299
+ declare const takeLatest: <A extends unknown[], R>(fn: ActionFn<A, R>) => CancellableAction<A, R>;
300
+ declare const takeFirst: <A extends unknown[], R>(fn: ActionFn<A, R>) => AsyncAction<A, R | undefined>;
301
+ declare const debounceAction: <A extends unknown[], R>(fn: ActionFn<A, R>, delayMs: number) => ((...args: A) => Promise<R>);
302
+ declare const throttleAction: <A extends unknown[], R>(fn: ActionFn<A, R>, intervalMs: number) => ((...args: A) => Promise<R | undefined>);
303
+ declare const dispatch: <T>(store: Store<T>, actionName: keyof T, ...args: unknown[]) => Promise<unknown>;
304
+ declare const dispatchSync: <T>(store: Store<T>, actionName: keyof T, ...args: unknown[]) => unknown;
305
+ declare const withAbortSignal: <A extends unknown[], R>(fn: ActionFn<A, R>) => ((signal: AbortSignal, ...args: A) => Promise<R>);
306
+
307
+ /**
308
+ * MIT License
309
+ *
310
+ * Copyright (c) 2025 Chris M. Perez
311
+ *
312
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
313
+ * of this software and associated documentation files (the "Software"), to deal
314
+ * in the Software without restriction, including without limitation the rights
315
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
316
+ * copies of the Software, and to permit persons to whom the Software is
317
+ * furnished to do so, subject to the following conditions:
318
+ *
319
+ * The above copyright notice and this permission notice shall be included in all
320
+ * copies or substantial portions of the Software.
321
+ *
322
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
323
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
324
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
325
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
326
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
327
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
328
+ * SOFTWARE.
329
+ */
330
+ declare const StoreNotFoundError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
331
+ readonly _tag: "StoreNotFoundError";
332
+ } & Readonly<A>;
333
+ declare class StoreNotFoundError extends StoreNotFoundError_base<{
334
+ readonly name: string;
335
+ }> {
336
+ get message(): string;
337
+ }
338
+ declare const StoreAlreadyExistsError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
339
+ readonly _tag: "StoreAlreadyExistsError";
340
+ } & Readonly<A>;
341
+ declare class StoreAlreadyExistsError extends StoreAlreadyExistsError_base<{
342
+ readonly name: string;
343
+ }> {
344
+ get message(): string;
345
+ }
346
+ declare const ActionNotFoundError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
347
+ readonly _tag: "ActionNotFoundError";
348
+ } & Readonly<A>;
349
+ declare class ActionNotFoundError extends ActionNotFoundError_base<{
350
+ readonly actionName: string;
351
+ }> {
352
+ }
353
+ declare const TimeoutError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
354
+ readonly _tag: "TimeoutError";
355
+ } & Readonly<A>;
356
+ declare class TimeoutError extends TimeoutError_base<{
357
+ readonly ms: number;
358
+ }> {
359
+ get message(): string;
360
+ }
361
+ declare const CancellationError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
362
+ readonly _tag: "CancellationError";
363
+ } & Readonly<A>;
364
+ declare class CancellationError extends CancellationError_base<{
365
+ readonly message: string;
366
+ }> {
367
+ }
368
+ declare const ValidationError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
369
+ readonly _tag: "ValidationError";
370
+ } & Readonly<A>;
371
+ declare class ValidationError extends ValidationError_base<{
372
+ readonly errors: string[];
373
+ }> {
374
+ get message(): string;
375
+ }
376
+
377
+ /**
378
+ * MIT License
379
+ *
380
+ * Copyright (c) 2025 Chris M. Perez
381
+ *
382
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
383
+ * of this software and associated documentation files (the "Software"), to deal
384
+ * in the Software without restriction, including without limitation the rights
385
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
386
+ * copies of the Software, and to permit persons to whom the Software is
387
+ * furnished to do so, subject to the following conditions:
388
+ *
389
+ * The above copyright notice and this permission notice shall be included in all
390
+ * copies or substantial portions of the Software.
391
+ *
392
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
393
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
394
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
395
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
396
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
397
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
398
+ * SOFTWARE.
399
+ */
400
+
401
+ interface CancellationToken {
402
+ readonly isCancelled: boolean;
403
+ cancel: () => void;
404
+ throwIfCancelled: () => void;
405
+ onCancel: (callback: () => void) => () => void;
406
+ }
407
+ declare const createCancellationToken: () => CancellationToken;
408
+ interface CancellationScope {
409
+ readonly token: CancellationToken;
410
+ createChild: () => CancellationToken;
411
+ dispose: () => void;
412
+ }
413
+ declare const createCancellationScope: () => CancellationScope;
414
+
415
+ /**
416
+ * MIT License
417
+ *
418
+ * Copyright (c) 2025 Chris M. Perez
419
+ *
420
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
421
+ * of this software and associated documentation files (the "Software"), to deal
422
+ * in the Software without restriction, including without limitation the rights
423
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
424
+ * copies of the Software, and to permit persons to whom the Software is
425
+ * furnished to do so, subject to the following conditions:
426
+ *
427
+ * The above copyright notice and this permission notice shall be included in all
428
+ * copies or substantial portions of the Software.
429
+ *
430
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
431
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
432
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
433
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
434
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
435
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
436
+ * SOFTWARE.
437
+ */
438
+
439
+ type StateSchema<T> = Schema.Schema<T, T>;
440
+ interface ValidationResult<T> {
441
+ success: boolean;
442
+ data: T | null;
443
+ errors: string[];
444
+ }
445
+ declare const validateState: <T>(schema: StateSchema<T>, state: unknown) => ValidationResult<T>;
446
+ declare const validateStateAsync: <T>(schema: StateSchema<T>, state: unknown, timeoutMs?: number) => Promise<ValidationResult<T>>;
447
+ declare const createValidatedSetter: <T extends Record<string, unknown>>(schema: StateSchema<T>, onValid: (state: T) => void, onInvalid?: (errors: string[]) => void) => ((state: unknown) => boolean);
448
+ declare const createFieldValidator: <T>(schema: Schema.Schema<T, T>) => ((value: unknown) => T);
449
+ declare const createSafeFieldSetter: <T>(schema: Schema.Schema<T, T>, setter: (value: T) => void) => ((value: unknown) => boolean);
450
+
451
+ /**
452
+ * MIT License
453
+ *
454
+ * Copyright (c) 2025 Chris M. Perez
455
+ *
456
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
457
+ * of this software and associated documentation files (the "Software"), to deal
458
+ * in the Software without restriction, including without limitation the rights
459
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
460
+ * copies of the Software, and to permit persons to whom the Software is
461
+ * furnished to do so, subject to the following conditions:
462
+ *
463
+ * The above copyright notice and this permission notice shall be included in all
464
+ * copies or substantial portions of the Software.
465
+ *
466
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
467
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
468
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
469
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
470
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
471
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
472
+ * SOFTWARE.
473
+ */
474
+
475
+ interface ComposedStore<T, D extends readonly Store<unknown>[]> {
476
+ store: Store<T>;
477
+ dependencies: D;
478
+ computed: <R>(selector: (state: T, deps: {
479
+ [K in keyof D]: ReturnType<D[K]['getSnapshot']>;
480
+ }) => R) => Signal<R>;
481
+ computedAsync: <R>(asyncSelector: (state: T, deps: {
482
+ [K in keyof D]: ReturnType<D[K]['getSnapshot']>;
483
+ }, token: CancellationToken) => Promise<R>, initialValue: R) => Signal<R> & {
484
+ pending: Signal<boolean>;
485
+ cleanup: () => void;
486
+ };
487
+ }
488
+ declare const composeStores: <T, D extends readonly Store<unknown>[]>(mainStore: Store<T>, dependencies: D) => ComposedStore<T, D>;
489
+ interface StoreSlice<T extends object, P extends object> {
490
+ create: (parent: Store<P>) => Store<T>;
491
+ }
492
+ declare const defineSlice: <T extends object, P extends object>(name: string, factory: (parent: Store<P>) => StoreDefinition<T>) => StoreSlice<T, P>;
493
+ declare const mergeStores: <A, B>(storeA: Store<A>, storeB: Store<B>) => {
494
+ getSnapshot: () => ReturnType<Store<A>["getSnapshot"]> & ReturnType<Store<B>["getSnapshot"]>;
495
+ subscribe: (callback: () => void) => () => void;
496
+ };
497
+
498
+ /**
499
+ * MIT License
500
+ *
501
+ * Copyright (c) 2025 Chris M. Perez
502
+ *
503
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
504
+ * of this software and associated documentation files (the "Software"), to deal
505
+ * in the Software without restriction, including without limitation the rights
506
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
507
+ * copies of the Software, and to permit persons to whom the Software is
508
+ * furnished to do so, subject to the following conditions:
509
+ *
510
+ * The above copyright notice and this permission notice shall be included in all
511
+ * copies or substantial portions of the Software.
512
+ *
513
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
514
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
515
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
516
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
517
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
518
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
519
+ * SOFTWARE.
520
+ */
521
+
522
+ declare const deriveFrom: <S extends Store<unknown>[], R>(stores: [...S], selector: (...snapshots: { [K in keyof S]: ReturnType<S[K]["getSnapshot"]>; }) => R) => Signal<R> & {
523
+ cleanup: () => void;
524
+ };
525
+ declare const deriveFromAsync: <S extends Store<unknown>[], R>(stores: [...S], asyncSelector: (snapshots: { [K in keyof S]: ReturnType<S[K]["getSnapshot"]>; }, token: CancellationToken) => Promise<R>, initialValue: R) => Signal<R> & {
526
+ cleanup: () => void;
527
+ pending: Signal<boolean>;
528
+ };
529
+ declare const serializeStores: () => string;
530
+ declare const hydrateStoresSync: (serialized: string) => void;
531
+
532
+ /**
533
+ * MIT License
534
+ *
535
+ * Copyright (c) 2025 Chris M. Perez
536
+ *
537
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
538
+ * of this software and associated documentation files (the "Software"), to deal
539
+ * in the Software without restriction, including without limitation the rights
540
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
541
+ * copies of the Software, and to permit persons to whom the Software is
542
+ * furnished to do so, subject to the following conditions:
543
+ *
544
+ * The above copyright notice and this permission notice shall be included in all
545
+ * copies or substantial portions of the Software.
546
+ *
547
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
548
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
549
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
550
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
551
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
552
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
553
+ * SOFTWARE.
554
+ */
555
+
556
+ interface StoreStream<T> {
557
+ subscribe: (handler: (value: T) => void) => () => void;
558
+ map: <R>(fn: (value: T) => R) => StoreStream<R>;
559
+ filter: (predicate: (value: T) => boolean) => StoreStream<T>;
560
+ debounce: (ms: number) => StoreStream<T>;
561
+ throttle: (ms: number) => StoreStream<T>;
562
+ takeLatest: <R>(asyncHandler: (value: T, token: CancellationToken) => Promise<R>) => StoreStream<R>;
563
+ }
564
+ declare const createStoreStream: <T, K extends keyof T>(store: Store<T>, key: K) => StoreStream<T[K]>;
565
+ declare const streamAll: <T>(store: Store<T>) => StoreStream<ReturnType<Store<T>["getSnapshot"]>>;
566
+
567
+ /**
568
+ * MIT License
569
+ *
570
+ * Copyright (c) 2025 Chris M. Perez
571
+ *
572
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
573
+ * of this software and associated documentation files (the "Software"), to deal
574
+ * in the Software without restriction, including without limitation the rights
575
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
576
+ * copies of the Software, and to permit persons to whom the Software is
577
+ * furnished to do so, subject to the following conditions:
578
+ *
579
+ * The above copyright notice and this permission notice shall be included in all
580
+ * copies or substantial portions of the Software.
581
+ *
582
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
583
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
584
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
585
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
586
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
587
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
588
+ * SOFTWARE.
589
+ */
590
+
591
+ declare const shallowEqual: <T>(a: T, b: T) => boolean;
592
+ type Selector<T, R> = (state: T) => R;
593
+ type EqualityFn<T> = (a: T, b: T) => boolean;
594
+ declare const createSelector: <T, R>(store: Store<T>, selector: Selector<ReturnType<Store<T>["getSnapshot"]>, R>, equalityFn?: EqualityFn<R>) => Signal<R>;
595
+ type AsyncSelector<T, R> = (state: T, token: CancellationToken) => Promise<R>;
596
+ declare const createSelectorAsync: <T, R>(store: Store<T>, asyncSelector: AsyncSelector<ReturnType<Store<T>["getSnapshot"]>, R>, initialValue: R) => Signal<R> & {
597
+ pending: Signal<boolean>;
598
+ cleanup: () => void;
599
+ };
600
+ declare const pick: <T, K extends keyof T>(store: Store<T>, keys: K[]) => Signal<Pick<T, K>>;
601
+ declare const combineSelectors: <T, R extends Record<string, unknown>>(store: Store<T>, selectors: { [K in keyof R]: Selector<ReturnType<Store<T>["getSnapshot"]>, R[K]>; }) => Signal<R>;
602
+
603
+ /**
604
+ * MIT License
605
+ *
606
+ * Copyright (c) 2025 Chris M. Perez
607
+ *
608
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
609
+ * of this software and associated documentation files (the "Software"), to deal
610
+ * in the Software without restriction, including without limitation the rights
611
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
612
+ * copies of the Software, and to permit persons to whom the Software is
613
+ * furnished to do so, subject to the following conditions:
614
+ *
615
+ * The above copyright notice and this permission notice shall be included in all
616
+ * copies or substantial portions of the Software.
617
+ *
618
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
619
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
620
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
621
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
622
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
623
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
624
+ * SOFTWARE.
625
+ */
626
+
627
+ interface MiddlewareManager<T extends Record<string, unknown>> {
628
+ add: (middleware: Middleware<T>) => () => void;
629
+ remove: (middleware: Middleware<T>) => void;
630
+ execute: (state: T, action: string, args: unknown[]) => T;
631
+ getAll: () => readonly Middleware<T>[];
632
+ }
633
+ declare const createMiddlewareManager: <T extends Record<string, unknown>>() => MiddlewareManager<T>;
634
+
635
+ /**
636
+ * MIT License
637
+ *
638
+ * Copyright (c) 2025 Chris M. Perez
639
+ *
640
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
641
+ * of this software and associated documentation files (the "Software"), to deal
642
+ * in the Software without restriction, including without limitation the rights
643
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
644
+ * copies of the Software, and to permit persons to whom the Software is
645
+ * furnished to do so, subject to the following conditions:
646
+ *
647
+ * The above copyright notice and this permission notice shall be included in all
648
+ * copies or substantial portions of the Software.
649
+ *
650
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
651
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
652
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
653
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
654
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
655
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
656
+ * SOFTWARE.
657
+ */
658
+
659
+ declare const hasDevTools: () => boolean;
660
+ declare const connectDevTools: <T>(store: Store<T>, options?: {
661
+ name?: string;
662
+ }) => (() => void);
663
+ declare const devToolsMiddleware: <T>(storeName: string) => (state: T, action: string, args: unknown[]) => T | undefined;
664
+ declare const createDevToolsMiddleware: <T>(storeName: string) => (state: T, action: string, args: unknown[]) => T | undefined;
665
+ declare const disconnectDevTools: (storeName: string) => void;
666
+ declare const disconnectAllDevTools: () => void;
667
+
668
+ /**
669
+ * MIT License
670
+ *
671
+ * Copyright (c) 2025 Chris M. Perez
672
+ *
673
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
674
+ * of this software and associated documentation files (the "Software"), to deal
675
+ * in the Software without restriction, including without limitation the rights
676
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
677
+ * copies of the Software, and to permit persons to whom the Software is
678
+ * furnished to do so, subject to the following conditions:
679
+ *
680
+ * The above copyright notice and this permission notice shall be included in all
681
+ * copies or substantial portions of the Software.
682
+ *
683
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
684
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
685
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
686
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
687
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
688
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
689
+ * SOFTWARE.
690
+ */
691
+
692
+ declare const getStore: <T>(name: string) => Store<T>;
693
+ declare const hasStore: (name: string) => boolean;
694
+ declare const removeStore: (name: string) => boolean;
695
+ declare const clearStores: () => void;
696
+ declare const getStoreNames: () => string[];
697
+
698
+ export { type ActionContext, ActionNotFoundError, type ActionResult, type AsyncAction, type AsyncSelector, type AtomicState, type CancellableAction, CancellationError, type CancellationScope, type CancellationToken, type ComposedStore, type CreateStoreOptions, DEFAULT_RETRY_BACKOFF_FACTOR, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_MAX_DELAY_MS, DEFAULT_TIMEOUT_MS, type EqualityFn, type InferStoreState, type Middleware, type MiddlewareManager, ROOT_SCOPE_ID, type RetryConfig, SCOPE_PREFIX, STORAGE_PREFIX, type ScopeId, type ScopeNode, type Selector, type StateSchema, type Store, StoreAlreadyExistsError, type StoreConfigValues, StoreConstants, type StoreContext, type StoreDefinition, StoreNotFoundError, type StoreOptions, type StoreSlice, type StoreState, type StoreStream, TimeoutError, ValidationError, type ValidationResult, clearStores, combineSelectors, composeStores, connectDevTools, createAsyncAction, createAtomicState, createCancellableAction, createCancellationScope, createCancellationToken, createDevToolsMiddleware, createFieldValidator, createMiddlewareManager, createSafeFieldSetter, createScope, createSelector, createSelectorAsync, createStore, createStoreStream, createValidatedSetter, debounceAction, defineSlice, deriveFrom, deriveFromAsync, devToolsMiddleware, disconnectAllDevTools, disconnectDevTools, dispatch, dispatchSync, disposeScope, enterScope, exitScope, getCurrentScope, getRootScope, getScopedStore, getStore, getStoreConfig, getStoreNames, hasDevTools, hasScopedStore, hasStore, hydrateStoresSync, mergeStores, pick, registerScopedStore, removeStore, resetStoreConfigCache, runInScope, serializeStores, shallowEqual, streamAll, takeFirst, takeLatest, throttleAction, validateState, validateStateAsync, withAbortSignal, withRetry, withScope, withTimeout };