@@ -18,9 +18,9 @@ enablePatches();
18
18
* the new state along with a set of patches describing the changes since the
19
19
* last update.
20
20
*
21
- * @param state - The new controller state
21
+ * @param state - The new controller state.
22
22
* @param patches - A list of patches describing any changes (see here for more
23
- * information: https://immerjs.github.io/immer/docs/patches)
23
+ * information: https://immerjs.github.io/immer/docs/patches)
24
24
*/
25
25
export type Listener < T > = ( state : T , patches : Patch [ ] ) => void ;
26
26
@@ -30,8 +30,8 @@ export type Listener<T> = (state: T, patches: Patch[]) => void;
30
30
* This function will accept one piece of the controller state (one property),
31
31
* and will return some derivation of that state.
32
32
*
33
- * @param value - A piece of controller state
34
- * @returns Something derived from controller state
33
+ * @param value - A piece of controller state.
34
+ * @returns Something derived from controller state.
35
35
*/
36
36
export type StateDeriver < T extends Json > = ( value : T ) => Json ;
37
37
@@ -49,12 +49,12 @@ export type StateMetadata<T extends Record<string, Json>> = {
49
49
* Metadata for a single state property
50
50
*
51
51
* @property persist - Indicates whether this property should be persisted
52
- * (`true` for persistent, `false` for transient), or is set to a function
53
- * that derives the persistent state from the state.
52
+ * (`true` for persistent, `false` for transient), or is set to a function
53
+ * that derives the persistent state from the state.
54
54
* @property anonymous - Indicates whether this property is already anonymous,
55
- * (`true` for anonymous, `false` if it has potential to be personally
56
- * identifiable), or is set to a function that returns an anonymized
57
- * representation of this state.
55
+ * (`true` for anonymous, `false` if it has potential to be personally
56
+ * identifiable), or is set to a function that returns an anonymized
57
+ * representation of this state.
58
58
*/
59
59
export interface StatePropertyMetadata < T extends Json > {
60
60
persist : boolean | StateDeriver < T > ;
@@ -101,12 +101,12 @@ export class BaseController<
101
101
/**
102
102
* Creates a BaseController instance.
103
103
*
104
- * @param options
105
- * @param options.messenger - Controller messaging system
104
+ * @param options - Controller options.
105
+ * @param options.messenger - Controller messaging system.
106
106
* @param options.metadata - State metadata, describing how to "anonymize" the state, and which
107
- * parts should be persisted.
108
- * @param options.name - The name of the controller, used as a namespace for events and actions
109
- * @param options.state - Initial controller state
107
+ * parts should be persisted.
108
+ * @param options.name - The name of the controller, used as a namespace for events and actions.
109
+ * @param options.state - Initial controller state.
110
110
*/
111
111
constructor ( {
112
112
messenger,
@@ -131,9 +131,9 @@ export class BaseController<
131
131
}
132
132
133
133
/**
134
- * Retrieves current controller state
134
+ * Retrieves current controller state.
135
135
*
136
- * @returns - Current state
136
+ * @returns The current state.
137
137
*/
138
138
get state ( ) {
139
139
return this . internalState ;
@@ -152,7 +152,7 @@ export class BaseController<
152
152
* applied to the controller state.
153
153
*
154
154
* @param callback - Callback for updating state, passed a draft state
155
- * object. Return a new state object or mutate the draft to update state.
155
+ * object. Return a new state object or mutate the draft to update state.
156
156
*/
157
157
protected update ( callback : ( state : Draft < S > ) => void | S ) {
158
158
// We run into ts2589, "infinite type depth", if we don't cast
@@ -193,10 +193,10 @@ export class BaseController<
193
193
* By "anonymized" we mean that it should not contain any information that could be personally
194
194
* identifiable.
195
195
*
196
- * @param state - The controller state
196
+ * @param state - The controller state.
197
197
* @param metadata - The controller state metadata, which describes how to derive the
198
- * anonymized state
199
- * @returns The anonymized controller state
198
+ * anonymized state.
199
+ * @returns The anonymized controller state.
200
200
*/
201
201
export function getAnonymizedState < S extends Record < string , Json > > (
202
202
state : S ,
@@ -206,11 +206,11 @@ export function getAnonymizedState<S extends Record<string, Json>>(
206
206
}
207
207
208
208
/**
209
- * Returns the subset of state that should be persisted
209
+ * Returns the subset of state that should be persisted.
210
210
*
211
- * @param state - The controller state
212
- * @param metadata - The controller state metadata, which describes which pieces of state should be persisted
213
- * @returns The subset of controller state that should be persisted
211
+ * @param state - The controller state.
212
+ * @param metadata - The controller state metadata, which describes which pieces of state should be persisted.
213
+ * @returns The subset of controller state that should be persisted.
214
214
*/
215
215
export function getPersistentState < S extends Record < string , Json > > (
216
216
state : S ,
@@ -219,6 +219,14 @@ export function getPersistentState<S extends Record<string, Json>>(
219
219
return deriveStateFromMetadata ( state , metadata , 'persist' ) ;
220
220
}
221
221
222
+ /**
223
+ * Use the metadata to derive state according to the given metadata property.
224
+ *
225
+ * @param state - The full controller state.
226
+ * @param metadata - The controller metadata.
227
+ * @param metadataProperty - The metadata property to use to derive state.
228
+ * @returns The metadata-derived controller state.
229
+ */
222
230
function deriveStateFromMetadata < S extends Record < string , Json > > (
223
231
state : S ,
224
232
metadata : StateMetadata < S > ,
0 commit comments