UNPKG

@devgrid/common

Version:
50 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimedMap = void 0; class TimedMap { constructor(timeoutMs, callback) { this.map = new Map(); this.timeout = timeoutMs ?? 1000; this.timeoutCallback = callback ?? ((key) => this.map.delete(key)); } set(key, value, callback, timeout) { this.clearTimeout(key); const timer = setTimeout(callback ? callback : this.timeoutCallback, Number.isInteger(timeout) ? timeout : this.timeout, key); this.map.set(key, { value, timer }); } get(key) { return this.map.get(key)?.value; } forEach(callback, thisArg) { this.map.forEach((obj, key) => { callback.call(thisArg, obj.value, key, this); }); } *entries() { for (const [key, obj] of this.map.entries()) { yield [key, obj.value]; } } *values() { for (const obj of this.map.values()) { yield obj.value; } } delete(key) { this.clearTimeout(key); return this.map.delete(key); } clear() { this.map.forEach((obj) => { clearTimeout(obj.timer); }); this.map.clear(); } clearTimeout(key) { if (this.map.has(key)) { clearTimeout(this.map.get(key).timer); } } } exports.TimedMap = TimedMap; //# sourceMappingURL=timed-map.js.map