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

+20 -21
View File
@@ -10,36 +10,35 @@ import { features, pruneFeatureValues, validateFeatureValues } from "./features.
import { SeedRole, SeedGroup, seedRoles, seedGroups, flattenSeedPermissions } from "./seed.js";
export class ControllerPlugin {
controller: Controller;
logger: lib.Logger;
name: string;
features!: lib.SubscribableDatastore<messages.FeatureRecord>;
private constructor(
public controller: Controller,
public logger: lib.Logger,
public name: string,
public features: lib.SubscribableDatastore<messages.FeatureRecord>,
) {}
constructor(context: ControllerPluginContext) {
this.controller = context.controller;
this.logger = context.logger;
this.name = context.plugin.name;
}
async init() {
const databaseDirectory = this.controller.config.get("controller.database_directory");
this.features = new lib.SubscribableDatastore(
static async fromContext(context: ControllerPluginContext) {
const controller = context.controller;
const databaseDirectory = controller.config.get("controller.database_directory");
const features = new lib.SubscribableDatastore(
...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "exp_scenario", "features.json"),
messages.FeatureRecord.fromJSON.bind(messages.FeatureRecord),
).bootstrap()
);
this.reconcileFeatures();
const plugin = new ControllerPlugin(controller, context.logger, context.plugin.name, features);
plugin.reconcileFeatures();
this.controller.subscriptions.handle(messages.FeatureUpdatedEvent, this.handleFeatureSubscription.bind(this));
this.features.on("update", this.featuresUpdated.bind(this));
controller.subscriptions.handle(messages.FeatureUpdatedEvent, plugin.handleFeatureSubscription.bind(plugin));
features.on("update", plugin.featuresUpdated.bind(plugin));
this.controller.handle(messages.SeedRequest, this.handleSeedRequest.bind(this));
this.controller.handle(messages.FeatureListRequest, this.handleFeatureListRequest.bind(this));
this.controller.handle(messages.FeatureUpdateRequest, this.handleFeatureUpdateRequest.bind(this));
controller.handle(messages.SeedRequest, plugin.handleSeedRequest.bind(plugin));
controller.handle(messages.FeatureListRequest, plugin.handleFeatureListRequest.bind(plugin));
controller.handle(messages.FeatureUpdateRequest, plugin.handleFeatureUpdateRequest.bind(plugin));
this.controller.hooks.shutdown.attach(this.name, this.onShutdown.bind(this));
controller.hooks.shutdown.attach(plugin.name, plugin.onShutdown.bind(plugin));
return plugin;
}
async onShutdown() {
@@ -233,7 +232,7 @@ export class ControllerPlugin {
}
export default async function (context: ControllerPluginContext) {
await new ControllerPlugin(context).init();
await ControllerPlugin.fromContext(context);
}
function newId(datastore: { has(id: number): boolean }) {
+15 -14
View File
@@ -5,23 +5,24 @@ import { features, FeatureValue } from "./features.js";
/** Sends feature config to the lua side, see module/features.lua. */
export class InstancePlugin {
instance: Instance;
logger: lib.Logger;
name: string;
/** Rcon is up and the lua side can take updates */
started = false;
constructor(context: InstancePluginContext) {
this.instance = context.instance;
this.logger = context.logger;
this.name = context.plugin.name;
}
private constructor(
public instance: Instance,
public logger: lib.Logger,
public name: string,
) {}
async init() {
this.instance.handle(messages.FeatureUpdatedEvent, this.handleFeatureUpdatedEvent.bind(this));
this.instance.hooks.start.attach(this.name, this.onStart.bind(this));
this.instance.hooks.exit.attach(this.name, this.onExit.bind(this));
this.instance.hooks.controllerConnectionEvent.attach(this.name, this.onControllerConnectionEvent.bind(this));
static async fromContext(context: InstancePluginContext) {
const instance = context.instance;
const plugin = new InstancePlugin(instance, context.logger, context.plugin.name);
instance.handle(messages.FeatureUpdatedEvent, plugin.handleFeatureUpdatedEvent.bind(plugin));
instance.hooks.start.attach(plugin.name, plugin.onStart.bind(plugin));
instance.hooks.exit.attach(plugin.name, plugin.onExit.bind(plugin));
instance.hooks.controllerConnectionEvent.attach(plugin.name, plugin.onControllerConnectionEvent.bind(plugin));
return plugin;
}
async onStart() {
@@ -81,5 +82,5 @@ export class InstancePlugin {
}
export default async function (context: InstancePluginContext) {
await new InstancePlugin(context).init();
await InstancePlugin.fromContext(context);
}
+4 -6
View File
@@ -41,13 +41,11 @@ async function startPlugin(t2, { withPlugins = true, databaseDirectory = t2.test
if (withPlugins) {
for (const [name, Plugin] of [["exp_roles", RolesPlugin], ["exp_groups", GroupsPlugin]]) {
const plugin = new Plugin({ plugin: { name }, controller, logger });
await plugin.init();
await Plugin.fromContext({ plugin: { name }, controller, logger });
}
}
const plugin = new ControllerPlugin({ plugin: { name: "exp_scenario" }, controller, logger });
await plugin.init();
const plugin = await ControllerPlugin.fromContext({ plugin: { name: "exp_scenario" }, controller, logger });
return { plugin, controller };
}
@@ -136,7 +134,7 @@ t.test("class ControllerPlugin", t2 => {
t3.notOk([...first.values()].filter(priority => priority === 2).length > 1, "seed priorities skip taken ones");
});
t2.test(".init() adds every feature with its defaults", async t3 => {
t2.test(".fromContext() adds every feature with its defaults", async t3 => {
const { plugin } = await startPlugin(t3, { withPlugins: false });
const records = await plugin.handleFeatureListRequest();
t3.strictSame(records.map(record => record.id), features.map(feature => feature.name), "one record per feature");
@@ -181,7 +179,7 @@ t.test("class ControllerPlugin", t2 => {
);
});
t2.test(".init() keeps stored features across restarts", async t3 => {
t2.test(".fromContext() keeps stored features across restarts", async t3 => {
const databaseDirectory = t3.testdir();
const { plugin } = await startPlugin(t3, { withPlugins: false, databaseDirectory });
await plugin.handleFeatureUpdateRequest(new messages.FeatureUpdateRequest("afk_kick", false, { afk_minutes: 5 }));
+1 -2
View File
@@ -47,8 +47,7 @@ async function startPlugin(t2, features = [new messages.FeatureRecord("death_mar
return request instanceof messages.FeatureListRequest ? features : undefined;
};
const plugin = new InstancePlugin({ plugin: { name: "exp_scenario" }, instance, host: {}, logger });
await plugin.init();
const plugin = await InstancePlugin.fromContext({ plugin: { name: "exp_scenario" }, instance, host: {}, logger });
return { plugin, instance, state };
}