()
| 20 | } |
| 21 | |
| 22 | private unlock() { |
| 23 | this.permits += 1; |
| 24 | if (this.permits > 1 && this.promiseResolverQueue.length > 0) { |
| 25 | throw new Error('this.permits should never be > 0 when there is someone waiting.'); |
| 26 | } else if (this.permits === 1 && this.promiseResolverQueue.length > 0) { |
| 27 | this.permits -= 1; |
| 28 | const nextResolver = this.promiseResolverQueue.shift(); |
| 29 | if (nextResolver) { |
| 30 | setTimeout(() => { |
| 31 | nextResolver(true); |
| 32 | }, 0); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |