-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.ts
135 lines (125 loc) · 3.34 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
/**
* @module reducer/reducer
* Description: Reducer, ReducerAction APIs
*/
import {
randomString,
} from "../utils";
import type {
Action,
Callable,
Json,
State,
} from "../types";
/**
* Name: Reducer
* Description: Reducer API object
* @public
* @typedef {Object}
*/
export type Reducer<
T extends Json = Json,
S extends State<T> = State<T>
> = {
dispatch: (state: S, action: Action<T>) => S;
getAction: (name: string | symbol) => ReducerAction<T, S>;
getAllActions: () => Record<string | symbol, ReducerAction<T, S>>;
hasAction: (name: string | symbol) => boolean;
name: string;
removeAction: (name: string | symbol) => void;
removeAllActions: () => void;
setAction: (name: string | symbol, action: ReducerAction<T, S>) => void;
setActions: (actions: Record<string | symbol, ReducerAction<T, S>>) => void;
};
/**
* Name: ReducerAction
* Description: ReducerAction API object
* @public
* @typedef {Object}
*/
export type ReducerAction<
T extends Json = Json,
S extends State<T> = State<T>,
A extends Action<T> = Action<T>
> = Callable<[state: S, action?: A], S>;
/**
* Name: ReducerCollection
* Description: ReducerCollection object
* @public
* @typedef {Object}
*/
export type ReducerCollection<
T extends Json = Json,
S extends State<T> = State<T>
> = Record<string | symbol, ReducerAction<T, S>>;
/**
* Name: CreateReducerParams
* Description: CreateReducerParams params object
* @private
* @typedef {Object}
*/
type CreateReducerParams<
T extends Json = Json,
S extends State<T> = State<T>
> = {
name?: string;
actions?: ReducerCollection<T, S>;
};
/**
* Name: createReducer
* Description: createReducer API constructor
* @public
* @template T, S
* @param params CreateReducerParams<T, S>
* @returns Reducer<T, S>
*/
export const createReducer = <
T extends Json = Json,
S extends State<T> = State<T>
>(params?: CreateReducerParams<T, S>): Reducer<T, S> => {
const {
actions = {},
name = randomString(),
} = params;
let reducerActions = new Map<string | symbol, ReducerAction<T, S>>();
const reducer = {
dispatch: (state: S, action: Action<T>): Readonly<S> => {
return reducer.hasAction(action.type) ?
reducer.getAction(action.type)(state) :
state;
},
getAction: (key: string | symbol) => {
return reducerActions.get(key);
},
getAllActions(): Readonly<ReducerCollection<T, S>> {
const reducerActionsObject: ReducerCollection<T, S> = {};
for (const [key, val] of reducerActions) {
reducerActionsObject[key] = val;
}
return reducerActionsObject;
},
hasAction: (name: string | symbol): boolean => {
return reducerActions.has(name);
},
get name(): Readonly<string> {
return name;
},
removeAction: (name: string | symbol): void => {
reducerActions.delete(name);
},
removeAllActions(): void {
reducerActions = new Map<string | symbol, ReducerAction<T, S>>();
},
setAction: (name: string | symbol, action: ReducerAction<T, S>) => {
reducerActions.set(name, action);
},
setActions: (actionsObject: ReducerCollection<T, S> = {}) => {
Reflect.ownKeys(actionsObject)
.forEach((action: string | symbol) => {
reducer.setAction(action, actionsObject[action]);
});
},
};
reducer.setActions(actions);
return reducer;
};