For AI agents: the complete documentation index is available at https://navios.tech/llms.txt, the full documentation bundle is available at https://navios.tech/llms-full.txt, and this page is available as Markdown at https://navios.tech/docs/di/essentials/lifecycle.md.

Lifecycle

An instance is constructed, initialised, used and eventually destroyed. Two optional methods let a service take part in the second and last step.

HookCalledMay return a promise
onServiceInit()Once, right after construction, before anyone receives the instanceYes
onServiceDestroy()Once, when the instance is destroyedYes
import { Injectable } from '@navios/di'

import type { OnServiceDestroy, OnServiceInit } from '@navios/di'

@Injectable()
class ConnectionPool implements OnServiceInit, OnServiceDestroy {
  private connections: Connection[] = []

  async onServiceInit() {
    this.connections = await openConnections(4)
  }

  async onServiceDestroy() {
    await Promise.all(this.connections.map((connection) => connection.close()))
  }
}

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.

// DatabaseService comes from the quick start
@Injectable()
class UserCache implements OnServiceInit {
  private readonly db = inject(DatabaseService)
  private readonly pool = inject(ConnectionPool)
  private admin: ReturnType<DatabaseService['findUser']>

  onServiceInit() {
    this.admin = this.db.findUser('1') // safe: db and pool are ready
  }
}

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:

InstanceonServiceInitonServiceDestroy
Built from a class (@Injectable, provide(…, Class))YesYes
Stored with addInstance()NoYes
Returned by a factory or given as { value }NoNo

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.

import { Container, inject, Injectable } from '@navios/di'

@Injectable()
class Database {
  onServiceDestroy() {
    console.log('database closed')
  }
}

@Injectable()
class Repository {
  private readonly db = inject(Database)

  onServiceDestroy() {
    console.log('repository flushed')
  }
}

const container = new Container()
await container.get(Repository)
await container.dispose()
// repository flushed
// database closed

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:

  1. Everything that depends on it is destroyed.
  2. Its own onServiceDestroy and destroy listeners run.
  3. 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.

process.on('SIGTERM', async () => {
  await container.dispose()
  process.exit(0)
})
container.dispose()scope.dispose()
DestroysSingletonsThe scope's transients, then request-scoped instances
Leaves aloneOpen scopes. Dispose those firstSingletons
AfterwardsThe container is closed for goodThe scope is closed for good
A second callReturns the disposal in flightReturns the disposal in flight

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.

void container.get(ConnectionPool) // starts an async init, nobody awaits it
await container.ready() // resolves once the pool is initialised

container.getSync(ConnectionPool) // safe now

A failed creation does not make ready() reject. The failure is reported to whoever asked for the service.