mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
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:
1 parent
eefb4be44b
commit
c8e52e7bde
12 files changed
+408
-29
No files matched your search
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import dgram from "node:dgram";
|
||||
import events from "node:events";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
@@ -250,6 +251,41 @@ describe("host/src/Host", function() {
|
||||
assert.equal(host.config.restartRequired, true);
|
||||
});
|
||||
});
|
||||
describe(".waitForPlugin()", function() {
|
||||
let dir;
|
||||
before(async function() {
|
||||
lib.addPluginConfigFields([
|
||||
{ name: "host_wait_early", version: "1.0.0", hostEntrypoint: "x" },
|
||||
{ name: "host_wait_late", version: "1.0.0", hostEntrypoint: "x" },
|
||||
]);
|
||||
dir = await fs.mkdtemp(path.join(os.tmpdir(), "clusterio-host-wait-"));
|
||||
await fs.writeFile(path.join(dir, "early.js"), `export default async function({ host, plugin }) {
|
||||
host.lateApi = await host.waitForPlugin(plugin.name, "host_wait_late");
|
||||
}`);
|
||||
await fs.writeFile(path.join(dir, "late.js"), "export default function() { return { late: true }; }");
|
||||
});
|
||||
after(async function() {
|
||||
for (const name of Object.keys(lib.HostConfig.fieldDefinitions)) {
|
||||
if (name.startsWith("host_wait_")) {
|
||||
delete lib.HostConfig.fieldDefinitions[name];
|
||||
}
|
||||
}
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
});
|
||||
it("should resolve to what a host plugin later in the list returned", async function() {
|
||||
const pluginInfos = [["host_wait_early", "early.js"], ["host_wait_late", "late.js"]].map(
|
||||
([name, hostEntrypoint]) => ({
|
||||
name, hostEntrypoint, packagePath: path.resolve(dir, "package.json"),
|
||||
})
|
||||
);
|
||||
const hostConfig = new lib.HostConfig("host");
|
||||
const host = new Host(new HostConnector(hostConfig, pluginInfos), hostConfig, pluginInfos);
|
||||
const missing = host.waitForPlugin("test", "missing", 60_000);
|
||||
await host.loadPlugins();
|
||||
assert.deepEqual(host.lateApi, { late: true });
|
||||
assert.equal(await missing, undefined);
|
||||
});
|
||||
});
|
||||
describe(".assignFreeGamePort()", function() {
|
||||
let host;
|
||||
let held;
|
||||
|
||||
Reference in new issue
Block a user