-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
reducer.ts
149 lines (140 loc) · 4.07 KB
/
reducer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* External dependencies
*/
import EquivalentKeyMap from 'equivalent-key-map';
import type { Reducer } from 'redux';
/**
* Internal dependencies
*/
import { selectorArgsToStateKey, onSubKey } from './utils';
type Action =
| ReturnType< typeof import('./actions').startResolution >
| ReturnType< typeof import('./actions').finishResolution >
| ReturnType< typeof import('./actions').failResolution >
| ReturnType< typeof import('./actions').startResolutions >
| ReturnType< typeof import('./actions').finishResolutions >
| ReturnType< typeof import('./actions').failResolutions >
| ReturnType< typeof import('./actions').invalidateResolution >
| ReturnType< typeof import('./actions').invalidateResolutionForStore >
| ReturnType<
typeof import('./actions').invalidateResolutionForStoreSelector
>;
type StateKey = unknown[] | unknown;
export type StateValue =
| { status: 'resolving' | 'finished' }
| { status: 'error'; error: Error | unknown };
export type Status = StateValue[ 'status' ];
export type State = EquivalentKeyMap< StateKey, StateValue >;
/**
* Reducer function returning next state for selector resolution of
* subkeys, object form:
*
* selectorName -> EquivalentKeyMap<Array,boolean>
*/
const subKeysIsResolved: Reducer< Record< string, State >, Action > = onSubKey<
State,
Action
>( 'selectorName' )( ( state = new EquivalentKeyMap(), action: Action ) => {
switch ( action.type ) {
case 'START_RESOLUTION': {
const nextState = new EquivalentKeyMap( state );
nextState.set( selectorArgsToStateKey( action.args ), {
status: 'resolving',
} );
return nextState;
}
case 'FINISH_RESOLUTION': {
const nextState = new EquivalentKeyMap( state );
nextState.set( selectorArgsToStateKey( action.args ), {
status: 'finished',
} );
return nextState;
}
case 'FAIL_RESOLUTION': {
const nextState = new EquivalentKeyMap( state );
nextState.set( selectorArgsToStateKey( action.args ), {
status: 'error',
error: action.error,
} );
return nextState;
}
case 'START_RESOLUTIONS': {
const nextState = new EquivalentKeyMap( state );
for ( const resolutionArgs of action.args ) {
nextState.set( selectorArgsToStateKey( resolutionArgs ), {
status: 'resolving',
} );
}
return nextState;
}
case 'FINISH_RESOLUTIONS': {
const nextState = new EquivalentKeyMap( state );
for ( const resolutionArgs of action.args ) {
nextState.set( selectorArgsToStateKey( resolutionArgs ), {
status: 'finished',
} );
}
return nextState;
}
case 'FAIL_RESOLUTIONS': {
const nextState = new EquivalentKeyMap( state );
action.args.forEach( ( resolutionArgs, idx ) => {
const resolutionState: StateValue = {
status: 'error',
error: undefined,
};
const error = action.errors[ idx ];
if ( error ) {
resolutionState.error = error;
}
nextState.set(
selectorArgsToStateKey( resolutionArgs as unknown[] ),
resolutionState
);
} );
return nextState;
}
case 'INVALIDATE_RESOLUTION': {
const nextState = new EquivalentKeyMap( state );
nextState.delete( selectorArgsToStateKey( action.args ) );
return nextState;
}
}
return state;
} );
/**
* Reducer function returning next state for selector resolution, object form:
*
* selectorName -> EquivalentKeyMap<Array, boolean>
*
* @param state Current state.
* @param action Dispatched action.
*
* @return Next state.
*/
const isResolved = ( state: Record< string, State > = {}, action: Action ) => {
switch ( action.type ) {
case 'INVALIDATE_RESOLUTION_FOR_STORE':
return {};
case 'INVALIDATE_RESOLUTION_FOR_STORE_SELECTOR': {
if ( action.selectorName in state ) {
const {
[ action.selectorName ]: removedSelector,
...restState
} = state;
return restState;
}
return state;
}
case 'START_RESOLUTION':
case 'FINISH_RESOLUTION':
case 'FAIL_RESOLUTION':
case 'START_RESOLUTIONS':
case 'FINISH_RESOLUTIONS':
case 'FAIL_RESOLUTIONS':
case 'INVALIDATE_RESOLUTION':
return subKeysIsResolved( state, action );
}
return state;
};
export default isResolved;