Skip to content

Commit 6a41a77

Browse files
authored
0.2.0. (#2)
1 parent 8808b84 commit 6a41a77

File tree

4 files changed

+86
-42
lines changed

4 files changed

+86
-42
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.2.0
2+
3+
* Added the `forEachChildren` method to the `DefinitionWalker` class.
4+
5+
### Breaking Changes
6+
7+
* The `forEach` method doesn't accept a sequence anymore. To foreach over a sequence, use the `forEachSequence` method.
8+
19
## 0.1.4
210

311
* Added the `forEach()` method to the `DefinitionWalker` class.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sequential-workflow-model",
33
"description": "Extendable data model of sequential workflow.",
4-
"version": "0.1.4",
4+
"version": "0.2.0",
55
"homepage": "https://nocode-js.com/",
66
"author": {
77
"name": "NoCode JS",

src/definition-walker/definition-walker.spec.ts

+38-9
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,25 @@ describe('DefinitionWalker', () => {
164164
expect(count).toEqual(6);
165165
});
166166

167+
it('stops when callback returns false', () => {
168+
let count = 0;
169+
170+
walker.forEach(definition, () => {
171+
count++;
172+
if (count === 2) {
173+
return false;
174+
}
175+
});
176+
177+
expect(count).toEqual(2);
178+
});
179+
});
180+
181+
describe('forEachSequence', () => {
167182
it('iterates over sequence', () => {
168183
const steps: Step[] = [];
169184

170-
walker.forEach(loop.sequence, step => {
185+
walker.forEachSequence(loop.sequence, step => {
171186
steps.push(step);
172187
});
173188

@@ -176,18 +191,32 @@ describe('DefinitionWalker', () => {
176191
expect(steps[1]).toBe(ifAlfa);
177192
expect(steps[2]).toBe(taskFoo);
178193
});
194+
});
179195

180-
it('stops when callback returns false', () => {
181-
let count = 0;
196+
describe('forEachChildren', () => {
197+
it('iterates when sequential step is passed', () => {
198+
const steps: Step[] = [];
182199

183-
walker.forEach(definition, () => {
184-
count++;
185-
if (count === 2) {
186-
return false;
187-
}
200+
walker.forEachChildren(loop, step => {
201+
steps.push(step);
188202
});
189203

190-
expect(count).toEqual(2);
204+
expect(steps.length).toEqual(3);
205+
expect(steps[0]).toBe(ifBeta);
206+
expect(steps[1]).toBe(ifAlfa);
207+
expect(steps[2]).toBe(taskFoo);
208+
});
209+
210+
it('iterates when branched step is passed', () => {
211+
const steps: Step[] = [];
212+
213+
walker.forEachChildren(ifBeta, step => {
214+
steps.push(step);
215+
});
216+
217+
expect(steps.length).toEqual(2);
218+
expect(steps[0]).toBe(ifAlfa);
219+
expect(steps[1]).toBe(taskFoo);
191220
});
192221
});
193222
});

src/definition-walker/definition-walker.ts

+39-32
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,16 @@ export class DefinitionWalker {
104104
return this.getParentSequence(definition, stepId).step;
105105
}
106106

107-
public forEach(sequenceOrDefinition: Sequence | Definition, callback: StepForEachCallback) {
108-
const sequence = Array.isArray(sequenceOrDefinition) ? sequenceOrDefinition : sequenceOrDefinition.sequence;
109-
this.iterate(sequence, callback);
107+
public forEach(definition: Definition, callback: StepForEachCallback) {
108+
this.iterateSequence(definition.sequence, callback);
109+
}
110+
111+
public forEachSequence(sequence: Sequence, callback: StepForEachCallback) {
112+
this.iterateSequence(sequence, callback);
113+
}
114+
115+
public forEachChildren(step: Step, callback: StepForEachCallback) {
116+
this.iterateStep(step, callback);
110117
}
111118

112119
private find(
@@ -138,7 +145,6 @@ export class DefinitionWalker {
138145
}
139146
}
140147
break;
141-
142148
case StepChildrenType.branches:
143149
{
144150
const branches = children.items as Branches;
@@ -153,51 +159,52 @@ export class DefinitionWalker {
153159
}
154160
}
155161
break;
156-
157162
default:
158-
throw new Error(`Step children type ${children.type} is not supported`);
163+
throw new Error(`Not supported step children type: ${children.type}`);
159164
}
160165
}
161166
}
162167
return false;
163168
}
164169

165-
private iterate(sequence: Sequence, callback: StepForEachCallback): boolean {
170+
private iterateSequence(sequence: Sequence, callback: StepForEachCallback): boolean {
166171
const count = sequence.length;
167172
for (let index = 0; index < count; index++) {
168173
const step = sequence[index];
169174
if (callback(step, index, sequence) === false) {
170175
return false;
171176
}
177+
if (!this.iterateStep(step, callback)) {
178+
return false;
179+
}
180+
}
181+
return true;
182+
}
172183

173-
const children = this.getChildren(step);
174-
if (children) {
175-
switch (children.type) {
176-
case StepChildrenType.sequence:
177-
{
178-
const childSequence = children.items as Sequence;
179-
if (this.iterate(childSequence, callback) === false) {
180-
return false;
181-
}
184+
private iterateStep(step: Step, callback: StepForEachCallback): boolean {
185+
const children = this.getChildren(step);
186+
if (children) {
187+
switch (children.type) {
188+
case StepChildrenType.sequence:
189+
{
190+
const sequence = children.items as Sequence;
191+
if (!this.iterateSequence(sequence, callback)) {
192+
return false;
182193
}
183-
break;
184-
185-
case StepChildrenType.branches:
186-
{
187-
const branches = children.items as Branches;
188-
const branchNames = Object.keys(branches);
189-
for (const branchName of branchNames) {
190-
const parentSequence = branches[branchName];
191-
if (this.iterate(parentSequence, callback) === false) {
192-
return false;
193-
}
194+
}
195+
break;
196+
case StepChildrenType.branches:
197+
{
198+
const sequences = Object.values(children.items as Branches);
199+
for (const sequence of sequences) {
200+
if (!this.iterateSequence(sequence, callback)) {
201+
return false;
194202
}
195203
}
196-
break;
197-
198-
default:
199-
throw new Error(`Step children type ${children.type} is not supported`);
200-
}
204+
}
205+
break;
206+
default:
207+
throw new Error(`Not supported step children type: ${children.type}`);
201208
}
202209
}
203210
return true;

0 commit comments

Comments
 (0)