FromObservable.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /** PURE_IMPORTS_START .._util_isArray,.._util_isArrayLike,.._util_isPromise,._PromiseObservable,._IteratorObservable,._ArrayObservable,._ArrayLikeObservable,.._symbol_iterator,.._Observable,.._operators_observeOn,.._symbol_observable 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 { isArray } from '../util/isArray';
  10. import { isArrayLike } from '../util/isArrayLike';
  11. import { isPromise } from '../util/isPromise';
  12. import { PromiseObservable } from './PromiseObservable';
  13. import { IteratorObservable } from './IteratorObservable';
  14. import { ArrayObservable } from './ArrayObservable';
  15. import { ArrayLikeObservable } from './ArrayLikeObservable';
  16. import { iterator as Symbol_iterator } from '../symbol/iterator';
  17. import { Observable } from '../Observable';
  18. import { ObserveOnSubscriber } from '../operators/observeOn';
  19. import { observable as Symbol_observable } from '../symbol/observable';
  20. /**
  21. * We need this JSDoc comment for affecting ESDoc.
  22. * @extends {Ignored}
  23. * @hide true
  24. */
  25. export var FromObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  26. __extends(FromObservable, _super);
  27. function FromObservable(ish, scheduler) {
  28. _super.call(this, null);
  29. this.ish = ish;
  30. this.scheduler = scheduler;
  31. }
  32. /**
  33. * Creates an Observable from an Array, an array-like object, a Promise, an
  34. * iterable object, or an Observable-like object.
  35. *
  36. * <span class="informal">Converts almost anything to an Observable.</span>
  37. *
  38. * <img src="./img/from.png" width="100%">
  39. *
  40. * Convert various other objects and data types into Observables. `from`
  41. * converts a Promise or an array-like or an
  42. * [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable)
  43. * object into an Observable that emits the items in that promise or array or
  44. * iterable. A String, in this context, is treated as an array of characters.
  45. * Observable-like objects (contains a function named with the ES2015 Symbol
  46. * for Observable) can also be converted through this operator.
  47. *
  48. * @example <caption>Converts an array to an Observable</caption>
  49. * var array = [10, 20, 30];
  50. * var result = Rx.Observable.from(array);
  51. * result.subscribe(x => console.log(x));
  52. *
  53. * // Results in the following:
  54. * // 10 20 30
  55. *
  56. * @example <caption>Convert an infinite iterable (from a generator) to an Observable</caption>
  57. * function* generateDoubles(seed) {
  58. * var i = seed;
  59. * while (true) {
  60. * yield i;
  61. * i = 2 * i; // double it
  62. * }
  63. * }
  64. *
  65. * var iterator = generateDoubles(3);
  66. * var result = Rx.Observable.from(iterator).take(10);
  67. * result.subscribe(x => console.log(x));
  68. *
  69. * // Results in the following:
  70. * // 3 6 12 24 48 96 192 384 768 1536
  71. *
  72. * @see {@link create}
  73. * @see {@link fromEvent}
  74. * @see {@link fromEventPattern}
  75. * @see {@link fromPromise}
  76. *
  77. * @param {ObservableInput<T>} ish A subscribable object, a Promise, an
  78. * Observable-like, an Array, an iterable or an array-like object to be
  79. * converted.
  80. * @param {Scheduler} [scheduler] The scheduler on which to schedule the
  81. * emissions of values.
  82. * @return {Observable<T>} The Observable whose values are originally from the
  83. * input object that was converted.
  84. * @static true
  85. * @name from
  86. * @owner Observable
  87. */
  88. FromObservable.create = function (ish, scheduler) {
  89. if (ish != null) {
  90. if (typeof ish[Symbol_observable] === 'function') {
  91. if (ish instanceof Observable && !scheduler) {
  92. return ish;
  93. }
  94. return new FromObservable(ish, scheduler);
  95. }
  96. else if (isArray(ish)) {
  97. return new ArrayObservable(ish, scheduler);
  98. }
  99. else if (isPromise(ish)) {
  100. return new PromiseObservable(ish, scheduler);
  101. }
  102. else if (typeof ish[Symbol_iterator] === 'function' || typeof ish === 'string') {
  103. return new IteratorObservable(ish, scheduler);
  104. }
  105. else if (isArrayLike(ish)) {
  106. return new ArrayLikeObservable(ish, scheduler);
  107. }
  108. }
  109. throw new TypeError((ish !== null && typeof ish || ish) + ' is not observable');
  110. };
  111. /** @deprecated internal use only */ FromObservable.prototype._subscribe = function (subscriber) {
  112. var ish = this.ish;
  113. var scheduler = this.scheduler;
  114. if (scheduler == null) {
  115. return ish[Symbol_observable]().subscribe(subscriber);
  116. }
  117. else {
  118. return ish[Symbol_observable]().subscribe(new ObserveOnSubscriber(subscriber, scheduler, 0));
  119. }
  120. };
  121. return FromObservable;
  122. }(Observable));
  123. //# sourceMappingURL=FromObservable.js.map