diff --git a/packages/modules/module/src/configurator.ts b/packages/modules/module/src/configurator.ts index 7eac84130..1c56062e9 100644 --- a/packages/modules/module/src/configurator.ts +++ b/packages/modules/module/src/configurator.ts @@ -19,27 +19,67 @@ import type { import { SemanticVersion } from './lib/semantic-version'; import { BaseModuleProvider, type IModuleProvider } from './lib/provider'; +/** + * Represents a configurator for modules. + * + * @template TModules - The array of modules. + * @template TRef - The reference type. + */ export interface IModulesConfigurator< TModules extends Array = Array, TRef = any, > { logger: IModuleConsoleLogger; + + /** + * Configures the modules using the provided module configurators. + * + * @param configs - The array of module configurators. + */ configure(...configs: Array>): void; + /** + * Adds a module configurator to the list of configurators. + * + * @param config - The module configurator to add. + */ addConfig(config: IModuleConfigurator): void; + /** + * Initializes the modules with the specified reference. + * + * @param ref - The reference object. + * @returns A promise that resolves to the initialized modules instance. + */ initialize | unknown>( ref?: TRef, ): Promise>>; + /** + * Registers a callback function to be called when the modules are configured. + * + * @param cb - The callback function. + */ onConfigured( cb: (config: ModulesConfigType>) => void | Promise, ): void; + /** + * Registers a callback function to be called when the modules are initialized. + * + * @param cb - The callback function. + */ onInitialized | unknown>( cb: (instance: ModulesInstanceType>) => void, ): void; + /** + * Disposes the modules instance with the specified reference. + * + * @param instance - The modules instance to dispose. + * @param ref - The reference object. + * @returns A promise that resolves when the disposal is complete. + */ dispose(instance: ModulesInstanceType, ref?: TRef): Promise; } @@ -50,6 +90,9 @@ export interface IModuleConfigurator) => void | Promise; } +/** + * Error thrown when a required module times out. + */ class RequiredModuleTimeoutError extends Error { constructor() { super('It was too slow'); @@ -57,39 +100,78 @@ class RequiredModuleTimeoutError extends Error { } } -/** entry for configuring a module */ +/** + * Callback function type for configuring modules. + * @template TRef The reference type. + * @param config The modules configuration. + * @param ref Optional reference parameter. + */ type ModulesConfiguratorConfigCallback = ( config: ModulesConfig<[AnyModule]>, ref?: TRef, ) => void | Promise; +/** + * Configurator class for modules. + * @template TModules - Array of modules. + * @template TRef - Reference type. + */ export class ModulesConfigurator = Array, TRef = any> implements IModulesConfigurator { + /** + * Logger instance for the configurator. + */ public logger: ModuleConsoleLogger = new ModuleConsoleLogger('ModulesConfigurator'); + /** + * Array of configuration callbacks. + */ protected _configs: Array> = []; - // eslint-disable-next-line @typescript-eslint/no-unused-vars + /** + * Array of callbacks to be executed after configuration. + */ protected _afterConfiguration: Array<(config: any) => void> = []; - // eslint-disable-next-line @typescript-eslint/no-unused-vars + /** + * Array of callbacks to be executed after initialization. + */ protected _afterInit: Array<(instance: any) => void> = []; + /** + * Set of modules. + */ protected _modules: Set; + /** + * Constructs a new ModulesConfigurator instance. + * @param modules - Array of modules. + */ constructor(modules?: Array) { this._modules = new Set(modules); } + /** + * Gets the array of modules. + * @returns Array of modules. + */ get modules(): Array { return [...this._modules]; } + /** + * Configures the modules with the provided configurators. + * @param configs - Array of module configurators. + */ public configure(...configs: Array>) { configs.forEach((x) => this.addConfig(x)); } + /** + * Adds a module configurator. + * @param config - Module configurator. + */ public addConfig(config: IModuleConfigurator) { const { module, afterConfig, afterInit, configure } = config; this._modules.add(module); @@ -98,18 +180,31 @@ export class ModulesConfigurator = Array afterInit(instances[module.name])); } + /** + * Registers a callback to be executed after configuration. + * @param cb - Callback function. + */ public onConfigured( cb: (config: ModulesConfigType>) => void | Promise, ) { this._afterConfiguration.push(cb); } + /** + * Registers a callback to be executed after initialization. + * @param cb - Callback function. + */ public onInitialized( cb: (instance: ModulesInstanceType>) => void, ): void { this._afterInit.push(cb); } + /** + * Initializes the modules with the provided reference. + * @param ref - Reference object. + * @returns Promise that resolves to the initialized module instance. + */ public async initialize( ref?: R, ): Promise>> { @@ -123,6 +218,11 @@ export class ModulesConfigurator = Array( ref?: R, ): Promise>> { @@ -132,6 +232,11 @@ export class ModulesConfigurator = Array( ref?: R, ): Promise>> { @@ -168,6 +273,11 @@ export class ModulesConfigurator = Array( config: ModulesConfigType>, ): Promise { @@ -202,6 +312,12 @@ export class ModulesConfigurator = Array( config: ModulesConfigType>, ref?: R, @@ -215,6 +331,16 @@ export class ModulesConfigurator = Array moduleNames.includes(name); + /** + * Requires an instance of a module by name. + * + * @template TKey - The key type of the module. + * @param name - The name of the module to require. + * @param wait - The timeout duration in seconds for waiting the module to be initialized. Default is 60 seconds. + * @returns A promise that resolves to the instance of the required module. + * @throws Error if the module is not defined. + * @throws RequiredModuleTimeoutError if the module initialization times out. + */ const requireInstance = < TKey extends keyof ModulesInstanceType>, >( @@ -312,6 +438,11 @@ export class ModulesConfigurator = Array( instance: ModulesInstanceType>, ref?: R, diff --git a/packages/modules/signalr/src/SignalRModuleConfigurator.ts b/packages/modules/signalr/src/SignalRModuleConfigurator.ts index 0e6b1b652..2ded7705f 100644 --- a/packages/modules/signalr/src/SignalRModuleConfigurator.ts +++ b/packages/modules/signalr/src/SignalRModuleConfigurator.ts @@ -47,6 +47,7 @@ export type SignalRConfig = { */ export class SignalRModuleConfigBuilder< TDeps extends AnyModule[] | unknown = unknown, + // TODO - use BaseConfigBuilder > extends ModuleConfigBuilder { async addHub(name: string, config: SignalRHubConfig) { this._config.addHub(name, config);