This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
storage.js
121 lines (112 loc) · 3.87 KB
/
storage.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
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
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {StorageTypes} from 'utils/constants';
import {getPrefix} from 'utils/storage_utils';
export function setItem(name, value) {
return (dispatch, getState) => {
const state = getState();
const prefix = getPrefix(state);
dispatch({
type: StorageTypes.SET_ITEM,
data: {prefix, name, value, timestamp: new Date()},
});
return {data: true};
};
}
export function removeItem(name) {
return (dispatch, getState) => {
const state = getState();
const prefix = getPrefix(state);
dispatch({
type: StorageTypes.REMOVE_ITEM,
data: {prefix, name},
});
return {data: true};
};
}
export function setGlobalItem(name, value) {
return (dispatch) => {
dispatch({
type: StorageTypes.SET_GLOBAL_ITEM,
data: {name, value, timestamp: new Date()},
});
return {data: true};
};
}
export function removeGlobalItem(name) {
return (dispatch) => {
dispatch({
type: StorageTypes.REMOVE_GLOBAL_ITEM,
data: {name},
});
return {data: true};
};
}
export function clear(options = {exclude: []}) {
return (dispatch) => {
dispatch({
type: StorageTypes.CLEAR,
data: options,
});
return {data: true};
};
}
export function actionOnGlobalItemsWithPrefix(prefix, action) {
return (dispatch) => {
dispatch({
type: StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX,
data: {prefix, action},
});
return {data: true};
};
}
export function actionOnItemsWithPrefix(prefix, action) {
return (dispatch, getState) => {
const state = getState();
const globalPrefix = getPrefix(state);
dispatch({
type: StorageTypes.ACTION_ON_ITEMS_WITH_PREFIX,
data: {globalPrefix, prefix, action},
});
return {data: true};
};
}
export function storageRehydrate(incoming, persistor) {
return (dispatch, getState) => {
const state = getState();
persistor.pause();
Object.keys(incoming).forEach((key) => {
const storage = {};
try {
const value = JSON.parse(incoming[key]);
if (value === null) {
storage[key] = {value, timestamp: new Date()};
} else if (typeof state.storage.storage[key] === 'undefined') {
if (typeof value.timestamp === 'undefined') {
storage[key] = {value, timestamp: new Date()};
} else {
storage[key] = {value: value.value, timestamp: new Date(value.timestamp)};
}
} else if (typeof value.timestamp === 'undefined') {
storage[key] = {value, timestamp: new Date()};
} else if (typeof state.storage.storage[key].timestamp === 'undefined') {
storage[key] = {value: value.value, timestamp: new Date(value.timestamp)};
} else if (new Date(value.timestamp) > state.storage.storage[key].timestamp) {
storage[key] = {value: value.value, timestamp: new Date(value.timestamp)};
} else {
return;
}
} catch (err) {
if (process.env.NODE_ENV !== 'production') { // eslint-disable-line no-process-env
console.warn('Error rehydrating data for key "storage"', err); // eslint-disable-line no-console
}
}
dispatch({
type: StorageTypes.STORAGE_REHYDRATE,
data: storage,
});
});
persistor.resume();
return {data: true};
};
}