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

+21 -21
View File
@@ -4,36 +4,36 @@ import * as messages from "./messages.js";
import * as path from "node:path";
export class ControllerPlugin {
controller: Controller;
logger: lib.Logger;
name: string;
reports!: lib.SubscribableDatastore<messages.ReportRecord>;
private constructor(
public controller: Controller,
public logger: lib.Logger,
public name: string,
public reports: lib.SubscribableDatastore<messages.ReportRecord>,
) {}
constructor(context: ControllerPluginContext) {
this.controller = context.controller;
this.logger = context.logger;
this.name = context.plugin.name;
}
static async fromContext(context: ControllerPluginContext) {
const controller = context.controller;
const databaseDirectory = controller.config.get("controller.database_directory");
async init() {
const databaseDirectory = this.controller.config.get("controller.database_directory");
this.reports = new lib.SubscribableDatastore(
const reports = new lib.SubscribableDatastore(
...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "exp_reports", "reports.json"),
messages.ReportRecord.fromJSON.bind(messages.ReportRecord),
).bootstrap()
);
this.controller.subscriptions.handle(messages.ReportUpdatedEvent, this.handleReportSubscription.bind(this));
this.reports.on("update", this.reportsUpdated.bind(this));
const plugin = new ControllerPlugin(controller, context.logger, context.plugin.name, reports);
this.controller.handle(messages.ReportListRequest, this.handleReportListRequest.bind(this));
this.controller.handle(messages.ReportGetRequest, this.handleReportGetRequest.bind(this));
this.controller.handle(messages.ReportCreateRequest, this.handleReportCreateRequest.bind(this));
this.controller.handle(messages.ReportDeleteRequest, this.handleReportDeleteRequest.bind(this));
controller.subscriptions.handle(messages.ReportUpdatedEvent, plugin.handleReportSubscription.bind(plugin));
reports.on("update", plugin.reportsUpdated.bind(plugin));
this.controller.hooks.shutdown.attach(this.name, this.onShutdown.bind(this));
controller.handle(messages.ReportListRequest, plugin.handleReportListRequest.bind(plugin));
controller.handle(messages.ReportGetRequest, plugin.handleReportGetRequest.bind(plugin));
controller.handle(messages.ReportCreateRequest, plugin.handleReportCreateRequest.bind(plugin));
controller.handle(messages.ReportDeleteRequest, plugin.handleReportDeleteRequest.bind(plugin));
controller.hooks.shutdown.attach(plugin.name, plugin.onShutdown.bind(plugin));
return plugin;
}
async onShutdown() {
@@ -154,5 +154,5 @@ export class ControllerPlugin {
}
export default async function (context: ControllerPluginContext) {
await new ControllerPlugin(context).init();
await ControllerPlugin.fromContext(context);
}