From 6f81216e7c04ef7d26685fe3949534f8fa29d9bd Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Fri, 8 Mar 2024 22:00:38 +0000 Subject: [PATCH] Addressed remaining review comments --- docs/writing-plugins.md | 14 ++++++++++++++ packages/create/templates/plugin-js/index.js | 9 ++++++++- packages/create/templates/plugin-js/instance.js | 2 +- packages/create/templates/plugin-ts/index.ts | 9 ++++++++- packages/create/templates/plugin-ts/instance.ts | 2 +- packages/host/src/server.ts | 2 +- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/docs/writing-plugins.md b/docs/writing-plugins.md index dd9e97a6..faf88a5f 100644 --- a/docs/writing-plugins.md +++ b/docs/writing-plugins.md @@ -301,6 +301,20 @@ local clusterio_api = require("modules/clusterio/api") clusterio_api.send_json("my_plugin_foo", { data = 123 }) ``` +For convience a helper function is exposed on server specifically for handling IPC events. + +```js +async init() { + this.instance.server.handle("my_plugin_foo", this.handleFoo.bind(this)); +} + +async handleFoo(content) { + // Do stuff with content +} +``` + +**Note:** This only handles IPC events, all other events must use `on`. You should not include the `ipc-` prefix when using this method. Error logging is handled for you, if you want custom error handling use `on`. + It's recommended to either use the plugin name as the channel name or to prefix the channel name with the name of the plugin if you need multiple channels. It's also important to catch any errors that might occur as they will otherwise be propogated to the instance code and kill the server. diff --git a/packages/create/templates/plugin-js/index.js b/packages/create/templates/plugin-js/index.js index 53392577..765ded4f 100644 --- a/packages/create/templates/plugin-js/index.js +++ b/packages/create/templates/plugin-js/index.js @@ -90,7 +90,14 @@ const plugin = { Messages.ExampleSubscribableUpdate, //%endif ], -//%if controller | web // The controller always includes web entry even if there is no content +//%if web // Web content template has an example route which is the plugin name + + webEntrypoint: "./web", + routes: [ + "/__plugin_name__", + ], +//%endif +//%if controller & !web // The controller always includes web entry even if there is no content webEntrypoint: "./web", routes: [], diff --git a/packages/create/templates/plugin-js/instance.js b/packages/create/templates/plugin-js/instance.js index 92fff909..d3bfa189 100644 --- a/packages/create/templates/plugin-js/instance.js +++ b/packages/create/templates/plugin-js/instance.js @@ -8,7 +8,7 @@ class InstancePlugin extends BaseInstancePlugin { this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this)); this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this)); //%if module - this.instance.server.handle("ipc-__plugin_name__-plugin_example_ipc", this.handlePluginExampleIPC.bind(this)); + this.instance.server.handle("__plugin_name__-plugin_example_ipc", this.handlePluginExampleIPC.bind(this)); //%endif } diff --git a/packages/create/templates/plugin-ts/index.ts b/packages/create/templates/plugin-ts/index.ts index dc913106..d90f22be 100644 --- a/packages/create/templates/plugin-ts/index.ts +++ b/packages/create/templates/plugin-ts/index.ts @@ -112,7 +112,14 @@ export const plugin: lib.PluginDeclaration = { Messages.ExampleSubscribableUpdate, //%endif ], -//%if controller | web // The controller always includes web entry even if there is no content +//%if web // Web content template has an example route which is the plugin name + + webEntrypoint: "./web", + routes: [ + "/__plugin_name__", + ], +//%endif +//%if controller & !web // The controller always includes web entry even if there is no content webEntrypoint: "./web", routes: [], diff --git a/packages/create/templates/plugin-ts/instance.ts b/packages/create/templates/plugin-ts/instance.ts index 335304ac..3cb7d18a 100644 --- a/packages/create/templates/plugin-ts/instance.ts +++ b/packages/create/templates/plugin-ts/instance.ts @@ -14,7 +14,7 @@ export class InstancePlugin extends BaseInstancePlugin { this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this)); this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this)); //%if module - this.instance.server.handle("ipc-__plugin_name__-plugin_example_ipc", this.handlePluginExampleIPC.bind(this)); + this.instance.server.handle("__plugin_name__-plugin_example_ipc", this.handlePluginExampleIPC.bind(this)); //%endif } diff --git a/packages/host/src/server.ts b/packages/host/src/server.ts index b2476ca8..e61f91c0 100644 --- a/packages/host/src/server.ts +++ b/packages/host/src/server.ts @@ -564,7 +564,7 @@ export class FactorioServer extends events.EventEmitter { // TODO at the moment this is just a helper function, it has the potential for schema checking handle(eventName: string, handler: (event: Event) => Promise) { - this.on(eventName, (event) => handler(event).catch(err => { + this.on(`ipc-${eventName}`, (event) => handler(event).catch(err => { this._logger.error(`Error handling ipc event:\n${err.stack}`); })); }