Skip to content

Commit 8d4d40b

Browse files
authored
Merge pull request #21 from nibble-4bits/feature/wait-state-pause-override
Feature/wait state pause override
2 parents 3826462 + 012db3f commit 8d4d40b

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

__tests__/StateMachine.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ describe('State Machine', () => {
852852
const stateMachine = new StateMachine(definition, { checkArn: false });
853853
const result = await stateMachine.run(input, {
854854
overrides: {
855-
taskResourceLocalHandler: {
855+
taskResourceLocalHandlers: {
856856
TaskState: localHandlerFn,
857857
},
858858
},
@@ -1215,6 +1215,37 @@ describe('State Machine', () => {
12151215

12161216
expect(mockSleepFunction).toHaveBeenCalledWith(20700000);
12171217
});
1218+
1219+
test('should pause execution for the specified amount of milliseconds if wait time override option is set', async () => {
1220+
const definition: StateMachineDefinition = {
1221+
StartAt: 'PassState',
1222+
States: {
1223+
PassState: {
1224+
Type: 'Pass',
1225+
Parameters: { waitUntil: '2022-12-05T05:45:00Z' },
1226+
Next: 'WaitState',
1227+
},
1228+
WaitState: {
1229+
Type: 'Wait',
1230+
TimestampPath: '$.waitUntil',
1231+
End: true,
1232+
},
1233+
},
1234+
};
1235+
const input = {};
1236+
1237+
const stateMachine = new StateMachine(definition);
1238+
await stateMachine.run(input, {
1239+
overrides: {
1240+
waitTimeOverrides: {
1241+
WaitState: 1500,
1242+
},
1243+
},
1244+
});
1245+
1246+
expect(mockSleepFunction).toHaveBeenCalledTimes(1);
1247+
expect(mockSleepFunction).toHaveBeenCalledWith(1500);
1248+
});
12181249
});
12191250

12201251
describe('Choice State', () => {

src/StateMachine.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ export class StateMachine {
262262

263263
try {
264264
// If local override for task resource is defined, use that
265-
if (options?.overrides?.taskResourceLocalHandler?.[this.currStateName]) {
266-
const overrideFn = options?.overrides?.taskResourceLocalHandler?.[this.currStateName];
265+
if (options?.overrides?.taskResourceLocalHandlers?.[this.currStateName]) {
266+
const overrideFn = options?.overrides?.taskResourceLocalHandlers?.[this.currStateName];
267267
const result = await overrideFn(this.currInput);
268268
this.currResult = result;
269269
return;
@@ -351,9 +351,17 @@ export class StateMachine {
351351
* Pauses the state machine execution for a certain amount of time
352352
* based on one of the `Seconds`, `Timestamp`, `SecondsPath` or `TimestampPath` fields.
353353
*/
354-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
355-
private async handleWaitState(_options?: RunOptions): Promise<void> {
354+
private async handleWaitState(options?: RunOptions): Promise<void> {
356355
const state = this.currState as WaitState;
356+
const waitTimeOverrideOption = options?.overrides?.waitTimeOverrides?.[this.currStateName];
357+
358+
if (typeof waitTimeOverrideOption === 'number') {
359+
// If the wait time override is set and is a number, sleep for the specified number of milliseconds
360+
// Else, if set to `true`, simply finish the state without sleeping at all.
361+
await sleep(waitTimeOverrideOption);
362+
this.currResult = this.currInput;
363+
return;
364+
}
357365

358366
if (state.Seconds) {
359367
await sleep(state.Seconds * 1000);

src/typings/StateMachineImplementation.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ type TaskStateResourceLocalHandler = {
44
[taskStateName: string]: (...args: any) => any;
55
};
66

7+
type WaitStateTimeOverride = {
8+
[waitStateName: string]: number;
9+
};
10+
711
interface Overrides {
8-
taskResourceLocalHandler?: TaskStateResourceLocalHandler;
12+
taskResourceLocalHandlers?: TaskStateResourceLocalHandler;
13+
waitTimeOverrides?: WaitStateTimeOverride;
914
}
1015

1116
export interface RunOptions {

0 commit comments

Comments
 (0)