Skip to content

Commit

Permalink
Modified the 'Workflow' class to stop some executions when possible i…
Browse files Browse the repository at this point in the history
…f the workflow ended
  • Loading branch information
AdrienCastex committed Jun 28, 2017
1 parent 1cc4a03 commit e0e4232
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
33 changes: 23 additions & 10 deletions lib/helper/Workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,36 @@ var Workflow = (function () {
this.started = false;
}
Workflow.prototype._done = function (subject, e, data) {
var _this = this;
if (this.counter <= 0)
return;
if (e) {
if (this.exitOnError)
this.counter = -1000;
if (this.errorFn)
this.errorFn(e);
if (this.exitOnError)
process.nextTick(function () { return _this.errorFn(e); });
if (this.exitOnError) {
this.started = false;
return;
}
}
if (this.intermediateFn)
this.intermediateFn(subject, e, data);
--this.counter;
this.data.push(data);
if (this.counter === 0 && this.doneFn)
this.doneFn(this.data);
if (this.counter === 0 && this.doneFn) {
this.started = false;
process.nextTick(function () { return _this.doneFn(_this.data); });
}
if (data && this.firstFn) {
this.counter = -1;
this.firstFn(data);
this.started = false;
process.nextTick(function () { return _this.firstFn(data); });
}
if (this.counter === 0 && this.notFound) {
this.started = false;
process.nextTick(function () { return _this.notFound(); });
}
if (this.counter === 0 && this.notFound)
this.notFound();
};
Workflow.prototype.each = function (subjects, fn) {
var _this = this;
Expand All @@ -55,10 +63,15 @@ var Workflow = (function () {
var _this = this;
this.counter = Object.keys(object).length;
process.nextTick(function () {
_this.started = true;
var _loop_1 = function (name_1) {
fn(name_1, object[name_1], function (e, d) {
return _this._done((_a = {}, _a[name_1] = object[name_1], _a), e, d);
var _a;
process.nextTick(function () {
if (!_this.started)
return;
fn(name_1, object[name_1], function (e, d) {
return _this._done((_a = {}, _a[name_1] = object[name_1], _a), e, d);
var _a;
});
});
};
for (var name_1 in object) {
Expand Down
25 changes: 20 additions & 5 deletions src/helper/Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ export class Workflow
if(this.exitOnError)
this.counter = -1000;
if(this.errorFn)
this.errorFn(e);
process.nextTick(() => this.errorFn(e));
if(this.exitOnError)
{
this.started = false;
return;
}
}

if(this.intermediateFn)
Expand All @@ -49,16 +52,23 @@ export class Workflow
this.data.push(data);

if(this.counter === 0 && this.doneFn)
this.doneFn(this.data);
{
this.started = false;
process.nextTick(() => this.doneFn(this.data));
}

if(data && this.firstFn)
{
this.counter = -1;
this.firstFn(data);
this.started = false;
process.nextTick(() => this.firstFn(data));
}

if(this.counter === 0 && this.notFound)
this.notFound();
{
this.started = false;
process.nextTick(() => this.notFound());
}
}

each<T>(subjects : T[], fn : (subject : T, done : (error ?: any, data ?: any) => void) => void)
Expand All @@ -73,8 +83,13 @@ export class Workflow
this.counter = Object.keys(object).length;
process.nextTick(() =>
{
this.started = true;
for(const name in object)
fn(name, object[name], (e, d) => this._done({ [name]: object[name] }, e, d));
process.nextTick(() => {
if(!this.started)
return;
fn(name, object[name], (e, d) => this._done({ [name]: object[name] }, e, d));
});
})
return this;
}
Expand Down

0 comments on commit e0e4232

Please sign in to comment.