async-lock
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -0,1 +1,5 @@ | ||
0.3.4 / 2015-06-09 | ||
================== | ||
* Bug fix | ||
0.3.3 / 2015-05-19 | ||
@@ -2,0 +6,0 @@ ================== |
@@ -53,15 +53,8 @@ 'use strict'; | ||
var finished = false; | ||
var resolved = false; | ||
var timer = null; | ||
var self = this; | ||
var done = function(isLock, err, ret){ | ||
// Make sure done is only called once | ||
if(finished){ | ||
return; | ||
} | ||
finished = true; | ||
if(isLock){ | ||
clearTimeout(timer); | ||
var done = function(locked, err, ret){ | ||
if(locked){ | ||
if(self.queues[key].length === 0){ | ||
@@ -73,19 +66,21 @@ delete self.queues[key]; | ||
//callback mode | ||
if(!deferred){ | ||
if(typeof(cb) === 'function'){ | ||
cb(err, ret); | ||
} | ||
if(!resolved){ | ||
if(!deferred){ | ||
if(typeof(cb) === 'function'){ | ||
cb(err, ret); | ||
} | ||
} | ||
else{ | ||
//promise mode | ||
if(err){ | ||
deferred.reject(err); | ||
} | ||
else{ | ||
deferred.resolve(ret); | ||
} | ||
} | ||
resolved = true; | ||
} | ||
else{ | ||
//promise mode | ||
if(err){ | ||
deferred.reject(err); | ||
} | ||
else{ | ||
deferred.resolve(ret); | ||
} | ||
} | ||
if(isLock){ | ||
if(locked){ | ||
//run next func | ||
@@ -98,15 +93,25 @@ if(!!self.queues[key]){ | ||
var exec = function(isLock){ | ||
if(isLock){ | ||
self.domains[key] = process.domain; | ||
var exec = function(locked){ | ||
if(resolved){ // may due to timed out | ||
return done(locked); | ||
} | ||
var timeout = opts.timeout ? opts.timeout : self.timeout; | ||
if(timeout){ | ||
timer = setTimeout(done.bind(null, isLock, new Error('async-lock timed out')), timeout); | ||
} | ||
if(timer){ | ||
clearTimeout(timer); | ||
timer = null; | ||
} | ||
if(locked){ | ||
self.domains[key] = process.domain; | ||
} | ||
// Callback mode | ||
if(fn.length === 1){ | ||
fn(done.bind(null, isLock)); | ||
var called = false; | ||
fn(function(err, ret){ | ||
if(!called){ | ||
called = true; | ||
done(locked, err, ret); | ||
} | ||
}); | ||
} | ||
@@ -118,3 +123,3 @@ else{ | ||
}) | ||
.nodeify(done.bind(null, isLock)); | ||
.nodeify(done.bind(null, locked)); | ||
} | ||
@@ -140,2 +145,10 @@ }; | ||
self.queues[key].push(exec.bind(null, true)); | ||
var timeout = opts.timeout || self.timeout; | ||
if(timeout){ | ||
timer = setTimeout(function(){ | ||
timer = null; | ||
done(false, new Error('async-lock timed out')); | ||
}, timeout); | ||
} | ||
} | ||
@@ -186,3 +199,3 @@ | ||
else{ | ||
var deferred = Promise.defer(); | ||
var deferred = this.Promise.defer(); | ||
fnx(function(err, ret){ | ||
@@ -189,0 +202,0 @@ if(err){ |
{ | ||
"name": "async-lock", | ||
"description": "", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"author": "rain1017", | ||
@@ -21,3 +21,2 @@ "private" : false, | ||
"engines": { | ||
"node": "0.10.x" | ||
}, | ||
@@ -24,0 +23,0 @@ "scripts": { |
@@ -93,8 +93,5 @@ # async-lock | ||
lock.acquire(key, fn, cb, function(err, ret){ | ||
// timed out error will be returned here if fn has not return within given time | ||
// timed out error will be returned here if lock not acquired in given time | ||
}); | ||
// Specify timeout for one function | ||
lock.acquire(key, fn, cb, {timeout : 5000}); | ||
// Set max pending tasks | ||
@@ -101,0 +98,0 @@ var lock = new AsyncLock({maxPending : 1000}); |
@@ -117,12 +117,24 @@ 'use strict'; | ||
it('Time out test', function(done){ | ||
var lock = new AsyncLock({timeout : 10}); | ||
var lock = new AsyncLock({timeout : 20}); | ||
var timedout = false; | ||
lock.acquire('key', function(cb){ | ||
setTimeout(function(){ | ||
cb(); //Should not called twice | ||
done(); | ||
}, 20); | ||
}, function(err){ | ||
assert(err); | ||
setTimeout(cb, 50); | ||
}) | ||
.then(function(err){ | ||
assert(timedout); | ||
done(); | ||
}); | ||
lock.acquire('key', function(cb){ | ||
assert('should not execute here'); | ||
cb(); | ||
}) | ||
.catch(function(err){ | ||
// timed out | ||
console.log(err); | ||
if(err){ | ||
timedout = true; | ||
} | ||
}); | ||
}); | ||
@@ -129,0 +141,0 @@ |
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
20882
493
130