FromObservable.js 5.0 KB

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