Skip to main content

Container

The Container class is a box to hold all registered factories.

In Octo, we minimize the use of the new keyword and get instance of a class using its factory. The factory has full control over how the instance is created.

By using the Container and Factory concepts, it is possible to override internal class definitions at runtime. It is an incredibly powerful tool to customize implementation.

@group

Functions/Container

Index

Methods

copyFactories

  • copyFactories(): {}
  • Returns {}

    • [key string]: FactoryContainer<unknown>[]

get

  • get<T, F>(type: string | Constructable<T>, options?: { args?: Parameters<F[create]>; metadata?: {} }): Promise<T>
  • container.get() allows to get an instance of a class using its factory. If the factory is not yet registered, it places a blocking promise in the queue to wait for the registration.


    Type parameters

    • T
    • F: Factory<T> = never

    Parameters

    • type: string | Constructable<T>

      The type or name of the class to get.

    • optionaloptions: { args?: Parameters<F[create]>; metadata?: {} }

      Allows selection of a specific factory to use to get the instance.

      • The args option supplies arguments to the factory.
      • The metadata option identifies the factory.
      • optionalargs: Parameters<F[create]>
      • optionalmetadata: {}

    Returns Promise<T>

    The instance of the class.

has

  • has<T>(type: string | Constructable<T>, options?: { metadata?: {} }): boolean
  • Type parameters

    • T

    Parameters

    • type: string | Constructable<T>
    • optionaloptions: { metadata?: {} }
      • optionalmetadata: {}

    Returns boolean

registerFactory

  • registerFactory<T>(type: string | Constructable<T>, factory: Factory<T>, options?: { metadata?: {} }): void
  • Container.registerFactory() allows to register a factory for a class.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

      The type or name of the class for which the factory is registered.

    • factory: Factory<T>

      The factory class being registered.

    • optionaloptions: { metadata?: {} }

      Distinguishes between different factories of the same class.

      • The metadata attaches custom metadata to the factory.
      • optionalmetadata: {}

    Returns void

registerStartupUnhandledPromise

  • registerStartupUnhandledPromise<T>(promise: Promise<T>): void
  • Type parameters

    • T

    Parameters

    • promise: Promise<T>

    Returns void

registerValue

  • registerValue<T>(type: string | Constructable<T>, value: T, options?: { metadata?: {} }): void
  • Type parameters

    • T

    Parameters

    • type: string | Constructable<T>
    • value: T
    • optionaloptions: { metadata?: {} }
      • optionalmetadata: {}

    Returns void

reset

  • reset(): void
  • Container.reset() clears all registered factories and empties the container. This is mostly used in testing.


    Returns void

setFactories

  • setFactories(factories: {}): void
  • Parameters

    • factories: {}

      Returns void

    setFactoryTimeout

    • setFactoryTimeout(timeoutInMs: number): void
    • The Container, by default, will wait a maximum of 5 seconds for a factory to resolve and provide an instance. This method allows to change that timeout.


      Parameters

      • timeoutInMs: number

        The timeout in milliseconds.

      Returns void

    unRegisterFactory

    • unRegisterFactory<T>(type: string | Constructable<T>, options?: { metadata?: {} }): void
    • Container.unRegisterFactory() will unregister all factories of a class.


      Type parameters

      • T

      Parameters

      • type: string | Constructable<T>

        The type or name of the class for which all factory is unregistered.

      • optionaloptions: { metadata?: {} }
        • optionalmetadata: {}

      Returns void

    waitToResolveAllFactories

    • waitToResolveAllFactories(): Promise<void>
    • Returns Promise<void>

    staticgetInstance