rrule-rust
Version:
RRule implementation for browsers and Node.js written in Rust
45 lines (44 loc) • 1.01 kB
JavaScript
export class OperationCache {
constructor({ disabled }) {
this._disabled = disabled;
this.store = new Map();
}
get disabled() {
return this._disabled;
}
getOrSet(key, defaultValue) {
if (this.disabled) {
return defaultValue;
}
if (this.store.has(key)) {
return this.store.get(key);
}
this.store.set(key, defaultValue);
return defaultValue;
}
getOrCompute(key, compute) {
if (this.disabled) {
return compute();
}
if (this.store.has(key)) {
return this.store.get(key);
}
const value = compute();
this.store.set(key, value);
return value;
}
disable() {
this._disabled = true;
}
enable() {
this._disabled = false;
}
clear() {
this.store.clear();
}
clone() {
return new OperationCache({
disabled: this.disabled,
});
}
}