'use strict';
const { AigleProxy } = require('aigle-core');
const Aigle = require('./aigle');
const { INTERNAL, PENDING } = require('./internal/util');
const { execute, setSeries } = require('./internal/collection');
class EachSeries extends AigleProxy {
constructor(collection, iterator, set = setDefault) {
super();
this._iterator = iterator;
this._promise = new Aigle(INTERNAL);
this._index = 0;
this._coll = undefined;
this._rest = undefined;
this._size = undefined;
this._keys = undefined;
this._result = undefined;
this._iterate = undefined;
if (collection === PENDING) {
this._set = set;
this._iterate = this._callResolve;
this._callResolve = execute;
} else {
set.call(this, collection);
}
}
_execute() {
if (this._rest === 0) {
this._promise._resolve(this._result);
} else {
this._iterate();
}
return this._promise;
}
_callResolve(value) {
if (--this._rest === 0 || value === false) {
this._promise._resolve(this._result);
} else {
this._iterate();
}
}
_callReject(reason) {
this._promise._reject(reason);
}
}
module.exports = { eachSeries, EachSeries };
function setDefault(collection) {
setSeries.call(this, collection);
this._result = collection;
return this;
}
/**
* `Aigle.eachSeries` is almost the same as [`Aigle.each`](https://suguru03.github.io/aigle/docs/Aigle.html#each), but it will work in series.
* @param {Array|Object} collection
* @param {Function} iterator
* @return {Aigle} Returns an Aigle instance
* @example
* const order = [];
* const collection = [1, 4, 2];
* const iterator = num => {
* return Aigle.delay(num * 10)
* .then(() => order.push(num));
* };
* Aigle.eachSeries(collection, iterator)
* .then(value => {
* console.log(value); // undefined
* console.log(order); // [1, 4, 2];
* });
*
* @example
* const order = [];
* const collection = { a: 1, b: 4, c: 2 };
* const iterator = num => {
* return Aigle.delay(num * 10)
* .then(() => order.push(num));
* };
* Aigle.eachSeries(collection, iterator)
* .then(value => {
* console.log(value); // undefined
* console.log(order); // [1, 4, 2];
* });
*
* @example
* const order = [];
* const collection = [1, 4, 2];
* const iterator = num => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* return num !== 4; // break
* });
* };
* Aigle.eachSeries(collection, iterator)
* .then(value => {
* console.log(value); // undefined
* console.log(order); // [1, 4];
* });
*/
function eachSeries(collection, iterator) {
return new EachSeries(collection, iterator)._execute();
}