source: trip-planner-front/node_modules/p-limit/index.js@ 84d0fbb

Last change on this file since 84d0fbb was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[6a3a178]1'use strict';
[e29cc2e]2const pTry = require('p-try');
[6a3a178]3
4const pLimit = concurrency => {
5 if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) {
[e29cc2e]6 return Promise.reject(new TypeError('Expected `concurrency` to be a number from 1 and up'));
[6a3a178]7 }
8
[e29cc2e]9 const queue = [];
[6a3a178]10 let activeCount = 0;
11
12 const next = () => {
13 activeCount--;
14
[e29cc2e]15 if (queue.length > 0) {
16 queue.shift()();
[6a3a178]17 }
18 };
19
[e29cc2e]20 const run = (fn, resolve, ...args) => {
[6a3a178]21 activeCount++;
22
[e29cc2e]23 const result = pTry(fn, ...args);
[6a3a178]24
25 resolve(result);
26
[e29cc2e]27 result.then(next, next);
[6a3a178]28 };
29
30 const enqueue = (fn, resolve, ...args) => {
[e29cc2e]31 if (activeCount < concurrency) {
32 run(fn, resolve, ...args);
33 } else {
34 queue.push(run.bind(null, fn, resolve, ...args));
35 }
[6a3a178]36 };
37
[e29cc2e]38 const generator = (fn, ...args) => new Promise(resolve => enqueue(fn, resolve, ...args));
[6a3a178]39 Object.defineProperties(generator, {
40 activeCount: {
41 get: () => activeCount
42 },
43 pendingCount: {
[e29cc2e]44 get: () => queue.length
[6a3a178]45 },
46 clearQueue: {
47 value: () => {
[e29cc2e]48 queue.length = 0;
[6a3a178]49 }
50 }
51 });
52
53 return generator;
54};
55
56module.exports = pLimit;
[e29cc2e]57module.exports.default = pLimit;
Note: See TracBrowser for help on using the repository browser.