-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (30 loc) · 1.06 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
import { diff as semverDiff, coerce } from 'semver';
export const versionPlugin = ({ version, name, action, diff, predicate = (oldVer, newVer) => oldVer !== newVer } = {}) => {
return !process.server ? (store) => {
const currentVersion = coerce(version || 1);
const itemKey = name || 'application-store-version';
const localVersion = coerce(localStorage.getItem(itemKey) || 0);
function cleanTheMess() {
localStorage.clear();
localStorage.setItem(itemKey, currentVersion);
if (action && store._actions[action]) {
store.dispatch(action);
}
}
function hasDiff() {
if (diff) {
const diffList = Array.isArray(diff) ? diff : [diff];
const currentDiff = semverDiff(localVersion, currentVersion);
if (diffList.includes(currentDiff)) {
return true;
}
return false;
} else {
return typeof predicate === 'function' && predicate(localVersion, currentVersion);
}
}
if (!localVersion || hasDiff()) {
cleanTheMess();
}
} : () => {};
};