BaseModule
This file defines the BaseModule
class, which serves as the base class for all internal core packages in the @sodacore/core
library. It provides basic logging, automatic module registration in the Registry
, and lifecycle hooks for initialization, starting, and stopping.
Features
- Logging: Provides a protected
logger
property for use in derived classes. - Automatic Registration: Registers the module instance in the global
Registry
using metadata (type
andname
) from the class. - Lifecycle Hooks: Defines
init
,start
, andstop
methods for module lifecycle management, intended to be overridden by subclasses.
Usage
typescript
import BaseModule from './base/module';
class MyModule extends BaseModule {
async init() {
this.logger.info('Initializing MyModule');
// Custom initialization logic
}
async start() {
this.logger.info('Starting MyModule');
// Custom start logic
}
async stop() {
this.logger.info('Stopping MyModule');
// Custom stop logic
}
}
API
Constructor
typescript
constructor(protected config: IConfig)
- config: The configuration object for the module.
- Registers the module in the
Registry
using metadata.
Properties
- logger: An injected instance of the
Logger
class, available to subclasses.
Methods
async init(): Promise<void>
Lifecycle hook for initialization. Intended to be overridden by subclasses.
async start(): Promise<void>
Lifecycle hook for starting the module. Intended to be overridden by subclasses.
async stop(): Promise<void>
Lifecycle hook for stopping the module. Intended to be overridden by subclasses.
Notes
- The class uses dependency injection for the logger.
- Registration in the
Registry
allows for easy module discovery and management within the core system. - Extend this class for all internal core modules to ensure consistent behavior and integration.