Skip to content

Commit 8025a9d

Browse files
authored
0.3.0. (#3)
* 0.3.0.
1 parent 3bd7568 commit 8025a9d

31 files changed

+128
-98
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
## 0.3.0
2+
3+
This version changes the syntax of all `create*Activity` functions. The first argument is the step type, the second argument is the configuration.
4+
5+
```ts
6+
// Old syntax
7+
const fooActivity = createAtomActivity<FooStep, MyGlobalState, FooStateState>({
8+
stepType: 'foo',
9+
init: /* ... */,
10+
handler: /* ... */,
11+
})
12+
13+
// New syntax
14+
const fooActivity = createAtomActivity<FooStep, MyGlobalState, FooStateState>('some', {
15+
init: /* ... */,
16+
handler: /* ... */,
17+
})
18+
```
19+
20+
Additionally this version introduces the `createAtomActivityFromHandler` function. It allows to create an activity by very short syntax. This function creates an activity without the activity state.
21+
22+
```ts
23+
const fooActivity = createAtomActivityFromHandler<FooStep, MyGlobalState>('foo', async (step, globalState) => {
24+
// handler
25+
});
26+
```
27+
128
## 0.2.0
229

330
**Breaking Changes**

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ interface DownloadHtmlStepState {
8686
attempt: number;
8787
}
8888

89-
const downloadHtmlActivity = createAtomActivity<DownloadHtmlStep, WorkflowGlobalState, DownloadHtmlStepState>({
90-
stepType: 'downloadHtml',
89+
const downloadHtmlActivity = createAtomActivity<DownloadHtmlStep, WorkflowGlobalState, DownloadHtmlStepState>('downloadHtml', {
9190
init: () => ({
9291
attempt: 0,
9392
}),
@@ -103,7 +102,7 @@ Now we can create the activity set. The activity set is a collection of all supp
103102
```ts
104103
import { activitySet } from 'sequential-workflow-machine';
105104

106-
const activitySet = createActivitySet<WorkflowGlobalState>([
105+
const activitySet = createActivitySet<WorkflowGlobalState>([
107106
downloadHtmlActivity,
108107
]);
109108
```

machine/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sequential-workflow-machine",
33
"description": "Powerful sequential workflow machine for frontend and backend applications.",
4-
"version": "0.2.0",
4+
"version": "0.3.0",
55
"type": "module",
66
"main": "./lib/esm/index.js",
77
"types": "./lib/index.d.ts",
@@ -32,7 +32,7 @@
3232
},
3333
"scripts": {
3434
"prepare": "cp ../LICENSE LICENSE && cp ../README.md README.md",
35-
"clean": "rm -rf lib && rm -rf build && rm -rf dist",
35+
"clean": "rm -rf lib && rm -rf build && rm -rf dist && rm -rf node_modules/.cache/rollup-plugin-typescript2",
3636
"start": "rollup -c --watch",
3737
"start:clean": "yarn clean && npm run start",
3838
"build": "yarn clean && rollup -c",
@@ -43,12 +43,12 @@
4343
"prettier:fix": "prettier --write ./src"
4444
},
4545
"peerDependencies": {
46-
"sequential-workflow-model": "^0.1.1",
47-
"xstate": "^4.37.2"
46+
"sequential-workflow-model": "^0.1.4",
47+
"xstate": "^4.38.1"
4848
},
4949
"dependencies": {
50-
"sequential-workflow-model": "^0.1.1",
51-
"xstate": "^4.37.2"
50+
"sequential-workflow-model": "^0.1.4",
51+
"xstate": "^4.38.1"
5252
},
5353
"devDependencies": {
5454
"@types/jest": "^29.4.0",

machine/src/activities/atom-activity/atom-activity-node-builder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { getStepNodeId } from '../../core/safe-node-id';
66
import { Step } from 'sequential-workflow-model';
77
import { isInterruptResult } from '../results/interrupt-result';
88

9-
export class AtomActivityNodeBuilder<TStep extends Step, TGlobalState, TActivityState> implements ActivityNodeBuilder<TGlobalState> {
9+
export class AtomActivityNodeBuilder<TStep extends Step, TGlobalState, TActivityState extends object>
10+
implements ActivityNodeBuilder<TGlobalState>
11+
{
1012
public constructor(private readonly config: AtomActivityConfig<TStep, TGlobalState, TActivityState>) {}
1113

1214
public build(step: TStep, nextNodeTarget: string): ActivityNodeConfig<TGlobalState> {

machine/src/activities/atom-activity/atom-activity.spec.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createAtomActivity } from './atom-activity';
1+
import { createAtomActivity, createAtomActivityFromHandler } from './atom-activity';
22
import { createActivitySet } from '../../core/activity-set';
33
import { createWorkflowMachineBuilder } from '../../workflow-machine-builder';
44
import { STATE_FINISHED_ID, STATE_INTERRUPTED_ID, STATE_FAILED_ID } from '../../types';
@@ -17,34 +17,21 @@ interface SetCounterStep extends Step {
1717
}
1818

1919
const activitySet = createActivitySet<TestGlobalState>([
20-
createAtomActivity<SetCounterStep, TestGlobalState, { x: number }>({
21-
stepType: 'setCounter',
20+
createAtomActivity<SetCounterStep, TestGlobalState, { x: number }>('setCounter', {
2221
init: () => ({ x: 987654321 }),
2322
handler: async (step, globalState, activityState) => {
2423
expect(activityState.x).toBe(987654321);
2524
globalState.counter = step.properties.value;
2625
}
2726
}),
28-
createAtomActivity<Step, TestGlobalState, null>({
29-
stepType: 'multiply2',
30-
init: () => null,
31-
handler: async (_, globalState) => {
32-
globalState.counter *= 2;
33-
}
27+
createAtomActivityFromHandler<Step, TestGlobalState>('multiply2', async (_, globalState) => {
28+
globalState.counter *= 2;
3429
}),
35-
createAtomActivity<Step, TestGlobalState, null>({
36-
stepType: 'interrupt',
37-
init: () => null,
38-
handler: async () => {
39-
return interrupt();
40-
}
30+
createAtomActivityFromHandler<Step, TestGlobalState>('interrupt', async () => {
31+
return interrupt();
4132
}),
42-
createAtomActivity<Step, TestGlobalState, null>({
43-
stepType: 'error',
44-
init: () => null,
45-
handler: async () => {
46-
throw new Error('TEST_ERROR');
47-
}
33+
createAtomActivityFromHandler<Step, TestGlobalState>('error', async () => {
34+
throw new Error('TEST_ERROR');
4835
})
4936
]);
5037

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { Activity } from '../../types';
22
import { AtomActivityNodeBuilder } from './atom-activity-node-builder';
3-
import { AtomActivityConfig } from './types';
3+
import { AtomActivityConfig, AtomActivityHandler } from './types';
44
import { Step } from 'sequential-workflow-model';
55

6-
export function createAtomActivity<TStep extends Step = Step, TGlobalState = object, TActivityState = object>(
6+
export function createAtomActivity<TStep extends Step = Step, TGlobalState = object, TActivityState extends object = object>(
7+
stepType: TStep['type'],
78
config: AtomActivityConfig<TStep, TGlobalState, TActivityState>
89
): Activity<TGlobalState> {
910
return {
10-
stepType: config.stepType,
11+
stepType: stepType,
1112
nodeBuilderFactory: () => new AtomActivityNodeBuilder(config)
1213
};
1314
}
15+
16+
export function createAtomActivityFromHandler<TStep extends Step = Step, TGlobalState = object>(
17+
stepType: TStep['type'],
18+
handler: AtomActivityHandler<TStep, TGlobalState, Record<string, never>>
19+
): Activity<TGlobalState> {
20+
return createAtomActivity(stepType, {
21+
init: () => ({}),
22+
handler
23+
});
24+
}

machine/src/activities/atom-activity/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Step } from 'sequential-workflow-model';
2-
import { ActivityConfig, ActivityStateInitializer } from '../../types';
2+
import { ActivityStateInitializer } from '../../types';
33
import { InterruptResult } from '../results/interrupt-result';
44

55
export type AtomActivityHandler<TStep extends Step, TGlobalState, TActivityState> = (
@@ -10,7 +10,7 @@ export type AtomActivityHandler<TStep extends Step, TGlobalState, TActivityState
1010

1111
export type AtomActivityHandlerResult = void | InterruptResult;
1212

13-
export interface AtomActivityConfig<TStep extends Step, TGlobalState, TActivityState> extends ActivityConfig<TStep> {
13+
export interface AtomActivityConfig<TStep extends Step, TGlobalState, TActivityState extends object> {
1414
init: ActivityStateInitializer<TStep, TGlobalState, TActivityState>;
1515
handler: AtomActivityHandler<TStep, TGlobalState, TActivityState>;
1616
}

machine/src/activities/break-activity/break-activity-node-builder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { ActivityStateProvider, catchUnhandledError, getStepNodeId } from '../..
1313
import { isInterruptResult } from '../results';
1414
import { isBreakResult } from './break-result';
1515

16-
export class BreakActivityNodeBuilder<TStep extends Step, TGlobalState, TActivityState> implements ActivityNodeBuilder<TGlobalState> {
16+
export class BreakActivityNodeBuilder<TStep extends Step, TGlobalState, TActivityState extends object>
17+
implements ActivityNodeBuilder<TGlobalState>
18+
{
1719
public constructor(private readonly config: BreakActivityConfig<TStep, TGlobalState, TActivityState>) {}
1820

1921
public build(step: TStep, nextNodeTarget: string, buildingContext: BuildingContext): ActivityNodeConfig<TGlobalState> {

machine/src/activities/break-activity/break-activity.spec.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ const definition: Definition = {
5353
};
5454

5555
const activitySet = createActivitySet<TestGlobalState>([
56-
createLoopActivity<SequentialStep, TestGlobalState>({
57-
stepType: 'loop',
56+
createLoopActivity<SequentialStep, TestGlobalState>('loop', {
5857
loopName: step => String(step.properties['loopName']),
5958
init: () => ({}),
6059
condition: async (_, globalState) => {
@@ -63,18 +62,16 @@ const activitySet = createActivitySet<TestGlobalState>([
6362
}
6463
}),
6564

66-
createAtomActivity({
67-
stepType: 'decrement',
68-
init: () => null,
65+
createAtomActivity('decrement', {
66+
init: () => ({}),
6967
handler: async (_, globalState) => {
7068
globalState.trace += '(decrement)';
7169
globalState.alfa--;
7270
}
7371
}),
7472

75-
createBreakActivity({
76-
stepType: 'breakIfZero',
77-
init: () => null,
73+
createBreakActivity('breakIfZero', {
74+
init: () => ({}),
7875
handler: async (_, globalState) => {
7976
globalState.trace += '(break)';
8077
if (globalState.alfa === 0) {

machine/src/activities/break-activity/break-activity.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { BreakActivityNodeBuilder } from './break-activity-node-builder';
33
import { Step } from 'sequential-workflow-model';
44
import { Activity } from '../../types';
55

6-
export function createBreakActivity<TStep extends Step = Step, GlobalState = object, ActivityState = object>(
6+
export function createBreakActivity<TStep extends Step = Step, GlobalState = object, ActivityState extends object = object>(
7+
stepType: TStep['type'],
78
config: BreakActivityConfig<TStep, GlobalState, ActivityState>
89
): Activity<GlobalState> {
910
return {
10-
stepType: config.stepType,
11+
stepType,
1112
nodeBuilderFactory: () => new BreakActivityNodeBuilder(config)
1213
};
1314
}

0 commit comments

Comments
 (0)