UNPKG

redis-smq

Version:

A high-performance, reliable, and scalable message queue for Node.js.

62 lines 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentRegistry = void 0; function isDisposable(disposable) { return (!!disposable && typeof disposable === 'object' && 'shutdown' in disposable && typeof disposable.shutdown === 'function'); } class ComponentRegistry { static track(instance) { if (isDisposable(instance)) { ComponentRegistry.components.add(instance); } return instance; } static untrack(component) { return ComponentRegistry.components.delete(component); } static shutdownComponents(cb) { const toShutdown = Array.from(ComponentRegistry.components); if (toShutdown.length === 0) { return cb(); } let pending = toShutdown.length; let firstErr = null; toShutdown.forEach((component) => { try { component.shutdown((err) => { if (err && !firstErr) { firstErr = err; } ComponentRegistry.components.delete(component); if (--pending === 0) { cb(firstErr || null); } }); } catch (syncError) { if (!firstErr && syncError instanceof Error) { firstErr = syncError; } ComponentRegistry.components.delete(component); if (--pending === 0) { cb(firstErr || null); } } }); } static clear() { ComponentRegistry.components.clear(); } static get size() { return ComponentRegistry.components.size; } static has(component) { return ComponentRegistry.components.has(component); } } exports.ComponentRegistry = ComponentRegistry; ComponentRegistry.components = new Set(); //# sourceMappingURL=component-registry.js.map