A container of resolve-able dependencies that are created via inversion-of-control.

Hierarchy

Constructors

Properties

blueprintSubscribers: Collection<Unsubscribe> = ...

Collection of subscriptions to ContainerBlueprint events. We keep this around so we can remove the subscriptions when the container is destroyed.

factories: Collection<AbstractFactory<unknown>> = ...

Collection of factories registered with this container.

instances: Collection<InstanceRef> = ...

Collection of singleton instances produced by this container.

staticOverrides: Collection<{
    base: StaticInstantiable<any>;
    override: StaticInstantiable<any>;
}> = ...

Collection of static-class overrides registered with this container.

waitingLifecycleCallbacks: Collection<WeakRef<AwareOfContainerLifecycle>> = ...

Collection of created objects that should have lifecycle events called on them, if they still exist.

waitingResolveCallbacks: Collection<{
    callback: ((t: unknown) => unknown);
    key: any;
}> = ...

Collection of callbacks waiting for a dependency key to be resolved.

makeHistory?: Collection<any>

The 100 most recent dependency keys that were make'd. Used to help with debugging cyclic dependency errors.

makeStack?: Collection<any>

List of dependency keys currently being make'd as a reverse stack. This is used to detect dependency cycles.

realizingContainer: boolean = false

Set to true when we're realizing a container. Used to prevent infinite recursion when getContainer() is accidentally called from somewhere within the realizeContainer() call.

Methods

  • Protected

    Check the makeStack for duplicates and throw an error if a dependency cycle is detected. This is used to prevent infinite mutual recursion when cyclic dependencies occur.

    Returns void

  • Perform any cleanup necessary to destroy this container instance.

    Returns void

  • Get a collection of dependency keys required by the given target, if it is registered with this container.

    Parameters

    • target: any

    Returns Collection<any>

  • Get the already-produced value for the given key, if one exists.

    Parameters

    • key: any

    Returns any

  • Returns true if the container has an already-produced value for the given key.

    Parameters

    • key: any

    Returns boolean

  • Returns true if the container has a factory for the given key.

    Parameters

    • key: any

    Returns boolean

  • Create an instance of the given target. The target can either be a DependencyKey registered with this container (in which case, the singleton value will be returned), or an instantiable class.

    If the instantiable class has the Injectable decorator, its injectable parameters will be automatically injected into the instance.

    Type Parameters

    • T

    Parameters

    • target: any
    • Rest ...parameters: any[]

    Returns T

  • Create a new instance of the dependency key using this container, ignoring any pre-existing instances in this container.

    Type Parameters

    • T

    Parameters

    • key: any
    • Rest ...parameters: any[]

    Returns T

  • Get a Promise that resolves the first time the given dependency key is resolved by the application. If it has already been resolved, the Promise will resolve immediately.

    Type Parameters

    • T

    Parameters

    • key: any

    Returns Promise<T>

  • Given a factory and manually-provided parameters, resolve the dependencies for the factory and produce its value.

    Type Parameters

    • T

    Parameters

    Returns T

  • Register a basic instantiable class as a standard Factory with this container, identified by a string name rather than static class.

    Parameters

    • name: string

      unique name to identify the factory in the container

    • dependency: Instantiable<any>

    Returns Container

  • Register the given function as a factory within the container.

    Parameters

    • name: any

      unique name to identify the factory in the container

    • producer: (() => any)

      factory to produce a value

        • (): any
        • Returns any

    Returns Container

  • Register a value as a singleton in the container. It will not be instantiated, but can be injected by its unique name.

    Type Parameters

    • T

    Parameters

    • key: any

      unique name to identify the singleton in the container

    • value: T

    Returns Container

  • Register a static class to the container along with its already-instantiated instance that will be used to resolve the class.

    Type Parameters

    • T

    Parameters

    • staticClass: any
    • instance: T

    Returns Container

  • Resolve the dependency key. If a singleton value for that key already exists in this container, return that value. Otherwise, use the factory and given parameters to produce and return the value.

    Parameters

    • key: any
    • Rest ...parameters: any[]

    Returns any

  • Given a Container instance, apply the ContainerBlueprint to it.

    Type Parameters

    Parameters

    • container: T

    Returns T