twostep
Advanced tools
Comparing version 0.3.1 to 0.3.2
module.exports = TwoStep; | ||
module.exports.Steppy = Steppy; | ||
var slice = Array.prototype.slice; | ||
function Group(callback) { | ||
/** | ||
* Group represents one step in Step's execution flow. Makes a decision when | ||
* the execution should jump to the next step. | ||
* | ||
* @param {function(err, args)} callback | ||
* Callback to be called on successful execution end | ||
* @param {function(err, args)} [errorCallback] | ||
* If defined, will be called in case of error (default value: callback arg) | ||
*/ | ||
function Group(callback, errorCallback) { | ||
this.args = [null]; | ||
this.left = 0; | ||
this.callback = callback; | ||
this.errorCallback = errorCallback || callback; | ||
this.isDone = false; | ||
@@ -28,4 +40,3 @@ } | ||
this.isDone = true; | ||
var callback = this.callback; | ||
callback(err); | ||
this.errorCallback(err); | ||
}; | ||
@@ -78,5 +89,21 @@ | ||
// Expose just for fun and extensibility | ||
TwoStep.Group = Group; | ||
/** | ||
* TwoStep section | ||
*/ | ||
// Execute steps immediately | ||
function TwoStep() { | ||
exec(slice.call(arguments), []); | ||
} | ||
// Create a composite function with steps built-in | ||
TwoStep.fn = function () { | ||
var steps = slice.call(arguments); | ||
return function () { | ||
var args = slice.call(arguments); | ||
var callback = args.pop(); | ||
exec(steps, args, callback); | ||
}; | ||
}; | ||
// Stepper function | ||
@@ -105,17 +132,5 @@ function exec(steps, args, callback) { | ||
// Execute steps immedietly | ||
function TwoStep() { | ||
exec(slice.call(arguments), []); | ||
} | ||
// Expose just for fun and extensibility | ||
TwoStep.Group = Group; | ||
// Create a composite function with steps built-in | ||
TwoStep.fn = function () { | ||
var steps = slice.call(arguments); | ||
return function () { | ||
var args = slice.call(arguments); | ||
var callback = args.pop(); | ||
exec(steps, args, callback); | ||
}; | ||
}; | ||
//Simple step function, just return callack | ||
@@ -135,1 +150,42 @@ TwoStep.simple = function(callback) { | ||
}; | ||
/** | ||
* Steppy section | ||
*/ | ||
// Execute steps immediately | ||
function Steppy() { | ||
execSteppy(slice.call(arguments), []); | ||
} | ||
// Stepper function | ||
function execSteppy(steps, args, callback) { | ||
if (steps.length < 2) { | ||
throw new Error('Actually, you don\'t want to run less than two steps'); | ||
} | ||
var pos = 0; | ||
var errorHandler = steps[steps.length - 1]; | ||
function next() { | ||
var step = steps[pos++]; | ||
if (!step) { | ||
callback && callback.apply(null, arguments); | ||
return; | ||
} | ||
var group = new Group(next, errorHandler); | ||
try { | ||
step.apply(group, arguments); | ||
} catch (e) { | ||
group.error(e); | ||
return; | ||
} | ||
if (group.left === 0) { | ||
group.done(); | ||
} | ||
}; | ||
next.apply(null, args); | ||
} | ||
// Expose just for fun and extensibility | ||
Steppy.Group = Group; |
{ | ||
"name": "twostep", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "A simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless.", | ||
@@ -5,0 +5,0 @@ "engine": [ "node >=0.2.0" ], |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
18301
14
534
0
8