UNPKG

xflight

Version:

Handle inflight promise to avoid async duplication

108 lines 3.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_assert_1 = __importDefault(require("node:assert")); const optional_require_1 = require("optional-require"); const getBluebirdPromiseImpl = () => (0, optional_require_1.optionalRequire)("bluebird", { default: globalThis.Promise }); const getAveazulPromiseImpl = () => (0, optional_require_1.optionalRequire)("aveazul", { default: globalThis.Promise }); class Inflight { _count = 0; _inflights = {}; Promise; constructor(xPromise) { this.Promise = xPromise || getBluebirdPromiseImpl() || getAveazulPromiseImpl(); } promise(key, func) { const f = this._inflights[key]; if (f) { return f.value; } const remove = () => this.remove(key); try { const p = func(); (0, node_assert_1.default)(p && typeof p.then === "function", `xflight: func for key ${key} didn't return a promise`); this.add(key, p).then(remove, remove); return p; } catch (err) { return this.Promise.reject(err); } } add(key, value, now) { (0, node_assert_1.default)(this._inflights[key] === undefined, `xflight: item ${key} already exist`); this._count++; now = now || Date.now(); this._inflights[key] = { start: now, lastXTime: now, value }; return value; } get(key) { const x = this._inflights[key]; return x && x.value; } remove(key) { (0, node_assert_1.default)(this._inflights[key] !== undefined, `xflight: removing non-existing item ${key}`); (0, node_assert_1.default)(this._count > 0, `xflight: removing item ${key} but list is empty - count ${this._count}`); this._count--; if (this._count === 0) { this._inflights = {}; } else { this._inflights[key] = undefined; } } get isEmpty() { return this._count === 0; } get count() { return this._count; } getStartTime(key) { const x = this._inflights[key]; return x && x.start; } time(key, now) { const x = this._inflights[key]; if (x) { return (now || Date.now()) - x.start; } return -1; } elapseTime(key, now) { return this.time(key, now); } getCheckTime(key) { const x = this._inflights[key]; return x && x.lastXTime; } lastCheckTime(key, now) { const x = this._inflights[key]; if (x) { const t = (now || Date.now()) - x.lastXTime; return t; } return -1; } elapseCheckTime(key, now) { return this.lastCheckTime(key, now); } resetCheckTime(key, now) { if (key) { const x = this._inflights[key]; if (x) { x.lastXTime = now || Date.now(); } } else { // If no key is provided, reset all Object.values(this._inflights).forEach(x => { if (x) x.lastXTime = now || Date.now(); }); } return this; } } exports.default = Inflight; //# sourceMappingURL=index.cjs.map