123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- "use strict";
- module.exports = function(Promise, INTERNAL, tryConvertToPromise,
- apiRejection) {
- var util = require("./util.js");
- var isArray = util.isArray;
- function toResolutionValue(val) {
- switch(val) {
- case -2: return [];
- case -3: return {};
- }
- }
- function PromiseArray(values) {
- var promise = this._promise = new Promise(INTERNAL);
- var parent;
- if (values instanceof Promise) {
- parent = values;
- promise._propagateFrom(parent, 1 | 4);
- }
- this._values = values;
- this._length = 0;
- this._totalResolved = 0;
- this._init(undefined, -2);
- }
- PromiseArray.prototype.length = function () {
- return this._length;
- };
- PromiseArray.prototype.promise = function () {
- return this._promise;
- };
- PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
- var values = tryConvertToPromise(this._values, this._promise);
- if (values instanceof Promise) {
- values = values._target();
- this._values = values;
- if (values._isFulfilled()) {
- values = values._value();
- if (!isArray(values)) {
- var err = new Promise.TypeError("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a");
- this.__hardReject__(err);
- return;
- }
- } else if (values._isPending()) {
- values._then(
- init,
- this._reject,
- undefined,
- this,
- resolveValueIfEmpty
- );
- return;
- } else {
- this._reject(values._reason());
- return;
- }
- } else if (!isArray(values)) {
- this._promise._reject(apiRejection("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a")._reason());
- return;
- }
- if (values.length === 0) {
- if (resolveValueIfEmpty === -5) {
- this._resolveEmptyArray();
- }
- else {
- this._resolve(toResolutionValue(resolveValueIfEmpty));
- }
- return;
- }
- var len = this.getActualLength(values.length);
- this._length = len;
- this._values = this.shouldCopyValues() ? new Array(len) : this._values;
- var promise = this._promise;
- for (var i = 0; i < len; ++i) {
- var isResolved = this._isResolved();
- var maybePromise = tryConvertToPromise(values[i], promise);
- if (maybePromise instanceof Promise) {
- maybePromise = maybePromise._target();
- if (isResolved) {
- maybePromise._unsetRejectionIsUnhandled();
- } else if (maybePromise._isPending()) {
- maybePromise._proxyPromiseArray(this, i);
- } else if (maybePromise._isFulfilled()) {
- this._promiseFulfilled(maybePromise._value(), i);
- } else {
- this._promiseRejected(maybePromise._reason(), i);
- }
- } else if (!isResolved) {
- this._promiseFulfilled(maybePromise, i);
- }
- }
- };
- PromiseArray.prototype._isResolved = function () {
- return this._values === null;
- };
- PromiseArray.prototype._resolve = function (value) {
- this._values = null;
- this._promise._fulfill(value);
- };
- PromiseArray.prototype.__hardReject__ =
- PromiseArray.prototype._reject = function (reason) {
- this._values = null;
- this._promise._rejectCallback(reason, false, true);
- };
- PromiseArray.prototype._promiseProgressed = function (progressValue, index) {
- this._promise._progress({
- index: index,
- value: progressValue
- });
- };
- PromiseArray.prototype._promiseFulfilled = function (value, index) {
- this._values[index] = value;
- var totalResolved = ++this._totalResolved;
- if (totalResolved >= this._length) {
- this._resolve(this._values);
- }
- };
- PromiseArray.prototype._promiseRejected = function (reason, index) {
- this._totalResolved++;
- this._reject(reason);
- };
- PromiseArray.prototype.shouldCopyValues = function () {
- return true;
- };
- PromiseArray.prototype.getActualLength = function (len) {
- return len;
- };
- return PromiseArray;
- };
|