forked from jeffbski/redux-logic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
110 lines (96 loc) · 3.1 KB
/
logic.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
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
import { createLogic } from 'redux-logic';
import { NOTIFY_CREATE, NOTIFY_REMOVE, NOTIFY_QUEUE,
NOTIFY_DISPLAY_QUEUED,
notifyQueue, notifyRemove, notifyDisplayQueued
} from './actions';
import { selectors as notifySel } from './reducer';
export const MAX_DISPLAY = 3;
export const DISPLAY_TIME = 3000;
export const notifyCreateLogic = createLogic({
type: NOTIFY_CREATE,
// check to see if we can add directly
validate({ getState, action }, allow, reject) {
const state = getState();
const current = notifySel.messages(state);
const queue = notifySel.queue(state);
if (current.length < MAX_DISPLAY && !queue.length) {
allow(action);
} else {
reject(notifyQueue(action.payload));
}
},
// if we had added directly then schedule remove
process({ action }, dispatch) {
const msg = action.payload;
setTimeout(() => {
dispatch(notifyRemove([msg]));
}, DISPLAY_TIME);
}
});
export const notifyRemoveLogic = createLogic({
type: NOTIFY_REMOVE,
// everytime we remove an item, if queued, send action to display one
process({ getState, action }, dispatch) {
// unless other middleware/logic introduces async behavior, the
// state will have been updated by the reducers before process runs
const state = getState();
const queue = notifySel.queue(state);
if (queue.length) {
dispatch(notifyDisplayQueued());
} else { // nothing to do
//tell process we're done with empty dispatch
dispatch();
}
}
});
export const notifyQueuedLogic = createLogic({
type: NOTIFY_QUEUE,
// after we queue an item, if display is already clear
// we add in a displayQueued to ensure things aren't stuck
process({ getState }, dispatch) {
// just in case things had already cleared out,
// check to see if can display yet, normally
// any remove actions trigger this check but if already
// empty we will queue one up for good measure
setTimeout(() => {
const state = getState();
const current = notifySel.messages(state);
if (!current.length) {
dispatch(notifyDisplayQueued());
} else { // the next remove will trigger display
dispatch(); // nothing to dispatch, but tell process we are done
}
}, 100);
}
});
export const notifyDisplayQueuedLogic = createLogic({
type: NOTIFY_DISPLAY_QUEUED,
// if we have opening(s) and queued, display them
validate({ getState, action }, allow, reject) {
const state = getState();
const current = notifySel.messages(state);
const queue = notifySel.queue(state);
const needed = MAX_DISPLAY - current.length;
if (needed > 0 && queue.length) {
allow({
...action,
payload: queue.slice(0, needed)
});
} else {
reject(); // preventing msg from continuing
}
},
// schedule removes for those displayed
process({ action }, dispatch) {
const arrMsgs = action.payload;
setTimeout(() => {
dispatch(notifyRemove(arrMsgs));
}, DISPLAY_TIME);
}
});
export default [
notifyCreateLogic,
notifyRemoveLogic,
notifyQueuedLogic,
notifyDisplayQueuedLogic
];