diff --git a/packages/core/server/src/plugin-manager/plugin-manager.ts b/packages/core/server/src/plugin-manager/plugin-manager.ts index eccc89fbb..b88ae36f0 100644 --- a/packages/core/server/src/plugin-manager/plugin-manager.ts +++ b/packages/core/server/src/plugin-manager/plugin-manager.ts @@ -404,6 +404,10 @@ export class PluginManager { } this.app.logger.debug(`before load plugin [${name}]`, { submodule: 'plugin-manager', method: 'load', name }); await plugin.beforeLoad(); + // beforeLoad features + for (const feature of plugin.featureInstances) { + await feature.beforeLoad(); + } } current = 0; @@ -426,6 +430,10 @@ export class PluginManager { await plugin.load(); plugin.state.loaded = true; await this.app.emitAsync('afterLoadPlugin', plugin, options); + // load features + for (const feature of plugin.featureInstances) { + await feature.load(); + } } this.app.setMaintainingMessage('loaded plugins'); @@ -463,6 +471,10 @@ export class PluginManager { plugin.installed = true; this.app.setMaintainingMessage(`after install plugin [${name}], ${current}/${total}`); await this.app.emitAsync('afterInstallPlugin', plugin, options); + // install features + for (const feature of plugin.featureInstances) { + await feature.install(options); + } } await this.repository.update({ filter: { diff --git a/packages/core/server/src/plugin.ts b/packages/core/server/src/plugin.ts index 72c456b2c..9d93e7403 100644 --- a/packages/core/server/src/plugin.ts +++ b/packages/core/server/src/plugin.ts @@ -35,6 +35,8 @@ export interface PluginOptions { export abstract class Plugin implements PluginInterface { options: any; app: Application; + features: (typeof Plugin)[]; + featureInstances: Plugin[]; /** * @deprecated @@ -54,6 +56,13 @@ export abstract class Plugin implements PluginInterface { constructor(app: Application, options?: any) { this.app = app; this.setOptions(options); + this.features = []; + this.featureInstances = []; + } + + addFeature(plugin: new (app: Application, options?: any) => T) { + this.features.push(plugin); + this.featureInstances.push(new plugin(this.app, this.options)); } get log() { diff --git a/packages/plugins/@tachybase/plugin-workflow/src/server/Plugin.ts b/packages/plugins/@tachybase/plugin-workflow/src/server/Plugin.ts index 6effa6ebe..0395a4127 100644 --- a/packages/plugins/@tachybase/plugin-workflow/src/server/Plugin.ts +++ b/packages/plugins/@tachybase/plugin-workflow/src/server/Plugin.ts @@ -60,41 +60,24 @@ export default class PluginWorkflowServer extends Plugin { private loggerCache: LRUCache; private meter = null; - pluginSql: PluginSql; - pluginRequest: PluginRequest; - pluginParallel: PluginParallel; - pluginManual: PluginManual; - pluginLoop: PluginLoop; - pluginDynamicCalculation: PluginDynamicCalculation; - pluginDelay: PluginDelay; - pluginAggregate: PluginAggregate; - pluginActionTrigger: PluginActionTrigger; - pluginJSONParse: PluginWorkflowJSONParseServer; - pluginJSParse: PluginWorkflowJSParseServer; - pluginAPIRegular: PluginWorkflowAPIRegularServer; - pluginInterception: PluginInterception; - pluginVariables: PluginVariables; - pluginResponse: PluginResponse; - pluginOmni: PluginOmniTrigger; - constructor(app: Application, options?: PluginOptions) { super(app, options); - this.pluginSql = new PluginSql(app, options); - this.pluginRequest = new PluginRequest(app, options); - this.pluginParallel = new PluginParallel(app, options); - this.pluginManual = new PluginManual(app, options); - this.pluginLoop = new PluginLoop(app, options); - this.pluginDynamicCalculation = new PluginDynamicCalculation(app, options); - this.pluginDelay = new PluginDelay(app, options); - this.pluginAggregate = new PluginAggregate(app, options); - this.pluginActionTrigger = new PluginActionTrigger(app, options); - this.pluginJSONParse = new PluginWorkflowJSONParseServer(app, options); - this.pluginJSParse = new PluginWorkflowJSParseServer(app, options); - this.pluginAPIRegular = new PluginWorkflowAPIRegularServer(app, options); - this.pluginInterception = new PluginInterception(app, options); - this.pluginVariables = new PluginVariables(app, options); - this.pluginResponse = new PluginResponse(app, options); - this.pluginOmni = new PluginOmniTrigger(app, options); + this.addFeature(PluginSql); + this.addFeature(PluginRequest); + this.addFeature(PluginParallel); + this.addFeature(PluginManual); + this.addFeature(PluginLoop); + this.addFeature(PluginDynamicCalculation); + this.addFeature(PluginDelay); + this.addFeature(PluginAggregate); + this.addFeature(PluginActionTrigger); + this.addFeature(PluginWorkflowJSONParseServer); + this.addFeature(PluginWorkflowJSParseServer); + this.addFeature(PluginWorkflowAPIRegularServer); + this.addFeature(PluginInterception); + this.addFeature(PluginVariables); + this.addFeature(PluginResponse); + this.addFeature(PluginOmniTrigger); } getLogger(workflowId: ID): Logger { @@ -309,22 +292,6 @@ export default class PluginWorkflowServer extends Plugin { await this.executing; } }); - await this.pluginSql.load(); - await this.pluginActionTrigger.load(); - await this.pluginAggregate.load(); - await this.pluginDelay.load(); - await this.pluginDynamicCalculation.load(); - await this.pluginLoop.load(); - await this.pluginManual.load(); - await this.pluginParallel.load(); - await this.pluginRequest.load(); - await this.pluginJSONParse.load(); - await this.pluginJSParse.load(); - await this.pluginAPIRegular.load(); - await this.pluginInterception.load(); - await this.pluginVariables.load(); - await this.pluginResponse.load(); - await this.pluginOmni.load(); } toggle(workflow: WorkflowModel, enable?: boolean) {