locker: refactor.

This commit is contained in:
Christopher Jeffrey 2016-09-30 11:28:39 -07:00
parent 2a43005c6b
commit 15350ead4f
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -33,8 +33,7 @@ function Locker(parent, add) {
this.pendingMap = {}; this.pendingMap = {};
this.add = add; this.add = add;
this._unlock = this.unlock.bind(this); this.unlocker = this.unlock.bind(this);
this._unlocker = this.unlocker.bind(this);
} }
utils.inherits(Locker, EventEmitter); utils.inherits(Locker, EventEmitter);
@ -90,28 +89,18 @@ Locker.prototype.lock = function lock(arg1, arg2) {
} }
if (this.busy) { if (this.busy) {
if (object) {
this.pending.push(object);
this.pendingMap[object.hash('hex')] = true;
}
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if (object) {
self.pending.push(object);
self.pendingMap[object.hash('hex')] = true;
}
self.jobs.push([resolve, object]); self.jobs.push([resolve, object]);
}); });
} }
this.busy = true; this.busy = true;
return new Promise(this._unlock); return Promise.resolve(this.unlocker);
};
/**
* Unlock callback to resolve a promise.
* @param {Function} resolve
* @param {Function} reject
*/
Locker.prototype.unlock = function unlock(resolve, reject) {
resolve(this._unlocker);
}; };
/** /**
@ -119,7 +108,7 @@ Locker.prototype.unlock = function unlock(resolve, reject) {
* @private * @private
*/ */
Locker.prototype.unlocker = function unlocker() { Locker.prototype.unlock = function unlock() {
var item, resolve, object; var item, resolve, object;
this.busy = false; this.busy = false;
@ -141,7 +130,7 @@ Locker.prototype.unlocker = function unlocker() {
this.busy = true; this.busy = true;
resolve(this._unlocker); resolve(this.unlocker);
}; };
/** /**
@ -233,9 +222,7 @@ MappedLock.prototype.lock = function lock(key, force) {
this.busy[key] = true; this.busy[key] = true;
return new Promise(function(resolve, reject) { return Promise.resolve(this.unlock(key));
resolve(self.unlock(key));
});
}; };
/** /**
@ -247,7 +234,7 @@ MappedLock.prototype.lock = function lock(key, force) {
MappedLock.prototype.unlock = function unlock(key) { MappedLock.prototype.unlock = function unlock(key) {
var self = this; var self = this;
return function unlock() { return function unlocker() {
var jobs = self.jobs[key]; var jobs = self.jobs[key];
var resolve; var resolve;
@ -264,7 +251,7 @@ MappedLock.prototype.unlock = function unlock(key) {
self.busy[key] = true; self.busy[key] = true;
resolve(unlock); resolve(unlocker);
}; };
}; };