forked from michaelcontento/redux-storage
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreateMiddleware.js
More file actions
111 lines (92 loc) · 3.7 KB
/
createMiddleware.js
File metadata and controls
111 lines (92 loc) · 3.7 KB
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
111
import isFunction from 'lodash.isfunction';
import isObject from 'lodash.isobject';
import { save as actionSave } from './actions';
import { LOAD, SAVE } from './constants';
function swallow() {
}
function warnAboutConfusingFiltering(blacklist, whitelist) {
blacklist
.filter((item) => whitelist.indexOf(item) !== -1)
.forEach((item) => {
console.warn( // eslint-disable-line no-console
`[redux-storage] Action ${item} is on BOTH black- and whitelist.`
+ ` This is most likely a mistake!`
);
});
}
function isValidAction(action) {
const isFunc = isFunction(action);
const isObj = isObject(action);
const hasType = isObj && action.hasOwnProperty('type');
if (!isFunc && isObj && hasType) {
return true;
}
if (process.env.NODE_ENV !== 'production') {
if (isFunc) {
console.warn( // eslint-disable-line no-console
`[redux-storage] ACTION IGNORED! Actions should be objects`
+ ` with a type property but received a function! Your`
+ ` function resolving middleware (e.g. redux-thunk) must be`
+ ` placed BEFORE redux-storage!`
);
} else if (!isObj) {
console.warn( // eslint-disable-line no-console
`[redux-storage] ACTION IGNORED! Actions should be objects`
+ ` with a type property but received: ${action}`
);
} else if (!hasType) {
console.warn( // eslint-disable-line no-console
`[redux-storage] ACTION IGNORED! Action objects should have`
+ ` a type property.`
);
}
}
return false;
}
function handleWhitelist(action, actionWhitelist) {
if (Array.isArray(actionWhitelist)) {
return actionWhitelist.length === 0
? true // Don't filter if the whitelist is empty
: actionWhitelist.indexOf(action.type) !== -1;
}
// actionWhitelist is a function that returns true or false
return actionWhitelist(action);
}
export default (engine, actionBlacklist = [], actionWhitelist = [], options = {}) => {
const opts = Object.assign({ disableDispatchSaveAction: false }, options);
// Also don't save if we process our own actions
const blacklistedActions = [...actionBlacklist, LOAD, SAVE];
if (process.env.NODE_ENV !== 'production' && Array.isArray(actionWhitelist)) {
warnAboutConfusingFiltering(actionBlacklist, actionWhitelist);
}
return ({ dispatch, getState }) => {
return (next) => (action) => {
const result = next(action);
if (!isValidAction(action)) {
return result;
}
const isOnBlacklist = blacklistedActions.indexOf(action.type) !== -1;
const isOnWhitelist = handleWhitelist(action, actionWhitelist);
// Skip blacklisted actions
if (!isOnBlacklist && isOnWhitelist) {
const saveState = getState();
const saveAction = actionSave(saveState);
if (process.env.NODE_ENV !== 'production') {
if (!saveAction.meta) {
saveAction.meta = {};
}
saveAction.meta.origin = action;
}
const dispatchSave = () => dispatch(saveAction);
engine.save(saveState)
.then(() => {
if (opts.disableDispatchSaveAction === false) {
return dispatchSave();
}
})
.catch(swallow);
}
return result;
};
};
};