createContainer
createContainer
is a function that creates and returns a container to manage modules in the application. Containers specify dependencies, define initialization conditions, and control the order of module startup.
Syntax
createContainer(config: ContainerConfig): Container
Parameters
-
config:
ContainerConfig
— configuration object for the container- id (
string
) - Required. Unique identifier of the container. - domain (
string
) - Required. A category for a container. - dependencies (
Container[]
) - Optional. List of strict dependencies. These containers must be in thedone
status for the current container to transition topending
. - optionalDependencies (
Container[]
) - Optional. List of optional dependencies. These containers can be in thedone
,fail
, oroff
status for the current container to activate. - start (
(api?: DepsApi & OptionalDepsApi, enabled: {keyof api: boolean}) => { api: {} }
) - Required. Initialization function of the container, invoked upon activation.- Accepts:
- api (optional) — an object with APIs from required and optional dependencies.
- enabled (optional) — an object showing which dependencies are enabled (true) or not (false).
- Returns: an object with an api property, which contains data or methods that other containers can use.
- Accepts:
- enable (
() => boolean | Promise<boolean>
) - Optional. Function that determines if the container will be activated. Returnstrue
to activate the container, andfalse
to set the container tooff
.- Accepts:
- api (optional) — an object with APIs from required and optional dependencies.
- enabled (optional) — an object showing which dependencies are enabled (true) or not (false).
- Returns:
boolean | Promise<boolean>
- Accepts:
- id (
Example
const userContainer = createContainer({ id: 'user', domain: 'users', dependencies: ['auth'], optionalDependencies: ['settings'], start: () => { return { api: { getUser: () => ({ id: 1, name: 'John Doe' }) } }; }, enable: () => true,});