Comparing version 1.1.2 to 1.2.0
77
index.js
@@ -17,2 +17,5 @@ module.exports = chainit; | ||
/** | ||
* push chained function into queue | ||
*/ | ||
function pushTo(depth, task) { | ||
@@ -22,3 +25,4 @@ var queue = queues[depth] || (queues[depth] = getNewQueue(depth)); | ||
if (depth > 0) { | ||
return queue.push(task); | ||
queue.push(task); | ||
return queue.start(); | ||
} | ||
@@ -33,2 +37,3 @@ | ||
queue.push(task); | ||
queue.start(); | ||
}); | ||
@@ -47,2 +52,6 @@ | ||
/** | ||
* initialize new queue as subqueue to API command | ||
* @param {Integer} newDepth queue depth | ||
*/ | ||
function getNewQueue(newDepth) { | ||
@@ -54,3 +63,3 @@ var queue = new Queue({ | ||
queue.on('drain', function() { | ||
queue.on('end', function() { | ||
if (newDepth > 0) { | ||
@@ -69,3 +78,3 @@ wakeupChain(newDepth); | ||
queues[depth - 1].concurrency = 1; | ||
queues[depth - 1].process(); | ||
queues[depth - 1].start(); | ||
} | ||
@@ -85,3 +94,5 @@ | ||
// static methods, not chained | ||
/** | ||
* register static methods, not chained | ||
*/ | ||
Object.keys(Constructor) | ||
@@ -92,3 +103,5 @@ .forEach(function(name) { | ||
// prototype methods, chained | ||
/** | ||
* register prototype methods, chained | ||
*/ | ||
Object | ||
@@ -113,2 +126,3 @@ .keys(Constructor.prototype) | ||
// if parent queue is running, stop it and run new subquene | ||
if (currentDepth > 0 && queues[currentDepth - 1].concurrency > 0) { | ||
@@ -125,2 +139,3 @@ queues[currentDepth - 1].concurrency = 0; | ||
// if API provides custom async callback, execute it | ||
if (customCb) { | ||
@@ -130,2 +145,3 @@ customCb.apply(ctx, cbArgs); | ||
// call required Queue callback | ||
cb(); | ||
@@ -138,4 +154,7 @@ }); | ||
// put async function into queue | ||
pushTo(currentDepth, task); | ||
// return this to make API chainable | ||
// like api.command1().command2() | ||
return this; | ||
@@ -149,5 +168,29 @@ } | ||
Chain.prototype.__start = function() { | ||
if(!queues.length || !queues[currentDepth-1]) { | ||
return false; | ||
} | ||
queues[currentDepth-1].start(); | ||
} | ||
Chain.prototype.__stop = function() { | ||
if(!queues.length || !queues[currentDepth-1]) { | ||
return false; | ||
} | ||
queues[currentDepth-1].stop(); | ||
} | ||
return Chain; | ||
} | ||
/** | ||
* add custom function into chain | ||
* @param {Object} to Context | ||
* @param {String} fnName function name | ||
* @param {Function} fn function | ||
*/ | ||
chainit.add = function add(to, fnName, fn) { | ||
@@ -161,4 +204,28 @@ if (to.prototype && to.prototype.__addToChain) { | ||
/** | ||
* start chain | ||
* @param {Object} to Context | ||
*/ | ||
chainit.start = function start(to) { | ||
if (to.prototype && to.prototype.__start) { | ||
to.prototype.__start(); | ||
} else { | ||
to.__start(); | ||
} | ||
} | ||
/** | ||
* stop chain | ||
* @param {Object} to Context | ||
*/ | ||
chainit.stop = function stop(to) { | ||
if (to.prototype && to.prototype.__stop) { | ||
to.prototype.__stop(); | ||
} else { | ||
to.__stop(); | ||
} | ||
} | ||
function hasPending(queue) { | ||
return queue.length >= 1; | ||
} |
{ | ||
"name": "chainit", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Turn an asynchronous JavaScript api into an asynchronous chainable JavaScript api.", | ||
@@ -15,7 +15,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "~1.16.2", | ||
"zuul": "~1.0.2" | ||
"mocha": "~1.17.1", | ||
"zuul": "~1.5.4" | ||
}, | ||
"dependencies": { | ||
"queue": "~1.0.2" | ||
"queue": "~2.2.0" | ||
}, | ||
@@ -22,0 +22,0 @@ "keywords": [ |
@@ -1,7 +0,5 @@ | ||
[![Build Status](https://travis-ci.org/vvo/chainit.png)](https://travis-ci.org/vvo/chainit) | ||
# chainit [![Build Status](https://travis-ci.org/vvo/chainit.png)](https://travis-ci.org/vvo/chainit) [![Dependency Status](https://david-dm.org/vvo/chainit.svg?theme=shields.io)](https://david-dm.org/vvo/chainit) [![devDependency Status](https://david-dm.org/vvo/chainit/dev-status.svg?theme=shields.io)](https://david-dm.org/vvo/chainit#info=devDependencies) | ||
[![Selenium Test Status](https://saucelabs.com/browser-matrix/chainitvvo.svg)](https://saucelabs.com/u/chainitvvo) | ||
# chainit | ||
Turn an asynchronous JavaScript api into an asynchronous | ||
@@ -8,0 +6,0 @@ [chainable](http://en.wikipedia.org/wiki/Method_chaining) JavaScript api. |
@@ -368,2 +368,25 @@ describe('chaining an Api', function() { | ||
it('supports starting and stopping a queue', function(done) { | ||
var hasStopped = false; | ||
setTimeout(function() { | ||
assert.equal(hasStopped,true); | ||
chainit.start(o); | ||
hasStopped = false; | ||
},300) | ||
o | ||
.concat('1') | ||
.concat('2') | ||
.call(function() { | ||
chainit.stop(o); | ||
hasStopped = true; | ||
}) | ||
.concat('3',function() { | ||
assert.equal(this.s,'123'); | ||
assert.equal(hasStopped, false); | ||
done(); | ||
}) | ||
}); | ||
}); |
23022
606
124
+ Addedinherits@2.0.4(transitive)
+ Addedqueue@2.2.0(transitive)
- Removedqueue@1.0.2(transitive)
Updatedqueue@~2.2.0