eachOfLimit.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _eachOfLimit;
  6. var _noop = require('lodash/noop');
  7. var _noop2 = _interopRequireDefault(_noop);
  8. var _once = require('./once');
  9. var _once2 = _interopRequireDefault(_once);
  10. var _iterator = require('./iterator');
  11. var _iterator2 = _interopRequireDefault(_iterator);
  12. var _onlyOnce = require('./onlyOnce');
  13. var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
  14. var _breakLoop = require('./breakLoop');
  15. var _breakLoop2 = _interopRequireDefault(_breakLoop);
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. function _eachOfLimit(limit) {
  18. return function (obj, iteratee, callback) {
  19. callback = (0, _once2.default)(callback || _noop2.default);
  20. if (limit <= 0 || !obj) {
  21. return callback(null);
  22. }
  23. var nextElem = (0, _iterator2.default)(obj);
  24. var done = false;
  25. var running = 0;
  26. var looping = false;
  27. function iterateeCallback(err, value) {
  28. running -= 1;
  29. if (err) {
  30. done = true;
  31. callback(err);
  32. } else if (value === _breakLoop2.default || done && running <= 0) {
  33. done = true;
  34. return callback(null);
  35. } else if (!looping) {
  36. replenish();
  37. }
  38. }
  39. function replenish() {
  40. looping = true;
  41. while (running < limit && !done) {
  42. var elem = nextElem();
  43. if (elem === null) {
  44. done = true;
  45. if (running <= 0) {
  46. callback(null);
  47. }
  48. return;
  49. }
  50. running += 1;
  51. iteratee(elem.value, elem.key, (0, _onlyOnce2.default)(iterateeCallback));
  52. }
  53. looping = false;
  54. }
  55. replenish();
  56. };
  57. }
  58. module.exports = exports['default'];