80 lines
2.1 KiB
JavaScript
80 lines
2.1 KiB
JavaScript
"use strict";
|
|
module.exports = function(
|
|
Promise, PromiseArray, tryConvertToPromise, apiRejection) {
|
|
var util = require("./util.js");
|
|
var isObject = util.isObject;
|
|
var es5 = require("./es5.js");
|
|
|
|
function PropertiesPromiseArray(obj) {
|
|
var keys = es5.keys(obj);
|
|
var len = keys.length;
|
|
var values = new Array(len * 2);
|
|
for (var i = 0; i < len; ++i) {
|
|
var key = keys[i];
|
|
values[i] = obj[key];
|
|
values[i + len] = key;
|
|
}
|
|
this.constructor$(values);
|
|
}
|
|
util.inherits(PropertiesPromiseArray, PromiseArray);
|
|
|
|
PropertiesPromiseArray.prototype._init = function () {
|
|
this._init$(undefined, -3) ;
|
|
};
|
|
|
|
PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
|
|
this._values[index] = value;
|
|
var totalResolved = ++this._totalResolved;
|
|
if (totalResolved >= this._length) {
|
|
var val = {};
|
|
var keyOffset = this.length();
|
|
for (var i = 0, len = this.length(); i < len; ++i) {
|
|
val[this._values[i + keyOffset]] = this._values[i];
|
|
}
|
|
this._resolve(val);
|
|
}
|
|
};
|
|
|
|
PropertiesPromiseArray.prototype._promiseProgressed = function (value, index) {
|
|
this._promise._progress({
|
|
key: this._values[index + this.length()],
|
|
value: value
|
|
});
|
|
};
|
|
|
|
PropertiesPromiseArray.prototype.shouldCopyValues = function () {
|
|
return false;
|
|
};
|
|
|
|
PropertiesPromiseArray.prototype.getActualLength = function (len) {
|
|
return len >> 1;
|
|
};
|
|
|
|
function props(promises) {
|
|
var ret;
|
|
var castValue = tryConvertToPromise(promises);
|
|
|
|
if (!isObject(castValue)) {
|
|
return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/OsFKC8\u000a");
|
|
} else if (castValue instanceof Promise) {
|
|
ret = castValue._then(
|
|
Promise.props, undefined, undefined, undefined, undefined);
|
|
} else {
|
|
ret = new PropertiesPromiseArray(castValue).promise();
|
|
}
|
|
|
|
if (castValue instanceof Promise) {
|
|
ret._propagateFrom(castValue, 4);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
Promise.prototype.props = function () {
|
|
return props(this);
|
|
};
|
|
|
|
Promise.props = function (promises) {
|
|
return props(promises);
|
|
};
|
|
};
|