ArrayObservable.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /** PURE_IMPORTS_START .._Observable,._ScalarObservable,._EmptyObservable,.._util_isScheduler 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 { Observable } from '../Observable';
  10. import { ScalarObservable } from './ScalarObservable';
  11. import { EmptyObservable } from './EmptyObservable';
  12. import { isScheduler } from '../util/isScheduler';
  13. /**
  14. * We need this JSDoc comment for affecting ESDoc.
  15. * @extends {Ignored}
  16. * @hide true
  17. */
  18. export var ArrayObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  19. __extends(ArrayObservable, _super);
  20. function ArrayObservable(array, scheduler) {
  21. _super.call(this);
  22. this.array = array;
  23. this.scheduler = scheduler;
  24. if (!scheduler && array.length === 1) {
  25. this._isScalar = true;
  26. this.value = array[0];
  27. }
  28. }
  29. ArrayObservable.create = function (array, scheduler) {
  30. return new ArrayObservable(array, scheduler);
  31. };
  32. /**
  33. * Creates an Observable that emits some values you specify as arguments,
  34. * immediately one after the other, and then emits a complete notification.
  35. *
  36. * <span class="informal">Emits the arguments you provide, then completes.
  37. * </span>
  38. *
  39. * <img src="./img/of.png" width="100%">
  40. *
  41. * This static operator is useful for creating a simple Observable that only
  42. * emits the arguments given, and the complete notification thereafter. It can
  43. * be used for composing with other Observables, such as with {@link concat}.
  44. * By default, it uses a `null` IScheduler, which means the `next`
  45. * notifications are sent synchronously, although with a different IScheduler
  46. * it is possible to determine when those notifications will be delivered.
  47. *
  48. * @example <caption>Emit 10, 20, 30, then 'a', 'b', 'c', then start ticking every second.</caption>
  49. * var numbers = Rx.Observable.of(10, 20, 30);
  50. * var letters = Rx.Observable.of('a', 'b', 'c');
  51. * var interval = Rx.Observable.interval(1000);
  52. * var result = numbers.concat(letters).concat(interval);
  53. * result.subscribe(x => console.log(x));
  54. *
  55. * @see {@link create}
  56. * @see {@link empty}
  57. * @see {@link never}
  58. * @see {@link throw}
  59. *
  60. * @param {...T} values Arguments that represent `next` values to be emitted.
  61. * @param {Scheduler} [scheduler] A {@link IScheduler} to use for scheduling
  62. * the emissions of the `next` notifications.
  63. * @return {Observable<T>} An Observable that emits each given input value.
  64. * @static true
  65. * @name of
  66. * @owner Observable
  67. */
  68. ArrayObservable.of = function () {
  69. var array = [];
  70. for (var _i = 0; _i < arguments.length; _i++) {
  71. array[_i - 0] = arguments[_i];
  72. }
  73. var scheduler = array[array.length - 1];
  74. if (isScheduler(scheduler)) {
  75. array.pop();
  76. }
  77. else {
  78. scheduler = null;
  79. }
  80. var len = array.length;
  81. if (len > 1) {
  82. return new ArrayObservable(array, scheduler);
  83. }
  84. else if (len === 1) {
  85. return new ScalarObservable(array[0], scheduler);
  86. }
  87. else {
  88. return new EmptyObservable(scheduler);
  89. }
  90. };
  91. ArrayObservable.dispatch = function (state) {
  92. var array = state.array, index = state.index, count = state.count, subscriber = state.subscriber;
  93. if (index >= count) {
  94. subscriber.complete();
  95. return;
  96. }
  97. subscriber.next(array[index]);
  98. if (subscriber.closed) {
  99. return;
  100. }
  101. state.index = index + 1;
  102. this.schedule(state);
  103. };
  104. /** @deprecated internal use only */ ArrayObservable.prototype._subscribe = function (subscriber) {
  105. var index = 0;
  106. var array = this.array;
  107. var count = array.length;
  108. var scheduler = this.scheduler;
  109. if (scheduler) {
  110. return scheduler.schedule(ArrayObservable.dispatch, 0, {
  111. array: array, index: index, count: count, subscriber: subscriber
  112. });
  113. }
  114. else {
  115. for (var i = 0; i < count && !subscriber.closed; i++) {
  116. subscriber.next(array[i]);
  117. }
  118. subscriber.complete();
  119. }
  120. };
  121. return ArrayObservable;
  122. }(Observable));
  123. //# sourceMappingURL=ArrayObservable.js.map