IteratorObservable.js 5.2 KB

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