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