Remove config groups

Refactor away the config group concept and simplyfy the way config
fields are defined.  Fields are defined directly as properties of the
static field fieldDefinitions on the class itself with a separate
interface to declare the types of fields.

This makes it possible to easily make accessing the config fields type
checked in a way that lets plugins extend the interfaces with their own
config options by adding additional fields to the interface via
declation merging.
This commit is contained in:
Hornwitser committed 2024-01-09 02:19:07 +01:00
1 parent 96210373f3
commit e74065a493
35 files changed
+1259 -2090

No files matched your search

@@ -1,5 +1,5 @@
import type {
CollectorResult, ConfigGroup, Event, InstanceStatus, Logger,
CollectorResult, Event, InstanceStatus, Logger,
ModPack, ModInfo, PlayerEvent, PluginNodeEnvInfo,
} from "@clusterio/lib";
import type Controller from "./Controller";
@@ -99,12 +99,11 @@ export default class BaseControllerPlugin {
* Invoked after the value of the config field given by `field` has
* changed on the controller.
*
* @param group -
* The group who's field got changed.
* @param field - Name of the field that changed.
* @param curr - The current value of the field.
* @param prev - The previous value of the field.
*/
async onControllerConfigFieldChanged(group: ConfigGroup, field: string, prev: unknown) { }
async onControllerConfigFieldChanged(field: string, curr: unknown, prev: unknown) { }
/**
* Called when the value of an instance config field changed.
@@ -114,12 +113,11 @@ export default class BaseControllerPlugin {
*
* @param instance -
* The instance the config changed on.
* @param group -
* The group who's field got changed.
* @param field - Name of the field that changed.
* @param curr - The current value of the field.
* @param prev - The previous value of the field.
*/
async onInstanceConfigFieldChanged(instance: InstanceInfo, group: ConfigGroup, field: string, prev: unknown) { }
async onInstanceConfigFieldChanged(instance: InstanceInfo, field: string, curr: unknown, prev: unknown) { }
/**
* Called before collecting Prometheus metrics
+4 -4
View File
@@ -174,12 +174,12 @@ export default class ControlConnection extends BaseConnection {
}
async handleControllerConfigSetFieldRequest(request: lib.ControllerConfigSetFieldRequest) {
this._controller.config.set(request.field, request.value, "control");
this._controller.config.set(request.field as keyof lib.ControllerConfigFields, request.value, "control");
}
async handleControllerConfigSetPropRequest(request: lib.ControllerConfigSetPropRequest) {
let { field, prop, value } = request;
this._controller.config.setProp(field, prop, value, "control");
this._controller.config.setProp(field as keyof lib.ControllerConfigFields, prop, value, "control");
}
async handleHostRevokeTokensRequest(request: lib.HostRevokeTokensRequest) {
@@ -261,14 +261,14 @@ export default class ControlConnection extends BaseConnection {
throw new lib.RequestError("Setting instance.id is not supported");
}
instance.config.set(request.field, request.value, "control");
instance.config.set(request.field as keyof lib.InstanceConfigFields, request.value, "control");
await this._controller.instanceConfigUpdated(instance);
}
async handleInstanceConfigSetPropRequest(request: lib.InstanceConfigSetPropRequest) {
let instance = this._controller.getRequestInstance(request.instanceId);
let { field, prop, value } = request;
instance.config.setProp(field, prop, value, "control");
instance.config.setProp(field as keyof lib.InstanceConfigFields, prop, value, "control");
await this._controller.instanceConfigUpdated(instance);
}
+8 -8
View File
@@ -216,11 +216,11 @@ export default class Controller {
this._startDevServer(args);
}
this.config.on("fieldChanged", (group, field, prev) => {
if (group.name === "controller" && field === "autosave_interval") {
this.config.on("fieldChanged", (field, curr, prev) => {
if (field === "controller.autosave_interval") {
this.onAutosaveIntervalChanged();
}
lib.invokeHook(this.plugins, "onControllerConfigFieldChanged", group, field, prev);
lib.invokeHook(this.plugins, "onControllerConfigFieldChanged", field, curr, prev);
});
for (let instance of this.instances.values()) {
this.addInstanceHooks(instance);
@@ -799,7 +799,7 @@ export default class Controller {
this.clearSavesOfInstance(instanceId);
// Assign to target
instance.config.set("instance.assigned_host", hostId);
instance.config.set("instance.assigned_host", hostId ?? null);
// "fieldChanged" event handler will set this.instancesDirty
if (hostId !== undefined && newHostConnection) {
await newHostConnection.send(
@@ -859,14 +859,14 @@ export default class Controller {
}
addInstanceHooks(instance: InstanceInfo) {
instance.config.on("fieldChanged", (group: lib.ConfigGroup, field: string, prev: any) => {
instance.config.on("fieldChanged", (field: string, curr: any, prev: any) => {
instance.updatedAt = Date.now();
if (group.name === "instance" && field === "name") {
if (field === "instance.name") {
this.instanceDetailsUpdated([instance]);
}
this.instancesDirty = true;
lib.invokeHook(this.plugins, "onInstanceConfigFieldChanged", instance, group, field, prev);
lib.invokeHook(this.plugins, "onInstanceConfigFieldChanged", instance, field, curr, prev);
});
}
@@ -988,7 +988,7 @@ export default class Controller {
logger.warn(`Unable to load dist/web/manifest.json for plugin ${pluginInfo.name}`);
}
if (!this.config.group(pluginInfo.name).get("load_plugin")) {
if (!this.config.get(`${pluginInfo.name}.load_plugin` as keyof lib.ControllerConfigFields)) {
continue;
}
+3 -1
View File
@@ -109,7 +109,9 @@ function getPlugins(req: Request, res: Response) {
for (let pluginInfo of req.app.locals.controller.pluginInfos) {
let name = pluginInfo.name;
let loaded = req.app.locals.controller.plugins.has(name);
let enabled = loaded && req.app.locals.controller.config.group(pluginInfo.name).get("load_plugin") as boolean;
let enabled = loaded && req.app.locals.controller.config.get(
`${pluginInfo.name}.load_plugin` as keyof lib.ControllerConfigFields,
) as unknown as boolean;
let web: { main:any, error:string|undefined } = { main: undefined, error: undefined};
let devPlugins = req.app.locals.devPlugins;
if (devPlugins && devPlugins.has(name)) {