-
Notifications
You must be signed in to change notification settings - Fork 9
/
idb_cache.ts
59 lines (51 loc) · 1.88 KB
/
idb_cache.ts
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
import { openDB, DBSchema } from 'idb';
import { Runtime } from './runtime';
import { log } from './util';
const IDB_CACHE_VERSION = 2;
type CachePredicate = (segment: Runtime.CacheSegment, key: string) => boolean;
interface CacheSchema extends DBSchema {
[Runtime.CacheSegment.Registration]: {
key: string;
value: Runtime.LoadedModule;
};
[Runtime.CacheSegment.Resolution]: {
key: string;
value: string;
};
}
export function createCache(name: string, predicate?: CachePredicate): Runtime.Cache {
const idbPromise = openDB<CacheSchema>(name, IDB_CACHE_VERSION, {
async upgrade(db, oldVersion, newVersion, tx) {
log('Upgrading cache from version %s to %s', oldVersion, newVersion);
switch (oldVersion) {
case 0:
db.createObjectStore(Runtime.CacheSegment.Registration);
db.createObjectStore(Runtime.CacheSegment.Resolution);
case 1:
await tx.objectStore(Runtime.CacheSegment.Registration).clear();
await tx.objectStore(Runtime.CacheSegment.Resolution).clear();
}
},
});
return {
clear(segment?: Runtime.CacheSegment) {
const clearSegments = segment ? [segment] : Object.values(Runtime.CacheSegment);
return Promise.all(clearSegments.map(segment => idbPromise.then(idb => idb.clear(segment))));
},
delete(segment: Runtime.CacheSegment, key: string) {
if (!predicate || predicate(segment, key)) {
return idbPromise.then(idb => idb.delete(segment, key));
}
},
get(segment: Runtime.CacheSegment, key: string) {
if (!predicate || predicate(segment, key)) {
return idbPromise.then(idb => idb.get(segment, key) as any);
}
},
set(segment: Runtime.CacheSegment, key: string, value: any) {
if (!predicate || predicate(segment, key)) {
return idbPromise.then(idb => idb.put(segment, value, key));
}
},
};
}