summaryrefslogtreecommitdiffhomepage
path: root/js/src/app
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-26 14:52:38 -0400
committerDax Raad <[email protected]>2025-05-26 14:52:38 -0400
commitb87ba57819a3dfa458b34c9cec9362c7028adf6e (patch)
treee12932e8dd7ac54c92ca337b9c975d7b4b143561 /js/src/app
parent802389a90eaa9a173a98305003b9e58b95584cd1 (diff)
downloadopencode-b87ba57819a3dfa458b34c9cec9362c7028adf6e.tar.gz
opencode-b87ba57819a3dfa458b34c9cec9362c7028adf6e.zip
shutdown
Diffstat (limited to 'js/src/app')
-rw-r--r--js/src/app/index.ts28
1 files changed, 23 insertions, 5 deletions
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<any, any>();
+ const services = new Map<
+ any,
+ {
+ state: any;
+ shutdown?: (input: any) => Promise<void>;
+ }
+ >();
const result = {
get services() {
@@ -39,15 +45,22 @@ export namespace App {
return result;
}
- export function state<T extends (app: Info) => any>(key: any, init: T) {
+ export function state<State>(
+ key: any,
+ init: (app: Info) => State,
+ shutdown?: (state: Awaited<State>) => Promise<void>,
+ ) {
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<T>;
+ 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;
});
}
}