Add typed hook system for plugins

Replace invokeHook and the per plugin on* methods with AsyncHook
collections on the controller, host, instance, clusterioctl and web
control objects. Plugin entrypoints export a default function that
receives a load context and attaches handlers to the hooks it needs.
Class exports still load through fromContext with a deprecation
warning. The create templates and docs use the entrypoint function.

This is clusterio/clusterio#921 recreated on top of master after the
ESM migration and the PluginDeclaration permissions change.

Closes #730

Co-Authored-By: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
committed 2026-09-11 22:49:09 +00:00
1 parent 8c03ac15ab
commit 7122cbccf8
64 files changed
+1878 -1184

No files matched your search

+11 -16
View File
@@ -1,21 +1,16 @@
import { BaseHostPlugin } from "@clusterio/host";
import { HostEcho, HostEchoReceived } from "./messages.js";
export class HostPlugin extends BaseHostPlugin {
async init() {
this.logger.info("test_plugin host loaded");
this.receivedEchoes = new Set();
this.host.handle(HostEcho, this.handleHostEcho.bind(this));
this.host.handle(HostEchoReceived, this.handleHostEchoReceived.bind(this));
}
// Uses the default export entrypoint, the controller and ctl parts of this
// plugin use the deprecated class export so both paths are exercised.
export default async function(context) {
const { host, logger } = context;
logger.info("test_plugin host loaded");
const receivedEchoes = new Set();
async handleHostEcho(event) {
this.logger.info(`test_plugin host echo ${event.text}`);
this.receivedEchoes.add(event.text);
}
host.handle(HostEcho, async (event) => {
logger.info(`test_plugin host echo ${event.text}`);
receivedEchoes.add(event.text);
});
async handleHostEchoReceived(request) {
return this.receivedEchoes.has(request.text);
}
host.handle(HostEchoReceived, async (request) => receivedEchoes.has(request.text));
}
+6 -5
View File
@@ -1,7 +1,8 @@
import { BaseInstancePlugin } from "@clusterio/host";
export default async function(context) {
const { instance, plugin, logger } = context;
logger.info("test_plugin instance loaded");
export class InstancePlugin extends BaseInstancePlugin {
async init() {
this.logger.info("test_plugin instance loaded");
}
instance.hooks.start.attach(plugin.name, async () => {
logger.info("test_plugin instance started");
});
}