UNPKG

@mdf.js/service-registry

Version:

MMS - API - Service Registry

75 lines 3.16 kB
/** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. */ import { Layer } from '@mdf.js/core'; import { LoggerInstance } from '@mdf.js/logger'; import EventEmitter from 'events'; import { ErrorRecord, HandleableError } from '../types'; /** * The Aggregator class is responsible for aggregating and managing error events * from various components within an application. It allows for centralized error * handling, supporting a structured approach to error logging and potentially * error recovery strategies. * * It extends EventEmitter to emit error events, enabling other parts of the * application to listen and respond to these error events as needed. */ export declare class Aggregator extends EventEmitter { private readonly logger; protected readonly maxSize: number; protected readonly includeStack: boolean; /** Hold registered errors */ private _errors; /** Hold registered errors from workers */ private _workersErrors; /** Timestamp of the last update */ private _lastUpdate; /** Map to keep track of registered components */ private readonly components; /** * Creates an instance of Aggregator. * @param logger - Logger instance for logging error registration and handling. * @param maxSize - Maximum number of errors to keep in the registry. Older errors are removed as * new ones are added. * @param includeStack - Flag to determine if stack traces should be included in the error * records. */ constructor(logger: LoggerInstance, maxSize?: number, includeStack?: boolean); /** * Register a component or a list of components to monitor for errors. * @param component - Component or list of components to be registered */ register(component: Layer.Observable | Layer.Observable[]): void; /** * Adds an error to the registry, converting it to a structured format. * @param error - The error to register. */ push(error: HandleableError): void; /** * Updates the registry with errors from worker threads/processes. * @param errors - Array of errors from workers. */ updateWorkersErrors(errors: ErrorRecord[]): void; /** @returns Last update date */ get lastUpdate(): string; /** @returns Returns a combined list of all the registered errors */ get errors(): ErrorRecord[]; /** @returns The current number of registered errors */ get size(): number; /** Clear the error registry */ clear(): void; /** Cleans up by removing error event listeners and clearing the registry. */ close(): void; /** * Generates an error handling function for a specific component. * @param subject - The name of the component. * @returns A function that takes an error, converts it, and adds it to the registry. */ private readonly errorEventHandler; /** Check if the check is valid to be monitored */ private isValidComponent; } //# sourceMappingURL=Aggregator.d.ts.map