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

+32 -32
View File
@@ -6,59 +6,59 @@ import * as path from "node:path";
const loaded = new WeakMap<Controller, ControllerPlugin>();
export class ControllerPlugin {
controller: Controller;
logger: lib.Logger;
name: string;
roleMeta!: lib.SubscribableDatastore<messages.RoleMetaRecord>;
/** The plugin loaded on a controller, for the exp_scenario seed. */
static get(controller: Controller) {
return loaded.get(controller);
}
constructor(context: ControllerPluginContext) {
this.controller = context.controller;
this.logger = context.logger;
this.name = context.plugin.name;
}
private constructor(
public controller: Controller,
public logger: lib.Logger,
public name: string,
public roleMeta: lib.SubscribableDatastore<messages.RoleMetaRecord>,
) {}
async init() {
loaded.set(this.controller, this);
const databaseDirectory = this.controller.config.get("controller.database_directory");
static async fromContext(context: ControllerPluginContext) {
const controller = context.controller;
const databaseDirectory = controller.config.get("controller.database_directory");
this.roleMeta = new lib.SubscribableDatastore(
const roleMeta = new lib.SubscribableDatastore(
...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "exp_roles", "role_meta.json"),
messages.RoleMetaRecord.fromJSON.bind(messages.RoleMetaRecord),
).bootstrap()
);
const plugin = new ControllerPlugin(controller, context.logger, context.plugin.name, roleMeta);
loaded.set(controller, plugin);
// The datastore can be out of step with the roles, either because the
// plugin was installed after they were created or because it was
// uninstalled while they were deleted
this.ensureRoleMeta();
this.sweepRoleMeta();
this.applyAutoAssign();
plugin.ensureRoleMeta();
plugin.sweepRoleMeta();
plugin.applyAutoAssign();
this.controller.subscriptions.handle(messages.RoleUpdatedEvent, this.handleRoleSubscription.bind(this));
this.controller.subscriptions.handle(
messages.AssignmentUpdatedEvent, this.handleAssignmentSubscription.bind(this)
controller.subscriptions.handle(messages.RoleUpdatedEvent, plugin.handleRoleSubscription.bind(plugin));
controller.subscriptions.handle(
messages.AssignmentUpdatedEvent, plugin.handleAssignmentSubscription.bind(plugin)
);
this.roleMeta.on("update", this.roleMetaUpdated.bind(this));
this.controller.roles.on("update", this.rolesUpdated.bind(this));
this.controller.users.records.on("update", this.usersUpdated.bind(this));
roleMeta.on("update", plugin.roleMetaUpdated.bind(plugin));
controller.roles.on("update", plugin.rolesUpdated.bind(plugin));
controller.users.records.on("update", plugin.usersUpdated.bind(plugin));
this.controller.handle(messages.RoleListRequest, this.handleRoleListRequest.bind(this));
this.controller.handle(messages.RoleMetaUpdateRequest, this.handleRoleMetaUpdateRequest.bind(this));
controller.handle(messages.RoleListRequest, plugin.handleRoleListRequest.bind(plugin));
controller.handle(messages.RoleMetaUpdateRequest, plugin.handleRoleMetaUpdateRequest.bind(plugin));
this.controller.handle(messages.AssignmentListRequest, this.handleAssignmentListRequest.bind(this));
this.controller.handle(messages.AssignmentUpdateRequest, this.handleAssignmentUpdateRequest.bind(this));
controller.handle(messages.AssignmentListRequest, plugin.handleAssignmentListRequest.bind(plugin));
controller.handle(messages.AssignmentUpdateRequest, plugin.handleAssignmentUpdateRequest.bind(plugin));
const hooks = this.controller.hooks;
hooks.shutdown.attach(this.name, this.onShutdown.bind(this));
hooks.controllerConfigFieldChanged.attach(this.name, this.onControllerConfigFieldChanged.bind(this));
hooks.playerEvent.attach(this.name, this.onPlayerEvent.bind(this));
const hooks = controller.hooks;
hooks.shutdown.attach(plugin.name, plugin.onShutdown.bind(plugin));
hooks.controllerConfigFieldChanged.attach(plugin.name, plugin.onControllerConfigFieldChanged.bind(plugin));
hooks.playerEvent.attach(plugin.name, plugin.onPlayerEvent.bind(plugin));
return plugin;
}
async onShutdown() {
@@ -335,5 +335,5 @@ export class ControllerPlugin {
}
export default async function (context: ControllerPluginContext) {
await new ControllerPlugin(context).init();
await ControllerPlugin.fromContext(context);
}
+16 -16
View File
@@ -10,24 +10,24 @@ export type IpcAssignmentUpdate = {
};
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.handle(messages.RoleUpdatedEvent, this.handleRoleUpdatedEvent.bind(this));
this.instance.handle(messages.AssignmentUpdatedEvent, this.handleAssignmentUpdatedEvent.bind(this));
this.instance.server.handle("exp_roles:assignment_update", this.handleAssignmentUpdateIPC.bind(this));
instance.handle(messages.RoleUpdatedEvent, plugin.handleRoleUpdatedEvent.bind(plugin));
instance.handle(messages.AssignmentUpdatedEvent, plugin.handleAssignmentUpdatedEvent.bind(plugin));
instance.server.handle("exp_roles:assignment_update", plugin.handleAssignmentUpdateIPC.bind(plugin));
const hooks = this.instance.hooks;
hooks.instanceConfigFieldChanged.attach(this.name, this.onInstanceConfigFieldChanged.bind(this));
hooks.start.attach(this.name, this.onStart.bind(this));
const hooks = instance.hooks;
hooks.instanceConfigFieldChanged.attach(plugin.name, plugin.onInstanceConfigFieldChanged.bind(plugin));
hooks.start.attach(plugin.name, plugin.onStart.bind(plugin));
return plugin;
}
get syncMode() {
@@ -122,5 +122,5 @@ export class InstancePlugin {
}
export default async function (context: InstancePluginContext) {
await new InstancePlugin(context).init();
await InstancePlugin.fromContext(context);
}
+2 -3
View File
@@ -44,8 +44,7 @@ async function startPlugin(t2, { roles = [] } = {}) {
permissionsUpdated(user);
};
const plugin = new ControllerPlugin({ plugin: { name: "exp_roles" }, controller, logger });
await plugin.init();
const plugin = await ControllerPlugin.fromContext({ plugin: { name: "exp_roles" }, controller, logger });
return { plugin, controller, state };
}
@@ -62,7 +61,7 @@ t.test("entrypoint", async t2 => {
});
t.test("class ControllerPlugin", t2 => {
t2.test(".init() and .sweepRoleMeta() manage the role properties", async t3 => {
t2.test(".fromContext() and .sweepRoleMeta() manage the role properties", async t3 => {
const { plugin } = await startPlugin(t3, { roles: [role(0, "Cluster Admin"), role(5, "Moderator")] });
t3.strictSame(plugin.roleMeta.get(0).order, 1, "the first role is ordered first");
+2 -3
View File
@@ -69,9 +69,8 @@ async function startPlugin(t2, { syncMode = "bidirectional", roles = sampleRoles
return undefined;
};
const plugin = new InstancePlugin({ plugin: { name: "exp_roles" }, instance, host: {}, logger });
plugin.logger = { ...logger, warn: message => state.warnings.push(message) };
await plugin.init();
const pluginLogger = { ...logger, warn: message => state.warnings.push(message) };
const plugin = await InstancePlugin.fromContext({ plugin: { name: "exp_roles" }, instance, host: {}, logger: pluginLogger });
return { plugin, state };
}