-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (78 loc) · 1.92 KB
/
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
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
/**
* @author Luke Brandon Farrell
* @description
*/
import { all, call, put } from "redux-saga/effects";
import _omit from "lodash.omit";
import _castArray from "lodash.castarray";
import _map from "lodash.map";
import _isNil from "lodash.isnil";
/**
* Relays a action / function in redux-saga
*
* @param option - either a string (to relay a action), function (to trigger a function)
* @param action
* @param error - a saga callback if an error occurs
* @param transform - transform the action before it is relayed
*
* @return {IterableIterator<*>}
*/
export function* relay (option, action, error, transform = _transform) {
try {
/*
* We want to check if action is not nil, then apply a transform, this is useful
* for pulling out data which should not be relayed, e.g. redux analytics.
*/
const transformedAction = !_isNil(transform) ? transform(action) : action;
if (typeof option === 'string' || Array.isArray(option)) {
/*
* We cast the string to array, and string (if multiple) them to put
* methods, this allows one action to trigger multiple actions.
*/
const anchors = _map(_castArray(option), (item) => {
return put({
...transformedAction,
type: item,
})
});
// Relays multiple actions
yield all(anchors);
} else {
yield call(option, transformedAction);
}
} catch (e) {
if(error) yield call(error, e);
}
}
/**
* Extends and action
*
* @param action
* @param payload
* @param meta
*
* @return object
*/
export function extend(action, payload, meta) {
return {
...action,
payload: {
...action.payload,
...payload,
},
meta: {
...action.meta,
...meta
}
}
}
/**
* Default transform for removing analytics form actions
*
* @param action
* @return {{}}
* @private
*/
function _transform(action){
return _omit(action, ["meta.analytics"]);
}