/
Store.react.js
230 lines (199 loc) · 5.81 KB
/
Store.react.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import R from 'ramda';
import React from 'react';
import PropTypes from 'prop-types';
function dataCheck(data, old) {
// Assuming data and old are of the same type.
if (R.isNil(old) || R.isNil(data)) {
return true;
}
const type = R.type(data);
if (type === 'Array') {
if (data.length !== old.length) {
return true;
}
for (let i = 0; i < data.length; i++) {
if (dataCheck(data[i], old[i])) {
return true;
}
}
} else if (R.contains(type, ['String', 'Number'])) {
return old !== data;
} else if (type === 'Object') {
return R.any(([k, v]) => dataCheck(v, old[k]))(Object.entries(data));
}
return false;
}
class MemStore {
constructor() {
this._data = {};
this._modified = -1;
}
getItem(key) {
return this._data[key];
}
setItem(key, value) {
this._data[key] = value;
this.setModified(key);
}
removeItem(key) {
delete this._data[key];
this.setModified(key);
}
// noinspection JSUnusedLocalSymbols
setModified(_) {
this._modified = Date.now();
}
// noinspection JSUnusedLocalSymbols
getModified(_) {
return this._modified;
}
}
class WebStore {
constructor(storage) {
this._storage = storage;
}
getItem(key) {
return JSON.parse(this._storage.getItem(key));
}
setItem(key, value) {
this._storage.setItem(key, JSON.stringify(value));
this.setModified(key);
}
removeItem(key) {
this._storage.removeItem(key);
this._storage.removeItem(`${key}-timestamp`);
}
setModified(key) {
this._storage.setItem(`${key}-timestamp`, Date.now());
}
getModified(key) {
return (
Number.parseInt(this._storage.getItem(`${key}-timestamp`), 10) || -1
);
}
}
const _localStore = new WebStore(window.localStorage);
const _sessionStore = new WebStore(window.sessionStorage);
/**
* Easily keep data on the client side with this component.
* The data is not inserted in the DOM.
* Data can be in memory, localStorage or sessionStorage.
* The data will be kept with the id as key.
*/
export default class Store extends React.Component {
constructor(props) {
super(props);
if (props.storage_type === 'local') {
this._backstore = _localStore;
} else if (props.storage_type === 'session') {
this._backstore = _sessionStore;
} else if (props.storage_type === 'memory') {
this._backstore = new MemStore();
}
this.onStorageChange = this.onStorageChange.bind(this);
}
onStorageChange(e) {
const {id, setProps} = this.props;
if (e.key === id && setProps && e.newValue !== e.oldValue) {
setProps({
data: JSON.parse(e.newValue),
modified_timestamp: this._backstore.getModified(id),
});
}
}
componentWillMount() {
const {setProps, id, data, storage_type} = this.props;
if (storage_type !== 'memory') {
window.addEventListener('storage', this.onStorageChange);
}
const old = this._backstore.getItem(id);
if (R.isNil(old) && data) {
// Initial data mount
this._backstore.setItem(id, data);
if (setProps) {
setProps({
modified_timestamp: this._backstore.getModified(id),
});
}
return;
}
if (setProps && dataCheck(old, data)) {
setProps({
data: old,
modified_timestamp: this._backstore.getModified(id),
});
}
}
componentWillUnmount() {
if (this.props.storage_type !== 'memory') {
window.removeEventListener('storage', this.onStorageChange);
}
}
componentDidUpdate() {
const {data, id, clear_data, setProps} = this.props;
if (clear_data) {
this._backstore.removeItem(id);
if (setProps) {
setProps({
clear_data: false,
data: null,
modified_timestamp: this._backstore.getModified(id),
});
}
} else if (data) {
const old = this._backstore.getItem(id);
// Only set the data if it's not the same data.
if (dataCheck(data, old)) {
this._backstore.setItem(id, data);
if (setProps) {
setProps({
modified_timestamp: this._backstore.getModified(id),
});
}
}
}
}
render() {
return null;
}
}
Store.defaultProps = {
storage_type: 'memory',
clear_data: false,
modified_timestamp: -1,
};
Store.propTypes = {
/**
* The key of the storage.
*/
id: PropTypes.string.isRequired,
/**
* The type of the web storage.
*
* memory: only kept in memory, reset on page refresh.
* local: window.localStorage, data is kept after the browser quit.
* session: window.sessionStorage, data is cleared once the browser quit.
*/
storage_type: PropTypes.oneOf(['local', 'session', 'memory']),
/**
* The stored data for the id.
*/
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array,
PropTypes.number,
PropTypes.string,
]),
/**
* Set to true to remove the data contained in `data_key`.
*/
clear_data: PropTypes.bool,
/**
* The last time the storage was modified.
*/
modified_timestamp: PropTypes.number,
/**
* Dash-assigned callback that gets fired when the value changes.
*/
setProps: PropTypes.func,
};