FromObservable.js 4.4 KB

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