UNPKG

@dudousxd/nestjs-diagnostics

Version:

A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.

93 lines • 3.35 kB
/** * A process-wide registry of every `aviary:<lib>:<event>` channel name created or * emitted through this package. * * `node:diagnostics_channel` has no wildcard subscription: you can only subscribe * to a channel by its exact name. A generic observer (the Telescope watcher) that * wants to record *all* diagnostics therefore needs to (a) subscribe to every * channel that exists now and (b) be told when a new one appears. This registry * provides both: {@link registeredChannels} for the current set and * {@link onChannelRegistered} for future additions. */ const REGISTRY_KEY = Symbol.for('@dudousxd/nestjs-diagnostics:registry'); const globalStore = globalThis; const registry = globalStore[REGISTRY_KEY] ?? { channels: new Set(), listeners: new Set(), }; globalStore[REGISTRY_KEY] = registry; const { channels, listeners } = registry; /** * Record a channel name, notifying {@link onChannelRegistered} listeners the first * time it is seen. Idempotent: re-registering an existing name is a no-op. * Called by {@link getChannel}/{@link emit} so every channel touched through this * package is discoverable. */ export function registerChannel(name) { if (channels.has(name)) return; channels.add(name); for (const listener of listeners) { try { listener(name); } catch { // A misbehaving observer must never break event emission. } } } /** A snapshot of every `aviary:<lib>:<event>` channel registered so far. */ export function registeredChannels() { return [...channels]; } /** * Subscribe to channel registrations. The callback fires once per channel name * registered *after* this call. Returns an unsubscribe function. * * Note: this does NOT replay existing channels — a generic watcher should pair * it with {@link registeredChannels} to cover current + future names: * * ```ts * for (const name of registeredChannels()) subscribe(name); * const off = onChannelRegistered(subscribe); * ``` */ export function onChannelRegistered(cb) { listeners.add(cb); return () => { listeners.delete(cb); }; } /** * Hooks run by {@link resetRegistry} so other modules can drop registry-derived * caches in lockstep. {@link getChannel} memoizes channels and only registers a * name on a cache MISS; if `resetRegistry` cleared `channels` without clearing * that cache, an already-cached channel would never re-register and would vanish * from discovery. Registering its cache-clear here keeps the test-only reset * exact: after a reset, the next `getChannel`/`emit` re-registers every channel. */ const resetHooks = new Set(); /** * Register a callback run by {@link resetRegistry}. Used internally by * {@link getChannel}'s memo cache; not part of the public contract. */ export function onRegistryReset(cb) { resetHooks.add(cb); } /** * Test-only: forget every registered channel and listener. Not part of the * public contract — exposed so suites can assert registration in isolation. */ export function resetRegistry() { channels.clear(); listeners.clear(); for (const hook of resetHooks) { try { hook(); } catch { // A misbehaving reset hook must never break the reset itself. } } } //# sourceMappingURL=registry.js.map