@superutils/store 0.1.15 → 0.1.17
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 +49 -10
- package/dist/browser/index.min.js +2 -2
- package/dist/browser/index.min.js.map +1 -1
- package/dist/index.cjs +62 -22
- package/dist/index.d.cts +261 -121
- package/dist/index.d.ts +261 -121
- package/dist/index.js +65 -25
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -2,9 +2,58 @@ import { Subject, BehaviorSubject } from 'rxjs';
|
|
|
2
2
|
import { ThrottleOptions, DebounceOptions, sort, SortOptions, DropFirst, search, ValueOrPromise, filter, FindOptions, find, TypedMap } from '@superutils/core';
|
|
3
3
|
export { TypedMap, objToMap } from '@superutils/core';
|
|
4
4
|
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Literal union of all operations that can be intercepted by a validator.
|
|
7
|
+
*/
|
|
8
|
+
type Store_ValidateAction = 'clear' | 'delete' | 'set' | 'setAll' | 'write';
|
|
9
|
+
/**
|
|
10
|
+
* Function signature for an operation-specific validation hook.
|
|
11
|
+
*
|
|
12
|
+
* **Behavior:**
|
|
13
|
+
* - Invoked immediately before the store's internal state is updated.
|
|
14
|
+
* - If validation fails (throw error), the write operation is aborted and error is propagated to the caller.
|
|
15
|
+
*
|
|
16
|
+
* @param params - The specific payload for the action:
|
|
17
|
+
* - `clear`: `[]`
|
|
18
|
+
* - `delete`: `[keys: Key[]]`
|
|
19
|
+
* - `set`: `[key: Key, value: Value]`
|
|
20
|
+
* - `setAll`: `[data: Map<Key, Value>, replace: boolean]`
|
|
21
|
+
* - 'write': `[data: Map<Key, Value>]`
|
|
22
|
+
* @param action - The literal name of the action being performed.
|
|
23
|
+
*
|
|
24
|
+
* @template Key - The type of keys in the store.
|
|
25
|
+
* @template Value - The type of values in the store.
|
|
26
|
+
* @template CacheDisabled - Whether caching is disabled.
|
|
27
|
+
*/
|
|
28
|
+
type Store_ValidateFn<Key, Value, CacheDisabled extends boolean, Action extends keyof IStore<Key, Value> & Store_ValidateAction> = (this: IStore<Key, Value, CacheDisabled>, params: 'clear' extends Action ? [] : 'delete' extends Action ? [keys: Key[]] : 'set' extends Action ? [key: Key, value: Value] : 'setAll' extends Action ? [data?: Map<Key, Value>, replace?: boolean] : 'write' extends Action ? [data: Map<Key, Value>] : [], action: Action) => void | undefined | boolean;
|
|
29
|
+
/**
|
|
30
|
+
* A configuration object containing optional validation hooks for specific store operations.
|
|
31
|
+
*
|
|
32
|
+
* This structure allows for granular control over write operations, enabling you
|
|
33
|
+
* to define custom logic to intercept and prevent invalid state updates.
|
|
34
|
+
*
|
|
35
|
+
* @template Key - The type of keys in the store.
|
|
36
|
+
* @template Value - The type of values in the store.
|
|
37
|
+
* @template CacheDisabled - Whether caching is disabled for the store.
|
|
38
|
+
*/
|
|
39
|
+
type Store_Validate<Key, Value, CacheDisabled extends boolean> = {
|
|
40
|
+
clear?: Store_ValidateFn<Key, Value, CacheDisabled, 'clear'>;
|
|
41
|
+
delete?: Store_ValidateFn<Key, Value, CacheDisabled, 'delete'>;
|
|
42
|
+
set?: Store_ValidateFn<Key, Value, CacheDisabled, 'set'>;
|
|
43
|
+
setAll?: Store_ValidateFn<Key, Value, CacheDisabled, 'setAll'>;
|
|
44
|
+
write?: Store_ValidateFn<Key, Value, CacheDisabled, 'write'>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Represents the minimal subset of the `Storage` interface required for persistence.
|
|
49
|
+
*
|
|
50
|
+
* Any object implementing this type can be used as a storage engine, making the Store
|
|
51
|
+
* compatible with `localStorage`, `sessionStorage`, or custom file-based implementations.
|
|
52
|
+
*/
|
|
6
53
|
type StorageCompact = Pick<Storage, 'getItem' | 'setItem'>;
|
|
7
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* Configuration for the timing strategy used when writing data to persistent storage.
|
|
56
|
+
*/
|
|
8
57
|
type Store_DelayOptions = ({
|
|
9
58
|
throttle: true;
|
|
10
59
|
} & Omit<ThrottleOptions, 'onError' | 'thisArg'>) | ({
|
|
@@ -36,7 +85,11 @@ declare enum Store_OnErrorType {
|
|
|
36
85
|
/** Occurs when the attempt to save data to the underlying storage (e.g., `localStorage.setItem`) fails. */
|
|
37
86
|
write = "write"
|
|
38
87
|
}
|
|
39
|
-
/**
|
|
88
|
+
/**
|
|
89
|
+
* Configuration options for initializing a {@link Store} or {@link ObjectStore}.
|
|
90
|
+
*
|
|
91
|
+
* These options define the behavior of caching, persistence, error handling, and validation.
|
|
92
|
+
*/
|
|
40
93
|
type Store_Options<Key, Value, CacheDisabled extends boolean = false> = {
|
|
41
94
|
/**
|
|
42
95
|
* An optional `Map` used to seed the storage if no persistent data is found for the instance.
|
|
@@ -56,25 +109,35 @@ type Store_Options<Key, Value, CacheDisabled extends boolean = false> = {
|
|
|
56
109
|
* Default: `undefined`
|
|
57
110
|
*/
|
|
58
111
|
initialValue?: Map<Key, Value>;
|
|
59
|
-
} & Pick<Partial<IStore<Key, Value, CacheDisabled>>, 'cacheDisabled' | 'onChange' | 'onError' | 'parse' | 'spaces' | 'storage' | 'stringify'> & {
|
|
60
|
-
/**
|
|
61
|
-
* If `true`, storage validation is delayed until initialization.
|
|
62
|
-
*
|
|
63
|
-
* By default, if a `name` is provided, the constructor throws an error if no valid
|
|
64
|
-
* storage mechanism (like `localStorage`) is found. Enabling this allows the
|
|
65
|
-
* instance to be created even if storage is temporarily missing, throwing the error
|
|
66
|
-
* only when `init()` is called - manually, on first read/write, or automatically
|
|
67
|
-
* when non-empty `initialValue` is provided..
|
|
68
|
-
*
|
|
69
|
-
* Default: `false`
|
|
70
|
-
*/
|
|
71
|
-
checkStorageOnInit?: boolean;
|
|
72
|
-
} & (CacheDisabled extends false ? Pick<Partial<IStore<Key, Value, CacheDisabled>>, 'delay' | 'delayOptions'> : {
|
|
112
|
+
} & Pick<Partial<IStore<Key, Value, CacheDisabled>>, 'cacheDisabled' | 'onChange' | 'onError' | 'parse' | 'spaces' | 'storage' | 'stringify' | 'validate'> & (CacheDisabled extends false ? Pick<Partial<IStore<Key, Value, CacheDisabled>>, 'delay' | 'delayOptions'> : {
|
|
73
113
|
delay?: never;
|
|
74
114
|
delayOptions?: never;
|
|
75
115
|
});
|
|
116
|
+
/**
|
|
117
|
+
* Function signature for custom data deserialization.
|
|
118
|
+
*
|
|
119
|
+
* @param text The raw string retrieved from the underlying storage.
|
|
120
|
+
* @returns The parsed data structure (usually a Map) or `void` to trigger fallback behavior.
|
|
121
|
+
*/
|
|
76
122
|
type Store_Parse<ResultMap, ThisArg> = (this: ThisArg, text: string | null | undefined) => ResultMap | void;
|
|
123
|
+
/**
|
|
124
|
+
* Function signature for the internal search utility.
|
|
125
|
+
*
|
|
126
|
+
* @template K - The type of keys.
|
|
127
|
+
* @template V - The type of values.
|
|
128
|
+
* @template MatchExact - Whether to perform exact matching.
|
|
129
|
+
* @template AsMap - Whether to return results as a Map.
|
|
130
|
+
*/
|
|
77
131
|
type Store_Search<K, V, MatchExact extends boolean = false, AsMap extends boolean = true> = (...args: DropFirst<Parameters<typeof search<K, V, MatchExact, AsMap>>>) => ReturnType<typeof search<K, V, MatchExact, AsMap>>;
|
|
132
|
+
/**
|
|
133
|
+
* Function Signature for the sort utility, supporting multiple sorting strategies:
|
|
134
|
+
* 1. By a custom comparator function.
|
|
135
|
+
* 2. By a specific property name (for object-like values).
|
|
136
|
+
* 3. By the map keys.
|
|
137
|
+
*
|
|
138
|
+
* @template K - The type of keys.
|
|
139
|
+
* @template V - The type of values.
|
|
140
|
+
*/
|
|
78
141
|
type Store_Sort<K, V> = (...args: Store_SortByComparator<K, V> | Store_SortByPropertyName<V> | Store_SortByKey) => Map<K, V>;
|
|
79
142
|
type Store_SortByComparator<K, V> = [
|
|
80
143
|
comparator: Parameters<typeof sort<K, V>>[1],
|
|
@@ -85,10 +148,27 @@ type Store_SortByPropertyName<V> = [
|
|
|
85
148
|
propertyName: keyof V & string,
|
|
86
149
|
options?: Store_SortOptions
|
|
87
150
|
];
|
|
151
|
+
/** Configuration options for sorting, including whether to persist the result. */
|
|
88
152
|
type Store_SortOptions = SortOptions & {
|
|
89
153
|
save?: boolean;
|
|
90
154
|
};
|
|
155
|
+
/**
|
|
156
|
+
* Function signature for custom data serialization.
|
|
157
|
+
*
|
|
158
|
+
* @param data The current data structure to be serialized.
|
|
159
|
+
* @returns A string representation of the data, or `void/undefined` to trigger fallback behavior.
|
|
160
|
+
*/
|
|
91
161
|
type Store_Stringify<Data, ThisArg> = (this: ThisArg, data: Data) => string | undefined | void;
|
|
162
|
+
/**
|
|
163
|
+
* Function signature for exporting the store content as a JSON string.
|
|
164
|
+
*
|
|
165
|
+
* @param replacer - A function or array that transforms the results.
|
|
166
|
+
* @param spacing - Indentation or whitespace formatting.
|
|
167
|
+
* @param data - The specific Map to stringify (defaults to all entries).
|
|
168
|
+
*
|
|
169
|
+
* @template K - The type of keys.
|
|
170
|
+
* @template V - The type of values.
|
|
171
|
+
*/
|
|
92
172
|
type Store_ToJSON<K, V> = (replacer?: null | ((key: K, value: V) => unknown), spacing?: string | number, data?: Map<K, V>) => string;
|
|
93
173
|
|
|
94
174
|
/**
|
|
@@ -130,12 +210,6 @@ interface IStore<Key, Value, CacheDisabled extends boolean = false> {
|
|
|
130
210
|
* Default: `null`
|
|
131
211
|
*/
|
|
132
212
|
readonly name?: string | null;
|
|
133
|
-
/**
|
|
134
|
-
* Indicates type of data parsed as
|
|
135
|
-
*
|
|
136
|
-
* Default: 'map'
|
|
137
|
-
*/
|
|
138
|
-
type: string;
|
|
139
213
|
/**
|
|
140
214
|
* A callback function executed whenever a data change occurs within the storage.
|
|
141
215
|
*
|
|
@@ -186,14 +260,19 @@ interface IStore<Key, Value, CacheDisabled extends boolean = false> {
|
|
|
186
260
|
* Notes:
|
|
187
261
|
* - Ignored when `name` is falsy (in-memory only mode)
|
|
188
262
|
* - For NodeJS or equivalent, an instance of `LocalStorage` from "node-localstoarge" NPM module can be used.
|
|
189
|
-
* - If `undefined`, will attempt to use `globalThis.localStorage`, if available
|
|
190
263
|
* - A custom storage that implements {@link StorageCompact} interface can also be used both in browser and NodeJS.
|
|
191
264
|
*
|
|
265
|
+
* Fallback behavior:
|
|
266
|
+
* - `undefined`: will attempt to use `globalThis.localStorage`, if available
|
|
267
|
+
* - `null`, will defer storage check until initialization on first read/write or when `init()` invoked manually.
|
|
268
|
+
* - If storage is `undefined` or `null` will attempt to assign `globalTihs.localStorage` again
|
|
269
|
+
* - If storage is still falsy, will throw an error.
|
|
270
|
+
*
|
|
192
271
|
* Default:
|
|
193
272
|
* - browser: `localStorage`
|
|
194
273
|
* - node: `undefined` (in-memory mode)
|
|
195
274
|
*/
|
|
196
|
-
readonly storage?: StorageCompact | Storage;
|
|
275
|
+
readonly storage?: StorageCompact | Storage | null;
|
|
197
276
|
/**
|
|
198
277
|
* A callback function to customize the serialization of data before it is written to storage.
|
|
199
278
|
*
|
|
@@ -234,6 +313,13 @@ interface IStore<Key, Value, CacheDisabled extends boolean = false> {
|
|
|
234
313
|
* ```
|
|
235
314
|
*/
|
|
236
315
|
stringify?: Store_Stringify<Map<Key, Value>, IStore<Key, Value, CacheDisabled>>;
|
|
316
|
+
/**
|
|
317
|
+
* Indicates type of data parsed as
|
|
318
|
+
*
|
|
319
|
+
* Default: 'map'
|
|
320
|
+
*/
|
|
321
|
+
type: string;
|
|
322
|
+
validate?: Store_Validate<Key, Value, CacheDisabled>;
|
|
237
323
|
/**
|
|
238
324
|
* The underlying RxJS Subject that serves as the primary reactive interface for observing data modifications.
|
|
239
325
|
*
|
|
@@ -403,6 +489,24 @@ interface IObjectStore<T extends object, CacheDisabled extends boolean = false,
|
|
|
403
489
|
stringify?: Store_Stringify<ObjectMap, IObjectStore<T, CacheDisabled>>;
|
|
404
490
|
toObject<O extends object = T>(data?: Map<keyof T, T[keyof T]>): O;
|
|
405
491
|
}
|
|
492
|
+
/**
|
|
493
|
+
* Defines the shape of the context object that can be attached to {@link IObjectStore} instance.
|
|
494
|
+
*
|
|
495
|
+
* A context can be:
|
|
496
|
+
* - A plain object containing utility methods or properties.
|
|
497
|
+
* - A factory function that receives the {@link IObjectStore} instance and returns an object.
|
|
498
|
+
* This is useful for creating methods that need to interact with the store's data
|
|
499
|
+
* using the store instance itself.
|
|
500
|
+
*
|
|
501
|
+
* @template Key - The type of keys in the store.
|
|
502
|
+
* @template Value - The type of values in the store.
|
|
503
|
+
* @template CacheDisabled - Whether caching is disabled for this store.
|
|
504
|
+
*/
|
|
505
|
+
type ObjectStore_Context<T extends object, CacheDisabled extends boolean = false> = object | ((store: IObjectStore<T, CacheDisabled>) => object);
|
|
506
|
+
type ObjectStore_Options<T extends object, CacheDisabled extends boolean, Context extends ObjectStore_Context<T, CacheDisabled>> = Omit<Store_Options<keyof T, T[keyof T], CacheDisabled>, 'initialValue'> & {
|
|
507
|
+
context?: Context;
|
|
508
|
+
initialValue?: T;
|
|
509
|
+
};
|
|
406
510
|
|
|
407
511
|
/**
|
|
408
512
|
* RxJS Subject to trigger forced update of cached data from underlying storage of {@link Store} instances.
|
|
@@ -598,13 +702,15 @@ This extends IStore<Key, Value, CacheDisabled> = IStore<Key, Value, CacheDisable
|
|
|
598
702
|
stringify?: This['stringify'];
|
|
599
703
|
readonly subject$: This['subject$'];
|
|
600
704
|
private subscriptions;
|
|
601
|
-
type:
|
|
705
|
+
type: This['type'];
|
|
706
|
+
validate?: This['validate'];
|
|
602
707
|
constructor(name?: This['name'], options?: Store_Options<Key, Value, CacheDisabled>);
|
|
603
708
|
clear: This['clear'];
|
|
604
709
|
delete: This['delete'];
|
|
605
710
|
static messages: {
|
|
606
711
|
invalidJsonEntries: string;
|
|
607
|
-
|
|
712
|
+
invalidStorageOptions: string;
|
|
713
|
+
validationError: string;
|
|
608
714
|
};
|
|
609
715
|
filter: This['filter'];
|
|
610
716
|
find: This['find'];
|
|
@@ -646,7 +752,7 @@ This extends IStore<Key, Value, CacheDisabled> = IStore<Key, Value, CacheDisable
|
|
|
646
752
|
*
|
|
647
753
|
* A context can be:
|
|
648
754
|
* - A plain object containing utility methods or properties.
|
|
649
|
-
* - A factory function that receives the {@link
|
|
755
|
+
* - A factory function that receives the {@link IStore} instance and returns an object.
|
|
650
756
|
* This is useful for creating methods that need to interact with the store's data
|
|
651
757
|
* using the store instance itself.
|
|
652
758
|
*
|
|
@@ -658,95 +764,9 @@ type Store_Context<Key, Value, CacheDisabled extends boolean = false> = undefine
|
|
|
658
764
|
type Store_ContextReturn<T> = {
|
|
659
765
|
context: undefined extends T ? never : T extends (...args: any[]) => infer R ? R : T;
|
|
660
766
|
};
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
*
|
|
664
|
-
* This function provides a convenient way to instantiate a store and attach supplemental
|
|
665
|
-
* logic (context) to it. It supports full type inference for both the store's data
|
|
666
|
-
* and the attached context.
|
|
667
|
-
*
|
|
668
|
-
* @param name - The name of the storage (e.g., localStorage key). If null/undefined, the store remains in-memory.
|
|
669
|
-
* @param options - Configuration options for the store, including the optional `context`.
|
|
670
|
-
*
|
|
671
|
-
* @template Context - The type of the context object or factory function.
|
|
672
|
-
* @template Key - The type of keys stored in the map.
|
|
673
|
-
* @template Value - The type of values stored in the map.
|
|
674
|
-
* @template CacheDisabled - Literal type determining whether to disable in-memory caching.
|
|
675
|
-
*
|
|
676
|
-
* @returns A {@link Store} instance augmented with a `context` property.
|
|
677
|
-
*
|
|
678
|
-
* @example
|
|
679
|
-
* #### Basic store without context
|
|
680
|
-
* ```javascript
|
|
681
|
-
* import { createStore } from '@superutils/store'
|
|
682
|
-
*
|
|
683
|
-
* const store = createStore<string, number>()
|
|
684
|
-
* store.set('count', 1)
|
|
685
|
-
* store.set('count', prevCount => prevCount + 1)
|
|
686
|
-
* ```
|
|
687
|
-
*
|
|
688
|
-
* @example
|
|
689
|
-
* #### Store with a static context object
|
|
690
|
-
* ```javascript
|
|
691
|
-
* import { createStore } from '@superutils/store'
|
|
692
|
-
*
|
|
693
|
-
* const store = createStore('user-settings', {
|
|
694
|
-
* context: {
|
|
695
|
-
* count: 0,
|
|
696
|
-
* log(msg) {
|
|
697
|
-
* console.log(`[${++this.count}] ${msg}`)
|
|
698
|
-
* }
|
|
699
|
-
* }
|
|
700
|
-
* })
|
|
701
|
-
*
|
|
702
|
-
* store.context.log('Setting updated')
|
|
703
|
-
* console.log(store.context.count)
|
|
704
|
-
* ```
|
|
705
|
-
*
|
|
706
|
-
* @example
|
|
707
|
-
* #### Store with a functional context (access to store instance)
|
|
708
|
-
* ```typescript
|
|
709
|
-
* import { createStore } from '@superutils/store'
|
|
710
|
-
*
|
|
711
|
-
* const authStore = createStore('auth', {
|
|
712
|
-
* context: (store) => ({
|
|
713
|
-
* isAuthenticated: () => store.has('token'),
|
|
714
|
-
* logout: () => store.delete('token')
|
|
715
|
-
* })
|
|
716
|
-
* })
|
|
717
|
-
*
|
|
718
|
-
* if (authStore.context.isAuthenticated()) {
|
|
719
|
-
* authStore.context.logout()
|
|
720
|
-
* }
|
|
721
|
-
* ```
|
|
722
|
-
*/
|
|
723
|
-
declare function createStore<Context extends Store_Context<Key, Value, CacheDisabled>, Key, Value, CacheDisabled extends boolean = false>(name?: ConstructorParameters<typeof Store<Key, Value, CacheDisabled>>[0], options?: Store_Options<Key, Value, CacheDisabled> & {
|
|
724
|
-
context?: Context;
|
|
725
|
-
}): IStore<Key, Value, CacheDisabled> & Store_ContextReturn<Context>;
|
|
726
|
-
/**
|
|
727
|
-
* Factory method to create a {@link Store} instance.
|
|
728
|
-
*
|
|
729
|
-
* @param args - Arguments passed directly to the {@link Store} constructor.
|
|
730
|
-
* @template Key - The type of keys stored in the map.
|
|
731
|
-
* @template Value - The type of values stored in the map.
|
|
732
|
-
* @template CacheDisabled - Whether to disable in-memory caching.
|
|
733
|
-
*/
|
|
734
|
-
declare function createStore<Key, Value, CacheDisabled extends boolean = false>(...args: ConstructorParameters<typeof Store<Key, Value, CacheDisabled>>): Store<Key, Value, CacheDisabled>;
|
|
767
|
+
type IStoreWithContext<Key, Value, CacheDisabled extends boolean, Context extends Store_Context<Key, Value, CacheDisabled>> = IStore<Key, Value, CacheDisabled> & Store_ContextReturn<Context>;
|
|
768
|
+
type IObjectStoreWithContext<T extends object, CacheDisabled extends boolean, Context extends Store_Context<keyof T, T[keyof T], CacheDisabled>> = IObjectStore<T, CacheDisabled> & Store_ContextReturn<Context>;
|
|
735
769
|
|
|
736
|
-
/**
|
|
737
|
-
* Defines the shape of the context object that can be attached to {@link IObjectStore} instance.
|
|
738
|
-
*
|
|
739
|
-
* A context can be:
|
|
740
|
-
* - A plain object containing utility methods or properties.
|
|
741
|
-
* - A factory function that receives the {@link Store} instance and returns an object.
|
|
742
|
-
* This is useful for creating methods that need to interact with the store's data
|
|
743
|
-
* using the store instance itself.
|
|
744
|
-
*
|
|
745
|
-
* @template Key - The type of keys in the store.
|
|
746
|
-
* @template Value - The type of values in the store.
|
|
747
|
-
* @template CacheDisabled - Whether caching is disabled for this store.
|
|
748
|
-
*/
|
|
749
|
-
type ObjectStore_Context<T extends object, CacheDisabled extends boolean = false> = object | ((store: IObjectStore<T, CacheDisabled>) => object);
|
|
750
770
|
/**
|
|
751
771
|
* Creates a {@link Store} instance initialized from a plain object.
|
|
752
772
|
*
|
|
@@ -759,6 +779,16 @@ type ObjectStore_Context<T extends object, CacheDisabled extends boolean = false
|
|
|
759
779
|
* @param name (optional) The name for the storage (e.g., localStorage key or filename).
|
|
760
780
|
* @param options (optional) Configuration options for the storage instance.
|
|
761
781
|
* @param options.initialValue (optional) An optional object to populate the storage if it's currently empty.
|
|
782
|
+
* @param options.context - (optional) A plain object or a factory function that returns an object.
|
|
783
|
+
*
|
|
784
|
+
* **Purpose:**
|
|
785
|
+
* Use `context` to encapsulate domain-specific business logic, helper methods, or non-reactive state
|
|
786
|
+
* directly alongside the store instance.
|
|
787
|
+
*
|
|
788
|
+
* **Behavior:**
|
|
789
|
+
* - **Non-Reactive:** Updates to context properties do **not** trigger `onChange` or RxJS emissions.
|
|
790
|
+
* - **Non-Persistent:** Context data is purely in-memory and is **not** saved to persistent storage.
|
|
791
|
+
* - **Access to Store:** When a factory function is used, it receives the store instance as an argument.
|
|
762
792
|
*
|
|
763
793
|
* @template T (optional) The structure of the object being stored. Can auto-infer from `options.initialValue`.
|
|
764
794
|
* @template CacheDisabled (optional) Literal type determining whether to disable in-memory caching.
|
|
@@ -820,13 +850,123 @@ type ObjectStore_Context<T extends object, CacheDisabled extends boolean = false
|
|
|
820
850
|
* ```
|
|
821
851
|
*
|
|
822
852
|
*/
|
|
823
|
-
declare function createObjectStore<T extends object = Record<string, unknown>,
|
|
824
|
-
|
|
825
|
-
initialValue?: T;
|
|
826
|
-
}): IObjectStore<T, CacheDisabled> & Store_ContextReturn<Context>;
|
|
853
|
+
declare function createObjectStore<T extends object = Record<string, unknown>, CacheDisabled extends boolean = false, Context extends ObjectStore_Context<T, CacheDisabled> = ObjectStore_Context<T, CacheDisabled>>(name?: string | null, options?: ObjectStore_Options<T, CacheDisabled, Context>): IObjectStoreWithContext<T, CacheDisabled, Context>;
|
|
854
|
+
declare function createObjectStore<T extends object = Record<string, unknown>, CacheDisabled extends boolean = false, Context extends ObjectStore_Context<T, CacheDisabled> = ObjectStore_Context<T, CacheDisabled>>(options: ObjectStore_Options<T, CacheDisabled, Context>): IObjectStoreWithContext<T, CacheDisabled, Context>;
|
|
827
855
|
/**
|
|
828
856
|
* Create a {@link Store} instance from an object using `options.initialValue`.
|
|
829
857
|
*/
|
|
830
858
|
declare function createObjectStore<T extends object = Record<string, unknown>, Key extends keyof T = keyof T, Value extends T[Key] = T[Key], CacheDisabled extends boolean = false>(...args: ConstructorParameters<typeof Store<Key, Value, CacheDisabled>>): IObjectStore<T, CacheDisabled>;
|
|
831
859
|
|
|
832
|
-
|
|
860
|
+
/**
|
|
861
|
+
* Factory function to create a {@link Store} instance with optional context.
|
|
862
|
+
*
|
|
863
|
+
* This function provides a convenient way to instantiate a store and attach supplemental
|
|
864
|
+
* logic (context) to it. It supports full type inference for both the store's data
|
|
865
|
+
* and the attached context.
|
|
866
|
+
*
|
|
867
|
+
* @param name - The name of the storage (e.g., localStorage key). If null/undefined, the store remains in-memory.
|
|
868
|
+
* @param options - Configuration options for the store
|
|
869
|
+
* @param options.context - (optional) A plain object or a factory function that returns an object.
|
|
870
|
+
*
|
|
871
|
+
* **Purpose:**
|
|
872
|
+
* Use `context` to encapsulate domain-specific business logic, helper methods, or non-reactive state
|
|
873
|
+
* directly alongside the store instance.
|
|
874
|
+
*
|
|
875
|
+
* **Behavior:**
|
|
876
|
+
* - **Non-Reactive:** Updates to context properties do **not** trigger `onChange` or RxJS emissions.
|
|
877
|
+
* - **Non-Persistent:** Context data is purely in-memory and is **not** saved to persistent storage.
|
|
878
|
+
* - **Access to Store:** When a factory function is used, it receives the store instance as an argument.
|
|
879
|
+
*
|
|
880
|
+
* @template Context - The type of the context object or factory function.
|
|
881
|
+
* @template Key - The type of keys stored in the map.
|
|
882
|
+
* @template Value - The type of values stored in the map.
|
|
883
|
+
* @template CacheDisabled - Literal type determining whether to disable in-memory caching.
|
|
884
|
+
*
|
|
885
|
+
* @returns A {@link Store} instance augmented with a `context` property.
|
|
886
|
+
*
|
|
887
|
+
* @example
|
|
888
|
+
* #### Basic store without context
|
|
889
|
+
* ```javascript
|
|
890
|
+
* import { createStore } from '@superutils/store'
|
|
891
|
+
*
|
|
892
|
+
* const store = createStore<string, number>()
|
|
893
|
+
* store.set('count', 1)
|
|
894
|
+
* store.set('count', prevCount => prevCount + 1)
|
|
895
|
+
* ```
|
|
896
|
+
*
|
|
897
|
+
* @example
|
|
898
|
+
* #### Store with a static context object
|
|
899
|
+
* ```javascript
|
|
900
|
+
* import { createStore } from '@superutils/store'
|
|
901
|
+
*
|
|
902
|
+
* const store = createStore('user-settings', {
|
|
903
|
+
* context: {
|
|
904
|
+
* count: 0,
|
|
905
|
+
* log(msg) {
|
|
906
|
+
* console.log(`[${++this.count}] ${msg}`)
|
|
907
|
+
* }
|
|
908
|
+
* }
|
|
909
|
+
* })
|
|
910
|
+
*
|
|
911
|
+
* store.context.log('Setting updated')
|
|
912
|
+
* console.log(store.context.count)
|
|
913
|
+
* ```
|
|
914
|
+
*
|
|
915
|
+
* @example
|
|
916
|
+
* #### In-memory store
|
|
917
|
+
* ```javascript
|
|
918
|
+
* import fetch from '@superutils/fetch'
|
|
919
|
+
* import { createStore } from '@superutils/store'
|
|
920
|
+
*
|
|
921
|
+
* const store = createStore({
|
|
922
|
+
* context: store => ({
|
|
923
|
+
* async getProducts() {
|
|
924
|
+
* const { products } = await fetch.get('https://dummyjson.com/products')
|
|
925
|
+
*
|
|
926
|
+
* const productsMap = new Map(products.map(p => [p.id, p]))
|
|
927
|
+
* store.setAll(productsMap)
|
|
928
|
+
*
|
|
929
|
+
* return productsMap
|
|
930
|
+
* },
|
|
931
|
+
* }),
|
|
932
|
+
* })
|
|
933
|
+
*
|
|
934
|
+
* store.context.getProducts().then(() => {
|
|
935
|
+
* console.log(store.getAll())
|
|
936
|
+
* })
|
|
937
|
+
* ```
|
|
938
|
+
*
|
|
939
|
+
* @example
|
|
940
|
+
* #### Store with a functional context (access to store instance)
|
|
941
|
+
* ```javascript
|
|
942
|
+
* import { createStore } from '@superutils/store'
|
|
943
|
+
*
|
|
944
|
+
* const authStore = createStore('auth', {
|
|
945
|
+
* context: store => ({
|
|
946
|
+
* isAuthenticated: () => store.has('token'),
|
|
947
|
+
* logout: () => store.delete('token')
|
|
948
|
+
* })
|
|
949
|
+
* })
|
|
950
|
+
*
|
|
951
|
+
* if (authStore.context.isAuthenticated()) {
|
|
952
|
+
* authStore.context.logout()
|
|
953
|
+
* }
|
|
954
|
+
* ```
|
|
955
|
+
*/
|
|
956
|
+
declare function createStore<Context extends Store_Context<Key, Value, CacheDisabled>, Key, Value, CacheDisabled extends boolean = false>(name?: ConstructorParameters<typeof Store<Key, Value, CacheDisabled>>[0], options?: Store_Options<Key, Value, CacheDisabled> & {
|
|
957
|
+
context?: Context;
|
|
958
|
+
}): IStoreWithContext<Key, Value, CacheDisabled, Context>;
|
|
959
|
+
declare function createStore<Context extends Store_Context<Key, Value, CacheDisabled>, Key, Value, CacheDisabled extends boolean = false>(options: Store_Options<Key, Value, CacheDisabled> & {
|
|
960
|
+
context?: Context;
|
|
961
|
+
}): IStoreWithContext<Key, Value, CacheDisabled, Context>;
|
|
962
|
+
/**
|
|
963
|
+
* Factory method to create a {@link Store} instance.
|
|
964
|
+
*
|
|
965
|
+
* @param args - Arguments passed directly to the {@link Store} constructor.
|
|
966
|
+
* @template Key - The type of keys stored in the map.
|
|
967
|
+
* @template Value - The type of values stored in the map.
|
|
968
|
+
* @template CacheDisabled - Whether to disable in-memory caching.
|
|
969
|
+
*/
|
|
970
|
+
declare function createStore<Key, Value, CacheDisabled extends boolean = false>(...args: ConstructorParameters<typeof Store<Key, Value, CacheDisabled>>): Store<Key, Value, CacheDisabled>;
|
|
971
|
+
|
|
972
|
+
export { type IObjectStore, type IStore, type ObjectStore_Context, type ObjectStore_Options, type StorageCompact, Store, type Store_DelayOptions, Store_OnErrorType, type Store_Options, type Store_Parse, type Store_Search, type Store_Sort, type Store_SortByComparator, type Store_SortByKey, type Store_SortByPropertyName, type Store_SortOptions, type Store_Stringify, type Store_ToJSON, createObjectStore, createStore, Store as default, forceUpdateCache$ };
|