Skip to content

DI Bag API / index / DisposerContext

Interface: DisposerContext ​

Defined in: acquisition-context.ts:33

Why a pushed disposer is running: the factory never returned, or it did and the service disposer — the withDisposal on the value this factory returned — has just run. Ownership a consumer attaches to a transformed value does not count.

See ​

https://dany-fedorov.github.io/di-bag/guides/tutorial.html#release-partial-acquisition

Example ​

ts
type Socket = { close(): Promise<void> };
declare function open(url: string): Promise<Socket>;
declare function authenticate(connection: Socket): Promise<void>;
const socket = DiBag.providerWithDisposal({
  provider: DiBag.createProvider(async ({ url }: { url: string }, factoryContext) => {
    const connection = await open(url);
    // Runs at once if authenticate() rejects; at close() it sees 'service-disposed' and does nothing.
    factoryContext.pushDisposer(async disposerContext => { if (disposerContext.reason !== 'service-disposed') await connection.close(); });
    await authenticate(connection);
    return connection;
  }, { factoryReceivesContext: true }),
  disposeService: connection => connection.close(),
});

Properties ​

reason ​

ts
readonly reason: 'factory-failed' | 'no-service-disposer' | 'service-disposed' | 'service-disposal-failed';

Defined in: acquisition-context.ts:40

'factory-failed': the factory threw, rejected, or was cancelled; no service exists. 'no-service-disposer': the factory returned and no withDisposal owns that value. 'service-disposed': the service disposer ran without throwing. 'service-disposal-failed': the service disposer threw; pushed disposers still run.

Ordinary services. Checked composition. Explicit ownership.