@logicflow/engine 0.0.1

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 (67) hide show
  1. package/README.md +22 -0
  2. package/cjs/EventEmitter.js +70 -0
  3. package/cjs/FlowModel.js +252 -0
  4. package/cjs/Scheduler.js +278 -0
  5. package/cjs/constant/LogCode.js +31 -0
  6. package/cjs/constant/constant.js +23 -0
  7. package/cjs/expression/browserVm.js +69 -0
  8. package/cjs/expression/index.js +63 -0
  9. package/cjs/expression/nodeVm.js +53 -0
  10. package/cjs/index.js +161 -0
  11. package/cjs/nodes/BaseNode.js +243 -0
  12. package/cjs/nodes/StartNode.js +27 -0
  13. package/cjs/nodes/TaskNode.js +27 -0
  14. package/cjs/recorder/index.js +112 -0
  15. package/cjs/util/ID.js +12 -0
  16. package/cjs/util/global.js +32 -0
  17. package/cjs/util/storage.js +41 -0
  18. package/es/EventEmitter.d.ts +7 -0
  19. package/es/EventEmitter.js +68 -0
  20. package/es/FlowModel.d.ts +104 -0
  21. package/es/FlowModel.js +250 -0
  22. package/es/Scheduler.d.ts +51 -0
  23. package/es/Scheduler.js +276 -0
  24. package/es/constant/LogCode.d.ts +12 -0
  25. package/es/constant/LogCode.js +28 -0
  26. package/es/constant/constant.d.ts +14 -0
  27. package/es/constant/constant.js +20 -0
  28. package/es/expression/browserVm.d.ts +2 -0
  29. package/es/expression/browserVm.js +66 -0
  30. package/es/expression/index.d.ts +2 -0
  31. package/es/expression/index.js +60 -0
  32. package/es/expression/nodeVm.d.ts +2 -0
  33. package/es/expression/nodeVm.js +50 -0
  34. package/es/index.d.ts +47 -0
  35. package/es/index.js +156 -0
  36. package/es/nodes/BaseNode.d.ts +109 -0
  37. package/es/nodes/BaseNode.js +241 -0
  38. package/es/nodes/StartNode.d.ts +5 -0
  39. package/es/nodes/StartNode.js +25 -0
  40. package/es/nodes/TaskNode.d.ts +5 -0
  41. package/es/nodes/TaskNode.js +25 -0
  42. package/es/recorder/index.d.ts +9 -0
  43. package/es/recorder/index.js +110 -0
  44. package/es/util/ID.d.ts +2 -0
  45. package/es/util/ID.js +9 -0
  46. package/es/util/global.d.ts +5 -0
  47. package/es/util/global.js +26 -0
  48. package/es/util/storage.d.ts +6 -0
  49. package/es/util/storage.js +39 -0
  50. package/lib/main.js +1 -0
  51. package/package.json +87 -0
  52. package/types/EventEmitter.d.ts +7 -0
  53. package/types/FlowModel.d.ts +104 -0
  54. package/types/Scheduler.d.ts +51 -0
  55. package/types/constant/LogCode.d.ts +12 -0
  56. package/types/constant/constant.d.ts +14 -0
  57. package/types/expression/browserVm.d.ts +2 -0
  58. package/types/expression/index.d.ts +2 -0
  59. package/types/expression/nodeVm.d.ts +2 -0
  60. package/types/index.d.ts +47 -0
  61. package/types/nodes/BaseNode.d.ts +109 -0
  62. package/types/nodes/StartNode.d.ts +5 -0
  63. package/types/nodes/TaskNode.d.ts +5 -0
  64. package/types/recorder/index.d.ts +9 -0
  65. package/types/util/ID.d.ts +2 -0
  66. package/types/util/global.d.ts +5 -0
  67. package/types/util/storage.d.ts +6 -0
