From b87ba57819a3dfa458b34c9cec9362c7028adf6e Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Mon, 26 May 2025 14:52:38 -0400 Subject: shutdown --- js/src/app/index.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'js/src/app') diff --git a/js/src/app/index.ts b/js/src/app/index.ts index 363c398d8..f0d371a34 100644 --- a/js/src/app/index.ts +++ b/js/src/app/index.ts @@ -22,7 +22,13 @@ export namespace App { log.info("created", { path: dataDir }); - const services = new Map(); + const services = new Map< + any, + { + state: any; + shutdown?: (input: any) => Promise; + } + >(); const result = { get services() { @@ -39,15 +45,22 @@ export namespace App { return result; } - export function state any>(key: any, init: T) { + export function state( + key: any, + init: (app: Info) => State, + shutdown?: (state: Awaited) => Promise, + ) { return () => { const app = ctx.use(); const services = app.services; if (!services.has(key)) { log.info("registering service", { name: key }); - services.set(key, init(app)); + services.set(key, { + state: init(app), + shutdown: shutdown, + }); } - return services.get(key) as ReturnType; + return services.get(key)?.state as State; }; } @@ -62,7 +75,12 @@ export namespace App { const app = await create(input); return ctx.provide(app, async () => { - return cb(app); + const result = await cb(app); + for (const [key, entry] of app.services.entries()) { + log.info("shutdown", { name: key }); + await entry.shutdown?.(await entry.state); + } + return result; }); } } -- cgit v1.2.3