Skip to main content

@versionzero/module-manager / ModuleManager

Class: ModuleManager

Defined in: module-manager.js:28

Configuration, dependency injection, and lifecycle management for modular applications.

Provides services based on lightweight coding conventions or sidecar information rather than requiring all participating systems to integrate with a framework.

Constructors​

Constructor​

new ModuleManager(options?): ModuleManager

Defined in: module-manager.js:70

Create a new ModuleManager instance.

You can provide your own Configurator or Schema (but not both), or use accessors after construction to tweak their behavior.

Parameters​

ParameterTypeDescription
options?{ schema?: Schema; configurator?: Configurator; }-
options.schema?Schemaoptional existing Schema that should be augmented
options.configurator?Configuratoroptional existing Configurator to use

Returns​

ModuleManager

Properties​

PropertyTypeDescription
abortControllerAbortControllerAvailable for other modules to request application shutdown. Modules can get the ModuleManager injected using DI, and call .abortController.abort().

Accessors​

moduleInfo​

Get Signature​

get static moduleInfo(): ModuleOptions

Defined in: module-manager.js:33

Returns​

ModuleOptions


configurator​

Get Signature​

get configurator(): Configurator

Defined in: module-manager.js:107

Get the Configurator in use by the ModuleManager.

Only getter access is provided; cannot be changed after construction.

Returns​

Configurator


resolver​

Get Signature​

get resolver(): SchemaResolver

Defined in: module-manager.js:118

Get the SchemaResolver that is being used to hold module schemas and reference schemas.

Only getter access is provided; cannot be changed after construction.

Returns​

SchemaResolver


schema​

Get Signature​

get schema(): Schema

Defined in: module-manager.js:129

Get the top-level Schema being built and managed by ModuleManager.

Only getter access is provided; cannot be changed after construction.

Returns​

Schema

Methods​

register()​

register(ModuleClass, options?): ModuleManager

Defined in: module-manager.js:147

Register a module with a ModuleClass that can be instantiated via a no-arguments constructor

A well-formed ModuleClass will either provide a moduleInfo static property containing multiple module settings, or individual static properties for each setting. For example, .moduleInfo.name or .moduleName.

Modules are stored internally keyed by the pascal-cased version of their name.

Parameters​

ParameterTypeDescription
ModuleClassModuleConstructableClass or factory function with static metadata.
options?ModuleOptionsOptional module properties

Returns​

ModuleManager


registerInstance()​

registerInstance(instance, options?): ModuleManager

Defined in: module-manager.js:160

Register a pre-instantiated module instance.

Parameters​

ParameterTypeDescription
instanceanyPre-created module instance.
options?ModuleOptionsOptional overrides (e.g. name)

Returns​

ModuleManager


getModule()​

getModule(moduleRequest): Module | undefined

Defined in: module-manager.js:171

Get a previously registered module by name or ModuleClass.

Parameters​

ParameterTypeDescription
moduleRequeststring | ModuleConstructable-

Returns​

Module | undefined


getModuleInstance()​

getModuleInstance(moduleRequest): any

Defined in: module-manager.js:180

Get the instantiated value of a previously registered module by name or ModuleClass.

Parameters​

ParameterTypeDescription
moduleRequeststring-

Returns​

any


findInstanceModule()​

findInstanceModule(instance): Module | undefined

Defined in: module-manager.js:192

Find the module that has the provided instance (if any).

Parameters​

ParameterTypeDescription
instanceany-

Returns​

Module | undefined


getInstantiatedModules()​

getInstantiatedModules(): Module[]

Defined in: module-manager.js:200

Get all instantiated modules in dependency order.

Returns​

Module[]


reference()​

reference(referenceRequest, options): ModuleManager

Defined in: module-manager.js:210

Register a module reference

Parameters​

ParameterTypeDescription
referenceRequeststring | ModuleConstructable-
optionsany-

Returns​

ModuleManager


getReference()​

getReference(referenceRequest): ModuleReference | undefined

Defined in: module-manager.js:228

Get the named ModuleReference.

Parameters​

ParameterTypeDescription
referenceRequeststring | ModuleConstructable-

Returns​

ModuleReference | undefined


hasReference()​

hasReference(referenceRequest): boolean

Defined in: module-manager.js:238

Check if the named ModuleReference already exists.

Parameters​

ParameterTypeDescription
referenceRequeststring | ModuleConstructable-

Returns​

boolean


invokeLifecycleMethod()​

invokeLifecycleMethod(methodName, options?): Promise<void>

Defined in: module-manager.js:262

Parameters​

ParameterTypeDescription
methodNamestring-
options?any-

Returns​

Promise<void>


invokeMainMethod()​

invokeMainMethod(methodName, options?): Promise<void>

Defined in: module-manager.js:289

Parameters​

ParameterTypeDescription
methodNamestring-
options?any-

Returns​

Promise<void>


configure()​

configure(context): Promise<any>

Defined in: module-manager.js:323

Configure modules

Loads configuration per the top-level aggregated module schema. Each module schema is gated by a condition that causes it to be skipped if there is no corresponding module instance, so initially all configuration is skipped except pre-instantiated modules (like the main module).

However, named references to modules within the active part of the configuration causes them to be instantiated, which has a cascading effect, activating new schemas, which contain new references, which trigger new instances...

(You normally want to use .run(context) to both configure and run lifecycle methods.)

Parameters​

ParameterTypeDescription
contextany-

Returns​

Promise<any>


initModules()​

initModules(config, reload?): Promise<void>

Defined in: module-manager.js:341

Initialize all instantiated modules

Runs the init lifecycle method and/or does dependency injection if the inject flag is set. If the reload flag is set, only modules that support hot reloading will be re-initialized.

Parameters​

ParameterTypeDefault valueDescription
configanyundefined-
reload?booleanfalse-

Returns​

Promise<void>


run()​

run(context?): Promise<void>

Defined in: module-manager.js:377

Main call that choreographs the entire module graph lifecycle.

  1. Uses Configurator to create configuration based on the aggregated module Schema definitions.
  2. Initializes and starts all referenced modules in dependency order.
  3. Awaits the main module (if any).
  4. Stops and terminates the modules.
  5. Exits.

Parameters​

ParameterTypeDescription
contextany-

Returns​

Promise<void>