Class Container

Binder and Injector (aka Container) to handle (a)synchronous dependency management.

Hierarchy

Implements

constructor

  • Create a new Container, with an optional parent Injector which will be searched if any given InjectableId is not bound within this Container.

    Parameters

    Returns Container

Properties

Protected Optional parent

parent: Injector

Protected providers

providers: Map<string | symbol | { constructor: any }, Provider<any>> = new Map<InjectableId<any>, Provider>()

Methods

bindAsyncFactory

  • Bind an InjectableId to an asynchronous factory that will be invoked on demand when the object is needed. The factory should produce the needed value (asynchronously of course). NOTE: The container will not invoke any @PostConstruct present on the class, this is the responsibility of the factory. WARNING!!! The factory may not throw and must return a valid Promise (which can be pending, resolved, rejected, etc.).

    Parameters

    Returns BindAs<T, AsyncFactory<T>>

bindClass

bindConstant

  • Bind an InjectableId to a constant value. Constants are by their very nature singleton, and are assumed to be error proof.

    Parameters

    Returns void

bindFactory

  • Bind an InjectableId to a synchronous factory that will be invoked on demand when the object is needed. The factory should produce the needed value NOTE: The container will not invoke any @PostConstruct present on the class, this is the responsibility of the factory.

    Parameters

    Returns BindAs<T, SyncFactory<T>>

get

  • Return an instance of <T> previously bound to 'id'.

    throws

    Error if the InjectableId was never registered, OR if there are unresolved asynchronous dependencies in the dependency tree for 'id'.

    Parameters

    Returns T

isIdKnown

  • Check to see if the existing InjectableId is known (aka has been bound). Error callbacks may wish to know if a particular InjectableId is available. Also the Binder's bindXXX calls always overwrite any previous bindings, so you may want to use this as a gate.

    Parameters

    Returns boolean

removeBinding

  • This method is not part of the Binding interface, because it is highly unusual. But that doesn't mean we can't imagine scenarios where you might require it.

    Parameters

    Returns void

resolve

resolveSingletons

  • resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<void>
  • This essentially pre creates/loads all singleton InjectableIds currently known to the Binder. This may be helpful if you wish to use Injector.get on a dependency tree that has asynchronous singletons within the tree.

    Parameters

    • Optional asyncOnly: boolean
    • Optional parentRecursion: boolean

    Returns Promise<void>

    A Promise that resolves when all Singleton's have been resolved, OR rejects if one or more of the Singleton's failed to resolve. NOTE: Rejection does not occur until all Singleton resolutions have settled, and the rejection reason/err will be a Map<InjectableId, Error>

Protected resolveState

  • As implied by the name prefix, this is a factored out method invoked only by the 'resolve' method. It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.

    Parameters

    Returns State

Generated using TypeDoc