-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
38 lines (34 loc) · 823 Bytes
/
index.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
export default (...epics) => store => {
const handlers = [];
const action$ = (start, sink) => {
if (start !== 0) return;
const handler = action => sink(1, action);
sink(0, t => {
if (t === 2) {
handlers.splice(handlers.indexOf(handler), 1);
}
});
handlers.push(handler);
}
epics.forEach(epic => {
const source = epic(action$, store);
source(0, (t, d) => {
if (t === 1) {
store.dispatch(d);
}
});
});
return next => action => {
const result = next(action);
handlers.forEach(handler => handler(action));
return result;
}
}
export const ofType = type => source => (start, sink) => {
if (start !== 0) return;
source(0, (t, d) => {
if (t === 1) {
if (d.type === type) sink(1, d);
} else sink(t, d);
});
}