-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredux.js
48 lines (35 loc) · 1.09 KB
/
redux.js
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
import produce from 'immer';
import { identity } from './fn.js';
// just trying some stuff out, yes I know redux toolkit is a thing
const mappedReducer = () => {
const newReducer = produce((state, action) => {
const red = newReducer.reducerMap[action.type];
red && red(state, action.data, action.type);
return state;
});
return newReducer;
};
export const reducer = (actionType, reducerFn) => {
const newReducer = mappedReducer();
const creator = data => ({
type: actionType,
data: data
});
creator.toString = () => actionType;
newReducer.actions = { [actionType]: creator };
newReducer.reducerMap = { [actionType]: reducerFn };
return newReducer;
};
export const caseReducer2 = (redf, redg) => {
const newReducer = mappedReducer();
newReducer.actions = {
...redf.actions,
...redg.actions
};
newReducer.reducerMap = {
...redf.reducerMap,
...redg.reducerMap
};
return newReducer;
};
export const caseReducers = (...reds) => reds.flat().reduce(caseReducer2, identity);