You've already forked Epicnabbo-Catalogus-Updated-Daily
100 lines
2.6 KiB
JavaScript
100 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
const { EachLimit } = require('./eachLimit');
|
|
const { setLimit } = require('./internal/collection');
|
|
|
|
class FindLimit extends EachLimit {
|
|
constructor(collection, limit, iterator) {
|
|
super(collection, limit, iterator, set);
|
|
}
|
|
}
|
|
|
|
module.exports = { findLimit, FindLimit };
|
|
|
|
function set(collection) {
|
|
setLimit.call(this, collection);
|
|
this._callResolve = this._keys === undefined ? callResolveArray : callResolveObject;
|
|
return this;
|
|
}
|
|
|
|
function callResolveArray(value, index) {
|
|
if (value) {
|
|
this._callRest = 0;
|
|
this._promise._resolve(this._coll[index]);
|
|
} else if (--this._rest === 0) {
|
|
this._promise._resolve();
|
|
} else if (this._callRest-- > 0) {
|
|
this._iterate();
|
|
}
|
|
}
|
|
|
|
function callResolveObject(value, index) {
|
|
if (value) {
|
|
this._callRest = 0;
|
|
this._promise._resolve(this._coll[this._keys[index]]);
|
|
} else if (--this._rest === 0) {
|
|
this._promise._resolve();
|
|
} else if (this._callRest-- > 0) {
|
|
this._iterate();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `Aigle.findLimit` is almost the as [`Aigle.find`](https://suguru03.github.io/aigle/docs/Aigle.html#find) and
|
|
* [`Aigle.findSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#findSeries), 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 === 0;
|
|
* });
|
|
* };
|
|
* Aigle.findLimit(collection, 2, iterator)
|
|
* .then(value => {
|
|
* console.log(value); // 2
|
|
* console.log(order); // [1, 3, 5, 2];
|
|
* });
|
|
*
|
|
* @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 === 0;
|
|
* });
|
|
* };
|
|
* Aigle.findLimit(collection, 2, iterator)
|
|
* .then(value => {
|
|
* console.log(value); // 2
|
|
* console.log(order); // [1, 3, 5, 2];
|
|
* });
|
|
*
|
|
* @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 === 0;
|
|
* });
|
|
* };
|
|
* Aigle.findLimit(collection, iterator)
|
|
* .then(value => {
|
|
* console.log(value); // 2
|
|
* console.log(order); // [1, 2];
|
|
* });
|
|
*/
|
|
function findLimit(collection, limit, iterator) {
|
|
return new FindLimit(collection, limit, iterator)._execute();
|
|
}
|