ArrayObservable.js 3.7 KB

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