mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-16 00:24:52 +09:00
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:
1 parent
757a36fc03
commit
ea9bf1fb78
17 files changed
+236
-241
No files matched your search
+16
-15
@@ -2,24 +2,25 @@ import * as lib from "@clusterio/lib";
|
||||
import type { Instance, InstancePluginContext } from "@clusterio/host";
|
||||
|
||||
export class InstancePlugin {
|
||||
instance: Instance;
|
||||
logger: lib.Logger;
|
||||
name: string;
|
||||
private updateInterval?: ReturnType<typeof setInterval>;
|
||||
private gameTimes: number[] = [];
|
||||
|
||||
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,
|
||||
) {}
|
||||
|
||||
init() {
|
||||
const hooks = this.instance.hooks;
|
||||
hooks.start.attach(this.name, this.onStart.bind(this));
|
||||
hooks.exit.attach(this.name, this.onExit.bind(this));
|
||||
hooks.instanceConfigFieldChanged.attach(this.name, this.onInstanceConfigFieldChanged.bind(this));
|
||||
hooks.playerEvent.attach(this.name, this.onPlayerEvent.bind(this));
|
||||
static async fromContext(context: InstancePluginContext) {
|
||||
const instance = context.instance;
|
||||
const plugin = new InstancePlugin(instance, context.logger, context.plugin.name);
|
||||
|
||||
const hooks = instance.hooks;
|
||||
hooks.start.attach(plugin.name, plugin.onStart.bind(plugin));
|
||||
hooks.exit.attach(plugin.name, plugin.onExit.bind(plugin));
|
||||
hooks.instanceConfigFieldChanged.attach(plugin.name, plugin.onInstanceConfigFieldChanged.bind(plugin));
|
||||
hooks.playerEvent.attach(plugin.name, plugin.onPlayerEvent.bind(plugin));
|
||||
return plugin;
|
||||
}
|
||||
|
||||
async onStart() {
|
||||
@@ -86,5 +87,5 @@ export class InstancePlugin {
|
||||
}
|
||||
|
||||
export default async function (context: InstancePluginContext) {
|
||||
new InstancePlugin(context).init();
|
||||
await InstancePlugin.fromContext(context);
|
||||
}
|
||||
Reference in new issue
Block a user