@zenflux/commands 0.0.0-stage → 0.0.1
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/LICENSE +674 -0
- package/package.json +22 -3
- package/readme.md +40 -0
- package/src/api.ts +158 -0
- package/src/core/commands-internal.ts +11 -0
- package/src/core/commands.ts +674 -0
- package/src/core/components.ts +103 -0
- package/src/core/hooks/base.ts +444 -0
- package/src/core/hooks/data.ts +54 -0
- package/src/core/hooks/ui.ts +40 -0
- package/src/core/hooks.ts +257 -0
- package/src/core/routes.ts +243 -0
- package/src/core/shortcuts.ts +351 -0
- package/src/core/store.ts +155 -0
- package/src/env.d.ts +15 -0
- package/src/extras/dev-tools.ts +427 -0
- package/src/index.ts +83 -0
- package/src/instance.ts +30 -0
- package/src/modules/args-object.ts +113 -0
- package/src/modules/command-base.ts +51 -0
- package/src/modules/command-callback-base.ts +42 -0
- package/src/modules/command-infra.ts +230 -0
- package/src/modules/command-internal-base.ts +19 -0
- package/src/modules/component-base.ts +479 -0
- package/src/modules/hook-base.ts +149 -0
- package/src/modules/hook-break.ts +8 -0
- package/src/modules/hooks/data/after.ts +15 -0
- package/src/modules/hooks/data/base.ts +11 -0
- package/src/modules/hooks/data/catch.ts +12 -0
- package/src/modules/hooks/data/dependency.ts +12 -0
- package/src/modules/hooks/data/index.ts +6 -0
- package/src/modules/hooks/ui/after.ts +14 -0
- package/src/modules/hooks/ui/base.ts +11 -0
- package/src/modules/hooks/ui/before.ts +12 -0
- package/src/modules/hooks/ui/catch.ts +12 -0
- package/src/modules/hooks/ui/index.ts +6 -0
- package/src/modules/instance-type.ts +114 -0
- package/src/modules/module.ts +216 -0
- package/src/types.ts +194 -0
- package/src/utils/console.ts +19 -0
- package/src/utils/instances-slice.ts +123 -0
- package/src/utils/object.ts +162 -0
- package/src/utils/value-store.ts +145 -0
- package/README.md +0 -3
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A slice of a component's store for the state of its instances: a React component drawn many times, each instance
|
|
3
|
+
* with its own state, by its unique name (@zenflux/react-commander's `withCommands()` components keep theirs here).
|
|
4
|
+
* `withInstances( config )` adds the slice to a component's store config (`defaultStore()`), beside its own state.
|
|
5
|
+
*
|
|
6
|
+
* The slice's reducer is where a change is found: a patch whose every value is the one already there returns the same
|
|
7
|
+
* state, so nothing that reads the store is told of it.
|
|
8
|
+
*/
|
|
9
|
+
import type { StoreAction } from "./value-store";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The slice's state, in the component's store.
|
|
13
|
+
*/
|
|
14
|
+
export interface InstancesState {
|
|
15
|
+
instances: { [ instance: string ]: object };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface PatchInstanceAction extends StoreAction {
|
|
19
|
+
type: "PATCH_INSTANCE";
|
|
20
|
+
instance: string;
|
|
21
|
+
patch: object;
|
|
22
|
+
initial: object;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface RemoveInstanceAction extends StoreAction {
|
|
26
|
+
type: "REMOVE_INSTANCE";
|
|
27
|
+
instance: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const isPatch = ( action: StoreAction ): action is PatchInstanceAction => "PATCH_INSTANCE" === action.type;
|
|
31
|
+
const isRemove = ( action: StoreAction ): action is RemoveInstanceAction => "REMOVE_INSTANCE" === action.type;
|
|
32
|
+
|
|
33
|
+
const NO_INSTANCES: InstancesState[ "instances" ] = {};
|
|
34
|
+
|
|
35
|
+
const instancesReducer = ( instances: InstancesState[ "instances" ] = NO_INSTANCES, action: StoreAction ): InstancesState[ "instances" ] => {
|
|
36
|
+
if ( isPatch( action ) ) {
|
|
37
|
+
const current: { [ key: string ]: unknown } = { ...( instances[ action.instance ] ?? action.initial ) },
|
|
38
|
+
changed = Object.entries( action.patch ).some( ( [ key, value ] ) => ! Object.is( current[ key ], value ) );
|
|
39
|
+
|
|
40
|
+
return changed ? { ...instances, [ action.instance ]: { ...current, ...action.patch } } : instances;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if ( isRemove( action ) && action.instance in instances ) {
|
|
44
|
+
return Object.fromEntries( Object.entries( instances ).filter( ( [ instance ] ) => instance !== action.instance ) );
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return instances;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const instancesActions = {
|
|
51
|
+
/**
|
|
52
|
+
* Sets some of an instance's state: over what it has, or over `initial` the first time.
|
|
53
|
+
*
|
|
54
|
+
* @param instance
|
|
55
|
+
* @param patch
|
|
56
|
+
* @param initial
|
|
57
|
+
*/
|
|
58
|
+
patchInstance: ( instance: string, patch: object, initial: object ): PatchInstanceAction => ( { type: "PATCH_INSTANCE", instance, patch, initial } ),
|
|
59
|
+
/**
|
|
60
|
+
* Drops an instance's state, as it unmounts.
|
|
61
|
+
*
|
|
62
|
+
* @param instance
|
|
63
|
+
*/
|
|
64
|
+
removeInstance: ( instance: string ): RemoveInstanceAction => ( { type: "REMOVE_INSTANCE", instance } ),
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const instancesSelectors = {
|
|
68
|
+
/**
|
|
69
|
+
* @param state
|
|
70
|
+
* @param instance
|
|
71
|
+
*
|
|
72
|
+
* @return an instance's state: undefined until it was first set
|
|
73
|
+
*/
|
|
74
|
+
getInstance: ( state: InstancesState, instance: string ): object | undefined => state.instances[ instance ],
|
|
75
|
+
/**
|
|
76
|
+
* @param state
|
|
77
|
+
*
|
|
78
|
+
* @return the state of every instance, by its unique name
|
|
79
|
+
*/
|
|
80
|
+
getInstances: ( state: InstancesState ): InstancesState[ "instances" ] => state.instances,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A store config's parts, as `withInstances()` takes them.
|
|
85
|
+
*/
|
|
86
|
+
interface SliceableConfig<TState extends object, TActions extends object, TSelectors extends object> {
|
|
87
|
+
reducer: ( state: TState | undefined, action: StoreAction ) => TState;
|
|
88
|
+
actions: TActions;
|
|
89
|
+
selectors: TSelectors;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param config A component's store config (`valueStore( { ... } )`, or its own).
|
|
94
|
+
*
|
|
95
|
+
* @return the config with the instances' slice: its state under `instances`, `patchInstance()` and `removeInstance()`,
|
|
96
|
+
* `getInstance()` and `getInstances()`
|
|
97
|
+
*/
|
|
98
|
+
export function withInstances<TState extends object, TActions extends object, TSelectors extends object>(
|
|
99
|
+
config: SliceableConfig<TState, TActions, TSelectors>
|
|
100
|
+
) {
|
|
101
|
+
return {
|
|
102
|
+
reducer( state: ( TState & InstancesState ) | undefined, action: StoreAction ): TState & InstancesState {
|
|
103
|
+
const own = config.reducer( state, action ),
|
|
104
|
+
instances = instancesReducer( state?.instances, action );
|
|
105
|
+
|
|
106
|
+
// The component's own reducer keeps the slice it was given (it spreads its state): the same state then.
|
|
107
|
+
return "instances" in own && own.instances === instances ? ( own as TState & InstancesState ) : { ...own, instances };
|
|
108
|
+
},
|
|
109
|
+
actions: { ...config.actions, ...instancesActions },
|
|
110
|
+
selectors: { ...config.selectors, ...instancesSelectors },
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @return the store config of a component whose only state is its instances'
|
|
116
|
+
*/
|
|
117
|
+
export const instancesStore = () => withInstances( {
|
|
118
|
+
reducer: ( state: object = {} ): object => state,
|
|
119
|
+
actions: {},
|
|
120
|
+
selectors: {},
|
|
121
|
+
} );
|
|
122
|
+
|
|
123
|
+
export type InstancesStoreConfig = ReturnType<typeof instancesStore>;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A JSON value: the modules' settings are JSON.
|
|
3
|
+
*/
|
|
4
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A JSON object. Its values may be undefined too, so an object type with optional properties is one; they are skipped
|
|
8
|
+
* where it is merged.
|
|
9
|
+
*/
|
|
10
|
+
export interface JsonObject {
|
|
11
|
+
[ key: string ]: JsonValue | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param value
|
|
16
|
+
*
|
|
17
|
+
* @return whether the value is a plain object (not an array, class instance or null)
|
|
18
|
+
*/
|
|
19
|
+
const isPlainObject = <TValue>( value: TValue ): boolean => {
|
|
20
|
+
if ( ! value || "object" !== typeof value ) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Typed as `any` by TS: an object's prototype is an object or null.
|
|
25
|
+
const prototype: object | null = Object.getPrototypeOf( value );
|
|
26
|
+
|
|
27
|
+
return null === prototype || Object.prototype === prototype;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param value
|
|
32
|
+
*
|
|
33
|
+
* @return whether a JSON value is an object: not an array, nor a primitive
|
|
34
|
+
*/
|
|
35
|
+
export const isJsonObject = ( value: JsonValue | undefined ): value is JsonObject => isPlainObject( value );
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param value Any value.
|
|
39
|
+
*
|
|
40
|
+
* @return whether the value is JSON all through: primitives, arrays and plain objects of JSON values (undefined skipped)
|
|
41
|
+
*/
|
|
42
|
+
const isJson = <TValue>( value: TValue ): boolean => {
|
|
43
|
+
if ( null === value || "string" === typeof value || "number" === typeof value || "boolean" === typeof value ) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if ( Array.isArray( value ) ) {
|
|
48
|
+
return value.every( <TItem>( item: TItem ) => isJson( item ) );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return isPlainObject( value ) && "object" === typeof value && null !== value &&
|
|
52
|
+
Object.values( value ).every( <TItem>( item: TItem ) => undefined === item || isJson( item ) );
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param value Any object.
|
|
57
|
+
*
|
|
58
|
+
* @return whether the object is a plain object of JSON values, all through
|
|
59
|
+
*/
|
|
60
|
+
export const isJsonObjectDeep = ( value: object ): value is JsonObject => isPlainObject( value ) && isJson( value );
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param current The target's value.
|
|
64
|
+
* @param value The source's.
|
|
65
|
+
*
|
|
66
|
+
* @return the source's value merged into the target's
|
|
67
|
+
*/
|
|
68
|
+
function mergeValue( current: JsonValue | undefined, value: JsonValue ): JsonValue {
|
|
69
|
+
if ( Array.isArray( value ) ) {
|
|
70
|
+
return mergeArray( Array.isArray( current ) ? current : [], value );
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if ( isJsonObject( value ) ) {
|
|
74
|
+
return mergeObject( isJsonObject( current ) ? current : {}, value );
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function mergeObject( target: JsonObject, source: JsonObject ): JsonObject {
|
|
81
|
+
Object.entries( source ).forEach( ( [ key, value ] ) => {
|
|
82
|
+
if ( undefined === value || "__proto__" === key || target === value ) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
target[ key ] = mergeValue( target[ key ], value );
|
|
87
|
+
} );
|
|
88
|
+
|
|
89
|
+
return target;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function mergeArray( target: JsonValue[], source: JsonValue[] ): JsonValue[] {
|
|
93
|
+
source.forEach( ( value, index ) => {
|
|
94
|
+
if ( undefined === value || target === value ) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
target[ index ] = mergeValue( target[ index ], value );
|
|
99
|
+
} );
|
|
100
|
+
|
|
101
|
+
return target;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Deep merge, as `jQuery.extend( true, target, ...sources )`: plain objects and arrays merge key by key into the
|
|
106
|
+
* target, anything else replaces; undefined values are skipped.
|
|
107
|
+
*
|
|
108
|
+
* @param target
|
|
109
|
+
* @param sources
|
|
110
|
+
*
|
|
111
|
+
* @return the target
|
|
112
|
+
*/
|
|
113
|
+
export const deepMerge = ( target: JsonObject, ...sources: ( JsonObject | null | undefined )[] ): JsonObject => {
|
|
114
|
+
sources.forEach( ( source ) => {
|
|
115
|
+
if ( source ) {
|
|
116
|
+
mergeObject( target, source );
|
|
117
|
+
}
|
|
118
|
+
} );
|
|
119
|
+
|
|
120
|
+
return target;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param value
|
|
125
|
+
*
|
|
126
|
+
* @return the object's own enumerable keys. TS types `Object.keys()` as strings, as an object may have more keys than
|
|
127
|
+
* its type names; these are the object's own, so they index it.
|
|
128
|
+
*/
|
|
129
|
+
export const ownKeys = <TObject extends object>( value: TObject ): ( keyof TObject & string )[] =>
|
|
130
|
+
Object.keys( value ) as ( keyof TObject & string )[];
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Deep equality of plain values (what `_.isEqual()` did for route args): objects and arrays by their keys,
|
|
134
|
+
* anything else by `Object.is()`.
|
|
135
|
+
*
|
|
136
|
+
* @param a
|
|
137
|
+
* @param b
|
|
138
|
+
*
|
|
139
|
+
* @return whether they are equal
|
|
140
|
+
*/
|
|
141
|
+
export const isEqual = <TValue>( a: TValue, b: TValue ): boolean => {
|
|
142
|
+
if ( Object.is( a, b ) ) {
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if ( ! a || ! b || "object" !== typeof a || "object" !== typeof b ) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Typed as `any` by TS: an object's prototype is an object or null.
|
|
151
|
+
const prototypeA: object | null = Object.getPrototypeOf( a ),
|
|
152
|
+
prototypeB: object | null = Object.getPrototypeOf( b );
|
|
153
|
+
|
|
154
|
+
if ( Array.isArray( a ) !== Array.isArray( b ) || prototypeA !== prototypeB ) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const keys = ownKeys( a );
|
|
159
|
+
|
|
160
|
+
return keys.length === Object.keys( b ).length &&
|
|
161
|
+
keys.every( ( key ) => Object.prototype.hasOwnProperty.call( b, key ) && isEqual( a[ key ], b[ key ] ) );
|
|
162
|
+
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For components' stores of plain values (`defaultStore()`), set one at a time: each action creator takes the value,
|
|
3
|
+
* or a function of the current value (as React's setState). A change is in the store at once, so the next line reads
|
|
4
|
+
* it.
|
|
5
|
+
*
|
|
6
|
+
* `valueStore( initialState )` makes the whole store config: the reducer, a setter for each key (`setSession()`) and a
|
|
7
|
+
* getter (`getSession()`), all typed from the state. `setter()`, `valuesReducer()`, `setters()` and `getters()` are its
|
|
8
|
+
* parts, for a store that adds its own actions and selectors to them.
|
|
9
|
+
*/
|
|
10
|
+
import { ownKeys } from "./object";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A new value, or a function of the current value that returns it. A value that is itself a function can only be set
|
|
14
|
+
* through such a function: `setRender( () => render )`.
|
|
15
|
+
*/
|
|
16
|
+
export type Update<TValue> = TValue | ( ( current: TValue ) => TValue );
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Any action a store's reducer gets: its own, and the store's first (`@@zenflux/commands/INIT`).
|
|
20
|
+
*/
|
|
21
|
+
export interface StoreAction {
|
|
22
|
+
type: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The action of a setter: set a key's value.
|
|
27
|
+
*/
|
|
28
|
+
export interface SetAction<TKey extends PropertyKey, TValue> extends StoreAction {
|
|
29
|
+
type: "SET";
|
|
30
|
+
key: TKey;
|
|
31
|
+
value: Update<TValue>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The setters of a state's keys: `setSession( value )` for `session`.
|
|
36
|
+
*/
|
|
37
|
+
export type Setters<TState> = {
|
|
38
|
+
[ TKey in keyof TState & string as `set${ Capitalize<TKey> }` ]: ( value: Update<TState[ TKey ]> ) => SetAction<TKey, TState[ TKey ]>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The selectors of a state's keys: `getSession( state )` for `session`.
|
|
43
|
+
*/
|
|
44
|
+
export type Getters<TState> = {
|
|
45
|
+
[ TKey in keyof TState & string as `get${ Capitalize<TKey> }` ]: ( state: TState ) => TState[ TKey ];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The config of a store of plain values (`valueStore()`).
|
|
50
|
+
*/
|
|
51
|
+
export interface ValueStoreConfig<TState extends object> {
|
|
52
|
+
reducer: ( state: TState | undefined, action: StoreAction ) => TState;
|
|
53
|
+
actions: Setters<TState>;
|
|
54
|
+
selectors: Getters<TState>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param key
|
|
59
|
+
*
|
|
60
|
+
* @return the action creator that sets the key's value. In TypeScript, name the value's type: `setter<string>( 'tab' )`
|
|
61
|
+
* (without it, it takes no value).
|
|
62
|
+
*/
|
|
63
|
+
export const setter =
|
|
64
|
+
<TValue = never, TKey extends string = string>( key: TKey ) =>
|
|
65
|
+
( value: Update<TValue> ): SetAction<TKey, TValue> => ( { type: "SET", key, value } );
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A 'SET' action is a setter's of this store (`setter()` makes them, for the keys of its state).
|
|
69
|
+
*
|
|
70
|
+
* @param action
|
|
71
|
+
*
|
|
72
|
+
* @return whether the action sets a value
|
|
73
|
+
*/
|
|
74
|
+
const isSetAction = <TState extends object>(
|
|
75
|
+
action: StoreAction
|
|
76
|
+
): action is SetAction<keyof TState, TState[ keyof TState ]> => "SET" === action.type;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param value
|
|
80
|
+
*
|
|
81
|
+
* @return whether the update is a function of the current value
|
|
82
|
+
*/
|
|
83
|
+
const isUpdater = <TValue>( value: Update<TValue> ): value is ( current: TValue ) => TValue => "function" === typeof value;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param initialState
|
|
87
|
+
*
|
|
88
|
+
* @return the reducer of setter()'s actions
|
|
89
|
+
*/
|
|
90
|
+
export const valuesReducer =
|
|
91
|
+
<TState extends object>( initialState: TState ) =>
|
|
92
|
+
( state: TState = initialState, action: StoreAction ): TState => {
|
|
93
|
+
if ( ! isSetAction<TState>( action ) ) {
|
|
94
|
+
return state;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const current = state[ action.key ],
|
|
98
|
+
value = isUpdater( action.value ) ? action.value( current ) : action.value;
|
|
99
|
+
|
|
100
|
+
return Object.is( value, current ) ? state : { ...state, [ action.key ]: value };
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const capitalize = ( key: string ): string => key.charAt( 0 ).toUpperCase() + key.slice( 1 );
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param initialState
|
|
107
|
+
*
|
|
108
|
+
* @return a setter for each key of the state: `setSession` for `session`
|
|
109
|
+
*/
|
|
110
|
+
export const setters = <TState extends object>( initialState: TState ): Setters<TState> => {
|
|
111
|
+
const built: object = Object.fromEntries(
|
|
112
|
+
ownKeys( initialState ).map( ( key ) => [ `set${ capitalize( key ) }`, setter<TState[ typeof key ], typeof key>( key ) ] )
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// TS can't type an object built from the state's keys at runtime: it has a setter for each of them.
|
|
116
|
+
return built as Setters<TState>;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param initialState
|
|
121
|
+
*
|
|
122
|
+
* @return a selector for each key of the state: `getSession` for `session`
|
|
123
|
+
*/
|
|
124
|
+
export const getters = <TState extends object>( initialState: TState ): Getters<TState> => {
|
|
125
|
+
const built: object = Object.fromEntries(
|
|
126
|
+
ownKeys( initialState ).map( ( key ) => [ `get${ capitalize( key ) }`, ( state: TState ) => state[ key ] ] )
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// TS can't type an object built from the state's keys at runtime: it has a getter for each of them.
|
|
130
|
+
return built as Getters<TState>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A store of plain values, typed from its state: `valueStore<ChatState>( { session: null, ... } )` has
|
|
135
|
+
* `setSession( value )` and `getSession()`. Name the state's type where an initial value does not say it (a null).
|
|
136
|
+
*
|
|
137
|
+
* @param initialState
|
|
138
|
+
*
|
|
139
|
+
* @return the store config ({ reducer, actions, selectors }) for `defaultStore()`
|
|
140
|
+
*/
|
|
141
|
+
export const valueStore = <TState extends object>( initialState: TState ): ValueStoreConfig<TState> => ( {
|
|
142
|
+
reducer: valuesReducer( initialState ),
|
|
143
|
+
actions: setters( initialState ),
|
|
144
|
+
selectors: getters( initialState ),
|
|
145
|
+
} );
|
package/README.md
DELETED