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
|
import { isSome } from "@jet/environment";
import { Wrapper } from "./wrapper";
/**
* A bag wrapper that implements caching. All accessed values are cached until this object is reinitialized via rebootstrap or recreated.
*
*/
export class CachedBag extends Wrapper {
constructor() {
super(...arguments);
/**
* A cache of entries
*
* Note that `Opt<T>` values which are nil are stored, representing that the bag has no value for a key, which
* is coalesced in the JS. This prevents infinite cache misses when the bag has no value for a key, resulting in
* unnecessary calls to the native client
*/
this.cache = {};
}
registerBagKeys(keys) {
this.implementation.registerBagKeys(keys);
}
string(key, forceDisableCache = false) {
return this.fromCache(key, forceDisableCache, () => {
return this.implementation.string(key);
});
}
double(key, forceDisableCache = false) {
return this.fromCache(key, forceDisableCache, () => {
return this.implementation.double(key);
});
}
integer(key, forceDisableCache = false) {
return this.fromCache(key, forceDisableCache, () => {
return this.implementation.integer(key);
});
}
boolean(key, forceDisableCache = false) {
return this.fromCache(key, forceDisableCache, () => {
return this.implementation.boolean(key);
});
}
array(key, forceDisableCache = false) {
return this.fromCache(key, forceDisableCache, () => {
return this.implementation.array(key);
});
}
dictionary(key, forceDisableCache = false) {
return this.fromCache(key, forceDisableCache, () => {
return this.implementation.dictionary(key);
});
}
url(key, forceDisableCache = false) {
return this.fromCache(key, forceDisableCache, () => {
return this.implementation.url(key);
});
}
/**
* Returns a cached value if possible to do so, otherwise uses the closure to fetch a new value which is cached and returned
*
* @param key The key on which to cache
* @param forceDisableCache Disables the cache to ensure a fresh value from the bag
* @param fetchValue A closure to fetch new values with
* @returns A previously or newly cached value
*/
fromCache(key, forceDisableCache = false, fetchValue) {
if (forceDisableCache) {
return fetchValue();
}
else {
const cacheEntry = this.cache[key];
if (isSome(cacheEntry)) {
return cacheEntry.value;
}
else {
const newValue = fetchValue();
this.cache[key] = { value: newValue };
return newValue;
}
}
}
}
//# sourceMappingURL=cached-bag.js.map
|