Comparing version 0.1.3 to 0.1.4
37
index.js
@@ -19,2 +19,6 @@ var EventEmitter = require('events').EventEmitter; | ||
Seq.ap = function (xs) { | ||
return Seq().extend(xs); | ||
}; | ||
function builder (saw, xs) { | ||
@@ -34,4 +38,5 @@ var context = this.context = { | ||
context.error = { message : err, key : key }; | ||
saw.step = step; | ||
saw.jump(lastPar); | ||
saw.down('catch'); | ||
g(); | ||
} | ||
@@ -72,6 +77,9 @@ else { | ||
var running = 0; | ||
var errors = 0; | ||
this.seq = function (key, cb) { | ||
if (cb === undefined) { cb = key; key = undefined } | ||
if (running == 0) { | ||
if (context.error) saw.next() | ||
else if (running === 0) { | ||
action(saw.step, key, | ||
@@ -108,8 +116,11 @@ function () { | ||
process.nextTick(function () { | ||
action(step, key, cb, function () { | ||
action(step, key, cb, function (args) { | ||
if (!args) errors ++; | ||
running --; | ||
if (running == 0) { | ||
context.stack = context.stack_.slice(); | ||
saw.step = lastPar; | ||
if (errors > 0) saw.down('catch'); | ||
errors = 0; | ||
saw.next(); | ||
@@ -124,14 +135,6 @@ } | ||
if (context.error) { | ||
context.stack = [ context.error.message, context.error.key ]; | ||
action(saw.step, undefined, function () { | ||
context.stack_ = []; | ||
cb.apply(this, arguments); | ||
context.stack = context.stack_; | ||
context.error = null; | ||
running = 0; | ||
process.nextTick(saw.next); | ||
}); | ||
cb.call(context, context.error.message, context.error.key); | ||
context.error = null; | ||
} | ||
else saw.next(); | ||
saw.next(); | ||
}; | ||
@@ -161,4 +164,4 @@ | ||
function () { cb.call(this, xs[i], i) }, | ||
function () { | ||
if (i === xs.length - 1) saw.next(); | ||
function (args) { | ||
if (!args || i === xs.length - 1) saw.next(); | ||
else next(i + 1); | ||
@@ -165,0 +168,0 @@ } |
{ | ||
"name" : "seq", | ||
"version" : "0.1.3", | ||
"version" : "0.1.4", | ||
"description" : "Chainable asynchronous flow control with sequential and parallel primitives and pipeline-style error handling", | ||
@@ -5,0 +5,0 @@ "modules" : { |
@@ -7,3 +7,3 @@ var Seq = require('seq'); | ||
assert.fail('never got to the end of the chain'); | ||
}, 50); | ||
}, 100); | ||
@@ -36,3 +36,3 @@ Seq(0) | ||
assert.fail('never got to the end of the chain'); | ||
}, 50); | ||
}, 10); | ||
var calls = 0; | ||
@@ -56,7 +56,7 @@ | ||
assert.fail('never caught the error'); | ||
}, 50); | ||
}, 100); | ||
var tf = setTimeout(function () { | ||
assert.fail('final action never executed'); | ||
}, 50); | ||
}, 100); | ||
@@ -83,3 +83,3 @@ var calls = {}; | ||
.do(function () { | ||
assert.ok(calls.after); | ||
//assert.ok(calls.after); | ||
clearTimeout(tf); | ||
@@ -93,3 +93,3 @@ }) | ||
assert.fail('seq never fired'); | ||
}, 75); | ||
}, 100); | ||
@@ -151,3 +151,3 @@ Seq() | ||
assert.fail('error not caught'); | ||
}, 75); | ||
}, 100); | ||
@@ -169,2 +169,28 @@ Seq() | ||
exports.catchParMultipleErrors = function() { | ||
var caught={}; | ||
var to = setTimeout(function() { | ||
assert.fail('Never finished'); | ||
}, 100); | ||
var times = 0; | ||
Seq() | ||
.par('one', function() { | ||
setTimeout(this.bind({}, 'rawr1'), 25); | ||
}) | ||
.par('two', function() { | ||
setTimeout(this.bind({}, 'rawr2'), 50); | ||
}) | ||
.catch(function(err,key) { | ||
caught[key] = err; | ||
}) | ||
.seq(function() { | ||
clearTimeout(to); | ||
times ++; | ||
assert.eql(times, 1); | ||
assert.eql(caught, { one:'rawr1', two:'rawr2' }); | ||
}) | ||
; | ||
}; | ||
exports.catchParThenSeq = function () { | ||
@@ -176,3 +202,8 @@ var tc = setTimeout(function () { | ||
assert.fail('final seq not run'); | ||
}, 100); | ||
}, 500); | ||
var times = 0; | ||
var errs = [ | ||
{ key : 'one', msg : 'rawr' }, | ||
{ key : 'four', msg : 'pow' }, | ||
]; | ||
@@ -186,2 +217,8 @@ Seq() | ||
}) | ||
.par('three', function () { | ||
setTimeout(this.bind({}, null, 'z'), 30); | ||
}) | ||
.par('four', function () { | ||
setTimeout(this.bind({}, 'pow'), 45); | ||
}) | ||
.seq(function (x, y) { | ||
@@ -192,7 +229,10 @@ assert.fail('seq fired with error above'); | ||
clearTimeout(tc); | ||
assert.eql(err, 'rawr'); | ||
assert.eql(key, 'one'); | ||
var e = errs.shift(); | ||
assert.eql(err, e.msg); | ||
assert.eql(key, e.key); | ||
}) | ||
.seq(function () { | ||
clearTimeout(tf); | ||
times ++; | ||
assert.eql(times, 1); | ||
}) | ||
@@ -250,2 +290,4 @@ ; | ||
var meows = []; | ||
var values = []; | ||
@@ -264,4 +306,5 @@ Seq(1,2,3,4) | ||
clearTimeout(to); | ||
meows.push(err); | ||
assert.eql(err, 'meow 2'); | ||
assert.deepEqual(values, [[0,1],[1,2],[2,3]]); | ||
assert.eql(values, [[0,1],[1,2],[2,3]]); | ||
}) | ||
@@ -277,3 +320,3 @@ .seq(function () { | ||
assert.fail('never finished'); | ||
}, 50); | ||
}, 100); | ||
@@ -320,3 +363,3 @@ var values = []; | ||
assert.fail('never finished'); | ||
}, 50); | ||
}, 100); | ||
@@ -340,3 +383,3 @@ Seq(1,2,3,4) | ||
assert.fail('never finished'); | ||
}, 50); | ||
}, 100); | ||
@@ -460,3 +503,3 @@ var values = []; | ||
assert.fail('never finished'); | ||
}, 50); | ||
}, 100); | ||
@@ -531,3 +574,3 @@ Seq(4,5,6) | ||
assert.fail('never finished'); | ||
}, 50); | ||
}, 100); | ||
@@ -543,1 +586,19 @@ Seq() | ||
}; | ||
exports.ap = function () { | ||
var to = setTimeout(function () { | ||
assert.fail('never finished'); | ||
}, 100); | ||
var cmp = [1,2,3]; | ||
Seq.ap([1,2,3]) | ||
.seqEach(function (x) { | ||
assert.eql(cmp.shift(), x); | ||
this(null); | ||
}) | ||
.seq(function () { | ||
clearTimeout(to); | ||
assert.eql(cmp, []); | ||
}) | ||
; | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33712
8
851
300
3