locker: refactor.
This commit is contained in:
parent
a2d6ed56e7
commit
5a353d1592
@ -73,10 +73,10 @@ Locker.prototype.hasPending = function hasPending(key) {
|
|||||||
|
|
||||||
Locker.prototype.lock = function lock(arg1, arg2) {
|
Locker.prototype.lock = function lock(arg1, arg2) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var force, object;
|
var force, item;
|
||||||
|
|
||||||
if (this.add) {
|
if (this.add) {
|
||||||
object = arg1;
|
item = arg1;
|
||||||
force = arg2;
|
force = arg2;
|
||||||
} else {
|
} else {
|
||||||
force = arg1;
|
force = arg1;
|
||||||
@ -91,13 +91,13 @@ Locker.prototype.lock = function lock(arg1, arg2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.busy) {
|
if (this.busy) {
|
||||||
if (object) {
|
if (item) {
|
||||||
this.pending.push(object);
|
this.pending.push(item);
|
||||||
this.pendingMap[object.hash('hex')] = true;
|
this.pendingMap[item.hash('hex')] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
self.jobs.push(new Job(resolve, reject, object));
|
self.jobs.push(new Job(resolve, reject, item));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,13 +125,13 @@ Locker.prototype.unlock = function unlock() {
|
|||||||
job = this.jobs.shift();
|
job = this.jobs.shift();
|
||||||
|
|
||||||
if (this.destroyed) {
|
if (this.destroyed) {
|
||||||
job.reject(new Error('Locker is destroyed.'));
|
job.reject(new Error('Locker was destroyed.'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (job.object) {
|
if (job.item) {
|
||||||
assert(job.object === this.pending.shift());
|
assert(job.item === this.pending.shift());
|
||||||
delete this.pendingMap[job.object.hash('hex')];
|
delete this.pendingMap[job.item.hash('hex')];
|
||||||
}
|
}
|
||||||
|
|
||||||
this.busy = true;
|
this.busy = true;
|
||||||
@ -258,7 +258,7 @@ MappedLock.prototype.unlock = function unlock(key) {
|
|||||||
delete self.jobs[key];
|
delete self.jobs[key];
|
||||||
|
|
||||||
if (self.destroyed) {
|
if (self.destroyed) {
|
||||||
job.reject(new Error('Locker is destroyed.'));
|
job.reject(new Error('Locker was destroyed.'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,13 +282,13 @@ MappedLock.prototype.destroy = function destroy() {
|
|||||||
* @constructor
|
* @constructor
|
||||||
* @param {Function} resolve
|
* @param {Function} resolve
|
||||||
* @param {Function} reject
|
* @param {Function} reject
|
||||||
* @param {Object?} object
|
* @param {Object?} item
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Job(resolve, reject, object) {
|
function Job(resolve, reject, item) {
|
||||||
this.resolve = resolve;
|
this.resolve = resolve;
|
||||||
this.reject = reject;
|
this.reject = reject;
|
||||||
this.object = object || null;
|
this.item = item || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user