-
Notifications
You must be signed in to change notification settings - Fork 0
/
semidux.js
68 lines (65 loc) · 1.81 KB
/
semidux.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function isFunction(functionToCheck) {
return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
}
const doSideEffect = (sideEffect, action, currentState, store) => {
const { effect } = sideEffect;
if (isFunction(effect)) {
const result = effect(action, currentState, store);
if (result && result.type) {
store.dispatch(result);
}
return;
}
if (effect && effect.length) {
effect.map((doAction) => {
if (isFunction(doAction)) {
const result = doAction(action, currentState, store);
if (result && result.type) {
store.dispatch(result);
}
if (result && result.length) {
result.forEach(a => store.dispatch(a));
}
}
return action;
});
}
};
const createSideEffect = (listEffects) => (store) => next => (action) => {
next(action);
const currentState = store.getState();
if (!action.type) {
return;
}
action.payload = action.payload || {};
if (listEffects && listEffects.length) {
listEffects.forEach((s) => {
const { filter: filterFn, type } = s;
if (
(isFunction(filterFn) && type)
&& filterFn(action, currentState)
&& action.type === type) {
return doSideEffect(s, action, currentState, store);
}
if (type && action.type === type
&& !isFunction(filterFn)) {
return doSideEffect(s, action, currentState, store);
}
if (isFunction(filterFn) && filterFn(action, currentState)
&& !type) {
return doSideEffect(s, action, currentState, store);
}
return s;
});
}
};
const combineSideEffect = (...props) => {
if (props && props.length) {
return createSideEffect(Array.prototype.concat.apply([], props));
}
return null;
};
export {
createSideEffect,
combineSideEffect
};