Lifecycle
An instance is constructed, initialised, used and eventually destroyed. Two optional methods let a service take part in the second and last step.
The OnServiceInit and OnServiceDestroy interfaces are optional type checks. The container looks for the methods, not the interfaces.
onServiceInit
Initialisation belongs here rather than in the constructor, for two reasons: the constructor cannot await, and it may run twice. onServiceInit runs once per instance, and inside it every injected dependency is fully initialised.
Dependencies initialise before the service that injects them: ConnectionPool.onServiceInit has resolved before UserCache.onServiceInit starts.
Return a promise only when the work is asynchronous. A hook that returns nothing keeps the service resolvable with getSync(); an async hook makes it, and everything that depends on it, asynchronous on first resolution.
If the constructor or the hook throws, resolution fails with a DIError with code InitializationError and the original error in cause. Nothing is stored, onServiceDestroy is not called, and the next get() tries again from scratch. If a decorator throws, the instance is initialised already and onServiceDestroy is called. A hook that acquires several resources should release the ones it already holds before rethrowing.
onServiceDestroy
onServiceDestroy runs when the instance is destroyed: by dispose() on its container or scope, or by invalidation. Release what onServiceInit acquired.
An error thrown or rejected by the hook does not stop the teardown: the rest still runs and dispose() still resolves. The error is passed to logger.error() of the container's logger, which is console.error() without one. logger: null drops it.
The hook is found on:
A factory registers cleanup for its result with ctx.addDestroyListener() instead.
Destruction order
A service is always destroyed before the things it injected, so onServiceDestroy can still use its dependencies. The one exception is a service that is still being built when a dependency is invalidated.
Services that do not depend on each other are destroyed in reverse order of creation, one after another. Each hook is awaited before the next instance is touched.
A hook may also resolve from the container. During dispose() it gets what has not been torn down yet; anything else fails with ContainerDisposed, since nothing is built any more. During invalidate(), resolve what the service depends on at the start of the hook, before the first await: after it, the container cannot tell the hook from other code, makes it wait for the invalidation, and the hook waits for itself.
For one instance, the steps are:
- Everything that depends on it is destroyed.
- Its own
onServiceDestroyand destroy listeners run. - The transients it injected are destroyed.
dispose
dispose() first waits for instances that are still being created, then destroys everything the container holds. From the moment it is called nothing new is built, and a get() that is still in flight fails with ContainerDisposed.
It waits for at most a second for what it does not hold: a transient that is still initialising, or the arguments that a factory token is computing. Either is destroyed as soon as it is ready, after dispose() has resolved if need be.
A closed container or scope throws ContainerDisposed. clear() of the testing containers destroys the same things and leaves the container usable.
dispose() called in a lifecycle hook starts the disposal and returns at once, since the disposal waits for that hook. Do not await it later in the hook, after another await: it then waits for itself.
Both implement Symbol.asyncDispose, so await using container = new Container() disposes at the end of the block.
ready
ready() resolves when no instance of the container is still being created. It does not start anything; it waits for what other code started.
A failed creation does not make ready() reject. The failure is reported to whoever asked for the service.