|
| 1 | +# Sequential Workflow Machine |
| 2 | + |
| 3 | +[](https://actions-badge.atrox.dev/b4rtaz/sequential-workflow-engine/goto?ref=main) [](/LICENSE) [](https://npmjs.org/package/sequential-workflow-engine) |
| 4 | + |
| 5 | +The powerful sequential workflow machine for frontend and backend applications. It provides a simple API for creating own step execution handlers (activities). It supports multiple types of activities. Internally is uses the [xstate](https://github.com/statelyai/xstate) library. |
| 6 | + |
| 7 | +This engine uses the same data model as the [Sequential Workflow Designer](https://github.com/nocode-js/sequential-workflow-designer). So you can create a workflow definition in the designer and then run it by this engine easily. |
| 8 | + |
| 9 | +📝 Check the [documentation](https://nocode-js.com/docs/category/sequential-workflow-machine) for more details. |
| 10 | + |
| 11 | +## 🚀 Installation |
| 12 | + |
| 13 | +Install the following packages by NPM command: |
| 14 | + |
| 15 | +``` |
| 16 | +npm i sequential-workflow-model sequential-workflow-machine |
| 17 | +``` |
| 18 | + |
| 19 | +## 🎬 Usage |
| 20 | + |
| 21 | +You can use the engine in a JavaScript or TypeScript application. We recommend to use TypeScript because a workflow uses a lot of data structures and it's hard to maintain data integrity. |
| 22 | + |
| 23 | +At the beginning you need to define the type of your workflow definition. |
| 24 | + |
| 25 | +```ts |
| 26 | +import { Definition } from 'sequential-workflow-model'; |
| 27 | + |
| 28 | +interface MyDefinition extends Definition { |
| 29 | + properties: { |
| 30 | + verbose: boolean; |
| 31 | + }; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +Next, define your step types. |
| 36 | + |
| 37 | +```ts |
| 38 | +import { Step } from 'sequential-workflow-model'; |
| 39 | + |
| 40 | +interface DownloadHtmlStep extends Step { |
| 41 | + componentType: 'task'; |
| 42 | + type: 'downloadHtml'; |
| 43 | + properties: { |
| 44 | + pageUrl: string; |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +// ... |
| 49 | +``` |
| 50 | + |
| 51 | +Prepare the workflow definition. |
| 52 | + |
| 53 | +```ts |
| 54 | +const definition: MyDefinition = { |
| 55 | + properties: { |
| 56 | + verbose: true, |
| 57 | + }, |
| 58 | + sequence: [ |
| 59 | + { |
| 60 | + id: '0x00001', |
| 61 | + componentType: 'task', |
| 62 | + type: 'downloadHtml', |
| 63 | + name: 'Download google.com', |
| 64 | + properties: { |
| 65 | + pageUrl: 'https://www.google.com', |
| 66 | + }, |
| 67 | + }, |
| 68 | + ], |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +Prepare the global state interface. |
| 73 | + |
| 74 | +```ts |
| 75 | +interface WorkflowGlobalState { |
| 76 | + html: string | null; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Prepare activities for your steps. The engine supports multiple types of activities. The basic activity is the atom activity. It's a simple handler that executes an atomic step and updates the global state. |
| 81 | + |
| 82 | +```ts |
| 83 | +import { createAtomActivity } from 'sequential-workflow-machine'; |
| 84 | + |
| 85 | +interface DownloadHtmlStepState { |
| 86 | + attempt: number; |
| 87 | +} |
| 88 | + |
| 89 | +const downloadHtmlActivity = createAtomActivity<DownloadHtmlStep, WorkflowGlobalState, DownloadHtmlStepState>({ |
| 90 | + stepType: 'downloadHtml', |
| 91 | + init: () => ({ |
| 92 | + attempt: 0, |
| 93 | + }), |
| 94 | + handler: async (step: DownloadHtmlStep, globalState: WorkflowGlobalState, activityState: DownloadHtmlStepState) => { |
| 95 | + globalState.html = await downloadHtml(step.properties.pageUrl); |
| 96 | + activityState.attempt++; |
| 97 | + }, |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +Now we can create the activity set. The activity set is a collection of all supported activities. |
| 102 | + |
| 103 | +```ts |
| 104 | +import { activitySet } from 'sequential-workflow-machine'; |
| 105 | + |
| 106 | +const activitySet = createActivitySet<WorkflowGlobalState>([ |
| 107 | + downloadHtmlActivity, |
| 108 | +]); |
| 109 | +``` |
| 110 | + |
| 111 | +Finally, we can create the workflow machine and run it. |
| 112 | + |
| 113 | +```ts |
| 114 | +import { createWorkflowMachineBuilder } from 'sequential-workflow-machine'; |
| 115 | + |
| 116 | +const builder = createWorkflowMachineBuilder<WorkflowGlobalState>(activitySet); |
| 117 | +const machine = builder.build(definition); |
| 118 | +const interpreter = machine.create({ |
| 119 | + init: () => { |
| 120 | + return { |
| 121 | + html: null, |
| 122 | + }; |
| 123 | + } |
| 124 | +}); |
| 125 | +interpreter.onChange(() => { /* ... */ }); |
| 126 | +interpreter.onDone(() => { /* ... */ }); |
| 127 | +interpreter.start(); |
| 128 | +``` |
| 129 | + |
| 130 | +That's it! |
| 131 | + |
| 132 | +## 💡 License |
| 133 | + |
| 134 | +This project is released under the MIT license. |
0 commit comments