Junyi 4b8915b616
refactor(plugin-workflow): add sync option for trigger (#3383)
* refactor(plugin-workflow): add sync option for trigger

* feat(plugin-workflow-request): support sync call in request

* fix(plugin-workflow-request): fix request async call

* refactor(plugin-workflow): add method to check if workflow is sync

* fix(plugin-workflow): fix useAvailable in nodes

* fix(plugin-workflow): fix node.isAvailable check

* test(plugin-workflow): limit mysql version to pass test

* fix(plugin-workflow-delay): fix test case

* fix(plugin-workflow-delay): fix test case

* feat(plugin-workflow): add sync field for workflow

* refactor(plugin-workflow): adjust end node logic

* fix(plugin-workflow): support sync mode in form trigger

* feat(plugin-workflow): add end instruction

* fix(plugin-workflow-form-trigger): fix test cases

* fix(plugin-workflow): fix transaction for sync event
2024-01-25 22:10:03 +08:00

122 lines
3.9 KiB
TypeScript

export * from './Branch';
export * from './FlowContext';
export * from './constants';
export * from './nodes';
export { Trigger, useTrigger } from './triggers';
export * from './variable';
export * from './components';
export * from './utils';
export * from './hooks/useGetAriaLabelOfAddButton';
export { default as useStyles } from './style';
export * from './variable';
export { getCollectionFieldOptions, useWorkflowVariableOptions } from './variable';
import React from 'react';
import { Plugin } from '@nocobase/client';
import { Registry } from '@nocobase/utils/client';
import { ExecutionPage } from './ExecutionPage';
import { WorkflowPage } from './WorkflowPage';
import { WorkflowPane } from './WorkflowPane';
import { Trigger } from './triggers';
import CollectionTrigger from './triggers/collection';
import ScheduleTrigger from './triggers/schedule';
import { Instruction } from './nodes';
import CalculationInstruction from './nodes/calculation';
import ConditionInstruction from './nodes/condition';
import EndInstruction from './nodes/end';
import QueryInstruction from './nodes/query';
import CreateInstruction from './nodes/create';
import UpdateInstruction from './nodes/update';
import DestroyInstruction from './nodes/destroy';
import { useTriggerWorkflowsActionProps } from './hooks/useTriggerWorkflowActionProps';
import { getWorkflowDetailPath, getWorkflowExecutionsPath } from './constant';
import { NAMESPACE } from './locale';
export default class PluginWorkflowClient extends Plugin {
triggers = new Registry<Trigger>();
instructions = new Registry<Instruction>();
getTriggersOptions = () => {
return Array.from(this.triggers.getEntities()).map(([value, { title, ...options }]) => ({
value,
label: title,
color: 'gold',
options,
}));
};
isWorkflowSync(workflow) {
return this.triggers.get(workflow.type).sync ?? workflow.sync;
}
registerTrigger(type: string, trigger: Trigger | { new (): Trigger }) {
if (typeof trigger === 'function') {
this.triggers.register(type, new trigger());
} else if (trigger) {
this.triggers.register(type, trigger);
} else {
throw new TypeError('invalid trigger type to register');
}
}
registerInstruction(type: string, instruction: Instruction | { new (): Instruction }) {
if (typeof instruction === 'function') {
this.instructions.register(type, new instruction());
} else if (instruction instanceof Instruction) {
this.instructions.register(type, instruction);
} else {
throw new TypeError('invalid instruction type to register');
}
}
async load() {
this.addRoutes();
this.addScopes();
this.addComponents();
this.app.pluginSettingsManager.add(NAMESPACE, {
icon: 'PartitionOutlined',
title: `{{t("Workflow", { ns: "${NAMESPACE}" })}}`,
Component: WorkflowPane,
aclSnippet: 'pm.workflow.workflows',
});
this.registerTrigger('collection', CollectionTrigger);
this.registerTrigger('schedule', ScheduleTrigger);
this.registerInstruction('calculation', CalculationInstruction);
this.registerInstruction('condition', ConditionInstruction);
this.registerInstruction('end', EndInstruction);
this.registerInstruction('query', QueryInstruction);
this.registerInstruction('create', CreateInstruction);
this.registerInstruction('update', UpdateInstruction);
this.registerInstruction('destroy', DestroyInstruction);
}
addScopes() {
this.app.addScopes({
useTriggerWorkflowsActionProps,
});
}
addComponents() {
this.app.addComponents({
WorkflowPage,
ExecutionPage,
});
}
addRoutes() {
this.app.router.add('admin.workflow.workflows.id', {
path: getWorkflowDetailPath(':id'),
element: <WorkflowPage />,
});
this.app.router.add('admin.workflow.executions.id', {
path: getWorkflowExecutionsPath(':id'),
element: <ExecutionPage />,
});
}
}