@@ -13,7 +13,7 @@ export type Listener<T> = (state: T, patches: Patch[]) => void;
13
13
14
14
export type Anonymizer < T > = ( value : T ) => T ;
15
15
16
- export type Schema < T > = {
16
+ export type StateMetadata < T > = {
17
17
[ P in keyof T ] : {
18
18
persist : boolean ;
19
19
anonymous : boolean | Anonymizer < T [ P ] > ;
@@ -28,18 +28,18 @@ export class BaseController<S extends Record<string, any>> {
28
28
29
29
private internalListeners : Set < Listener < S > > = new Set ( ) ;
30
30
31
- public readonly schema : Schema < S > ;
31
+ public readonly metadata : StateMetadata < S > ;
32
32
33
33
/**
34
34
* Creates a BaseController instance.
35
35
*
36
36
* @param state - Initial controller state
37
- * @param schema - State schema , describing how to "anonymize" the state,
37
+ * @param metadata - State metadata , describing how to "anonymize" the state,
38
38
* and which parts should be persisted.
39
39
*/
40
- constructor ( state : S , schema : Schema < S > ) {
40
+ constructor ( state : S , metadata : StateMetadata < S > ) {
41
41
this . internalState = state ;
42
- this . schema = schema ;
42
+ this . metadata = metadata ;
43
43
}
44
44
45
45
/**
@@ -109,23 +109,23 @@ function isAnonymizingFunction<T>(x: boolean | Anonymizer<T>): x is Anonymizer<T
109
109
return typeof x === 'function' ;
110
110
}
111
111
112
- export function getAnonymizedState < S extends Record < string , any > > ( state : S , schema : Schema < S > ) {
112
+ export function getAnonymizedState < S extends Record < string , any > > ( state : S , metadata : StateMetadata < S > ) {
113
113
return Object . keys ( state ) . reduce ( ( anonymizedState , _key ) => {
114
114
const key : keyof S = _key ; // https://stackoverflow.com/questions/63893394/string-cannot-be-used-to-index-type-t
115
- const schemaValue = schema [ key ] . anonymous ;
116
- if ( isAnonymizingFunction ( schemaValue ) ) {
117
- anonymizedState [ key ] = schemaValue ( state [ key ] ) ;
118
- } else if ( schemaValue ) {
115
+ const metadataValue = metadata [ key ] . anonymous ;
116
+ if ( isAnonymizingFunction ( metadataValue ) ) {
117
+ anonymizedState [ key ] = metadataValue ( state [ key ] ) ;
118
+ } else if ( metadataValue ) {
119
119
anonymizedState [ key ] = state [ key ] ;
120
120
}
121
121
return anonymizedState ;
122
122
} , { } as Partial < S > ) ;
123
123
}
124
124
125
- export function getPersistentState < S extends Record < string , any > > ( state : S , schema : Schema < S > ) {
125
+ export function getPersistentState < S extends Record < string , any > > ( state : S , metadata : StateMetadata < S > ) {
126
126
return Object . keys ( state ) . reduce ( ( persistedState , _key ) => {
127
127
const key : keyof S = _key ; // https://stackoverflow.com/questions/63893394/string-cannot-be-used-to-index-type-t
128
- if ( schema [ key ] . persist ) {
128
+ if ( metadata [ key ] . persist ) {
129
129
persistedState [ key ] = state [ key ] ;
130
130
}
131
131
return persistedState ;
0 commit comments