mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 23:24:52 +09:00
Provide control at init time for web plugins
The control interface is required for adding handlers for message types a plugin defines, but the plugins were loaded before the Control instance was created. This meant there was no sensible way for web plugins to register handlers for their own message types. Move the creating of the Control instance to before plugins are loaded and set this.control for web plugins before init is called.
This commit is contained in:
1 parent
70d2b319b3
commit
8c724e3bc5
4 files changed
+17
-16
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
import type React from "react";
|
import type React from "react";
|
||||||
import type { AccountRole, Link, Logger, PluginWebpackEnvInfo } from "@clusterio/lib";
|
import type { AccountRole, Logger, PluginWebpackEnvInfo } from "@clusterio/lib";
|
||||||
|
import type { Control } from "./util/websocket";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin supplied login form
|
* Plugin supplied login form
|
||||||
@@ -77,11 +78,6 @@ export default class BaseWebPlugin {
|
|||||||
* Contents of the plugin's package.json file
|
* Contents of the plugin's package.json file
|
||||||
*/
|
*/
|
||||||
package: any;
|
package: any;
|
||||||
/**
|
|
||||||
* Control link to the controller, not available until the
|
|
||||||
* connect event in onControllerConnectionEvent is signaled.
|
|
||||||
*/
|
|
||||||
control?: Link;
|
|
||||||
/**
|
/**
|
||||||
* Logger for this plugin
|
* Logger for this plugin
|
||||||
*
|
*
|
||||||
@@ -152,6 +148,12 @@ export default class BaseWebPlugin {
|
|||||||
* The plugin's own info module
|
* The plugin's own info module
|
||||||
*/
|
*/
|
||||||
public info: PluginWebpackEnvInfo,
|
public info: PluginWebpackEnvInfo,
|
||||||
|
/**
|
||||||
|
* Control link to the controller
|
||||||
|
*
|
||||||
|
* Not connected at the time init is invoked.
|
||||||
|
*/
|
||||||
|
public control: Control,
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
) {
|
) {
|
||||||
this.package = packageData;
|
this.package = packageData;
|
||||||
|
|||||||
@@ -67,8 +67,8 @@ async function loadPluginInfos(): Promise<lib.PluginWebpackEnvInfo[]> {
|
|||||||
return pluginInfos;
|
return pluginInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadPlugins(pluginInfos: lib.PluginWebpackEnvInfo[]) {
|
async function loadPlugins(pluginInfos: lib.PluginWebpackEnvInfo[], control: Control) {
|
||||||
let plugins = new Map();
|
let plugins = new Map<string, BaseWebPlugin>();
|
||||||
for (let pluginInfo of pluginInfos) {
|
for (let pluginInfo of pluginInfos) {
|
||||||
if (!pluginInfo.enabled) {
|
if (!pluginInfo.enabled) {
|
||||||
continue;
|
continue;
|
||||||
@@ -83,7 +83,7 @@ async function loadPlugins(pluginInfos: lib.PluginWebpackEnvInfo[]) {
|
|||||||
WebPluginClass = webModule.WebPlugin;
|
WebPluginClass = webModule.WebPlugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
let plugin = new WebPluginClass(pluginInfo.container, pluginInfo.package, pluginInfo, logger);
|
let plugin = new WebPluginClass(pluginInfo.container, pluginInfo.package, pluginInfo, control, logger);
|
||||||
await plugin.init();
|
await plugin.init();
|
||||||
plugins.set(pluginInfo.name, plugin);
|
plugins.set(pluginInfo.name, plugin);
|
||||||
|
|
||||||
@@ -106,11 +106,11 @@ export default async function bootstrap() {
|
|||||||
lib.registerPluginMessages(pluginInfos);
|
lib.registerPluginMessages(pluginInfos);
|
||||||
lib.registerPluginConfigGroups(pluginInfos);
|
lib.registerPluginConfigGroups(pluginInfos);
|
||||||
lib.finalizeConfigs();
|
lib.finalizeConfigs();
|
||||||
let plugins = await loadPlugins(pluginInfos);
|
|
||||||
|
|
||||||
let wsUrl = new URL(webRoot, document.location.href);
|
let wsUrl = new URL(webRoot, document.location.href);
|
||||||
let controlConnector = new ControlConnector(wsUrl.href, 120, undefined);
|
let controlConnector = new ControlConnector(wsUrl.href, 120, undefined);
|
||||||
let control = new Control(controlConnector, plugins);
|
let control = new Control(controlConnector);
|
||||||
|
control.plugins = await loadPlugins(pluginInfos, control);
|
||||||
|
|
||||||
const root = createRoot(document.getElementById("root") as HTMLDivElement);
|
const root = createRoot(document.getElementById("root") as HTMLDivElement);
|
||||||
root.render(<App control={control}/>);
|
root.render(<App control={control}/>);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
export { default as bootstrap } from "./bootstrap";
|
export { default as bootstrap } from "./bootstrap";
|
||||||
export { default as notify, notifyErrorHandler } from "./util/notify";
|
export { default as notify, notifyErrorHandler } from "./util/notify";
|
||||||
export { default as BaseWebPlugin } from "./BaseWebPlugin";
|
export { default as BaseWebPlugin } from "./BaseWebPlugin";
|
||||||
|
export * from "./util/websocket";
|
||||||
|
|
||||||
export * from "./model/instance";
|
export * from "./model/instance";
|
||||||
export * from "./model/host";
|
export * from "./model/host";
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ export class Control extends lib.Link {
|
|||||||
/** Roles of the account this control link is connected as. */
|
/** Roles of the account this control link is connected as. */
|
||||||
accountRoles: lib.AccountRole[] | null = null;
|
accountRoles: lib.AccountRole[] | null = null;
|
||||||
|
|
||||||
|
/** Plugins loaded in the web interface */
|
||||||
|
public plugins = new Map<string, BaseWebPlugin>();
|
||||||
|
|
||||||
/** Updates not handled by the subscription service */
|
/** Updates not handled by the subscription service */
|
||||||
accountUpdateHandlers: accountHandler[] = [];
|
accountUpdateHandlers: accountHandler[] = [];
|
||||||
logHandlers: Map<lib.LogFilter, logHandler[]> = new Map();
|
logHandlers: Map<lib.LogFilter, logHandler[]> = new Map();
|
||||||
@@ -85,14 +88,9 @@ export class Control extends lib.Link {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
connector: ControlConnector,
|
connector: ControlConnector,
|
||||||
public plugins: Map<string, BaseWebPlugin>,
|
|
||||||
) {
|
) {
|
||||||
super(connector);
|
super(connector);
|
||||||
|
|
||||||
for (let plugin of plugins.values()) {
|
|
||||||
plugin.control = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.connector.on("connect", data => {
|
this.connector.on("connect", data => {
|
||||||
this.accountName = data.account.name;
|
this.accountName = data.account.name;
|
||||||
this.accountRoles = data.account.roles;
|
this.accountRoles = data.account.roles;
|
||||||
|
|||||||
Reference in new issue
Block a user