react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / bluebird / js / main / reduce.js
81160 views"use strict";1module.exports = function(Promise,2PromiseArray,3apiRejection,4tryConvertToPromise,5INTERNAL) {6var async = require("./async.js");7var util = require("./util.js");8var tryCatch = util.tryCatch;9var errorObj = util.errorObj;10function ReductionPromiseArray(promises, fn, accum, _each) {11this.constructor$(promises);12this._promise._captureStackTrace();13this._preservedValues = _each === INTERNAL ? [] : null;14this._zerothIsAccum = (accum === undefined);15this._gotAccum = false;16this._reducingIndex = (this._zerothIsAccum ? 1 : 0);17this._valuesPhase = undefined;18var maybePromise = tryConvertToPromise(accum, this._promise);19var rejected = false;20var isPromise = maybePromise instanceof Promise;21if (isPromise) {22maybePromise = maybePromise._target();23if (maybePromise._isPending()) {24maybePromise._proxyPromiseArray(this, -1);25} else if (maybePromise._isFulfilled()) {26accum = maybePromise._value();27this._gotAccum = true;28} else {29this._reject(maybePromise._reason());30rejected = true;31}32}33if (!(isPromise || this._zerothIsAccum)) this._gotAccum = true;34this._callback = fn;35this._accum = accum;36if (!rejected) async.invoke(init, this, undefined);37}38function init() {39this._init$(undefined, -5);40}41util.inherits(ReductionPromiseArray, PromiseArray);4243ReductionPromiseArray.prototype._init = function () {};4445ReductionPromiseArray.prototype._resolveEmptyArray = function () {46if (this._gotAccum || this._zerothIsAccum) {47this._resolve(this._preservedValues !== null48? [] : this._accum);49}50};5152ReductionPromiseArray.prototype._promiseFulfilled = function (value, index) {53var values = this._values;54values[index] = value;55var length = this.length();56var preservedValues = this._preservedValues;57var isEach = preservedValues !== null;58var gotAccum = this._gotAccum;59var valuesPhase = this._valuesPhase;60var valuesPhaseIndex;61if (!valuesPhase) {62valuesPhase = this._valuesPhase = new Array(length);63for (valuesPhaseIndex=0; valuesPhaseIndex<length; ++valuesPhaseIndex) {64valuesPhase[valuesPhaseIndex] = 0;65}66}67valuesPhaseIndex = valuesPhase[index];6869if (index === 0 && this._zerothIsAccum) {70this._accum = value;71this._gotAccum = gotAccum = true;72valuesPhase[index] = ((valuesPhaseIndex === 0)73? 1 : 2);74} else if (index === -1) {75this._accum = value;76this._gotAccum = gotAccum = true;77} else {78if (valuesPhaseIndex === 0) {79valuesPhase[index] = 1;80} else {81valuesPhase[index] = 2;82this._accum = value;83}84}85if (!gotAccum) return;8687var callback = this._callback;88var receiver = this._promise._boundTo;89var ret;9091for (var i = this._reducingIndex; i < length; ++i) {92valuesPhaseIndex = valuesPhase[i];93if (valuesPhaseIndex === 2) {94this._reducingIndex = i + 1;95continue;96}97if (valuesPhaseIndex !== 1) return;98value = values[i];99this._promise._pushContext();100if (isEach) {101preservedValues.push(value);102ret = tryCatch(callback).call(receiver, value, i, length);103}104else {105ret = tryCatch(callback)106.call(receiver, this._accum, value, i, length);107}108this._promise._popContext();109110if (ret === errorObj) return this._reject(ret.e);111112var maybePromise = tryConvertToPromise(ret, this._promise);113if (maybePromise instanceof Promise) {114maybePromise = maybePromise._target();115if (maybePromise._isPending()) {116valuesPhase[i] = 4;117return maybePromise._proxyPromiseArray(this, i);118} else if (maybePromise._isFulfilled()) {119ret = maybePromise._value();120} else {121return this._reject(maybePromise._reason());122}123}124125this._reducingIndex = i + 1;126this._accum = ret;127}128129this._resolve(isEach ? preservedValues : this._accum);130};131132function reduce(promises, fn, initialValue, _each) {133if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");134var array = new ReductionPromiseArray(promises, fn, initialValue, _each);135return array.promise();136}137138Promise.prototype.reduce = function (fn, initialValue) {139return reduce(this, fn, initialValue, null);140};141142Promise.reduce = function (promises, fn, initialValue, _each) {143return reduce(promises, fn, initialValue, _each);144};145};146147148