d6f9e64b074f2895220942fcddf6d0622137ec47
[aai/esr-gui.git] /
1 "use strict";
2 module.exports = function(
3     Promise, PromiseArray, tryConvertToPromise, apiRejection) {
4 var util = require("./util.js");
5 var isObject = util.isObject;
6 var es5 = require("./es5.js");
7
8 function PropertiesPromiseArray(obj) {
9     var keys = es5.keys(obj);
10     var len = keys.length;
11     var values = new Array(len * 2);
12     for (var i = 0; i < len; ++i) {
13         var key = keys[i];
14         values[i] = obj[key];
15         values[i + len] = key;
16     }
17     this.constructor$(values);
18 }
19 util.inherits(PropertiesPromiseArray, PromiseArray);
20
21 PropertiesPromiseArray.prototype._init = function () {
22     this._init$(undefined, -3) ;
23 };
24
25 PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
26     this._values[index] = value;
27     var totalResolved = ++this._totalResolved;
28     if (totalResolved >= this._length) {
29         var val = {};
30         var keyOffset = this.length();
31         for (var i = 0, len = this.length(); i < len; ++i) {
32             val[this._values[i + keyOffset]] = this._values[i];
33         }
34         this._resolve(val);
35     }
36 };
37
38 PropertiesPromiseArray.prototype._promiseProgressed = function (value, index) {
39     this._promise._progress({
40         key: this._values[index + this.length()],
41         value: value
42     });
43 };
44
45 PropertiesPromiseArray.prototype.shouldCopyValues = function () {
46     return false;
47 };
48
49 PropertiesPromiseArray.prototype.getActualLength = function (len) {
50     return len >> 1;
51 };
52
53 function props(promises) {
54     var ret;
55     var castValue = tryConvertToPromise(promises);
56
57     if (!isObject(castValue)) {
58         return apiRejection("cannot await properties of a non-object\u000a\u000a    See http://goo.gl/OsFKC8\u000a");
59     } else if (castValue instanceof Promise) {
60         ret = castValue._then(
61             Promise.props, undefined, undefined, undefined, undefined);
62     } else {
63         ret = new PropertiesPromiseArray(castValue).promise();
64     }
65
66     if (castValue instanceof Promise) {
67         ret._propagateFrom(castValue, 4);
68     }
69     return ret;
70 }
71
72 Promise.prototype.props = function () {
73     return props(this);
74 };
75
76 Promise.props = function (promises) {
77     return props(promises);
78 };
79 };