UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

36 lines (31 loc) 974 B
const commons = require('../../commons'); const utils = require('../utils/cache.utils'); class ResourceCache { #name; #class; #known; #max; constructor(name, _class, max) { this.#name = name; this.#class = _class; this.#max = max; this.reset(); } getSize = () => Object.values(this.#known).length; has = id => this.#known.hasOwnProperty(id); get = id => Promise.resolve(this.#known[id]); set = (id, resource) => { return Promise.all([utils.resolveId(id), utils.resolveResource(resource, this.#class, this.#name)]) .then(([id, resource]) => { if (this.getSize() >= this.#max) return commons.errors.too.big.reject(`${this.#name} cache`, this.#max); this.#known[id] = resource; return Promise.resolve(resource); }) .catch(error => Promise.reject(error)); }; reset = () => { this.#known = {}; return this; }; } module.exports = ResourceCache;