feat: support features (#1189)

Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachybase#1189
This commit is contained in:
sealday 2024-06-14 01:21:37 +08:00
parent 5acee2f57f
commit 4d6371b119
3 changed files with 37 additions and 49 deletions

View File

@ -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: {

View File

@ -35,6 +35,8 @@ export interface PluginOptions {
export abstract class Plugin<O = any> implements PluginInterface {
options: any;
app: Application;
features: (typeof Plugin)[];
featureInstances: Plugin[];
/**
* @deprecated
@ -54,6 +56,13 @@ export abstract class Plugin<O = any> implements PluginInterface {
constructor(app: Application, options?: any) {
this.app = app;
this.setOptions(options);
this.features = [];
this.featureInstances = [];
}
addFeature<T extends Plugin>(plugin: new (app: Application, options?: any) => T) {
this.features.push(plugin);
this.featureInstances.push(new plugin(this.app, this.options));
}
get log() {

View File

@ -60,41 +60,24 @@ export default class PluginWorkflowServer extends Plugin {
private loggerCache: LRUCache<string, Logger>;
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) {