Skip to content

Commit d2a37d4

Browse files
authored
0.3.2. (#5)
1 parent 20c6b4b commit d2a37d4

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.2
2+
3+
The `LoopActivityEventHandler` can return `void` or `Promise` now.
4+
15
## 0.3.1
26

37
This version adds a new feature to the `break` activity. Now it is possible to break a parent loop without specifying the name of the loop. The previous approach is still supported.
@@ -18,14 +22,14 @@ This version changes the syntax of all `create*Activity` functions. The first ar
1822
const fooActivity = createAtomActivity<FooStep, MyGlobalState, FooStateState>({
1923
stepType: 'foo',
2024
init: /* ... */,
21-
handler: /* ... */,
22-
})
25+
handler: /* ... */
26+
});
2327

2428
// New syntax
2529
const fooActivity = createAtomActivity<FooStep, MyGlobalState, FooStateState>('foo', {
2630
init: /* ... */,
27-
handler: /* ... */,
28-
})
31+
handler: /* ... */
32+
});
2933
```
3034

3135
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.

machine/package.json

Lines changed: 1 addition & 1 deletion
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.3.1",
4+
"version": "0.3.2",
55
"type": "module",
66
"main": "./lib/esm/index.js",
77
"types": "./lib/index.d.ts",

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export class LoopActivityNodeBuilder<TStep extends SequentialStep, TGlobalState,
4848
src: catchUnhandledError(async (context: MachineContext<TGlobalState>) => {
4949
if (this.config.onEnter) {
5050
const internalState = activityStateProvider.get(context, nodeId);
51-
this.config.onEnter(step, context.globalState, internalState.activityState);
51+
const promise = this.config.onEnter(step, context.globalState, internalState.activityState);
52+
if (promise) {
53+
await promise;
54+
}
5255
}
5356
}),
5457
onDone: 'CONDITION',
@@ -95,7 +98,10 @@ export class LoopActivityNodeBuilder<TStep extends SequentialStep, TGlobalState,
9598
src: catchUnhandledError(async (context: MachineContext<TGlobalState>) => {
9699
if (this.config.onLeave) {
97100
const internalState = activityStateProvider.get(context, nodeId);
98-
this.config.onLeave(step, context.globalState, internalState.activityState);
101+
const promise = this.config.onLeave(step, context.globalState, internalState.activityState);
102+
if (promise) {
103+
await promise;
104+
}
99105
}
100106
}),
101107
onDone: nextNodeTarget,

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Definition, SequentialStep, Step } from 'sequential-workflow-model';
66

77
interface TestGlobalState {
88
counter: number;
9+
trace: string;
910
}
1011

1112
function createTaskStep(id: string): Step {
@@ -44,7 +45,24 @@ const activitySet = createActivitySet<TestGlobalState>([
4445
createLoopActivity<SequentialStep, TestGlobalState>('loop', {
4546
loopName: () => 'loop',
4647
init: () => ({}),
48+
onEnter: (_, globalState) => {
49+
return new Promise(resolve => {
50+
setTimeout(() => {
51+
globalState.trace += '(onEnter)';
52+
resolve();
53+
}, 10);
54+
});
55+
},
56+
onLeave: (_, globalState) => {
57+
return new Promise(resolve => {
58+
setTimeout(() => {
59+
globalState.trace += '(onLeave)';
60+
resolve();
61+
}, 10);
62+
});
63+
},
4764
condition: async (_, globalState) => {
65+
globalState.trace += '(condition)';
4866
return globalState.counter < 3;
4967
}
5068
})
@@ -58,7 +76,8 @@ describe('LoopActivity', () => {
5876
const interpreter = machine
5977
.create({
6078
init: () => ({
61-
counter: 0
79+
counter: 0,
80+
trace: ''
6281
})
6382
})
6483
.start();
@@ -67,6 +86,7 @@ describe('LoopActivity', () => {
6786
const globalState = interpreter.getSnapshot().globalState;
6887

6988
expect(globalState.counter).toBe(4);
89+
expect(globalState.trace).toBe('(onEnter)(condition)(condition)(condition)(onLeave)');
7090

7191
done();
7292
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type LoopActivityEventHandler<TStep extends SequentialStep, TGlobalState,
77
step: TStep,
88
globalState: TGlobalState,
99
activityState: TActivityState
10-
) => void;
10+
) => Promise<void> | void;
1111
export type LoopActivityConditionHandler<TStep extends SequentialStep, TGlobalState, TActivityState> = (
1212
step: TStep,
1313
globalState: TGlobalState,

0 commit comments

Comments
 (0)