IteratorObservable.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var root_1 = require('../util/root');
  8. var Observable_1 = require('../Observable');
  9. var iterator_1 = require('../symbol/iterator');
  10. /**
  11. * We need this JSDoc comment for affecting ESDoc.
  12. * @extends {Ignored}
  13. * @hide true
  14. */
  15. var IteratorObservable = (function (_super) {
  16. __extends(IteratorObservable, _super);
  17. function IteratorObservable(iterator, scheduler) {
  18. _super.call(this);
  19. this.scheduler = scheduler;
  20. if (iterator == null) {
  21. throw new Error('iterator cannot be null.');
  22. }
  23. this.iterator = getIterator(iterator);
  24. }
  25. IteratorObservable.create = function (iterator, scheduler) {
  26. return new IteratorObservable(iterator, scheduler);
  27. };
  28. IteratorObservable.dispatch = function (state) {
  29. var index = state.index, hasError = state.hasError, iterator = state.iterator, subscriber = state.subscriber;
  30. if (hasError) {
  31. subscriber.error(state.error);
  32. return;
  33. }
  34. var result = iterator.next();
  35. if (result.done) {
  36. subscriber.complete();
  37. return;
  38. }
  39. subscriber.next(result.value);
  40. state.index = index + 1;
  41. if (subscriber.closed) {
  42. if (typeof iterator.return === 'function') {
  43. iterator.return();
  44. }
  45. return;
  46. }
  47. this.schedule(state);
  48. };
  49. /** @deprecated internal use only */ IteratorObservable.prototype._subscribe = function (subscriber) {
  50. var index = 0;
  51. var _a = this, iterator = _a.iterator, scheduler = _a.scheduler;
  52. if (scheduler) {
  53. return scheduler.schedule(IteratorObservable.dispatch, 0, {
  54. index: index, iterator: iterator, subscriber: subscriber
  55. });
  56. }
  57. else {
  58. do {
  59. var result = iterator.next();
  60. if (result.done) {
  61. subscriber.complete();
  62. break;
  63. }
  64. else {
  65. subscriber.next(result.value);
  66. }
  67. if (subscriber.closed) {
  68. if (typeof iterator.return === 'function') {
  69. iterator.return();
  70. }
  71. break;
  72. }
  73. } while (true);
  74. }
  75. };
  76. return IteratorObservable;
  77. }(Observable_1.Observable));
  78. exports.IteratorObservable = IteratorObservable;
  79. var StringIterator = (function () {
  80. function StringIterator(str, idx, len) {
  81. if (idx === void 0) { idx = 0; }
  82. if (len === void 0) { len = str.length; }
  83. this.str = str;
  84. this.idx = idx;
  85. this.len = len;
  86. }
  87. StringIterator.prototype[iterator_1.iterator] = function () { return (this); };
  88. StringIterator.prototype.next = function () {
  89. return this.idx < this.len ? {
  90. done: false,
  91. value: this.str.charAt(this.idx++)
  92. } : {
  93. done: true,
  94. value: undefined
  95. };
  96. };
  97. return StringIterator;
  98. }());
  99. var ArrayIterator = (function () {
  100. function ArrayIterator(arr, idx, len) {
  101. if (idx === void 0) { idx = 0; }
  102. if (len === void 0) { len = toLength(arr); }
  103. this.arr = arr;
  104. this.idx = idx;
  105. this.len = len;
  106. }
  107. ArrayIterator.prototype[iterator_1.iterator] = function () { return this; };
  108. ArrayIterator.prototype.next = function () {
  109. return this.idx < this.len ? {
  110. done: false,
  111. value: this.arr[this.idx++]
  112. } : {
  113. done: true,
  114. value: undefined
  115. };
  116. };
  117. return ArrayIterator;
  118. }());
  119. function getIterator(obj) {
  120. var i = obj[iterator_1.iterator];
  121. if (!i && typeof obj === 'string') {
  122. return new StringIterator(obj);
  123. }
  124. if (!i && obj.length !== undefined) {
  125. return new ArrayIterator(obj);
  126. }
  127. if (!i) {
  128. throw new TypeError('object is not iterable');
  129. }
  130. return obj[iterator_1.iterator]();
  131. }
  132. var maxSafeInteger = Math.pow(2, 53) - 1;
  133. function toLength(o) {
  134. var len = +o.length;
  135. if (isNaN(len)) {
  136. return 0;
  137. }
  138. if (len === 0 || !numberIsFinite(len)) {
  139. return len;
  140. }
  141. len = sign(len) * Math.floor(Math.abs(len));
  142. if (len <= 0) {
  143. return 0;
  144. }
  145. if (len > maxSafeInteger) {
  146. return maxSafeInteger;
  147. }
  148. return len;
  149. }
  150. function numberIsFinite(value) {
  151. return typeof value === 'number' && root_1.root.isFinite(value);
  152. }
  153. function sign(value) {
  154. var valueAsNumber = +value;
  155. if (valueAsNumber === 0) {
  156. return valueAsNumber;
  157. }
  158. if (isNaN(valueAsNumber)) {
  159. return valueAsNumber;
  160. }
  161. return valueAsNumber < 0 ? -1 : 1;
  162. }
  163. //# sourceMappingURL=IteratorObservable.js.map