설명 없음

enumerator.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {
  2. isArray,
  3. isMaybeThenable
  4. } from './utils';
  5. import {
  6. noop,
  7. reject,
  8. fulfill,
  9. subscribe,
  10. FULFILLED,
  11. REJECTED,
  12. PENDING
  13. } from './-internal';
  14. function Enumerator(Constructor, input) {
  15. var enumerator = this;
  16. enumerator._instanceConstructor = Constructor;
  17. enumerator.promise = new Constructor(noop);
  18. if (enumerator._validateInput(input)) {
  19. enumerator._input = input;
  20. enumerator.length = input.length;
  21. enumerator._remaining = input.length;
  22. enumerator._init();
  23. if (enumerator.length === 0) {
  24. fulfill(enumerator.promise, enumerator._result);
  25. } else {
  26. enumerator.length = enumerator.length || 0;
  27. enumerator._enumerate();
  28. if (enumerator._remaining === 0) {
  29. fulfill(enumerator.promise, enumerator._result);
  30. }
  31. }
  32. } else {
  33. reject(enumerator.promise, enumerator._validationError());
  34. }
  35. }
  36. Enumerator.prototype._validateInput = function(input) {
  37. return isArray(input);
  38. };
  39. Enumerator.prototype._validationError = function() {
  40. return new Error('Array Methods must be provided an Array');
  41. };
  42. Enumerator.prototype._init = function() {
  43. this._result = new Array(this.length);
  44. };
  45. export default Enumerator;
  46. Enumerator.prototype._enumerate = function() {
  47. var enumerator = this;
  48. var length = enumerator.length;
  49. var promise = enumerator.promise;
  50. var input = enumerator._input;
  51. for (var i = 0; promise._state === PENDING && i < length; i++) {
  52. enumerator._eachEntry(input[i], i);
  53. }
  54. };
  55. Enumerator.prototype._eachEntry = function(entry, i) {
  56. var enumerator = this;
  57. var c = enumerator._instanceConstructor;
  58. if (isMaybeThenable(entry)) {
  59. if (entry.constructor === c && entry._state !== PENDING) {
  60. entry._onerror = null;
  61. enumerator._settledAt(entry._state, i, entry._result);
  62. } else {
  63. enumerator._willSettleAt(c.resolve(entry), i);
  64. }
  65. } else {
  66. enumerator._remaining--;
  67. enumerator._result[i] = entry;
  68. }
  69. };
  70. Enumerator.prototype._settledAt = function(state, i, value) {
  71. var enumerator = this;
  72. var promise = enumerator.promise;
  73. if (promise._state === PENDING) {
  74. enumerator._remaining--;
  75. if (state === REJECTED) {
  76. reject(promise, value);
  77. } else {
  78. enumerator._result[i] = value;
  79. }
  80. }
  81. if (enumerator._remaining === 0) {
  82. fulfill(promise, enumerator._result);
  83. }
  84. };
  85. Enumerator.prototype._willSettleAt = function(promise, i) {
  86. var enumerator = this;
  87. subscribe(promise, undefined, function(value) {
  88. enumerator._settledAt(FULFILLED, i, value);
  89. }, function(reason) {
  90. enumerator._settledAt(REJECTED, i, reason);
  91. });
  92. };