@@ -2,9 +2,9 @@ export type Patch<T> = (value: T) => T;
2
2
export type ValueOrPatch < T > = T | Patch < T > ;
3
3
export type Handler < T > = ( nextValue : T , previousValue : T | undefined ) => void ;
4
4
export type Unsubscribe = ( ) => void ;
5
-
6
- export type Unregister = Unsubscribe ;
7
- export type Modifier < T > = Handler < T > ;
5
+ // aliases
6
+ export type RemovePreprocessor = Unsubscribe ;
7
+ export type Preprocessor < T > = Handler < T > ;
8
8
9
9
export const isPatch = < T > ( value : ValueOrPatch < T > ) : value is Patch < T > =>
10
10
typeof value === 'function' ;
@@ -14,12 +14,14 @@ const noop = () => {};
14
14
15
15
export class StateStore < T extends Record < string , unknown > > {
16
16
protected handlers = new Set < Handler < T > > ( ) ;
17
- protected modifiers = new Set < Handler < T > > ( ) ;
17
+ protected preprocessors = new Set < Preprocessor < T > > ( ) ;
18
18
19
19
constructor ( protected value : T ) { }
20
20
21
21
/**
22
22
* Allows merging two stores only if their keys differ otherwise there's no way to ensure the data type stability.
23
+ * @experimental
24
+ * This method is experimental and may change in future versions.
23
25
*/
24
26
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25
27
public merge < Q extends StateStore < any > > (
@@ -44,7 +46,7 @@ export class StateStore<T extends Record<string, unknown>> {
44
46
// do not notify subscribers if the value hasn't changed
45
47
if ( newValue === this . value ) return ;
46
48
47
- this . modifiers . forEach ( ( modifier ) => modifier ( newValue , this . value ) ) ;
49
+ this . preprocessors . forEach ( ( preprocessor ) => preprocessor ( newValue , this . value ) ) ;
48
50
49
51
const oldValue = this . value ;
50
52
this . value = newValue ;
@@ -104,17 +106,17 @@ export class StateStore<T extends Record<string, unknown>> {
104
106
} ;
105
107
106
108
/**
107
- * Registers a modifier function that will be called before the state is updated.
109
+ * Registers a preprocessor function that will be called before the state is updated.
108
110
*
109
- * Modifiers are invoked with the new and previous values whenever `next` or `partialNext`
110
- * is called, allowing you to mutate or react to the new value before it is set. Modifiers run in the
111
+ * Preprocessors are invoked with the new and previous values whenever `next` or `partialNext` methods
112
+ * are called, allowing you to mutate or react to the new value before it is set. Preprocessors run in the
111
113
* order they were registered.
112
114
*
113
115
* @example
114
116
* ```ts
115
117
* const store = new StateStore<{ count: number; isMaxValue: bool; }>({ count: 0, isMaxValue: false });
116
118
*
117
- * store.registerModifier ((nextValue, prevValue) => {
119
+ * store.addPreprocessor ((nextValue, prevValue) => {
118
120
* if (nextValue.count > 10) {
119
121
* nextValue.count = 10; // Clamp the value to a maximum of 10
120
122
* }
@@ -135,14 +137,14 @@ export class StateStore<T extends Record<string, unknown>> {
135
137
* store.getLatestValue(); // { count: 5, isMaxValue: false }
136
138
* ```
137
139
*
138
- * @param modifier - The function to be called with the next and previous values before the state is updated.
139
- * @returns An `Unregister ` function that removes the modifier when called.
140
+ * @param preprocessor - The function to be called with the next and previous values before the state is updated.
141
+ * @returns A `RemovePreprocessor ` function that removes the preprocessor when called.
140
142
*/
141
- public registerModifier ( modifier : Modifier < T > ) : Unregister {
142
- this . modifiers . add ( modifier ) ;
143
+ public addPreprocessor ( preprocessor : Preprocessor < T > ) : RemovePreprocessor {
144
+ this . preprocessors . add ( preprocessor ) ;
143
145
144
146
return ( ) => {
145
- this . modifiers . delete ( modifier ) ;
147
+ this . preprocessors . delete ( preprocessor ) ;
146
148
} ;
147
149
}
148
150
}
@@ -154,7 +156,7 @@ export class StateStore<T extends Record<string, unknown>> {
154
156
* It extends StateStore with the combined type of both source stores.
155
157
* Changes to either the original or merged store will propagate to the combined store.
156
158
*
157
- * Note: Direct mutations (next, partialNext, registerModifier ) are disabled on the merged store.
159
+ * Note: Direct mutations (next, partialNext, addPreprocessor ) are disabled on the merged store.
158
160
* You should instead call these methods on the original or merged stores.
159
161
*
160
162
* @template O The type of the original state store
@@ -292,46 +294,10 @@ export class MergedStateStore<
292
294
`${ MergedStateStore . name } .partialNext is disabled, call original.partialNext or merged.partialNext instead` ,
293
295
) ;
294
296
} ;
295
- public registerModifier ( ) {
297
+ public addPreprocessor ( ) {
296
298
console . warn (
297
- `${ MergedStateStore . name } .registerModifier is disabled, call original.registerModifier or merged.registerModifier instead` ,
299
+ `${ MergedStateStore . name } .addPreprocessor is disabled, call original.addPreprocessor or merged.addPreprocessor instead` ,
298
300
) ;
299
301
return noop ;
300
302
}
301
303
}
302
-
303
- /** EXAMPLE:
304
- const Uninitialized = Symbol('uninitialized');
305
-
306
- const b = new StateStore<{
307
- previous: string | null | symbol;
308
- hasPrevious: boolean | symbol;
309
- }>({
310
- previous: Uninitialized,
311
- hasPrevious: Uninitialized,
312
- });
313
-
314
- const a = new StateStore<{
315
- hasNext: boolean | symbol;
316
- next: string | null | symbol;
317
- }>({
318
- next: Uninitialized,
319
- hasNext: Uninitialized,
320
- }).merge(b);
321
-
322
- a.original.registerModifier((nextValue) => {
323
- if (typeof nextValue.next === 'string') {
324
- nextValue.hasNext = true;
325
- } else if (nextValue.next === Uninitialized) {
326
- nextValue.hasNext = Uninitialized;
327
- } else {
328
- nextValue.hasNext = false;
329
- }
330
- });
331
-
332
- a.subscribe((ns) => console.log(ns));
333
-
334
- a.original.partialNext({ next: 'next' });
335
- a.original.partialNext({ next: null });
336
- a.original.partialNext({ next: Uninitialized });
337
- */
0 commit comments