UNPKG

redis-smq

Version:

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

58 lines 1.85 kB
function isDisposable(disposable) { return (!!disposable && typeof disposable === 'object' && 'shutdown' in disposable && typeof disposable.shutdown === 'function'); } export class ComponentRegistry { static components = new Set(); 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); } } //# sourceMappingURL=component-registry.js.map