Skip to content

Commit

Permalink
refactor(model): use internal parallelLimit() instead of async
Browse files Browse the repository at this point in the history
Re: #8073
  • Loading branch information
vkarpov15 committed Sep 2, 2019
1 parent 412e387 commit 03dfc6a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions lib/helpers/parallelLimit.js
@@ -0,0 +1,51 @@
'use strict';

module.exports = parallelLimit;

/*!
* ignore
*/

function parallelLimit(fns, limit, callback) {
let numInProgress = 0;
let numFinished = 0;
let error = null;

if (fns.length === 0) {
return callback();
}

for (let i = 0; i < fns.length && i <= limit; ++i) {
_start();
}

function _start() {
fns[numFinished + numInProgress](_done(numFinished + numInProgress));
++numInProgress;
}

const results = [];

function _done(index) {
return (err, res) => {
--numInProgress;
++numFinished;

if (error != null) {
return;
}
if (err != null) {
error = err;
return callback(error);
}

results[index] = res;

if (numFinished === fns.length) {
return callback(null, results);
} else if (numFinished + numInProgress < fns.length) {
_start();
}
};
}
}
2 changes: 1 addition & 1 deletion lib/model.js
Expand Up @@ -38,7 +38,7 @@ const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedIncl
const get = require('./helpers/get');
const leanPopulateMap = require('./helpers/populate/leanPopulateMap');
const modifiedPaths = require('./helpers/update/modifiedPaths');
const parallelLimit = require('async/parallelLimit');
const parallelLimit = require('./helpers/parallelLimit');
const util = require('util');
const utils = require('./utils');

Expand Down

0 comments on commit 03dfc6a

Please sign in to comment.