@puregram/scenes
Version:
Simple implementation of middleware-based scene management for puregram
49 lines (48 loc) • 1.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheRepository = void 0;
class CacheRepository {
constructor({ sortingValues } = {}) {
this.collection = new Map();
this.keys = [];
this.values = [];
this.sortingValues = sortingValues;
}
/** Checks has value by key */
has(key) {
return this.collection.has(key);
}
/** Sets value by key */
set(key, value) {
this.collection.set(key, value);
this.keys = [...this.collection.keys()];
this.values = [...this.collection.values()];
if (this.sortingValues) {
this.values.sort(this.sortingValues);
}
}
/** Returns value by key */
get(key) {
return this.collection.get(key);
}
/** Sets value by key else error if exits */
strictSet(key, value) {
if (this.collection.has(key)) {
throw new Error(`value by ${key} already exists`);
}
return this.set(key, value);
}
/** Returns value by key else error */
strictGet(key) {
const value = this.get(key);
if (!value) {
throw new Error(`value by ${key} not found`);
}
return value;
}
/** Returns iterator */
[Symbol.iterator]() {
return this.collection[Symbol.iterator]();
}
}
exports.CacheRepository = CacheRepository;