Skip to content

Commit 017b1cc

Browse files
committed
Add deinit for plugins
1 parent 3c6fac9 commit 017b1cc

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/node/plugin.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ interface Application extends pluginapi.Application {
4646
/*
4747
* Clone of the above without functions.
4848
*/
49-
plugin: Omit<Plugin, "init" | "router" | "applications">
49+
plugin: Omit<Plugin, "init" | "deinit" | "router" | "applications">
5050
}
5151

5252
/**
@@ -254,6 +254,21 @@ export class PluginAPI {
254254

255255
return p
256256
}
257+
258+
public async dispose(): Promise<void> {
259+
await Promise.all(
260+
Array.from(this.plugins.values()).map(async (p) => {
261+
if (!p.deinit) {
262+
return
263+
}
264+
try {
265+
await p.deinit()
266+
} catch (error) {
267+
this.logger.error("plugin failed to deinit", field("name", p.name), field("error", error.message))
268+
}
269+
}),
270+
)
271+
}
257272
}
258273

259274
interface PackageJSON {

src/node/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Heart } from "../heart"
1515
import { redirect, replaceTemplates } from "../http"
1616
import { PluginAPI } from "../plugin"
1717
import { getMediaMime, paths } from "../util"
18+
import { wrapper } from "../wrapper"
1819
import * as apps from "./apps"
1920
import * as domainProxy from "./domainProxy"
2021
import * as health from "./health"
@@ -148,6 +149,7 @@ export const register = async (
148149
await papi.loadPlugins()
149150
papi.mount(app, wsApp)
150151
app.use("/api/applications", apps.router(papi))
152+
wrapper.onDispose(() => papi.dispose())
151153

152154
app.use(() => {
153155
throw new HttpError("Not Found", HttpCode.NotFound)

typings/pluginapi.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ export interface Plugin {
167167
*/
168168
init(config: PluginConfig): void
169169

170+
/**
171+
* Called when the plugin should dispose/shutdown everything.
172+
*/
173+
deinit?(): Promise<void>
174+
170175
/**
171176
* Returns the plugin's router.
172177
*

0 commit comments

Comments
 (0)