-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a 'Workflow' class to replace similar codes in the project
- Loading branch information
1 parent
0bc3801
commit eb22a3d
Showing
11 changed files
with
277 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export declare class Workflow { | ||
counter: number; | ||
data: any[]; | ||
errorFn: (error?: any) => void; | ||
doneFn: (data?: any[]) => void; | ||
intermediateFn: (subject: any, e?: any, data?: any) => void; | ||
exitOnError: boolean; | ||
constructor(exitOnError?: boolean); | ||
protected _done(subject: any, e?: any, data?: any): void; | ||
each<T>(subjects: T[], fn: (subject: T, done: (error?: any, data?: any) => void) => void): this; | ||
eachProperties(object: any, fn: (name: string, value: any, done: (error?: any, data?: any) => void) => void): this; | ||
intermediate(fn: (subject: any, e?: any, data?: any) => void): this; | ||
error(fn: (error?: any) => void): this; | ||
done(fn: (data?: any[]) => void): this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Workflow = (function () { | ||
function Workflow(exitOnError) { | ||
if (exitOnError === void 0) { exitOnError = true; } | ||
this.exitOnError = !!exitOnError; | ||
this.intermediateFn = null; | ||
this.counter = 0; | ||
this.data = []; | ||
this.errorFn = null; | ||
this.doneFn = null; | ||
} | ||
Workflow.prototype._done = function (subject, e, data) { | ||
if (this.counter <= 0) | ||
return; | ||
if (e) { | ||
if (this.exitOnError) | ||
this.counter = -1000; | ||
if (this.errorFn) | ||
this.errorFn(e); | ||
if (this.exitOnError) | ||
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); | ||
}; | ||
Workflow.prototype.each = function (subjects, fn) { | ||
var _this = this; | ||
this.counter = subjects.length; | ||
subjects.forEach(function (s) { return process.nextTick(function () { return fn(s, function (e, d) { return _this._done(s, e, d); }); }); }); | ||
return this; | ||
}; | ||
Workflow.prototype.eachProperties = function (object, fn) { | ||
var _this = this; | ||
this.counter = Object.keys(object).length; | ||
process.nextTick(function () { | ||
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; | ||
}); | ||
}; | ||
for (var name_1 in object) { | ||
_loop_1(name_1); | ||
} | ||
}); | ||
return this; | ||
}; | ||
Workflow.prototype.intermediate = function (fn) { | ||
this.intermediateFn = fn; | ||
return this; | ||
}; | ||
Workflow.prototype.error = function (fn) { | ||
this.errorFn = fn; | ||
return this; | ||
}; | ||
Workflow.prototype.done = function (fn) { | ||
var _this = this; | ||
this.doneFn = fn; | ||
if (this.counter === 0) | ||
process.nextTick(function () { return _this.doneFn(_this.data); }); | ||
return this; | ||
}; | ||
return Workflow; | ||
}()); | ||
exports.Workflow = Workflow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { IResource, ReturnCallback, ETag } from '../resource/IResource' | ||
import { MethodCallArgs } from '../server/MethodCallArgs' | ||
import { Errors } from '../Errors' | ||
import * as url from 'url' | ||
|
||
export class Workflow | ||
{ | ||
counter : number; | ||
data : any[]; | ||
errorFn : (error ?: any) => void; | ||
doneFn : (data ?: any[]) => void; | ||
intermediateFn : (subject : any, e ?: any, data ?: any) => void; | ||
|
||
exitOnError : boolean; | ||
|
||
constructor(exitOnError : boolean = true) | ||
{ | ||
this.exitOnError = !!exitOnError; | ||
this.intermediateFn = null; | ||
this.counter = 0; | ||
this.data = []; | ||
this.errorFn = null; | ||
this.doneFn = null; | ||
} | ||
|
||
protected _done(subject : any, e ?: any, data ?: any) | ||
{ | ||
if(this.counter <= 0) | ||
return; | ||
|
||
if(e) | ||
{ | ||
if(this.exitOnError) | ||
this.counter = -1000; | ||
if(this.errorFn) | ||
this.errorFn(e); | ||
if(this.exitOnError) | ||
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); | ||
} | ||
|
||
each<T>(subjects : T[], fn : (subject : T, done : (error ?: any, data ?: any) => void) => void) | ||
{ | ||
this.counter = subjects.length; | ||
subjects.forEach((s) => process.nextTick(() => fn(s, (e, d) => this._done(s, e, d)))); | ||
return this; | ||
} | ||
|
||
eachProperties(object, fn : (name : string, value : any, done : (error ?: any, data ?: any) => void) => void) | ||
{ | ||
this.counter = Object.keys(object).length; | ||
process.nextTick(() => | ||
{ | ||
for(const name in object) | ||
fn(name, object[name], (e, d) => this._done({ [name]: object[name] }, e, d)); | ||
}) | ||
return this; | ||
} | ||
|
||
intermediate(fn : (subject : any, e ?: any, data ?: any) => void) | ||
{ | ||
this.intermediateFn = fn; | ||
return this; | ||
} | ||
|
||
error(fn : (error ?: any) => void) | ||
{ | ||
this.errorFn = fn; | ||
return this; | ||
} | ||
|
||
done(fn : (data ?: any[]) => void) | ||
{ | ||
this.doneFn = fn; | ||
|
||
if(this.counter === 0) | ||
process.nextTick(() => this.doneFn(this.data)); | ||
|
||
return this; | ||
} | ||
} |
Oops, something went wrong.