RangeObservable.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** PURE_IMPORTS_START .._Observable 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. /**
  11. * We need this JSDoc comment for affecting ESDoc.
  12. * @extends {Ignored}
  13. * @hide true
  14. */
  15. export var RangeObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  16. __extends(RangeObservable, _super);
  17. function RangeObservable(start, count, scheduler) {
  18. _super.call(this);
  19. this.start = start;
  20. this._count = count;
  21. this.scheduler = scheduler;
  22. }
  23. /**
  24. * Creates an Observable that emits a sequence of numbers within a specified
  25. * range.
  26. *
  27. * <span class="informal">Emits a sequence of numbers in a range.</span>
  28. *
  29. * <img src="./img/range.png" width="100%">
  30. *
  31. * `range` operator emits a range of sequential integers, in order, where you
  32. * select the `start` of the range and its `length`. By default, uses no
  33. * IScheduler and just delivers the notifications synchronously, but may use
  34. * an optional IScheduler to regulate those deliveries.
  35. *
  36. * @example <caption>Emits the numbers 1 to 10</caption>
  37. * var numbers = Rx.Observable.range(1, 10);
  38. * numbers.subscribe(x => console.log(x));
  39. *
  40. * @see {@link timer}
  41. * @see {@link interval}
  42. *
  43. * @param {number} [start=0] The value of the first integer in the sequence.
  44. * @param {number} [count=0] The number of sequential integers to generate.
  45. * @param {Scheduler} [scheduler] A {@link IScheduler} to use for scheduling
  46. * the emissions of the notifications.
  47. * @return {Observable} An Observable of numbers that emits a finite range of
  48. * sequential integers.
  49. * @static true
  50. * @name range
  51. * @owner Observable
  52. */
  53. RangeObservable.create = function (start, count, scheduler) {
  54. if (start === void 0) {
  55. start = 0;
  56. }
  57. if (count === void 0) {
  58. count = 0;
  59. }
  60. return new RangeObservable(start, count, scheduler);
  61. };
  62. RangeObservable.dispatch = function (state) {
  63. var start = state.start, index = state.index, count = state.count, subscriber = state.subscriber;
  64. if (index >= count) {
  65. subscriber.complete();
  66. return;
  67. }
  68. subscriber.next(start);
  69. if (subscriber.closed) {
  70. return;
  71. }
  72. state.index = index + 1;
  73. state.start = start + 1;
  74. this.schedule(state);
  75. };
  76. /** @deprecated internal use only */ RangeObservable.prototype._subscribe = function (subscriber) {
  77. var index = 0;
  78. var start = this.start;
  79. var count = this._count;
  80. var scheduler = this.scheduler;
  81. if (scheduler) {
  82. return scheduler.schedule(RangeObservable.dispatch, 0, {
  83. index: index, count: count, start: start, subscriber: subscriber
  84. });
  85. }
  86. else {
  87. do {
  88. if (index++ >= count) {
  89. subscriber.complete();
  90. break;
  91. }
  92. subscriber.next(start++);
  93. if (subscriber.closed) {
  94. break;
  95. }
  96. } while (true);
  97. }
  98. };
  99. return RangeObservable;
  100. }(Observable));
  101. //# sourceMappingURL=RangeObservable.js.map