'use strict'; const { EachLimit } = require('./eachLimit'); const { setLimit } = require('./internal/collection'); const { createEmptyObject } = require('./internal/util'); class MapValuesLimit extends EachLimit { constructor(collection, limit, iterator) { super(collection, limit, iterator, set); } } module.exports = { mapValuesLimit, MapValuesLimit }; function set(collection) { setLimit.call(this, collection); if (this._keys === undefined) { this._result = {}; this._callResolve = callResolveArray; } else { this._result = createEmptyObject(collection, this._keys); this._callResolve = callResolveObject; } return this; } function callResolveArray(value, index) { this._result[index] = value; if (--this._rest === 0) { this._promise._resolve(this._result); } else if (this._callRest-- > 0) { this._iterate(); } } function callResolveObject(value, index) { this._result[this._keys[index]] = value; if (--this._rest === 0) { this._promise._resolve(this._result); } else if (this._callRest-- > 0) { this._iterate(); } } /** * `Aigle.mapValuesLimit` is almost the same as [`Aigle.mapValues`](https://suguru03.github.io/aigle/docs/Aigle.html#mapValues) and * [`Aigle.mapValuesSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#mapValuesSeries), but it will work with concurrency. * @param {Array|Object} collection * @param {integer} [limit=8] * @param {Function} iterator * @return {Aigle} Returns an Aigle instance * @example * const order = []; * const collection = [1, 5, 3, 4, 2]; * const iterator = (num, index) => { * return Aigle.delay(num * 10) * .then(() => { * order.push(num); * return num * 2; * }); * }; * Aigle.mapValuesLimit(collection, 2, iterator) * .then(object => { * console.log(object); // { '0': 2, '1': 10, '2': 6, '3': 8, '4': 4 } * console.log(order); // [1, 3, 5, 2, 4] * }); * * @example * const order = []; * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 }; * const iterator = (num, key) => { * return Aigle.delay(num * 10) * .then(() => { * order.push(num); * return num * 2; * }); * }; * Aigle.mapValuesLimit(collection, 2, iterator) * .then(object => { * console.log(object); // { a: 2, b: 10, c: 6, d: 8, e: 4 } * console.log(order); // [1, 3, 5, 2, 4] * }); * * @example * const order = []; * const collection = [1, 5, 3, 4, 2]; * const iterator = num => { * return Aigle.delay(num * 10) * .then(() => { * order.push(num); * return num * 2; * }); * }; * Aigle.mapValuesLimit(collection, iterator) * .then(object => { * console.log(object); // { '0': 2, '1': 10, '2': 6, '3': 8, '4': 4 } * console.log(order); // [1, 2, 3, 4, 5] * }); */ function mapValuesLimit(collection, limit, iterator) { return new MapValuesLimit(collection, limit, iterator)._execute(); }