Application teardown
defineAppTeardown creates an async cleanup function for application-owned resources.
This macro is experimental and may be removed in a future release. Avoid exposing it as part of a library's stable public API.
import {
createSingleton,
defineAppTeardown,
} from "compdi/macros";
const database = createSingleton({
factory: () => openDatabase(),
});
const server = createSingleton({
factory: (database: Database) => startServer(database),
deps: [database],
});
export const teardown = defineAppTeardown([database, server]);
process.once("SIGTERM", async () => {
await teardown();
process.exit(0);
});
Resources are disposed in reverse order, so the server is released before the database it depends on. Supported disposal protocols are resolved during the transform.
For scoped resources, prefer the scope's release(context) method. Application teardown is intended for resources owned by the full application lifecycle.