Add waitForPlugin and load plugins concurrently

Plugins can wait for another plugin on the same controller, host or
instance with waitForPlugin(waiter, name, timeoutMs). It resolves to what
the other plugin's entrypoint returned, or to undefined with a warning
naming both plugins if it is not loaded once loading finishes or the
timeout runs out.

Plugin entrypoints now run concurrently so a plugin can await a plugin
later in the list from its entrypoint. loadedPlugins is still filled in
plugin list order, as the instance builds its Lua module list from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-19 14:24:56 +00:00
1 parent eefb4be44b
commit c8e52e7bde
12 files changed
+408 -29

No files matched your search

+28 -5
View File
@@ -331,6 +331,7 @@ export default class Host extends lib.Link {
hooks = new HostHooks(logger);
/** Plugins which are currently loaded */
loadedPlugins: Set<lib.PluginNodeEnvInfo> = new Set();
private _pluginLoader = new lib.PluginLoader(logger);
/** A map from instance id to instance connection. Only present when instance is running. */
instanceConnections = new Map<number, InstanceConnection>();
@@ -487,8 +488,22 @@ export default class Host extends lib.Link {
this.handle(lib.InstanceDeleteInternalRequest, this.handleInstanceDeleteInternalRequest.bind(this));
}
/**
* Wait for another host plugin to load
*
* @param waiter - Name of the plugin waiting, used in warnings.
* @param name - Name of the plugin to wait for.
* @param timeoutMs - Time to wait before giving up.
* @returns
* What the plugin's host entrypoint returned, or undefined with
* a warning logged if it did not load.
*/
waitForPlugin<T = unknown>(waiter: string, name: string, timeoutMs?: number) {
return this._pluginLoader.waitFor<T>(waiter, name, timeoutMs);
}
async loadPlugins() {
const context = { logger, host: this };
const toLoad: lib.PluginNodeEnvInfo[] = [];
for (let pluginInfo of this.pluginInfos) {
if (
!pluginInfo.hostEntrypoint && !pluginInfo.instanceEntrypoint
@@ -502,17 +517,20 @@ export default class Host extends lib.Link {
continue;
}
toLoad.push(pluginInfo);
}
const context = { logger, host: this };
await this._pluginLoader.load(toLoad, async pluginInfo => {
let api;
try {
await lib.loadPlugin(
api = await lib.loadPlugin(
pluginInfo,
"host",
context,
"HostPlugin",
BaseHostPlugin,
);
this.loadedPlugins.add(pluginInfo);
} catch (err: any) {
if (err.code === "InstallationError") {
throw err;
@@ -521,6 +539,11 @@ export default class Host extends lib.Link {
}
logger.info(`Loaded plugin ${pluginInfo.name}`);
return api;
});
for (const pluginInfo of toLoad) {
this.loadedPlugins.add(pluginInfo);
}
}
+34 -12
View File
@@ -181,6 +181,7 @@ export default class Instance extends lib.Link {
hooks: InstanceHooks;
/** Plugins which are currently loaded */
loadedPlugins: Set<lib.PluginNodeEnvInfo> = new Set();
private _pluginLoader: lib.PluginLoader;
_host: Host;
@@ -211,6 +212,7 @@ export default class Instance extends lib.Link {
this.id = this.config.get("instance.id");
this.logger = instanceLogger(instanceConfig);
this._pluginLoader = new lib.PluginLoader(this.logger);
this.hooks = new InstanceHooks(this.logger);
@@ -766,14 +768,27 @@ end`.replace(/\r?\n/g, " ");
return instance;
}
/**
* Wait for another instance plugin to load
*
* @param waiter - Name of the plugin waiting, used in warnings.
* @param name - Name of the plugin to wait for.
* @param timeoutMs - Time to wait before giving up.
* @returns
* What the plugin's instance entrypoint returned, or undefined with
* a warning logged if it did not load.
*/
waitForPlugin<T = unknown>(waiter: string, name: string, timeoutMs?: number) {
return this._pluginLoader.waitFor<T>(waiter, name, timeoutMs);
}
/**
* Load the instance plugins and report the instance as initialised
*
* Must be called once the instance is connected to the host.
*/
async loadPlugins(pluginInfos: lib.PluginNodeEnvInfo[]) {
// load plugins
const plugins: Record<string, string> = {};
const toLoad: lib.PluginNodeEnvInfo[] = [];
for (let pluginInfo of pluginInfos) {
if (
!pluginInfo.instanceEntrypoint
@@ -788,26 +803,33 @@ end`.replace(/\r?\n/g, " ");
continue;
}
try {
toLoad.push(pluginInfo);
}
try {
await this._pluginLoader.load(toLoad, async pluginInfo => {
let pluginLoadStartedMs = Date.now();
await lib.loadPlugin(
const api = await lib.loadPlugin(
pluginInfo,
"instance",
{ logger: this.logger, host: this._host, instance: this },
"InstancePlugin",
BaseInstancePlugin,
);
this.loadedPlugins.add(pluginInfo);
plugins[pluginInfo.name] = pluginInfo.version;
this.logger.info(`Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStartedMs}ms`);
} catch (err) {
this.notifyExit();
await this.sendSaveListUpdate();
throw err;
}
return api;
});
} catch (err) {
this.notifyExit();
await this.sendSaveListUpdate();
throw err;
}
const plugins: Record<string, string> = {};
for (const pluginInfo of toLoad) {
this.loadedPlugins.add(pluginInfo);
plugins[pluginInfo.name] = pluginInfo.version;
}
this.send(new lib.InstanceInitialisedEvent(plugins));
}