-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (55 loc) · 1.56 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
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
export class AppCacher {
constructor(name, ttl) {
this.name = name;
this.ttl = ttl;
this.cache = null;
if (!isCacheAvailable()) {
return console.error('cacheAPI is not supported in your browser.');
}
}
create() {
caches.open(this.name).then((res) => {
this.cache = res;
});
if(this.ttl) setTimeout(()=> { this.delete() }, this.ttl)
}
add(req) {
this.cache.add(req).then(r => console.log('Request added to cache.'));
}
addAll(req) {
this.cache.addAll(req).then(r => console.log('Requests added to cache.'));
}
async get(req, options = {}) {
return await this.cache.match(req, options);
}
async getAll(req, options = {}) {
return await this.cache.matchAll(req, options);
}
remove(req){
this.cache.delete(req).then(r => console.log('Request removed.'));
}
delete() {
caches.delete(this.name).then(() => console.log('Cache deleted.'));
}
put(req, resp) {
caches.put(req, resp).then(() => console.log('Request successfully put to cache.'));
}
has(cacheName) {
caches.has(cacheName).then((boolean) => {
return boolean;
});
}
getAllCacheNames() {
caches.keys().then((cacheNames) => {
return cacheNames
});
}
getStorageQouta() {
navigator.storage.estimate().then((estimate) => {
return estimate;
});
}
}
function isCacheAvailable() {
return 'caches' in self;
}