r3bl-ts-utils
Version:
The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu
114 lines • 4.07 kB
JavaScript
/*
* Copyright (c) 2022 R3BL LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheImpl = void 0;
const kotlin_lang_utils_1 = require("../lang-utils/kotlin-lang-utils");
const analytics_1 = require("./analytics");
const debug_1 = require("./debug");
class CacheImpl {
name;
maxSize;
evictionPolicy;
_map = new Map();
analytics = analytics_1.Analytics.createInstance();
constructor(name, maxSize, evictionPolicy) {
this.name = name;
this.maxSize = maxSize;
this.evictionPolicy = evictionPolicy;
}
clear = () => {
const { _map: map } = this;
map.clear();
};
get = (arg) => {
const { _map: map, analytics } = this;
analytics.update(arg);
return map.has(arg)
? (0, kotlin_lang_utils_1._let)(map.get(arg), (value) => {
// eslint-disable-next-line
if (!value)
throw Error(`Value could not be found for key: ${arg}`);
return value;
})
: undefined;
};
getAndComputeIfAbsent = (arg, keyNotFoundFn) => {
const { _map: map, analytics, cleanUp } = this;
analytics.update(arg);
return map.has(arg)
? (0, kotlin_lang_utils_1._let)(map.get(arg), (value) => {
// eslint-disable-next-line
if (!value)
throw Error(`Value could not be found for key: ${arg}`);
return value;
})
: (0, kotlin_lang_utils_1._also)(keyNotFoundFn(arg), (value) => {
map.set(arg, value);
cleanUp();
});
};
getAndComputeIfAbsentAsync = (arg, keyNotFoundAsyncFn) => {
const { _map: map, analytics, cleanUp } = this;
analytics.update(arg);
return map.has(arg)
? new Promise((resolveFn) => {
resolveFn(map.get(arg));
})
: new Promise((resolveFn, rejectFn) => {
keyNotFoundAsyncFn(arg).then((value) => {
map.set(arg, value);
cleanUp();
resolveFn(value);
debug_1.DEBUG && console.log("⏰ keyNotFoundAsync resolved", value);
}, (error) => {
debug_1.DEBUG && console.error(`Error while computing value for key: ${arg}`, error);
rejectFn(error);
});
});
};
cleanUp = () => {
const { _map: map, maxSize, evictionPolicy: policy, analytics } = this;
if (map.size <= maxSize)
return;
let keyToDelete = undefined;
switch (policy) {
case "least-recently-used":
keyToDelete = analytics.findLRUKey(this);
break;
case "least-frequently-used":
keyToDelete = analytics.findLFUKey();
break;
}
if (keyToDelete) {
debug_1.DEBUG && console.log("🪓 keyToDelete", keyToDelete);
map.delete(keyToDelete);
analytics.purge(keyToDelete);
}
};
put = (arg, value) => {
const { _map: map } = this;
map.set(arg, value);
};
contains = (arg) => this._map.has(arg);
/** Getter that returns the size. */
get size() {
return this._map.size;
}
}
exports.CacheImpl = CacheImpl;
//# sourceMappingURL=cache-impl.js.map
;