Container
Index
Methods
copyFactories
Returns {}
[key string]: FactoryContainer<unknown>[]
get
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.
- The
optionalargs: Parameters<F[create]>
optionalmetadata: {}
Returns Promise<T>
The instance of the class.
has
Type parameters
- T
Parameters
type: string | Constructable<T>
optionaloptions: { metadata?: {} }
optionalmetadata: {}
Returns boolean
registerFactory
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.
- The
optionalmetadata: {}
Returns void
registerStartupUnhandledPromise
Type parameters
- T
Parameters
promise: Promise<T>
Returns void
registerValue
Type parameters
- T
Parameters
type: string | Constructable<T>
value: T
optionaloptions: { metadata?: {} }
optionalmetadata: {}
Returns void
reset
Container.reset()
clears all registered factories and empties the container. This is mostly used in testing.Returns void
setFactories
Parameters
factories: {}
Returns void
setFactoryTimeout
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
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
Returns Promise<void>
staticgetInstance
Returns 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.
Functions/Container