-
Notifications
You must be signed in to change notification settings - Fork 0
/
saga.js
59 lines (53 loc) · 1.56 KB
/
saga.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
import { call, take, getContext, setContext } from "redux-saga/effects";
function localRule(key, rule) {
if (!rule || rule === "*") {
return action => action.widgetKey === key || action.widgetKey === "*";
}
if (typeof rule === "function") {
if (rule.hasOwnProperty("toString")) {
rule = rule.toString();
} else {
return action =>
(action.widgetKey === key || action.widgetKey === "*") && rule(action);
}
}
if (Array.isArray(rule)) {
return rule.map(v => localRule(key, v));
}
return action => {
return (
(action.widgetKey === key || action.widgetKey === "*") &&
action.type === rule
);
};
}
export function takeInWidget(rule) {
return call(function*() {
const key = yield getContext("widgetKey");
return yield take(localRule(key, rule));
});
}
export function takeEveryInWidget(rule, worker, ...args) {
return call(function*() {
const key = yield getContext("widgetKey");
return yield takeEvery(localRule(key, rule), worker, ...args);
});
}
export function takeLatestInWidget(rule, worker, ...args) {
return call(function*() {
const key = yield getContext("widgetKey");
return yield takeLatest(localRule(key, rule), worker, ...args);
});
}
export function putInWidget(action, widgetKey) {
if (widgetKey) {
return put({ ...action, widgetKey });
}
return call(function*() {
const widgetKey = yield getContext("widgetKey");
return yield put({ ...action, widgetKey });
});
}
export function putToAllWidgets(action) {
return put({ ...action, widgetKey: "*" });
}