Code coverage report for debug\reduce.js

Statements: 100% (109 / 109)      Branches: 98.15% (53 / 54)      Functions: 100% (8 / 8)      Lines: 100% (103 / 103)      Ignored: none     

All files » debug\ » reduce.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158  8         8 8 8 8 8 970 970 970 970 970 970 970 970 970 970 970 288 288 273 15 12 12   3 3     970 970 970 970   8   8   8 69 39         8 3734   3734 3734 3734 3734 3734 3734 3734 3734 3734 904 904 2379     3734   3734 424   424 424 424   3310 273   273 273   3037 1915   1122   1122 1122     3734   3104 3104 3104   3104 4112 4112 1122 1122   2990 1903 1903   1903 1903 78 78     1825     1903   1903   1890 1890 1131 1131 1122 1122 9 3   6       762 762     876   876     8 974 970 970     8 39     8 935          
(function () { "use strict";
module.exports = function(Promise,
                          PromiseArray,
                          apiRejection,
                          tryConvertToPromise,
                          INTERNAL) {
var ASSERT = require("./assert.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) 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) {
    ASSERT((! this._isResolved()),
    "!this._isResolved()");
    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<length; ++valuesPhaseIndex) {
            valuesPhase[valuesPhaseIndex] = 0;
        }
    }
    valuesPhaseIndex = valuesPhase[index];
 
    if (index === 0 && this._zerothIsAccum) {
        ASSERT((! gotAccum),
    "!gotAccum");
        this._accum = value;
        this._gotAccum = gotAccum = true;
        valuesPhase[index] = ((valuesPhaseIndex === 0)
            ? 1 : 2);
    } else if (index === -1) {
        ASSERT((! gotAccum),
    "!gotAccum");
        this._accum = value;
        this._gotAccum = gotAccum = true;
    } else {
        if (valuesPhaseIndex === 0) {
            valuesPhase[index] = 1;
        } else {
            ASSERT(gotAccum,
    "gotAccum");
            valuesPhase[index] = 2;
            this._accum = value;
        }
    }
    if (!gotAccum) return;
 
    var callback = this._callback;
    var receiver = this._promise._boundTo;
    var ret;
 
    for (var i = this._reducingIndex; i < length; ++i) {
        valuesPhaseIndex = valuesPhase[i];
        if (valuesPhaseIndex === 2) {
            this._reducingIndex = i + 1;
            continue;
        }
        if (valuesPhaseIndex !== 1) return;
        value = values[i];
        ASSERT((! (value instanceof Promise)),
    "!(value instanceof Promise)");
        this._promise._pushContext();
        if (isEach) {
            preservedValues.push(value);
            ret = tryCatch(callback).call(receiver, value, i, length);
        }
        else {
            ret = tryCatch(callback)
                .call(receiver, this._accum, value, i, length);
        }
        this._promise._popContext();
 
        if (ret === errorObj) return this._reject(ret.e);
 
        var maybePromise = tryConvertToPromise(ret, this._promise);
        if (maybePromise instanceof Promise) {
            maybePromise = maybePromise._target();
            if (maybePromise._isPending()) {
                valuesPhase[i] = 4;
                return maybePromise._proxyPromiseArray(this, i);
            } else if (maybePromise._isFulfilled()) {
                ret = maybePromise._value();
            } else {
                return this._reject(maybePromise._reason());
            }
        }
 
        this._reducingIndex = i + 1;
        this._accum = ret;
    }
 
    ASSERT((this._reducingIndex === length),
    "this._reducingIndex === length");
    this._resolve(isEach ? preservedValues : this._accum);
};
 
function reduce(promises, fn, initialValue, _each) {
    if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a    See http://goo.gl/916lJJ\u000a");
    var array = new ReductionPromiseArray(promises, fn, initialValue, _each);
    return array.promise();
}
 
Promise.prototype.reduce = function (fn, initialValue) {
    return reduce(this, fn, initialValue, null);
};
 
Promise.reduce = function (promises, fn, initialValue, _each) {
    return reduce(promises, fn, initialValue, _each);
};
};
 
}());