Skip to content

Commit a9823f9

Browse files
authored
Merge pull request #26 from nibble-4bits/feature/map-state-max-concurrency
Feature/map state max concurrency
2 parents 6ce16df + f77b034 commit a9823f9

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

docs/feature-support.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
- [x] Pass
1414
- [x] Wait
1515
- [x] Succeed
16+
- [x] Map
17+
- [x] `Iterator`
18+
- [x] `ItemsPath`
19+
- [x] `MaxConcurrency`
1620

1721
## Limited support
1822

@@ -23,10 +27,6 @@
2327
- [ ] `HeartbeatSeconds`
2428
- [ ] `TimeoutSecondsPath`
2529
- [ ] `HeartbeatSecondsPath`
26-
- Map
27-
- [x] `Iterator`
28-
- [x] `ItemsPath`
29-
- [ ] `MaxConcurrency`
3030
- Choice
3131
- Boolean expressions
3232
- [x] `And`

package-lock.json

+2-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"asl-validator": "^3.0.8",
6161
"jsonpath-plus": "^6.0.1",
6262
"lodash": "^4.17.21",
63+
"p-limit": "^3.1.0",
6364
"wildcard-match": "^5.1.2"
6465
}
6566
}

src/StateMachine.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { testChoiceRule } from './ChoiceHelper';
1616
import aslValidator from 'asl-validator';
1717
import set from 'lodash/set.js';
1818
import cloneDeep from 'lodash/cloneDeep.js';
19+
import pLimit from 'p-limit';
1920

2021
export class StateMachine {
2122
/**
@@ -302,14 +303,11 @@ export class StateMachine {
302303
return;
303304
}
304305

305-
const result = new Array(items.length);
306-
let paramValue;
307-
for (let i = 0; i < items.length; i++) {
308-
const item = items[i];
309-
306+
const processItem = (item: JSONValue, index: number): Promise<JSONValue> => {
307+
let paramValue;
310308
this.context['Map'] = {
311309
Item: {
312-
Index: i,
310+
Index: index,
313311
Value: item,
314312
},
315313
};
@@ -321,8 +319,13 @@ export class StateMachine {
321319

322320
// Pass the current parameter value if defined, otherwise pass the current item being iterated
323321
const mapStateMachine = new StateMachine(state.Iterator, this.validationOptions);
324-
result[i] = await mapStateMachine.run(paramValue ?? item, options);
325-
}
322+
return mapStateMachine.run(paramValue ?? item, options);
323+
};
324+
325+
const DEFAULT_MAX_CONCURRENCY = 40; // If `MaxConcurrency` is 0 or not specified, default to running 40 iterations concurrently
326+
const limit = pLimit(state.MaxConcurrency || DEFAULT_MAX_CONCURRENCY);
327+
const input = items.map((item, i) => limit(() => processItem(item, i)));
328+
const result = await Promise.all(input);
326329

327330
delete this.context['Map'];
328331
this.currResult = result;

0 commit comments

Comments
 (0)