Modules
Modules are essentially named singletons structured in a dependency graph.
They may be classes (which will be instantiated on-demand):
class MyModule {}
moduleManager.register(MyModule, options);
These classes should have a no-argument constructor.
Or, they can be pre-created instances if they correspond to an existing value, or if their construction is complex:
const myComplicatedModule = new MyComplicatedModule('xyzzy', 'plugh');
moduleManager.registerInstance(myComplicatedModule, options);
You can also register fairly arbitrary objects as instances, but you will need to specify a module name via the registration options:
const foo = {bar: true}
moduleManager.registerInstance(foo, {name: "foo"})
Module Settings
Each module has optional settings (documented in other sections) that control how it is managed.
| setting | type | purpose |
|---|---|---|
name | string | module name; inferred when possible |
ModuleClass | ModuleConstructable | class reference with no-argument constructor |
instance | any | resolved singleton instance associated with the module |
schema | Schema|object | configurable schema definition |
inject | boolean | whether to inject dependencies; inferred true if init is not implemented |
reload | boolean | true if the module supports hot reloading of configuration |
lifecycle | boolean | whether to run lifecycle methods |
isMain | boolean | marks entry point; inferred true if main is implemented |
definitions | array | other module defintions to register |
provides | string | abstract/interface type name to provide |
initConfig | string | if full, init receives entire application config |
lifecycleInit | string | method name override; defaults to init |
lifecycleStart | string | ...defaults to start |
lifecycleMain | string | ...defaults to main |
lifecycleStop | string | ...defaults to stop |
lifecycleTerminate | string | ...defaults to terminate |
These settings can be provided in one of three ways:
- as an individual static value in the module class prepended with
module, in camelCase:
class MyModule {
static moduleLifecycle = false;
}
- as a member of a static
moduleInfoobject:
class MyModule {
static moduleInfo = {
lifecycle: false
}
}
- as part of the
optionsobject passed to theregistercall:
class MyModule {}
moduleManager.register(MyModule, { lifecycle: false })
Module settings are individually merged with base class settings, allowing selective overrides. Any settings passed to the register call then override any internally defined settings.