You've already forked Epicnabbo-Catalogus-Updated-Daily
94 lines
2.4 KiB
JavaScript
94 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
const { EachLimit } = require('./eachLimit');
|
|
const { concatArray } = require('./internal/util');
|
|
const { setLimit } = require('./internal/collection');
|
|
|
|
class ConcatLimit extends EachLimit {
|
|
constructor(collection, limit, iterator) {
|
|
super(collection, limit, iterator, set);
|
|
}
|
|
|
|
_callResolve(value, index) {
|
|
this._result[index] = value;
|
|
if (--this._rest === 0) {
|
|
this._promise._resolve(concatArray(this._result));
|
|
} else if (this._callRest-- > 0) {
|
|
this._iterate();
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { concatLimit, ConcatLimit };
|
|
|
|
function set(collection) {
|
|
setLimit.call(this, collection);
|
|
this._result = Array(this._rest);
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* `Aigle.concatLimit` is almost the as [`Aigle.concat`](https://suguru03.github.io/aigle/docs/Aigle.html#concat) and
|
|
* [`Aigle.concatSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#concatSeries), 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, collection) => {
|
|
* return Aigle.delay(num * 10)
|
|
* .then(() => {
|
|
* order.push(num);
|
|
* return num;
|
|
* });
|
|
* };
|
|
* Aigle.concatLimit(collection, 2, iterator)
|
|
* .then(array => {
|
|
* console.log(array); // [1, 3, 5, 2, 4];
|
|
* console.log(order); // [1, 3, 5, 2, 4];
|
|
* });
|
|
*
|
|
* @example
|
|
* const order = [];
|
|
* const collection = {
|
|
* task1: 1,
|
|
* task2: 5,
|
|
* task3: 3,
|
|
* task4: 4,
|
|
* task5: 2
|
|
* };
|
|
* const iterator = (num, key, collection) => {
|
|
* return Aigle.delay(num * 10)
|
|
* .then(() => {
|
|
* order.push(num);
|
|
* return num;
|
|
* });
|
|
* };
|
|
* Aigle.concatLimit(collection, 2, iterator)
|
|
* .then(array => {
|
|
* console.log(array); // [1, 3, 5, 2, 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;
|
|
* });
|
|
* };
|
|
* Aigle.concatLimit(collection, iterator)
|
|
* .then(array => {
|
|
* console.log(array); // [1, 2, 3, 4, 5];
|
|
* console.log(order); // [1, 2, 3, 4, 5];
|
|
* });
|
|
*/
|
|
function concatLimit(collection, limit, iterator) {
|
|
return new ConcatLimit(collection, limit, iterator)._execute();
|
|
}
|