Skip to content

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 the done status for the current container to transition to pending.
    • optionalDependencies (Container[]) - Optional. List of optional dependencies. These containers can be in the done, fail, or off 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.
    • enable (() => boolean | Promise<boolean>) - Optional. Function that determines if the container will be activated. Returns true to activate the container, and false to set the container to off.
      • 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>

Example

const userContainer = createContainer({
id: 'user',
domain: 'users',
dependencies: ['auth'],
optionalDependencies: ['settings'],
start: () => {
return { api: { getUser: () => ({ id: 1, name: 'John Doe' }) } };
},
enable: () => true,
});