diff --git a/packages/host/src/Instance.ts b/packages/host/src/Instance.ts index 64a03bba..c77cdb92 100644 --- a/packages/host/src/Instance.ts +++ b/packages/host/src/Instance.ts @@ -200,24 +200,18 @@ export default class Instance extends lib.Link { this.hooks = new InstanceHooks(this.logger); this._configFieldChanged = (field: string, curr: unknown, prev: unknown) => { - let hook = () => this.hooks.instanceConfigFieldChanged.invoke(field, curr, prev); - if (field === "factorio.shutdown_timeout") { this.server.shutdownTimeoutMs = curr as number * 1000; + } else if (field === "factorio.max_concurrent_commands") { + this.server.maxConcurrentCommands = curr as number; } else if (field === "factorio.settings") { this._applyWhenRunning( "server settings", () => this.updateFactorioSettings(curr as any, prev as any) ); - hook(); } else if (field === "factorio.enable_whitelist") { this._applyWhenRunning("whitelist", () => this.updateFactorioWhitelist(curr as any)); - hook(); - } else { - if (field === "factorio.max_concurrent_commands") { - this.server.maxConcurrentCommands = curr as number; - } - hook(); } + this.hooks.instanceConfigFieldChanged.invoke(field, curr, prev); }; this.config.on("fieldChanged", this._configFieldChanged); diff --git a/test/host/Instance.js b/test/host/Instance.js index 6712493d..b64658d6 100644 --- a/test/host/Instance.js +++ b/test/host/Instance.js @@ -128,6 +128,35 @@ describe("class Instance", function() { instance.notifyStatus("stopped"); await wait(10); }); + + it("should invoke the hook for fields applied to the server directly", async function() { + instance.config.set("factorio.shutdown_timeout", 30); + assert.equal(await hookInvoked, "factorio.shutdown_timeout"); + assert.equal(instance.server.shutdownTimeoutMs, 30000); + await wait(10); + assert.deepEqual(errors, []); + assert.deepEqual(rejections, []); + }); + + it("should invoke the hook exactly once per field changed", async function() { + let invoked = []; + instance._host.whitelist = new Set(); + instance.hooks.instanceConfigFieldChanged.attach("count", field => { invoked.push(field); }); + instance.config.set("factorio.settings", { tags: ["a"] }); + instance.config.set("factorio.enable_whitelist", true); + instance.config.set("factorio.shutdown_timeout", 30); + instance.config.set("factorio.max_concurrent_commands", 3); + await wait(10); + assert.deepEqual(invoked, [ + "factorio.settings", + "factorio.enable_whitelist", + "factorio.shutdown_timeout", + "factorio.max_concurrent_commands", + ]); + assert.equal(instance.server.maxConcurrentCommands, 3); + assert.deepEqual(errors, []); + assert.deepEqual(rejections, []); + }); }); describe("list update events", function() {