"use strict"; module.exports = function(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL) { var async = require("./async.js"); var util = require("./util.js"); var tryCatch = util.tryCatch; var errorObj = util.errorObj; function ReductionPromiseArray(promises, fn, accum, _each) { this.constructor$(promises); this._promise._captureStackTrace(); this._preservedValues = _each === INTERNAL ? [] : null; this._zerothIsAccum = (accum === undefined); this._gotAccum = false; this._reducingIndex = (this._zerothIsAccum ? 1 : 0); this._valuesPhase = undefined; var maybePromise = tryConvertToPromise(accum, this._promise); var rejected = false; var isPromise = maybePromise instanceof Promise; if (isPromise) { maybePromise = maybePromise._target(); if (maybePromise._isPending()) { maybePromise._proxyPromiseArray(this, -1); } else if (maybePromise._isFulfilled()) { accum = maybePromise._value(); this._gotAccum = true; } else { this._reject(maybePromise._reason()); rejected = true; } } if (!(isPromise || this._zerothIsAccum)) this._gotAccum = true; this._callback = fn; this._accum = accum; if (!rejected) async.invoke(init, this, undefined); } function init() { this._init$(undefined, -5); } util.inherits(ReductionPromiseArray, PromiseArray); ReductionPromiseArray.prototype._init = function () {}; ReductionPromiseArray.prototype._resolveEmptyArray = function () { if (this._gotAccum || this._zerothIsAccum) { this._resolve(this._preservedValues !== null ? [] : this._accum); } }; ReductionPromiseArray.prototype._promiseFulfilled = function (value, index) { var values = this._values; values[index] = value; var length = this.length(); var preservedValues = this._preservedValues; var isEach = preservedValues !== null; var gotAccum = this._gotAccum; var valuesPhase = this._valuesPhase; var valuesPhaseIndex; if (!valuesPhase) { valuesPhase = this._valuesPhase = new Array(length); for (valuesPhaseIndex=0; valuesPhaseIndex