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