diff options
| author | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
|---|---|---|
| committer | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
| commit | bce557cc2dc767628bed6aac87301a1be7c5431b (patch) | |
| tree | b51a051228d01fe3306cd7626d4a96768aadb944 /node_modules/@jet-app/app-store/tmp/src/foundation/wrappers/cached-bag.js | |
init commit
Diffstat (limited to 'node_modules/@jet-app/app-store/tmp/src/foundation/wrappers/cached-bag.js')
| -rw-r--r-- | node_modules/@jet-app/app-store/tmp/src/foundation/wrappers/cached-bag.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/node_modules/@jet-app/app-store/tmp/src/foundation/wrappers/cached-bag.js b/node_modules/@jet-app/app-store/tmp/src/foundation/wrappers/cached-bag.js new file mode 100644 index 0000000..e7ac2db --- /dev/null +++ b/node_modules/@jet-app/app-store/tmp/src/foundation/wrappers/cached-bag.js @@ -0,0 +1,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
\ No newline at end of file |
