mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 15:04:52 +09:00
Migrate the usage of CommonJS specific code and imports to ESM. Roughly summarised this: - Changes all require() calls to import statement or expressions. - Changes usage of module.exports to export statements. - Replaces require.resolve with import.meta.resolve. - Replaces conditional imports with conditional subpath imports. - Replaces module === require.main with import.meta.main. - Adds the full filename with extention to all relative imports. - Changes all usage of __dirname and __filename to their import.meta equivalents. - Sets "type": "module" in package.json files. - Removes usage of "use strict". - Adds .ts an an alias of .js for the webpack build. Due to differences in how require and import resolves files the plugin entrypoints will have to include the filename and extension after this migration in order to work.
81 lines
2.6 KiB
JavaScript
81 lines
2.6 KiB
JavaScript
import * as lib from "@clusterio/lib";
|
|
import { BaseControllerPlugin } from "@clusterio/controller";
|
|
//%if multi_context // Messages requires multi context
|
|
|
|
import {
|
|
PluginExampleEvent, PluginExampleRequest,
|
|
//%endif
|
|
//%if controller & web // Subscribing requires web content and the controller
|
|
ExampleSubscribableUpdate, ExampleSubscribableValue,
|
|
//%endif
|
|
//%if multi_context // Messages requires multi context
|
|
} from "./messages.js";
|
|
//%endif
|
|
|
|
export class ControllerPlugin extends BaseControllerPlugin {
|
|
//%if controller & web // Subscribing requires web content and the controller
|
|
exampleDatabase;
|
|
storageDirty = false;
|
|
|
|
//%endif
|
|
//%if multi_context // Subscribing requires multi context
|
|
async init() {
|
|
this.controller.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
|
|
this.controller.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
|
|
//%endif
|
|
//%if controller & web // Subscribing requires web content and the controller
|
|
this.controller.subscriptions.handle(ExampleSubscribableUpdate, this.handleExampleSubscription.bind(this));
|
|
// If needed, replace with loading from database file such as lib.Datastore
|
|
this.exampleDatabase = new Map([["foo", new ExampleSubscribableValue("foo", 0, false)]]);
|
|
//%endif
|
|
//%if multi_context // Subscribing requires multi context
|
|
}
|
|
//%endif
|
|
|
|
async onControllerConfigFieldChanged(field, curr, prev) {
|
|
this.logger.info(`controller::onControllerConfigFieldChanged ${field}`);
|
|
}
|
|
//%if instance
|
|
|
|
async onInstanceConfigFieldChanged(instance, field, curr, prev) {
|
|
this.logger.info(`controller::onInstanceConfigFieldChanged ${instance.id} ${field}`);
|
|
}
|
|
//%endif
|
|
|
|
async onSaveData() {
|
|
this.logger.info("controller::onSaveData");
|
|
}
|
|
|
|
async onShutdown() {
|
|
this.logger.info("controller::onShutdown");
|
|
}
|
|
|
|
async onPlayerEvent(instance, event) {
|
|
this.logger.info(`controller::onPlayerEvent ${instance.id} ${JSON.stringify(event)}`);
|
|
}
|
|
//%if multi_context
|
|
|
|
async handlePluginExampleEvent(event) {
|
|
this.logger.info(JSON.stringify(event));
|
|
}
|
|
|
|
async handlePluginExampleRequest(request) {
|
|
this.logger.info(JSON.stringify(request));
|
|
return {
|
|
myResponseString: request.myString,
|
|
myResponseNumbers: request.myNumberArray,
|
|
};
|
|
}
|
|
//%endif
|
|
//%if controller & web // Subscribing requires web content and the controller
|
|
|
|
async handleExampleSubscription(request) {
|
|
this.logger.info(JSON.stringify(request));
|
|
const values = [...this.exampleDatabase.values()].filter(
|
|
value => value.updatedAtMs > request.lastRequestTimeMs,
|
|
);
|
|
return values.length ? new ExampleSubscribableUpdate(values) : null;
|
|
}
|
|
//%endif
|
|
}
|