-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathcustom-state.ts
77 lines (68 loc) · 1.98 KB
/
custom-state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Construct } from 'constructs';
import { State } from './state';
import { Chain } from '..';
import { CatchProps, IChainable, INextable, RetryProps } from '../types';
/**
* Properties for defining a custom state definition
*/
export interface CustomStateProps {
/**
* Amazon States Language (JSON-based) definition of the state
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html
*/
readonly stateJson: { [key: string]: any };
}
/**
* State defined by supplying Amazon States Language (ASL) in the state machine.
*
*/
export class CustomState extends State implements IChainable, INextable {
public readonly endStates: INextable[];
/**
* Amazon States Language (JSON-based) definition of the state
*/
private readonly stateJson: { [key: string]: any };
constructor(scope: Construct, id: string, props: CustomStateProps) {
super(scope, id, {});
this.endStates = [this];
this.stateJson = props.stateJson;
}
/**
* Add retry configuration for this state
*
* This controls if and how the execution will be retried if a particular
* error occurs.
*/
public addRetry(props: RetryProps = {}): CustomState {
super._addRetry(props);
return this;
}
/**
* Add a recovery handler for this state
*
* When a particular error occurs, execution will continue at the error
* handler instead of failing the state machine execution.
*/
public addCatch(handler: IChainable, props: CatchProps = {}): CustomState {
super._addCatch(handler.startState, props);
return this;
}
/**
* Continue normal execution with the given state
*/
public next(next: IChainable): Chain {
super.makeNext(next.startState);
return Chain.sequence(this, next);
}
/**
* Returns the Amazon States Language object for this state
*/
public toStateJson(): object {
return {
...this.renderNextEnd(),
...this.stateJson,
...this.renderRetryCatch(),
};
}
}