2 /* --------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 * ------------------------------------------------------------------------------------------ */
6 Object.defineProperty(exports, "__esModule", { value: true });
7 exports.Semaphore = void 0;
8 const ral_1 = require("./ral");
10 constructor(capacity = 1) {
12 throw new Error('Capacity must be greater than 0');
14 this._capacity = capacity;
19 return new Promise((resolve, reject) => {
20 this._waiting.push({ thunk, resolve, reject });
28 if (this._waiting.length === 0 || this._active === this._capacity) {
31 ral_1.default().timer.setImmediate(() => this.doRunNext());
34 if (this._waiting.length === 0 || this._active === this._capacity) {
37 const next = this._waiting.shift();
39 if (this._active > this._capacity) {
40 throw new Error(`To many thunks active`);
43 const result = next.thunk();
44 if (result instanceof Promise) {
45 result.then((value) => {
68 exports.Semaphore = Semaphore;
69 //# sourceMappingURL=semaphore.js.map