Comparing version 1.0.1 to 1.1.1
24
index.js
@@ -14,8 +14,30 @@ module.exports = chainit; | ||
var currentDepth = 0; | ||
var flushTimeout; | ||
var flushedTasks = []; | ||
function pushTo(depth, task) { | ||
var queue = queues[depth] || (queues[depth] = getNewQueue(depth)); | ||
queue.push(task); | ||
if (depth > 0) { | ||
return queue.push(task); | ||
} | ||
// hack to handle cases where first chained calls | ||
// are not added synchronously | ||
// it means first chain start will occur after 4ms max | ||
clearTimeout(flushTimeout); | ||
flushedTasks.unshift(function() { | ||
queue.push(task); | ||
}); | ||
flushTimeout = setTimeout(flush, 4); | ||
} | ||
function flush() { | ||
var addTask; | ||
while (addTask = flushedTasks.pop()) { | ||
addTask(); | ||
} | ||
} | ||
function getNewQueue(newDepth) { | ||
@@ -22,0 +44,0 @@ var queue = new Queue({ |
{ | ||
"name": "chainit", | ||
"version": "1.0.1", | ||
"version": "1.1.1", | ||
"description": "Turn an asynchronous JavaScript api into an asynchronous chainable JavaScript api.", | ||
@@ -15,3 +15,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "~1.14.0", | ||
"mocha": "~1.16.2", | ||
"zuul": "~1.0.2" | ||
@@ -18,0 +18,0 @@ }, |
@@ -57,3 +57,3 @@ [![Build Status](https://travis-ci.org/vvo/chainit.png)](https://travis-ci.org/vvo/chainit) | ||
// revert original method | ||
chainit.add(MyChainApi, 'method1', MyApi.prototype.method1); | ||
chainit.add(obj, 'method1', MyApi.prototype.method1); | ||
@@ -60,0 +60,0 @@ // override prototype method |
@@ -5,9 +5,72 @@ describe('chaining an Api', function() { | ||
var chainit = require('../index.js'); | ||
var ChainApi = chainit(Api); | ||
var ChainApi; | ||
var o; | ||
beforeEach(function() { | ||
ChainApi = chainit(Api); | ||
o = new ChainApi(); | ||
}); | ||
// theses tests relates to task push() flushingF | ||
describe('before calls', function() { | ||
var ChainApi = chainit(Api); | ||
var b = new ChainApi(); | ||
before(function() { | ||
b.slowConcat('cou0').slowConcat('cou1'); | ||
}); | ||
it('are supported', function(done) { | ||
b.concat('bouh0').concat('bouh1', function() { | ||
assert.equal(b.s, 'cou0cou1bouh0bouh1') | ||
done(); | ||
}) | ||
}); | ||
}); | ||
describe('mocha without before', function() { | ||
var ChainApi = chainit(Api); | ||
var b = new ChainApi(); | ||
b.slowConcat('cou0').slowConcat('cou1'); | ||
it('supports it', function(done) { | ||
b.concat('bouh0').concat('bouh1', function() { | ||
assert.equal(b.s, 'cou0cou1bouh0bouh1') | ||
done(); | ||
}) | ||
}); | ||
}); | ||
describe('nexticks, timeout', function () { | ||
it('works with after nexttick', function(done) { | ||
o.slowConcat('cou0').slowConcat('cou1'); | ||
process.nextTick(function(){ | ||
o.concat('bouh0', function() { | ||
assert.equal('cou0cou1bouh0', o.s); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('works with double nexttick', function(done) { | ||
o.slowConcat('cou0'); | ||
process.nextTick(function(){ | ||
o.slowConcat('cou1').concat('cou2'); | ||
}); | ||
process.nextTick(function(){ | ||
o | ||
.concat('bouh1') | ||
.slowConcat('bouh2', function() { | ||
assert.equal('cou0cou1cou2bouh1bouh2', o.s); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('has an s prop', function() { | ||
@@ -14,0 +77,0 @@ assert.equal(o.s, ''); |
@@ -8,6 +8,14 @@ module.exports = Api; | ||
Api.prototype.concat = function concat(sub, cb) { | ||
this.s = this.s.concat(sub); | ||
setTimeout(cb, getRandomArbitrary(4, 30)); | ||
this.s = this.s.concat(sub); | ||
setTimeout(cb, getRandomArbitrary(4, 30)); | ||
} | ||
Api.prototype.slowConcat = function concat(sub, cb) { | ||
var api = this; | ||
setTimeout(function() { | ||
api.s = api.s.concat(sub); | ||
cb(); | ||
}, getRandomArbitrary(100, 200)); | ||
} | ||
Api.prototype.getError = function getError(text, cb) { | ||
@@ -14,0 +22,0 @@ setTimeout(function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
20757
523