source: imaps-frontend/node_modules/webpack/lib/util/Semaphore.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8class Semaphore {
9 /**
10 * Creates an instance of Semaphore.
11 * @param {number} available the amount available number of "tasks"
12 * in the Semaphore
13 */
14 constructor(available) {
15 this.available = available;
16 /** @type {(function(): void)[]} */
17 this.waiters = [];
18 /** @private */
19 this._continue = this._continue.bind(this);
20 }
21
22 /**
23 * @param {function(): void} callback function block to capture and run
24 * @returns {void}
25 */
26 acquire(callback) {
27 if (this.available > 0) {
28 this.available--;
29 callback();
30 } else {
31 this.waiters.push(callback);
32 }
33 }
34
35 release() {
36 this.available++;
37 if (this.waiters.length > 0) {
38 process.nextTick(this._continue);
39 }
40 }
41
42 _continue() {
43 if (this.available > 0 && this.waiters.length > 0) {
44 this.available--;
45 const callback = /** @type {(function(): void)} */ (this.waiters.pop());
46 callback();
47 }
48 }
49}
50
51module.exports = Semaphore;
Note: See TracBrowser for help on using the repository browser.