@nocobase/plugin-workflow-parallel 0.17.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +9 -0
  3. package/README.zh-CN.md +9 -0
  4. package/client.d.ts +2 -0
  5. package/client.js +1 -0
  6. package/dist/client/ParallelInstruction.d.ts +30 -0
  7. package/dist/client/index.d.ts +6 -0
  8. package/dist/client/index.js +21 -0
  9. package/dist/externalVersion.js +9 -0
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.js +39 -0
  12. package/dist/locale/en-US.json +11 -0
  13. package/dist/locale/es-ES.json +10 -0
  14. package/dist/locale/fr-FR.json +10 -0
  15. package/dist/locale/index.d.ts +3 -0
  16. package/dist/locale/index.js +39 -0
  17. package/dist/locale/ja-JP.json +7 -0
  18. package/dist/locale/pt-BR.json +10 -0
  19. package/dist/locale/ru-RU.json +7 -0
  20. package/dist/locale/tr-TR.json +7 -0
  21. package/dist/locale/zh-CN.json +11 -0
  22. package/dist/server/ParallelInstruction.d.ts +10 -0
  23. package/dist/server/ParallelInstruction.js +124 -0
  24. package/dist/server/Plugin.d.ts +6 -0
  25. package/dist/server/Plugin.js +42 -0
  26. package/dist/server/index.d.ts +1 -0
  27. package/dist/server/index.js +33 -0
  28. package/package.json +25 -0
  29. package/server.d.ts +2 -0
  30. package/server.js +1 -0
  31. package/src/client/ParallelInstruction.tsx +147 -0
  32. package/src/client/index.ts +19 -0
  33. package/src/index.ts +2 -0
  34. package/src/locale/en-US.json +11 -0
  35. package/src/locale/es-ES.json +10 -0
  36. package/src/locale/fr-FR.json +10 -0
  37. package/src/locale/index.ts +12 -0
  38. package/src/locale/ja-JP.json +7 -0
  39. package/src/locale/pt-BR.json +10 -0
  40. package/src/locale/ru-RU.json +7 -0
  41. package/src/locale/tr-TR.json +7 -0
  42. package/src/locale/zh-CN.json +11 -0
  43. package/src/server/ParallelInstruction.ts +119 -0
  44. package/src/server/Plugin.ts +14 -0
  45. package/src/server/__tests__/instruction.test.ts +560 -0
  46. package/src/server/index.ts +1 -0
