Comparing version 0.1.2 to 0.2.0
@@ -15,17 +15,21 @@ //============================== | ||
function firstIsSync (rqst, accum, next) { | ||
function firstIsSync (err, rqst, accum, next) { | ||
console.log('firstIsSync:', 'rqst:', rqst); | ||
accum.push("firstIsSync"); | ||
next(); | ||
next(null, rqst, accum); | ||
} | ||
function secondIsAsync (rqst, accum, next) { | ||
function secondIsAsync (err, rqst, accum, next) { | ||
console.log('secondIsAsync:', 'rqst:', rqst); | ||
accum.push("secondIsAsync"); | ||
setTimeout(next, 1000); | ||
function afterTimeout () { | ||
accum.push("secondIsAsync"); | ||
next(null, rqst, accum); | ||
} | ||
setTimeout(afterTimeout, 1000); | ||
} | ||
function thirdIsFinal (rqst, accum, next) { | ||
function thirdIsFinal (err, rqst, accum, next) { | ||
console.log('thirdIsFinal:', 'accum:', accum); | ||
@@ -36,8 +40,11 @@ | ||
function ifError (err, rqst, accum) { | ||
console.log('had an error:', err, 'rqst:', rqst, 'accum:', accum); | ||
} | ||
var steps = [firstIsSync, secondIsAsync, thirdIsFinal]; | ||
var seq = new Sequencer(steps); | ||
var seq = new Sequencer(steps, ifError); | ||
var rqst = {'i am': 'tbd'}; | ||
var rqst = {'i am': 'the request'}; | ||
@@ -47,3 +54,3 @@ var accum = []; | ||
seq.run(rqst, accum); | ||
seq.next(null, rqst, accum); // call 'next()' to start; first arg is err (which is null) | ||
@@ -15,10 +15,10 @@ //============================== | ||
function firstIsSync (rqst, accum, next) { | ||
function firstIsSync (err, rqst, accum, next) { | ||
console.log('firstIsSync:', 'rqst:', rqst); | ||
accum.push("firstIsSync"); | ||
next(); | ||
next(null, rqst, accum); | ||
} | ||
function secondHasAsyncError (rqst, accum, next) { | ||
function secondHasAsyncError (err, rqst, accum, next) { | ||
console.log('secondIsAsync:', 'rqst:', rqst); | ||
@@ -28,3 +28,3 @@ accum.push("secondIsAsync"); | ||
function afterTimeout () { | ||
next(new Error('Boom!')); | ||
next(new Error('Boom!'), rqst, accum); | ||
} | ||
@@ -35,3 +35,3 @@ | ||
function thirdIsFinal (rqst, accum, next) { | ||
function thirdIsFinal (err, rqst, accum, next) { | ||
console.log('thirdIsFinal:', 'accum:', accum); | ||
@@ -42,5 +42,4 @@ | ||
function ifError (rqst, accum, err) { | ||
console.log('had an error:', err); | ||
console.log('rqst:', rqst, 'accum:', accum); | ||
function ifError (err, rqst, accum) { | ||
console.log('had an error:', err, 'rqst:', rqst, 'accum:', accum); | ||
} | ||
@@ -52,3 +51,3 @@ | ||
var rqst = {'i am': 'tbd'}; | ||
var rqst = {'i am': 'the arguments'}; | ||
@@ -58,3 +57,3 @@ var accum = []; | ||
seq.run(rqst, accum); | ||
seq.next(null, rqst, accum); | ||
@@ -33,2 +33,5 @@ //============================== | ||
// One of the options can be an object, which if supplied will be set as the 'this' context on all function invocations. | ||
// One of the options can be a function, which if supplied will be called if/when an invoked function calls 'next(err)'' | ||
if (typeof opt1 === 'function') { | ||
@@ -46,44 +49,43 @@ ifError = opt1; | ||
this.context = context || null; | ||
this.ifError = ifError; | ||
this.numSteps = steps.length; | ||
this.steps = steps; | ||
this.stepIndex = 0; | ||
this.passArgs = []; | ||
this.context = context || null; | ||
this.ifError = ifError; | ||
this.nextFunction = this.next.bind(this); | ||
this.numSteps = steps.length; | ||
this.steps = steps; | ||
this.stepIndex = 0; | ||
this.passArgs = []; | ||
} | ||
//=========================== | ||
// Public Methods | ||
// Private Methods | ||
//=========================== | ||
Sequencer.prototype.run = function () { | ||
var i; | ||
var numArgs = arguments.length; | ||
var passArgs = this.passArgs; | ||
for (i = 0; i < numArgs; i +=1) { | ||
passArgs.push(arguments[i]); | ||
} | ||
passArgs.push(this.next.bind(this)); | ||
//=========================== | ||
// Public Methods | ||
//=========================== | ||
this.next(); | ||
}; | ||
Sequencer.prototype.next = function (nextArg) { | ||
Sequencer.prototype.next = function () { | ||
var applyArgs; | ||
var err; | ||
var i; | ||
var numArgs = arguments.length; | ||
var steps = this.steps; | ||
var theArgs = new Array(numArgs); | ||
var thisStep; | ||
if (nextArg instanceof Error) { | ||
if (this.ifError) { | ||
applyArgs = this.passArgs; | ||
applyArgs.pop(); | ||
applyArgs.push(nextArg); | ||
this.ifError.apply(this.context, applyArgs); | ||
} | ||
return; | ||
// capture all arguments | ||
for (i = 0; i < numArgs; i +=1) { | ||
theArgs[i] = arguments[i]; | ||
} | ||
// if an error was supplied and the Sequence has an error handler, invoke it | ||
if (theArgs[0] && this.ifError) { | ||
this.ifError.apply(this.context, theArgs); | ||
return; // DON'T CONTINUE | ||
} | ||
if (this.stepIndex >= this.numSteps) { | ||
@@ -94,6 +96,8 @@ console.log("Sequencer: EGADS!!!", "this.stepIndex >= this.numSteps"); | ||
// invoke next step | ||
thisStep = this.steps[this.stepIndex]; | ||
this.stepIndex += 1; | ||
thisStep.apply(this.context, this.passArgs); | ||
theArgs.push(this.nextFunction); | ||
thisStep.apply(this.context, theArgs); | ||
}; | ||
{ | ||
"name": "ordination", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"title": "Ordination", | ||
@@ -5,0 +5,0 @@ "description": "Organize and regulate the flow of execution.", |
8083
201