Skip to content

Commit

Permalink
clear empty locks
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanLi committed May 19, 2018
1 parent d0cd4aa commit 05471e4
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class PromiseCBS {
this.resolve = resolve;
this.reject = reject;
}
}
};

class JSLock {
/**
Expand Down Expand Up @@ -55,14 +55,31 @@ class JSLock {
this.index = Math.max(this.index - 1, 0);

if (this.lockPromiseCBS.length == 0) {
clearEmptyLock(this);
return;
}

const cbs = this.lockPromiseCBS.shift();
cbs.resolve();
}
}
};

/** @type {Map<string, JSLock>} */
JSLock._cacheLocks = new Map();

/**
* clear empty lock.
* @param {JSLock} lock
*/
function clearEmptyLock(lock) {
if (lock && lock.ley && lock.lockPromiseCBS && lock.lockPromiseCBS.length == 0) {

const map = JSLock._cacheLocks;

if (map.get(lock.key) === lock) {
map.delete(lock.key);
}
}
}

module.exports = JSLock;

0 comments on commit 05471e4

Please sign in to comment.