Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

props.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. module.exports = function(
  3. Promise, PromiseArray, tryConvertToPromise, apiRejection) {
  4. var util = require("./util");
  5. var isObject = util.isObject;
  6. var es5 = require("./es5");
  7. var Es6Map;
  8. if (typeof Map === "function") Es6Map = Map;
  9. var mapToEntries = (function() {
  10. var index = 0;
  11. var size = 0;
  12. function extractEntry(value, key) {
  13. this[index] = value;
  14. this[index + size] = key;
  15. index++;
  16. }
  17. return function mapToEntries(map) {
  18. size = map.size;
  19. index = 0;
  20. var ret = new Array(map.size * 2);
  21. map.forEach(extractEntry, ret);
  22. return ret;
  23. };
  24. })();
  25. var entriesToMap = function(entries) {
  26. var ret = new Es6Map();
  27. var length = entries.length / 2 | 0;
  28. for (var i = 0; i < length; ++i) {
  29. var key = entries[length + i];
  30. var value = entries[i];
  31. ret.set(key, value);
  32. }
  33. return ret;
  34. };
  35. function PropertiesPromiseArray(obj) {
  36. var isMap = false;
  37. var entries;
  38. if (Es6Map !== undefined && obj instanceof Es6Map) {
  39. entries = mapToEntries(obj);
  40. isMap = true;
  41. } else {
  42. var keys = es5.keys(obj);
  43. var len = keys.length;
  44. entries = new Array(len * 2);
  45. for (var i = 0; i < len; ++i) {
  46. var key = keys[i];
  47. entries[i] = obj[key];
  48. entries[i + len] = key;
  49. }
  50. }
  51. this.constructor$(entries);
  52. this._isMap = isMap;
  53. this._init$(undefined, isMap ? -6 : -3);
  54. }
  55. util.inherits(PropertiesPromiseArray, PromiseArray);
  56. PropertiesPromiseArray.prototype._init = function () {};
  57. PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
  58. this._values[index] = value;
  59. var totalResolved = ++this._totalResolved;
  60. if (totalResolved >= this._length) {
  61. var val;
  62. if (this._isMap) {
  63. val = entriesToMap(this._values);
  64. } else {
  65. val = {};
  66. var keyOffset = this.length();
  67. for (var i = 0, len = this.length(); i < len; ++i) {
  68. val[this._values[i + keyOffset]] = this._values[i];
  69. }
  70. }
  71. this._resolve(val);
  72. return true;
  73. }
  74. return false;
  75. };
  76. PropertiesPromiseArray.prototype.shouldCopyValues = function () {
  77. return false;
  78. };
  79. PropertiesPromiseArray.prototype.getActualLength = function (len) {
  80. return len >> 1;
  81. };
  82. function props(promises) {
  83. var ret;
  84. var castValue = tryConvertToPromise(promises);
  85. if (!isObject(castValue)) {
  86. return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  87. } else if (castValue instanceof Promise) {
  88. ret = castValue._then(
  89. Promise.props, undefined, undefined, undefined, undefined);
  90. } else {
  91. ret = new PropertiesPromiseArray(castValue).promise();
  92. }
  93. if (castValue instanceof Promise) {
  94. ret._propagateFrom(castValue, 2);
  95. }
  96. return ret;
  97. }
  98. Promise.prototype.props = function () {
  99. return props(this);
  100. };
  101. Promise.props = function (promises) {
  102. return props(promises);
  103. };
  104. };