"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ncpAsync = ncpAsync;
exports.Semaphore = void 0;
function _ncp() {
const data = require("ncp");
_ncp = function () {
return data;
};
return data;
}
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function ncpAsync(source, dest, options = {}) {
return new Promise((resolve, reject) => {
(0, _ncp().ncp)(source, dest, options, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
class Semaphore {
constructor() {
_defineProperty(this, "queue", []);
_defineProperty(this, "available", 1);
}
async acquire() {
if (this.available > 0) {
this.available -= 1;
return Promise.resolve(true);
} // If there is no permit available, we return a promise that resolves once the semaphore gets
// signaled enough times that "available" is equal to one.
return new Promise(resolver => this.queue.push(resolver));
}
release() {
this.available += 1;
if (this.available > 1 && this.queue.length > 0) {
throw new Error('this.available should never be > 0 when there is someone waiting.');
} else if (this.available === 1 && this.queue.length > 0) {
// If there is someone else waiting, immediately consume the permit that was released
// at the beginning of this function and let the waiting function resume.
this.available -= 1;
const nextResolver = this.queue.shift();
if (nextResolver) {
nextResolver(true);
}
}
}
}
exports.Semaphore = Semaphore;
//# sourceMappingURL=__sourcemaps__/Utils.js.map