Comparing version 3.0.4 to 3.1.0
31
index.js
@@ -18,2 +18,3 @@ module.exports = chainit; | ||
queue.push(task); | ||
queue.lastState = task.state; | ||
setImmediate(function() { | ||
@@ -71,6 +72,6 @@ if (!queue.running) queue.start(); | ||
.forEach(function(fnName) { | ||
Chain.prototype[fnName] = makeChain(fnName, Constructor.prototype[fnName]); | ||
Chain.prototype[fnName] = makeChain(Constructor.prototype[fnName]); | ||
}); | ||
function makeChain(fnName, fn) { | ||
function makeChain(fnq, fnn) { | ||
@@ -82,3 +83,3 @@ return function chained() { | ||
if (fn.length <= args.length && typeof args[args.length - 1] === 'function') { | ||
if (fnq.length <= args.length && typeof args[args.length - 1] === 'function') { | ||
customCb = args.pop(); | ||
@@ -111,3 +112,3 @@ } | ||
try { | ||
fn.apply(ctx, args); | ||
fnq.apply(ctx, args); | ||
} catch(e) { | ||
@@ -132,2 +133,14 @@ current.error = e; | ||
}); }; | ||
var queue = queues[ldepth]; | ||
var prevTask = queue && queue.slice(-1).pop(); | ||
var state = prevTask && prevTask.state || queue && queue.lastState || previous && previous.lastState; | ||
if (fnn) { | ||
ctx.chainState = state; | ||
fnn.apply(ctx, args); | ||
state = ctx.chainState; // fnn can create a new state | ||
} | ||
task.state = state; | ||
pushTo(ldepth, task); | ||
@@ -139,4 +152,4 @@ | ||
Chain.prototype.__addToChain = function(fnName, fn) { | ||
this[fnName] = makeChain(fnName, fn); | ||
Chain.prototype.__addToChain = function(fnName, fnq, fnn) { | ||
this[fnName] = makeChain(fnq, fnn); | ||
} | ||
@@ -147,7 +160,7 @@ | ||
chainit.add = function add(to, fnName, fn) { | ||
chainit.add = function add(to, fnName, fnq, fnn) { | ||
if (to.prototype && to.prototype.__addToChain) { | ||
to.prototype.__addToChain(fnName, fn); | ||
to.prototype.__addToChain(fnName, fnq, fnn); | ||
} else { | ||
to.__addToChain(fnName, fn); | ||
to.__addToChain(fnName, fnq, fnn); | ||
} | ||
@@ -154,0 +167,0 @@ } |
{ | ||
"name": "chainit3", | ||
"version": "3.0.4", | ||
"description": "Turn an asynchronous JavaScript api into an asynchronous chainable JavaScript api.", | ||
"version": "3.1.0", | ||
"description": "Turn an asynchronous js api into an asynchronous chainable js api with a state machine", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "directories": { |
@@ -449,2 +449,37 @@ describe('chaining an Api', function() { | ||
it("propagates a chainState object across queued and immediate methods", function(done) { | ||
chainit.add(o, 'step1', function(sub, cb) { | ||
this.s = 'step1-' + sub; | ||
setImmediate(cb); | ||
}, function(sub) { | ||
assert.equal(typeof sub, "string"); | ||
this.chainState = { one: true }; | ||
}); | ||
chainit.add(o, 'step2', function(sub, cb) { | ||
this.s += '-step2-' + sub; | ||
setImmediate(cb); | ||
}, function(sub) { | ||
assert.equal(typeof sub, "string"); | ||
if (!this.chainState || !this.chainState.one) throw new Error("step2 must be called after step1"); | ||
this.chainState.one = false; | ||
}); | ||
// this works | ||
o.step1("test1").step2("test2", function(err) { | ||
assert.equal(this.s, "step1-test1-step2-test2"); | ||
var err; | ||
try { | ||
o.step2('fail', function(err) { | ||
assert.equal(true, false); | ||
}); | ||
} catch(e) { | ||
err = e; | ||
} | ||
assert.equal(!!err, true); | ||
o.step1("test3").step2("test4", function(err) { | ||
assert.equal(!!err, false); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('inherited APIS', function() { | ||
@@ -576,2 +611,2 @@ var ChainedInheritedApi; | ||
}); | ||
}); |
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
29946
765
203