/
redux.ts
62 lines (56 loc) · 935 Bytes
/
redux.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
/**
* @author author
* @description description
*/
import {
composeTypes,
createAction,
ActionCreator,
AsyncTuple,
NO_ERROR_TYPES,
createFetchAction,
ThunkAction,
ActionType,
safeGet
} from '../index';
/**
* 定义 types
*/
const prefix = 'prefix/';
enum BasicTypes {
addNum,
changeKeyword
}
enum FetchTypes {
fetchData
}
const Types = composeTypes({
prefix,
BasicTypes,
FetchTypes
});
/**
* actions 和 InitialState
*/
const actions = {
addNum: createAction(Types.addNum, 'num')<number>(),
changeKeyword: createAction(Types.changeKeyword, 'keyword')<string>()
};
class InitialState {
num = 0;
keyword = '';
}
/**
* reducer
*/
function reducer(
state = new InitialState(),
action: ActionType<typeof actions>
): InitialState {
switch (action.type) {
default: {
return AsyncTuple.handleAll(prefix, state, action);
}
}
}
export { actions, reducer, InitialState };