You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
538 B

10 years ago
var _ = require('lodash');
var locks = {};
var Lock = function () {
this.taken = false;
this.queue = [];
10 years ago
};
Lock.prototype.free = function () {
if (this.queue.length > 0) {
var f = this.queue.shift();
f(this);
} else {
this.taken = false;
}
10 years ago
};
Lock.get = function (key, callback) {
if (_.isUndefined(locks[key])) {
locks[key] = new Lock();
}
var lock = locks[key];
10 years ago
if (lock.taken) {
lock.queue.push(callback);
} else {
lock.taken = true;
callback(lock);
}
10 years ago
};
module.exports = Lock;