Skip to content

Commit

Permalink
Merge pull request #26 from nibble-4bits/feature/map-state-max-concur…
Browse files Browse the repository at this point in the history
…rency

Feature/map state max concurrency
  • Loading branch information
nibble-4bits authored Dec 25, 2022
2 parents 6ce16df + f77b034 commit a9823f9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
8 changes: 4 additions & 4 deletions docs/feature-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- [x] Pass
- [x] Wait
- [x] Succeed
- [x] Map
- [x] `Iterator`
- [x] `ItemsPath`
- [x] `MaxConcurrency`

## Limited support

Expand All @@ -23,10 +27,6 @@
- [ ] `HeartbeatSeconds`
- [ ] `TimeoutSecondsPath`
- [ ] `HeartbeatSecondsPath`
- Map
- [x] `Iterator`
- [x] `ItemsPath`
- [ ] `MaxConcurrency`
- Choice
- Boolean expressions
- [x] `And`
Expand Down
11 changes: 2 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"asl-validator": "^3.0.8",
"jsonpath-plus": "^6.0.1",
"lodash": "^4.17.21",
"p-limit": "^3.1.0",
"wildcard-match": "^5.1.2"
}
}
19 changes: 11 additions & 8 deletions src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { testChoiceRule } from './ChoiceHelper';
import aslValidator from 'asl-validator';
import set from 'lodash/set.js';
import cloneDeep from 'lodash/cloneDeep.js';
import pLimit from 'p-limit';

export class StateMachine {
/**
Expand Down Expand Up @@ -302,14 +303,11 @@ export class StateMachine {
return;
}

const result = new Array(items.length);
let paramValue;
for (let i = 0; i < items.length; i++) {
const item = items[i];

const processItem = (item: JSONValue, index: number): Promise<JSONValue> => {
let paramValue;
this.context['Map'] = {
Item: {
Index: i,
Index: index,
Value: item,
},
};
Expand All @@ -321,8 +319,13 @@ export class StateMachine {

// Pass the current parameter value if defined, otherwise pass the current item being iterated
const mapStateMachine = new StateMachine(state.Iterator, this.validationOptions);
result[i] = await mapStateMachine.run(paramValue ?? item, options);
}
return mapStateMachine.run(paramValue ?? item, options);
};

const DEFAULT_MAX_CONCURRENCY = 40; // If `MaxConcurrency` is 0 or not specified, default to running 40 iterations concurrently
const limit = pLimit(state.MaxConcurrency || DEFAULT_MAX_CONCURRENCY);
const input = items.map((item, i) => limit(() => processItem(item, i)));
const result = await Promise.all(input);

delete this.context['Map'];
this.currResult = result;
Expand Down

0 comments on commit a9823f9

Please sign in to comment.