IntervalObservable.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { isNumeric } from '../util/isNumeric';
  2. import { Observable } from '../Observable';
  3. import { async } from '../scheduler/async';
  4. /**
  5. * We need this JSDoc comment for affecting ESDoc.
  6. * @extends {Ignored}
  7. * @hide true
  8. */
  9. export class IntervalObservable extends Observable {
  10. constructor(period = 0, scheduler = async) {
  11. super();
  12. this.period = period;
  13. this.scheduler = scheduler;
  14. if (!isNumeric(period) || period < 0) {
  15. this.period = 0;
  16. }
  17. if (!scheduler || typeof scheduler.schedule !== 'function') {
  18. this.scheduler = async;
  19. }
  20. }
  21. /**
  22. * Creates an Observable that emits sequential numbers every specified
  23. * interval of time, on a specified IScheduler.
  24. *
  25. * <span class="informal">Emits incremental numbers periodically in time.
  26. * </span>
  27. *
  28. * <img src="./img/interval.png" width="100%">
  29. *
  30. * `interval` returns an Observable that emits an infinite sequence of
  31. * ascending integers, with a constant interval of time of your choosing
  32. * between those emissions. The first emission is not sent immediately, but
  33. * only after the first period has passed. By default, this operator uses the
  34. * `async` IScheduler to provide a notion of time, but you may pass any
  35. * IScheduler to it.
  36. *
  37. * @example <caption>Emits ascending numbers, one every second (1000ms)</caption>
  38. * var numbers = Rx.Observable.interval(1000);
  39. * numbers.subscribe(x => console.log(x));
  40. *
  41. * @see {@link timer}
  42. * @see {@link delay}
  43. *
  44. * @param {number} [period=0] The interval size in milliseconds (by default)
  45. * or the time unit determined by the scheduler's clock.
  46. * @param {Scheduler} [scheduler=async] The IScheduler to use for scheduling
  47. * the emission of values, and providing a notion of "time".
  48. * @return {Observable} An Observable that emits a sequential number each time
  49. * interval.
  50. * @static true
  51. * @name interval
  52. * @owner Observable
  53. */
  54. static create(period = 0, scheduler = async) {
  55. return new IntervalObservable(period, scheduler);
  56. }
  57. static dispatch(state) {
  58. const { index, subscriber, period } = state;
  59. subscriber.next(index);
  60. if (subscriber.closed) {
  61. return;
  62. }
  63. state.index += 1;
  64. this.schedule(state, period);
  65. }
  66. /** @deprecated internal use only */ _subscribe(subscriber) {
  67. const index = 0;
  68. const period = this.period;
  69. const scheduler = this.scheduler;
  70. subscriber.add(scheduler.schedule(IntervalObservable.dispatch, period, {
  71. index, subscriber, period
  72. }));
  73. }
  74. }
  75. //# sourceMappingURL=IntervalObservable.js.map