64007233a1c81f59b5ef3bc093ec90f892a59871
[yaffs-website] / node_modules / bluebird / js / release / promise_array.js
1 "use strict";
2 module.exports = function(Promise, INTERNAL, tryConvertToPromise,
3     apiRejection, Proxyable) {
4 var util = require("./util");
5 var isArray = util.isArray;
6
7 function toResolutionValue(val) {
8     switch(val) {
9     case -2: return [];
10     case -3: return {};
11     }
12 }
13
14 function PromiseArray(values) {
15     var promise = this._promise = new Promise(INTERNAL);
16     if (values instanceof Promise) {
17         promise._propagateFrom(values, 3);
18     }
19     promise._setOnCancel(this);
20     this._values = values;
21     this._length = 0;
22     this._totalResolved = 0;
23     this._init(undefined, -2);
24 }
25 util.inherits(PromiseArray, Proxyable);
26
27 PromiseArray.prototype.length = function () {
28     return this._length;
29 };
30
31 PromiseArray.prototype.promise = function () {
32     return this._promise;
33 };
34
35 PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
36     var values = tryConvertToPromise(this._values, this._promise);
37     if (values instanceof Promise) {
38         values = values._target();
39         var bitField = values._bitField;
40         ;
41         this._values = values;
42
43         if (((bitField & 50397184) === 0)) {
44             this._promise._setAsyncGuaranteed();
45             return values._then(
46                 init,
47                 this._reject,
48                 undefined,
49                 this,
50                 resolveValueIfEmpty
51            );
52         } else if (((bitField & 33554432) !== 0)) {
53             values = values._value();
54         } else if (((bitField & 16777216) !== 0)) {
55             return this._reject(values._reason());
56         } else {
57             return this._cancel();
58         }
59     }
60     values = util.asArray(values);
61     if (values === null) {
62         var err = apiRejection(
63             "expecting an array or an iterable object but got " + util.classString(values)).reason();
64         this._promise._rejectCallback(err, false);
65         return;
66     }
67
68     if (values.length === 0) {
69         if (resolveValueIfEmpty === -5) {
70             this._resolveEmptyArray();
71         }
72         else {
73             this._resolve(toResolutionValue(resolveValueIfEmpty));
74         }
75         return;
76     }
77     this._iterate(values);
78 };
79
80 PromiseArray.prototype._iterate = function(values) {
81     var len = this.getActualLength(values.length);
82     this._length = len;
83     this._values = this.shouldCopyValues() ? new Array(len) : this._values;
84     var result = this._promise;
85     var isResolved = false;
86     var bitField = null;
87     for (var i = 0; i < len; ++i) {
88         var maybePromise = tryConvertToPromise(values[i], result);
89
90         if (maybePromise instanceof Promise) {
91             maybePromise = maybePromise._target();
92             bitField = maybePromise._bitField;
93         } else {
94             bitField = null;
95         }
96
97         if (isResolved) {
98             if (bitField !== null) {
99                 maybePromise.suppressUnhandledRejections();
100             }
101         } else if (bitField !== null) {
102             if (((bitField & 50397184) === 0)) {
103                 maybePromise._proxy(this, i);
104                 this._values[i] = maybePromise;
105             } else if (((bitField & 33554432) !== 0)) {
106                 isResolved = this._promiseFulfilled(maybePromise._value(), i);
107             } else if (((bitField & 16777216) !== 0)) {
108                 isResolved = this._promiseRejected(maybePromise._reason(), i);
109             } else {
110                 isResolved = this._promiseCancelled(i);
111             }
112         } else {
113             isResolved = this._promiseFulfilled(maybePromise, i);
114         }
115     }
116     if (!isResolved) result._setAsyncGuaranteed();
117 };
118
119 PromiseArray.prototype._isResolved = function () {
120     return this._values === null;
121 };
122
123 PromiseArray.prototype._resolve = function (value) {
124     this._values = null;
125     this._promise._fulfill(value);
126 };
127
128 PromiseArray.prototype._cancel = function() {
129     if (this._isResolved() || !this._promise.isCancellable()) return;
130     this._values = null;
131     this._promise._cancel();
132 };
133
134 PromiseArray.prototype._reject = function (reason) {
135     this._values = null;
136     this._promise._rejectCallback(reason, false);
137 };
138
139 PromiseArray.prototype._promiseFulfilled = function (value, index) {
140     this._values[index] = value;
141     var totalResolved = ++this._totalResolved;
142     if (totalResolved >= this._length) {
143         this._resolve(this._values);
144         return true;
145     }
146     return false;
147 };
148
149 PromiseArray.prototype._promiseCancelled = function() {
150     this._cancel();
151     return true;
152 };
153
154 PromiseArray.prototype._promiseRejected = function (reason) {
155     this._totalResolved++;
156     this._reject(reason);
157     return true;
158 };
159
160 PromiseArray.prototype._resultCancelled = function() {
161     if (this._isResolved()) return;
162     var values = this._values;
163     this._cancel();
164     if (values instanceof Promise) {
165         values.cancel();
166     } else {
167         for (var i = 0; i < values.length; ++i) {
168             if (values[i] instanceof Promise) {
169                 values[i].cancel();
170             }
171         }
172     }
173 };
174
175 PromiseArray.prototype.shouldCopyValues = function () {
176     return true;
177 };
178
179 PromiseArray.prototype.getActualLength = function (len) {
180     return len;
181 };
182
183 return PromiseArray;
184 };