Construct plugins with a static fromContext (#487)

Each plugin class now has a private constructor and a static async
fromContext(context) that loads its datastores, constructs the instance
and attaches its handlers and hooks. This replaces the public constructor
followed by init(), and AutoRestart's start().

Datastores are loaded before construction and passed to the constructor,
so the definite assignment assertions on them are gone. Tests build the
plugins through fromContext and pass a wrapped logger in the context
instead of overwriting plugin.logger before init().

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
BastiaanandClaude Opus 5.5 authored and GitHub committed 2026-10-05 01:19:53 +01:00
1 parent 757a36fc03
commit ea9bf1fb78
17 files changed
+236 -241

No files matched your search

+13 -13
View File
@@ -30,20 +30,20 @@ export type IpcReportDelete = {
* stopped by then, in which case the answer is dropped.
*/
export class InstancePlugin {
instance: Instance;
logger: lib.Logger;
name: string;
private constructor(
public instance: Instance,
public logger: lib.Logger,
public name: string,
) {}
constructor(context: InstancePluginContext) {
this.instance = context.instance;
this.logger = context.logger;
this.name = context.plugin.name;
}
static async fromContext(context: InstancePluginContext) {
const instance = context.instance;
const plugin = new InstancePlugin(instance, context.logger, context.plugin.name);
async init() {
this.instance.server.handle("exp_reports:create", this.handleCreateIPC.bind(this));
this.instance.server.handle("exp_reports:list", this.handleListIPC.bind(this));
this.instance.server.handle("exp_reports:delete", this.handleDeleteIPC.bind(this));
instance.server.handle("exp_reports:create", plugin.handleCreateIPC.bind(plugin));
instance.server.handle("exp_reports:list", plugin.handleListIPC.bind(plugin));
instance.server.handle("exp_reports:delete", plugin.handleDeleteIPC.bind(plugin));
return plugin;
}
async handleCreateIPC(event: IpcReportCreate) {
@@ -106,5 +106,5 @@ export class InstancePlugin {
}
export default async function (context: InstancePluginContext) {
await new InstancePlugin(context).init();
await InstancePlugin.fromContext(context);
}