RangeObservable.js 2.8 KB

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