@@ -0,0 +1,147 @@
1
+ import React, { useState } from 'react';
2
+ import { Button, Tooltip } from 'antd';
3
+ import { PlusOutlined } from '@ant-design/icons';
4
+
5
+ import { css } from '@nocobase/client';
6
+
7
+ import {
8
+ NodeDefaultView,
9
+ Branch,
10
+ useFlowContext,
11
+ useStyles,
12
+ useGetAriaLabelOfAddButton,
13
+ RadioWithTooltip,
14
+ Instruction,
15
+ } from '@nocobase/plugin-workflow/client';
16
+
17
+ import { NAMESPACE, useLang } from '../locale';
18
+
19
+ export default class extends Instruction {
20
+ title = `{{t("Parallel branch", { ns: "${NAMESPACE}" })}}`;
21
+ type = 'parallel';
22
+ group = 'control';
23
+ description = `{{t("Run multiple branch processes in parallel.", { ns: "${NAMESPACE}" })}}`;
24
+ fieldset = {
25
+ mode: {
26
+ type: 'string',
27
+ title: `{{t("Mode", { ns: "${NAMESPACE}" })}}`,
28
+ 'x-decorator': 'FormItem',
29
+ 'x-component': 'RadioWithTooltip',
30
+ 'x-component-props': {
31
+ options: [
32
+ {
33
+ value: 'all',
34
+ label: `{{t('All succeeded', { ns: "${NAMESPACE}" })}}`,
35
+ tooltip: `{{t('Continue after all branches succeeded', { ns: "${NAMESPACE}" })}}`,
36
+ },
37
+ {
38
+ value: 'any',
39
+ label: `{{t('Any succeeded', { ns: "${NAMESPACE}" })}}`,
40
+ tooltip: `{{t('Continue after any branch succeeded', { ns: "${NAMESPACE}" })}}`,
41
+ },
42
+ {
43
+ value: 'race',
44
+ label: `{{t('Any succeeded or failed', { ns: "${NAMESPACE}" })}}`,
45
+ tooltip: `{{t('Continue after any branch succeeded, or exit after any branch failed.', { ns: "${NAMESPACE}" })}}`,
46
+ },
47
+ ],
48
+ },
49
+ default: 'all',
50
+ },
51
+ };
52
+ components = {
53
+ RadioWithTooltip,
54
+ };
55
+ Component({ data }) {
56
+ // eslint-disable-next-line react-hooks/rules-of-hooks
57
+ const { styles } = useStyles();
58
+ const {
59
+ id,
60
+ config: { mode },
61
+ } = data;
62
+ // eslint-disable-next-line react-hooks/rules-of-hooks
63
+ const { workflow, nodes } = useFlowContext();
64
+ const branches = nodes
65
+ .reduce((result, node) => {
66
+ if (node.upstreamId === id && node.branchIndex != null) {
67
+ return result.concat(node);
68
+ }
69
+ return result;
70
+ }, [])
71
+ .sort((a, b) => a.branchIndex - b.branchIndex);
72
+ // eslint-disable-next-line react-hooks/rules-of-hooks
73
+ const [branchCount, setBranchCount] = useState(Math.max(2, branches.length));
74
+ // eslint-disable-next-line react-hooks/rules-of-hooks
75
+ const { getAriaLabel } = useGetAriaLabelOfAddButton(data);
76
+ // eslint-disable-next-line react-hooks/rules-of-hooks
77
+ const langAddBranch = useLang('Add branch');
78
+
79
+ const tempBranches = Array(Math.max(0, branchCount - branches.length)).fill(null);
80
+ const lastBranchHead = branches[branches.length - 1];
81
+
82
+ return (
83
+ <NodeDefaultView data={data}>
84
+ <div className={styles.nodeSubtreeClass}>
85
+ <div className={styles.branchBlockClass}>
86
+ {branches.map((branch) => (
87
+ <Branch key={branch.id} from={data} entry={branch} branchIndex={branch.branchIndex} />
88
+ ))}
89
+ {tempBranches.map((_, i) => (
90
+ <Branch
91
+ key={`temp_${branches.length + i}`}
92
+ from={data}
93
+ branchIndex={(lastBranchHead ? lastBranchHead.branchIndex : 0) + i + 1}
94
+ controller={
95
+ branches.length + i > 1 ? (
96
+ <div
97
+ className={css`
98
+ padding-top: 2em;
99
+
100
+ > button {
101
+ .anticon {
102
+ transform: rotate(45deg);
103
+ }
104
+ }
105
+ `}
106
+ >
107
+ <Button
108
+ shape="circle"
109
+ icon={<PlusOutlined />}
110
+ onClick={() => setBranchCount(branchCount - 1)}
111
+ disabled={workflow.executed}
112
+ />
113
+ </div>
114
+ ) : null
115
+ }
116
+ />
117
+ ))}
118
+ </div>
119
+ <Tooltip
120
+ title={langAddBranch}
121
+ className={css`
122
+ visibility: ${workflow.executed ? 'hidden' : 'visible'};
123
+ `}
124
+ >
125
+ <Button
126
+ aria-label={getAriaLabel('add-branch')}
127
+ icon={<PlusOutlined />}
128
+ className={css`
129
+ position: relative;
130
+ top: 1em;
131
+ transform-origin: center;
132
+ transform: rotate(45deg);
133
+
134
+ .anticon {
135
+ transform-origin: center;
136
+ transform: rotate(-45deg);
137
+ }
138
+ `}
139
+ onClick={() => setBranchCount(branchCount + 1)}
140
+ disabled={workflow.executed}
141
+ />
142
+ </Tooltip>
143
+ </div>
144
+ </NodeDefaultView>
145
+ );
146
+ }
147
+ }
@@ -0,0 +1,19 @@
1
+ import { Plugin } from '@nocobase/client';
2
+ import WorkflowPlugin from '@nocobase/plugin-workflow/client';
3
+
4
+ import ParallelInstruction from './ParallelInstruction';
5
+
6
+ export default class extends Plugin {
7
+ async afterAdd() {
8
+ // await this.app.pm.add()
9
+ }
10
+
11
+ async beforeLoad() {}
12
+
13
+ // You can get and modify the app instance here
14
+ async load() {
15
+ const workflow = this.app.pm.get(WorkflowPlugin);
16
+ const parallelInstruction = new ParallelInstruction();
17
+ workflow.instructions.register(parallelInstruction.type, parallelInstruction);
18
+ }
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './server';
2
+ export { default } from './server';
@@ -0,0 +1,11 @@
1
+ {
2
+ "Parallel branch": "Parallel branch",
3
+ "Run multiple branch processes in parallel.": "Run multiple branch processes in parallel.",
4
+ "Add branch": "Add branch",
5
+ "All succeeded": "All succeeded",
6
+ "Any succeeded": "Any succeeded",
7
+ "Any succeeded or failed": "Any succeeded or failed",
8
+ "Continue after all branches succeeded": "Continue after all branches succeeded",
9
+ "Continue after any branch succeeded": "Continue after any branch succeeded",
10
+ "Continue after any branch succeeded, or exit after any branch failed.": "Continue after any branch succeeded, or exit after any branch failed."
11
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "Parallel branch": "Rama paralela",
3
+ "Add branch": "Añadir rama",
4
+ "All succeeded": "Todo correcto",
5
+ "Any succeeded": "Cualquiera con éxito",
6
+ "Any succeeded or failed": "Cualquiera tuvo éxito o falló",
7
+ "Continue after all branches succeeded": "Continuar después que todas las ramas han tenido éxito",
8
+ "Continue after any branch succeeded": "Continuar después que cualquier rama tenga éxito",
9
+ "Continue after any branch succeeded, or exit after any branch failed": "Continuar después de que cualquier rama tenga éxito, o salir después de que cualquier rama falle"
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "Parallel branch": "Branche parallèle",
3
+ "Add branch": "Ajouter une branche",
4
+ "All succeeded": "Tous réussis",
5
+ "Any succeeded": "Un réussi",
6
+ "Any succeeded or failed": "Un réussi ou un échoué",
7
+ "Continue after all branches succeeded": "Continuer après la réussite de toutes les branches",
8
+ "Continue after any branch succeeded": "Continuer après la réussite d'une branche",
9
+ "Continue after any branch succeeded, or exit after any branch failed": "Continuer après la réussite d'une branche, ou quitter après l'échec d'une branche"
10
+ }
@@ -0,0 +1,12 @@
1
+ import { useTranslation } from 'react-i18next';
2
+
3
+ export const NAMESPACE = 'workflow-parallel';
4
+
5
+ export function useLang(key: string, options = {}) {
6
+ const { t } = usePluginTranslation(options);
7
+ return t(key);
8
+ }
9
+
10
+ export function usePluginTranslation(options) {
11
+ return useTranslation(NAMESPACE, options);
12
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "Parallel branch": "分岐",
3
+ "All succeeded": "すべて成功",
4
+ "Any succeeded": "いずれかが成功",
5
+ "Continue after all branches succeeded": "すべての分岐が成功した後に続行",
6
+ "Continue after any branch succeeded": "いずれかの分岐が成功した後に続行"
7
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "Parallel branch": "Ramo paralelo",
3
+ "Add branch": "Adicionar ramo",
4
+ "All succeeded": "Todos com sucesso",
5
+ "Any succeeded": "Qualquer um com sucesso",
6
+ "Any succeeded or failed": "Qualquer um com sucesso ou falha",
7
+ "Continue after all branches succeeded": "Continuar após todos os ramos com sucesso",
8
+ "Continue after any branch succeeded": "Continuar após qualquer ramo com sucesso",
9
+ "Continue after any branch succeeded, or exit after any branch failed": "Continuar após qualquer ramo com sucesso ou sair após qualquer ramo falhar"
10
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "Parallel branch": "Параллельная ветвь",
3
+ "All succeeded": "Всё успешно",
4
+ "Any succeeded": "Что-то успешно",
5
+ "Continue after all branches succeeded": "Продолжать после успеха на всех ветвях",
6
+ "Continue after any branch succeeded": "Продолжать после успеха на любой из ветвей"
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "Parallel branch": "Paralel dal",
3
+ "All succeeded": "Hepsi başarılı",
4
+ "Any succeeded": "Herhangi biri başarılı",
5
+ "Continue after all branches succeeded": "Tüm dallar başarılı olduktan sonra devam et",
6
+ "Continue after any branch succeeded": "Herhangi bir dal başarılı olduktan sonra devam et"
7
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "Parallel branch": "分支",
3
+ "Run multiple branch processes in parallel.": "并行运行多个分支流程。",
4
+ "Add branch": "增加分支",
5
+ "All succeeded": "全部成功",
6
+ "Any succeeded": "任意成功",
7
+ "Any succeeded or failed": "任意成功或失败",
8
+ "Continue after all branches succeeded": "全部分支都成功后才能继续",
9
+ "Continue after any branch succeeded": "任意分支成功后就继续",
10
+ "Continue after any branch succeeded, or exit after any branch failed.": "任意分支成功就继续流程,或者任意分支失败就退出流程。"
11
+ }
@@ -0,0 +1,119 @@
1
+ import { Processor, Instruction, JOB_STATUS, FlowNodeModel, JobModel } from '@nocobase/plugin-workflow';
2
+
3
+ export const PARALLEL_MODE = {
4
+ ALL: 'all',
5
+ ANY: 'any',
6
+ RACE: 'race',
7
+ } as const;
8
+
9
+ const Modes = {
10
+ [PARALLEL_MODE.ALL]: {
11
+ next(previous) {
12
+ return previous.status >= JOB_STATUS.PENDING;
13
+ },
14
+ getStatus(result) {
15
+ const failedStatus = result.find((status) => status != null && status < JOB_STATUS.PENDING);
16
+ if (typeof failedStatus !== 'undefined') {
17
+ return failedStatus;
18
+ }
19
+ if (result.every((status) => status != null && status === JOB_STATUS.RESOLVED)) {
20
+ return JOB_STATUS.RESOLVED;
21
+ }
22
+ return JOB_STATUS.PENDING;
23
+ },
24
+ },
25
+ [PARALLEL_MODE.ANY]: {
26
+ next(previous) {
27
+ return previous.status <= JOB_STATUS.PENDING;
28
+ },
29
+ getStatus(result) {
30
+ if (result.some((status) => status != null && status === JOB_STATUS.RESOLVED)) {
31
+ return JOB_STATUS.RESOLVED;
32
+ }
33
+ if (result.some((status) => (status != null ? status === JOB_STATUS.PENDING : true))) {
34
+ return JOB_STATUS.PENDING;
35
+ }
36
+ return JOB_STATUS.FAILED;
37
+ },
38
+ },
39
+ [PARALLEL_MODE.RACE]: {
40
+ next(previous) {
41
+ return previous.status === JOB_STATUS.PENDING;
42
+ },
43
+ getStatus(result) {
44
+ if (result.some((status) => status != null && status === JOB_STATUS.RESOLVED)) {
45
+ return JOB_STATUS.RESOLVED;
46
+ }
47
+ const failedStatus = result.find((status) => status != null && status < JOB_STATUS.PENDING);
48
+ if (typeof failedStatus !== 'undefined') {
49
+ return failedStatus;
50
+ }
51
+ return JOB_STATUS.PENDING;
52
+ },
53
+ },
54
+ };
55
+
56
+ export default class extends Instruction {
57
+ async run(node: FlowNodeModel, prevJob: JobModel, processor: Processor) {
58
+ const branches = processor.getBranches(node);
59
+
60
+ const job = await processor.saveJob({
61
+ status: JOB_STATUS.PENDING,
62
+ result: Array(branches.length).fill(null),
63
+ nodeId: node.id,
64
+ upstreamId: prevJob?.id ?? null,
65
+ });
66
+
67
+ // NOTE:
68
+ // use `reduce` but not `Promise.all` here to avoid racing manupulating db.
69
+ // for users, this is almost equivalent to `Promise.all`,
70
+ // because of the delay is not significant sensible.
71
+ // another benifit of this is, it could handle sequenced branches in future.
72
+ const { mode = PARALLEL_MODE.ALL } = node.config;
73
+ await branches.reduce(
74
+ (promise: Promise<any>, branch, i) =>
75
+ promise.then(async (previous) => {
76
+ if (i && !Modes[mode].next(previous)) {
77
+ return previous;
78
+ }
79
+ await processor.run(branch, job);
80
+
81
+ // find last job of the branch
82
+ return processor.findBranchLastJob(branch, job);
83
+ }),
84
+ Promise.resolve(),
85
+ );
86
+
87
+ return null;
88
+ }
89
+
90
+ async resume(node: FlowNodeModel, branchJob, processor: Processor) {
91
+ const job = processor.findBranchParentJob(branchJob, node) as JobModel;
92
+
93
+ const { result, status } = job;
94
+ // if parallel has been done (resolved / rejected), do not care newly executed branch jobs.
95
+ if (status !== JOB_STATUS.PENDING) {
96
+ return processor.exit();
97
+ }
98
+
99
+ // find the index of the node which start the branch
100
+ const jobNode = processor.nodesMap.get(branchJob.nodeId) as FlowNodeModel;
101
+ const branchStartNode = processor.findBranchStartNode(jobNode, node) as FlowNodeModel;
102
+ const branches = processor.getBranches(node);
103
+ const branchIndex = branches.indexOf(branchStartNode);
104
+ const { mode = PARALLEL_MODE.ALL } = node.config || {};
105
+
106
+ const newResult = [...result.slice(0, branchIndex), branchJob.status, ...result.slice(branchIndex + 1)];
107
+ job.set({
108
+ result: newResult,
109
+ status: Modes[mode].getStatus(newResult),
110
+ });
111
+
112
+ if (job.status === JOB_STATUS.PENDING) {
113
+ await job.save({ transaction: processor.transaction });
114
+ return processor.exit();
115
+ }
116
+
117
+ return job;
118
+ }
119
+ }
@@ -0,0 +1,14 @@
1
+ import { Plugin } from '@nocobase/server';
2
+ import WorkflowPlugin from '@nocobase/plugin-workflow';
3
+
4
+ import ParallelInstruction from './ParallelInstruction';
5
+
6
+ export default class extends Plugin {
7
+ workflow: WorkflowPlugin;
8
+
9
+ async load() {
10
+ const workflowPlugin = this.app.getPlugin('workflow') as WorkflowPlugin;
11
+ this.workflow = workflowPlugin;
12
+ workflowPlugin.instructions.register('parallel', new ParallelInstruction(workflowPlugin));
13
+ }
14
+ }