-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.js
More file actions
66 lines (58 loc) · 1.82 KB
/
Cache.js
File metadata and controls
66 lines (58 loc) · 1.82 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: magic;
class Cache {
constructor(fetchKey, cacheExpiresInMinutes = 1440) {
this.fetchKey = fetchKey;
this.cacheExpiresInMinutes = cacheExpiresInMinutes;
}
get() {
const cacheObj = Keychain.get(this.fetchKey);
if (cacheObj) {
return JSON.parse(cacheObj);
} else {
return null;
}
}
set(data) {
const expires_at = new Date(
Date.now() + this.cacheExpiresInMinutes * 60 * 1000
);
Keychain.set(this.fetchKey, JSON.stringify({ data, expire_at: expires_at }));
}
contains(key) {
return Keychain.contains(key);
}
isCacheValid(cachedData) {
return new Date(cachedData.expire_at) > Date.now();
}
static async isConnectedToInternet() {
try {
await new Request("https://www.google.com").load();
return true;
} catch {
return false;
}
}
async getOrFetch(fetchFn) {
const hasCache = this.contains(this.fetchKey);
let cacheObj = hasCache ? this.get() : null;
const isCacheValid = cacheObj && this.isCacheValid(cacheObj);
if (isCacheValid) return cacheObj.data;
const canFetch = await Cache.isConnectedToInternet();
if (canFetch) {
try {
const newData = await fetchFn();
this.set(newData);
return newData;
} catch (e) {
if (cacheObj) return cacheObj.data;
throw e;
}
} else {
if (cacheObj) return cacheObj.data;
throw Error("No cached data & internet connection.");
}
}
}
module.exports = Cache;