synchronize
Advanced tools
Comparing version 0.9.11 to 0.9.12
{ | ||
"name" : "synchronize", | ||
"main" : "./sync", | ||
"version" : "0.9.11", | ||
"version" : "0.9.12", | ||
"homepage" : "http://alexeypetrushin.github.com/synchronize", | ||
@@ -6,0 +6,0 @@ "repository": { |
25
sync.js
@@ -94,7 +94,14 @@ /*jshint node: true, indent:2, loopfunc: true, asi: true, undef:true*/ | ||
if(!fiber) throw new Error("no current Fiber, defer can't be used without Fiber!") | ||
if(fiber._defered) throw new Error("invalid usage, should be clear previous defer!") | ||
fiber._defered = true | ||
// Prevent recursive call | ||
var called = false | ||
// Returning asynchronous callback. | ||
return function(err, result){ | ||
if (called) throw new Error("defer can't be used twice!") | ||
called = true | ||
// Wrapping in nextTick as a safe measure against not asynchronous usage. | ||
process.nextTick(function(){ | ||
fiber._defered = false | ||
if(fiber._syncIsTerminated) return | ||
@@ -152,9 +159,17 @@ if(err){ | ||
if(!fiber) throw new Error("no current Fiber, defer can't be used without Fiber!") | ||
if(fiber._defered) throw new Error("invalid usage, should be clear previous defer!") | ||
fiber._defered = true | ||
var kwds = Array.prototype.slice.call(arguments) | ||
// Prevent recursive call | ||
var called = false | ||
// Returning asynchronous callback. | ||
return function(err) { | ||
if (called) throw new Error("defer can't be used twice!") | ||
called = true | ||
// Wrapping in nextTick as a safe measure against not asynchronous usage. | ||
var args = Array.prototype.slice.call(arguments, 1) | ||
process.nextTick(function(){ | ||
fiber._defered = false | ||
if(fiber._syncIsTerminated) return | ||
@@ -242,3 +257,5 @@ if (err) { | ||
var that = this | ||
Fiber(function(){ | ||
var fiber = Fiber(function(){ | ||
// Prevent restart fiber | ||
if (fiber._started) return | ||
if (done) { | ||
@@ -258,3 +275,5 @@ var result | ||
} | ||
}).run() | ||
}) | ||
fiber.run() | ||
fiber._started = true | ||
} | ||
@@ -261,0 +280,0 @@ |
@@ -330,2 +330,81 @@ // require('longjohn') | ||
it('should throw error if defer called twice', function(done){ | ||
sync.fiber(function(){ | ||
var defer = sync.defer() | ||
defer() | ||
sync.await() | ||
expect(defer).to.throw(Error) | ||
}, done) | ||
}) | ||
it('should call defer only once in the fiber process', function(done){ | ||
var broken = function(cb) { | ||
sync.fiber(function() { | ||
cb() | ||
}, cb) | ||
} | ||
sync.fiber(function(){ | ||
broken(sync.defer()) | ||
sync.await() | ||
throw new Error('an error') | ||
}, function(err) { | ||
expect(err).to.exist | ||
expect(err.message).to.eql("defer can't be used twice!") | ||
done() | ||
}) | ||
}) | ||
it('should throw error if defers called twice', function(done){ | ||
sync.fiber(function(){ | ||
var defer = sync.defers() | ||
defer() | ||
sync.await() | ||
expect(defer).to.throw(Error) | ||
}, done) | ||
}) | ||
it('should prevent defers call just once in fiber process', function(done){ | ||
var broken = function(cb) { | ||
sync.fiber(function() { | ||
cb() | ||
}, cb) | ||
} | ||
sync.fiber(function(){ | ||
broken(sync.defers()) | ||
sync.await() | ||
throw new Error('an error') | ||
}, function(err) { | ||
expect(err).to.exist | ||
expect(err.message).to.eql("defer can't be used twice!") | ||
done() | ||
}) | ||
}) | ||
it('should prevent restart fiber', function(done){ | ||
var currentFiber | ||
var called = 0 | ||
sync.fiber(function(){ | ||
called += 1 | ||
currentFiber = sync.Fiber.current | ||
}) | ||
setTimeout(function() { | ||
currentFiber.run() | ||
}, 1) | ||
setTimeout(function() { | ||
expect(called).to.eql(1) | ||
done() | ||
}, 10); | ||
}) | ||
it('should throw error when not matched defer-await pair', function(done){ | ||
sync.fiber(function(){ | ||
process.nextTick(sync.defer()); | ||
expect(function() { process.nextTick(sync.defer()) }).to.throw(Error) | ||
sync.await() | ||
process.nextTick(sync.defers()); | ||
expect(function() { process.nextTick(sync.defers()) }).to.throw(Error) | ||
sync.await() | ||
}, done) | ||
}) | ||
beforeEach(function(){ | ||
@@ -332,0 +411,0 @@ this.someKey = 'some value' |
201196
6164