@@ -0,0 +1,51 @@
1
+ import EventEmitter from './EventEmitter';
2
+ import type { TaskParam, NodeParam, ResumeParam } from './types.d';
3
+ import type FlowModel from './FlowModel';
4
+ import type Recorder from './recorder';
5
+ declare type TaskParamMap = Map<string, TaskParam>;
6
+ /**
7
+ * 调度器
8
+ * 通过一个队列维护需要执行的节点,一个集合维护正在执行的节点
9
+ */
10
+ export default class Scheduler extends EventEmitter {
11
+ nodeQueueMap: Map<string, NodeParam[]>;
12
+ taskRunningMap: Map<string, TaskParamMap>;
13
+ flowModel: FlowModel;
14
+ recorder: Recorder;
15
+ currentTask: TaskParam | null;
16
+ constructor(config: any);
17
+ /**
18
+ * 添加一个任务到队列中。
19
+ * 1. 由流程模型将所有的开始节点添加到队列中。
20
+ * 2. 当一个节点执行完成后,将后续的节点添加到队列中。
21
+ */
22
+ addTask(nodeParam: NodeParam): void;
23
+ /**
24
+ * 调度器执行下一个任务
25
+ * 1. 提供给流程模型,用户开始执行第一个任务。
26
+ * 2. 内部任务执行完成后,调用此方法继续执行下一个任务。
27
+ * 3. 当判断没有可以继续执行的任务后,触发流程结束事件。
28
+ */
29
+ run(runParams: {
30
+ executionId: string;
31
+ nodeId?: string;
32
+ taskId?: string;
33
+ }): void;
34
+ /**
35
+ * 恢复某个任务的执行。
36
+ * 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行。
37
+ */
38
+ resume(resumeParam: ResumeParam): Promise<void>;
39
+ stop(data: any): void;
40
+ private pushTaskToRunningMap;
41
+ private removeTaskFromRunningMap;
42
+ private hasRunningTask;
43
+ private exec;
44
+ private interrupted;
45
+ private next;
46
+ /**
47
+ * 为了防止多次添加导致
48
+ */
49
+ private saveTaskResult;
50
+ }
51
+ export {};
@@ -0,0 +1,12 @@
1
+ export declare enum ErrorCode {
2
+ NONE_START_NODE = 1000,
3
+ NONE_NODE_ID = 1001,
4
+ NO_DOCUMENT_BODY = 2001
5
+ }
6
+ export declare enum WarningCode {
7
+ NONE_START_NODE_IN_DATA = 2000,
8
+ START_NODE_INCOMING = 2001,
9
+ EXPRESSION_EXEC_ERROR = 3000
10
+ }
11
+ export declare const getErrorMsg: (code: ErrorCode) => string;
12
+ export declare const getWarningMsg: (code: WarningCode) => string;
@@ -0,0 +1,14 @@
1
+ export declare const BASE_START_NODE = "start";
2
+ export declare const EVENT_INSTANCE_COMPLETE = "instance:complete";
3
+ export declare const EVENT_INSTANCE_INTERRUPTED = "instance:interrupted";
4
+ export declare enum FlowStatus {
5
+ COMPLETED = "completed",
6
+ INTERRUPTED = "interrupted",
7
+ RUNNING = "running",
8
+ ERROR = "error"
9
+ }
10
+ export declare enum TaskStatus {
11
+ SUCCESS = "success",
12
+ ERROR = "error",
13
+ INTERRUPTED = "interrupted"
14
+ }
@@ -0,0 +1,2 @@
1
+ declare const runInBrowserContext: (code: string, globalData?: any) => Promise<any>;
2
+ export { runInBrowserContext, };
@@ -0,0 +1,2 @@
1
+ declare const getExpressionResult: (code: string, context: any) => Promise<any>;
2
+ export { getExpressionResult, };
@@ -0,0 +1,2 @@
1
+ declare const runInNewContext: (code: string, globalData?: any) => Promise<any>;
2
+ export { runInNewContext, };
@@ -0,0 +1,47 @@
1
+ import type { ResumeParams, GraphConfigData } from './types.d';
2
+ import FlowModel, { TaskParams } from './FlowModel';
3
+ import StartNode from './nodes/StartNode';
4
+ import TaskNode from './nodes/TaskNode';
5
+ import Recorder from './recorder';
6
+ export default class Engine {
7
+ global: Record<string, any>;
8
+ graphData: GraphConfigData;
9
+ nodeModelMap: Map<string, any>;
10
+ flowModel: FlowModel;
11
+ recorder: Recorder;
12
+ constructor();
13
+ /**
14
+ * 注册节点
15
+ * @param nodeConfig { type: 'custom-node', model: Class }
16
+ */
17
+ register(nodeConfig: any): void;
18
+ /**
19
+ * 自定义执行记录的存储,默认浏览器使用 sessionStorage,nodejs 使用内存存储。
20
+ * 注意:由于执行记录不会主动删除,所以需要自行清理。
21
+ * nodejs环境建议自定义为持久化存储。
22
+ * engine.setCustomRecorder({
23
+ * async addTask(task) {}
24
+ * async getTask(taskId) {}
25
+ * async getExecutionTasks(executionId) {}
26
+ * clear() {}
27
+ * });
28
+ */
29
+ setCustomRecorder(recorder: Recorder): void;
30
+ /**
31
+ * 加载流程图数据
32
+ */
33
+ load({ graphData, startNodeType, globalData, context, }: {
34
+ graphData: any;
35
+ startNodeType?: string;
36
+ globalData?: {};
37
+ context?: {};
38
+ }): FlowModel;
39
+ /**
40
+ * 执行流程,允许多次调用。
41
+ */
42
+ execute(execParam?: TaskParams): Promise<unknown>;
43
+ resume(resumeParam: ResumeParams): Promise<unknown>;
44
+ getExecutionRecord(executionId: any): Promise<any[]>;
45
+ }
46
+ export { Engine, TaskNode, StartNode, };
47
+ export type { TaskParams, };
@@ -0,0 +1,109 @@
1
+ import type { ActionResult, NodeExecResult, ExecResumeParams, ExecParams } from '../types.d';
2
+ export interface BaseNodeInterface {
3
+ outgoing: Record<string, any>[];
4
+ incoming: Record<string, any>[];
5
+ nodeId: string;
6
+ type: string;
7
+ readonly baseType: string;
8
+ execute(taskParam: any): Promise<NodeExecResult>;
9
+ }
10
+ export declare type NodeConstructor = {
11
+ new (config: {
12
+ nodeConfig: NodeConfig;
13
+ context: Record<string, any>;
14
+ globalData: Record<string, any>;
15
+ }): BaseNode;
16
+ };
17
+ export declare type IncomingConfig = {
18
+ id: string;
19
+ properties?: Record<string, any>;
20
+ source: string;
21
+ };
22
+ export declare type OutgoingConfig = {
23
+ id: string;
24
+ target: string;
25
+ properties?: Record<string, any>;
26
+ };
27
+ export declare type NodeConfig = {
28
+ id: string;
29
+ type: string;
30
+ properties?: Record<string, any>;
31
+ incoming: IncomingConfig[];
32
+ outgoing: OutgoingConfig[];
33
+ };
34
+ export declare type NextTaskParam = {
35
+ executionId: string;
36
+ nodeId: string;
37
+ taskId: string;
38
+ nodeType: string;
39
+ outgoing: OutgoingConfig[];
40
+ properties?: Record<string, any>;
41
+ };
42
+ export default class BaseNode implements BaseNodeInterface {
43
+ static nodeTypeName: string;
44
+ /**
45
+ * 节点的出边
46
+ */
47
+ outgoing: OutgoingConfig[];
48
+ /**
49
+ * 节点的入边
50
+ */
51
+ incoming: IncomingConfig[];
52
+ /**
53
+ * 节点的属性
54
+ */
55
+ properties?: Record<string, any>;
56
+ nodeId: string;
57
+ type: string;
58
+ /**
59
+ * 节点的上下文,是调用流程时传入的上下文
60
+ */
61
+ context: Record<string, any>;
62
+ /**
63
+ * 节点的全局数据,是调用流程时传入的全局数据。
64
+ * 在计算表达式时,即基于全局数据进行计算。
65
+ */
66
+ globalData: Record<string, any>;
67
+ readonly baseType: string;
68
+ constructor({ nodeConfig, context, globalData }: {
69
+ nodeConfig: any;
70
+ context: any;
71
+ globalData: any;
72
+ });
73
+ /**
74
+ * 节点的每一次执行都会生成一个唯一的taskId
75
+ */
76
+ execute(params: ExecParams): Promise<NodeExecResult>;
77
+ /**
78
+ * 节点在执行中断后,可以通过resume方法恢复执行。
79
+ * 自定义节点时不建议重写此方法
80
+ */
81
+ resume(params: ExecResumeParams): Promise<undefined>;
82
+ private getOutgoing;
83
+ private isPass;
84
+ /**
85
+ * 节点的执行逻辑
86
+ * @overridable 可以自定义节点重写此方法。
87
+ * @param params.executionId 流程执行记录ID
88
+ * @param params.taskId 此节点执行记录ID
89
+ * @param params.nodeId 节点ID
90
+ */
91
+ action(params: {
92
+ executionId: string;
93
+ taskId: string;
94
+ nodeId: string;
95
+ }): Promise<ActionResult>;
96
+ /**
97
+ * 节点的重新恢复执行逻辑
98
+ * @overridable 可以自定义节点重写此方法。
99
+ * @param params.executionId 流程执行记录ID
100
+ * @param params.taskId 此节点执行记录ID
101
+ * @param params.nodeId 节点ID
102
+ */
103
+ onResume(params: {
104
+ executionId: string;
105
+ taskId: string;
106
+ nodeId: string;
107
+ data?: Record<string, any>;
108
+ }): Promise<void>;
109
+ }
@@ -0,0 +1,5 @@
1
+ import BaseNode from './BaseNode';
2
+ export default class StartNode extends BaseNode {
3
+ static nodeTypeName: string;
4
+ readonly baseType = "start";
5
+ }
@@ -0,0 +1,5 @@
1
+ import BaseNode from './BaseNode';
2
+ export default class TaskNode extends BaseNode {
3
+ static nodeTypeName: string;
4
+ readonly baseType = "task";
5
+ }
@@ -0,0 +1,9 @@
1
+ import type { RecorderData, RecorderInterface } from '../types.d';
2
+ export default class Recorder implements RecorderInterface {
3
+ addTask(task: RecorderData): Promise<void>;
4
+ getTask(taskId: string): Promise<RecorderData>;
5
+ getExecutionTasks(executionId: any): Promise<any>;
6
+ clear(): void;
7
+ private pushExecution;
8
+ private pushTaskToExecution;
9
+ }
@@ -0,0 +1,2 @@
1
+ export declare const createExecId: () => string;
2
+ export declare const createTaskId: () => string;
@@ -0,0 +1,5 @@
1
+ declare const isInBrowser: boolean;
2
+ declare const isInNodeJS: boolean;
3
+ declare const isInWebWorker: Function;
4
+ declare const globalScope: Record<string, any>;
5
+ export { globalScope, isInBrowser, isInWebWorker, isInNodeJS, };
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ setItem(key: any, value: any): void;
3
+ getItem(key: any): any;
4
+ removeItem(key: any): void;
5
+ };
6
+ export default _default;