github-pages-deploy-action/node_modules/p-limit/index.js

53 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2020-01-28 13:07:56 +08:00
'use strict';
const pTry = require('p-try');
2020-03-31 20:57:48 +08:00
const pLimit = concurrency => {
if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) {
return Promise.reject(new TypeError('Expected `concurrency` to be a number from 1 and up'));
2020-01-28 13:07:56 +08:00
}
const queue = [];
let activeCount = 0;
const next = () => {
activeCount--;
if (queue.length > 0) {
queue.shift()();
}
};
2020-03-31 20:57:48 +08:00
const run = (fn, resolve, ...args) => {
activeCount++;
2020-01-28 13:07:56 +08:00
2020-03-31 20:57:48 +08:00
const result = pTry(fn, ...args);
resolve(result);
result.then(next, next);
};
const enqueue = (fn, resolve, ...args) => {
2020-01-28 13:07:56 +08:00
if (activeCount < concurrency) {
2020-03-31 20:57:48 +08:00
run(fn, resolve, ...args);
2020-01-28 13:07:56 +08:00
} else {
2020-03-31 20:57:48 +08:00
queue.push(run.bind(null, fn, resolve, ...args));
}
};
const generator = (fn, ...args) => new Promise(resolve => enqueue(fn, resolve, ...args));
Object.defineProperties(generator, {
activeCount: {
get: () => activeCount
},
pendingCount: {
get: () => queue.length
2020-01-28 13:07:56 +08:00
}
});
2020-03-31 20:57:48 +08:00
return generator;
2020-01-28 13:07:56 +08:00
};
2020-03-31 20:57:48 +08:00
module.exports = pLimit;
module.exports.default = pLimit;