73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.Semaphore = undefined;
|
|
exports.ncpAsync = ncpAsync;
|
|
|
|
var _ncp;
|
|
|
|
function _load_ncp() {
|
|
return _ncp = _interopRequireDefault(require('ncp'));
|
|
}
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
|
|
|
|
function ncpAsync(source, dest, options = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
(0, (_ncp || _load_ncp()).default)(source, dest, options, err => {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
class Semaphore {
|
|
constructor() {
|
|
this.queue = [];
|
|
this.available = 1;
|
|
}
|
|
|
|
acquire() {
|
|
var _this = this;
|
|
|
|
return _asyncToGenerator(function* () {
|
|
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(function (resolver) {
|
|
return _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
|