This repository was archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResourceCache.js
More file actions
331 lines (270 loc) · 9.34 KB
/
Copy pathResourceCache.js
File metadata and controls
331 lines (270 loc) · 9.34 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, MapCreator
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import mitt from 'mitt';
/**
* Used for caching resources. Requires the resource to have an unique id field
* @see {@link PaginatedResourceWrapper}
* @todo Add periodic data refreshing while idle, most likely implemented in cache
* @todo Hook into api network traffic to figure out if updates are needed
*/
export default class ResourceCache {
constructor(api, cacheTime = api.defaults.cacheSeconds) {
this._api = api;
this.cacheTime = cacheTime;
this.emitter = mitt();
this._storage = {};
// Prevent observers
Object.freeze(this);
}
/**
* Push a page into the cache
* @param {PaginatedResourceListing} page - Data to be cached
* @param {boolean} [diff=false] - Differential result used for updating the cache. Cache entry won't be used to for key dropping.
* @returns {void}
* @todo diff documentation
*/
push(page, diff = false) {
if (page.rows === 0) {
return; // Don't insert empty pages
}
delete page.__ob__; // Remove VueJs observer
Object.freeze(page);
// Test if this is data we can actually work with by testing if there are any non-numeric ids (undefined etc)
const invalidData = page.data.map(row => row.id).filter(x => typeof x !== 'number').length > 0;
if (invalidData) {
throw new TypeError('Missing or invalid row.id for page.data. Data rows must to contain a numeric "id" field.');
}
const validThrough = this._timestamp() + this.cacheTime;
const data = {
page, validThrough, diff,
timeout: setTimeout(
() => this.revalidate(page.route),
this.cacheTime * 1000,
),
};
const storage = this._storage[page.route] || (this._storage[page.route] = {});
(storage[page.cacheToken] || (storage[page.cacheToken] = [])).push(data);
this.emitter.emit('push', {page, validThrough, resourceUrl: page.route});
this.emitter.emit('invalidate', {resourceUrl: page.route});
}
/**
* Revalidate all data and delete stale data
* @param {String} resourceUrl - Resource url
* @returns {void}
*/
revalidate(resourceUrl = null) {
if (!resourceUrl) {
Object.keys(this._storage).map(this.revalidate);
} else if (this._storage[resourceUrl]) {
const storage = this._storage[resourceUrl];
// Remove old data from the cache and stop old timeouts
Object.keys(storage).forEach(key => {
storage[key]
.filter(row => row.validThrough < this._timestamp())
.forEach(row => clearTimeout(row.timeout));
storage[key] = storage[key].filter(row => row.validThrough >= this._timestamp());
});
const junk = Object.keys(storage).filter(key => storage[key].length === 0);
// Delete empty
junk.forEach(key => delete storage[key]);
if (Object.keys(storage).length === 0) {
delete this._storage[resourceUrl];
}
if (junk.length > 0) {
this.emitter.emit('invalidate', {resourceUrl});
}
}
}
/**
* Collect relevant cached pages
* @param {String} resourceUrl - resource url
* @param {String} cacheToken - Cache token
* @see {@link PaginatedResourceListing#cacheToken}
* @returns {Array<PaginatedResourceListing>} - Relevant cached pages
*/
collectPages(resourceUrl, cacheToken = '') {
cacheToken = cacheToken.toLowerCase();
// Storage array or []
const storage = (this._storage[resourceUrl] || {})[cacheToken] || [];
// Sort by validThrough and extract pages
return storage.sort((a, b) => a.validThrough - b.validThrough);
}
/**
* Clears the cache
* @param {String} resourceUrl - Resource url
* @returns {void}
*/
clear(resourceUrl = '') {
if (!resourceUrl) {
Object.keys(this._storage).forEach(url => {
this.emitter.emit('invalidate', {resourceUrl: url});
});
this._storage = {};
} else {
delete this._storage[resourceUrl];
this.emitter.emit('invalidate', {resourceUrl});
}
}
/**
* Resolve cache and return indexed data
* @param {String} resourceUrl - Resource url
* @param {String} cacheToken - Cache token
* @see {@link PaginatedResourceListing#cacheToken}
* @returns {Array} - Indexed relevant data
* @todo add page numbers or range as optional parameter
*/
resolve(resourceUrl, cacheToken = '') {
cacheToken = cacheToken.toLowerCase();
const data = this.collectPages(resourceUrl, cacheToken);
const out = [];
let lastStart;
let lastEnd;
let lastPage;
for (const row of data) {
const page = row.page;
if (page.rows === 0) {
// We can't do anything if we don't have any data
continue;
}
const ids = page.data.map(row => row.id);
const startId = Math.min(...ids);
const endId = Math.max(...ids);
const badKeys = [];
if (page.page - 1 === lastPage || page.page === lastPage) {
badKeys.push(...this._diffRange(lastEnd, startId));
}
if (page.page + 1 === lastPage || page.page === lastPage) {
badKeys.push(...this._diffRange(endId, lastStart));
}
lastStart = startId;
lastEnd = endId;
for (const row of page.data) {
out[row.id] = row;
}
if (!row.diff) {
// Grab list of applicable ids and delete offending
// keys that no longer exist in the newer data set
Object
.keys(out)
.map(Number)
.filter(key => startId <= key && key <= endId)
.filter(key => !ids.includes(key))
.forEach(key => badKeys.push(key));
}
badKeys.forEach(key => delete out[key]);
}
return out;
}
/**
* Update records in the cache manually
* @param {ResourceBase|Array<ResourceBase>} rows - Data to be updated
* @returns {void} - nothing
*/
update(rows) {
if (!(rows instanceof Array)) {
this.update([rows]);
return;
}
// Split up data into types
const data = {};
const ids = {};
for (const row of rows) {
const key = row.constructor.name;
(data[key] || (data[key] = [])).push(row);
(ids[key] || (ids[key] = [])).push(row.id);
}
const models = Object.keys(data);
for (const resourceUrl of Object.keys(this._storage)) {
let invalidate = false;
for (const token of Object.keys(this._storage[resourceUrl])) {
const entries = this._storage[resourceUrl][token];
for (const entry of entries) {
const page = entry.page;
if (page.data.length === 0) {
continue;
}
const key = page.data[0].constructor.name;
if (!models.includes(key)) {
break;
}
for (const row of page.data) {
if (!ids[key].includes(row.id)) {
continue;
}
const index = ids[key].findIndex(x => x === row.id);
const value = data[key][index];
value.sanitize();
value.fieldNames.forEach(x => {
row[x] = value[x];
});
invalidate = true;
}
}
}
if (invalidate) {
this.emitter.emit('invalidate', {resourceUrl: resourceUrl});
}
}
}
/**
* Used for key elimination. Calculates the keys between two indexes.
* @param {Number} start - Start index
* @param {Number} end - End index
* @returns {Array} - keys
* @private
* @example
* cache._diffRange(1, 5) === [2, 3, 4]
*/
_diffRange(start, end) {
if (start > end) {
const _x = end;
end = start;
start = _x;
}
if (start === end || start - end === 1) {
return [];
}
const out = [];
for (let i = start + 1; i < end; i++) {
out.push(i);
}
return out;
}
/**
* Get a usable timestamp
* @returns {number} - timestamp
* @private
*/
_timestamp() {
return Math.floor(Date.now() / 1000);
}
}