TimerObservable.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { isNumeric } from '../util/isNumeric';
  2. import { Observable } from '../Observable';
  3. import { async } from '../scheduler/async';
  4. import { isScheduler } from '../util/isScheduler';
  5. import { isDate } from '../util/isDate';
  6. /**
  7. * We need this JSDoc comment for affecting ESDoc.
  8. * @extends {Ignored}
  9. * @hide true
  10. */
  11. export class TimerObservable extends Observable {
  12. constructor(dueTime = 0, period, scheduler) {
  13. super();
  14. this.period = -1;
  15. this.dueTime = 0;
  16. if (isNumeric(period)) {
  17. this.period = Number(period) < 1 && 1 || Number(period);
  18. }
  19. else if (isScheduler(period)) {
  20. scheduler = period;
  21. }
  22. if (!isScheduler(scheduler)) {
  23. scheduler = async;
  24. }
  25. this.scheduler = scheduler;
  26. this.dueTime = isDate(dueTime) ?
  27. (+dueTime - this.scheduler.now()) :
  28. dueTime;
  29. }
  30. /**
  31. * Creates an Observable that starts emitting after an `initialDelay` and
  32. * emits ever increasing numbers after each `period` of time thereafter.
  33. *
  34. * <span class="informal">Its like {@link interval}, but you can specify when
  35. * should the emissions start.</span>
  36. *
  37. * <img src="./img/timer.png" width="100%">
  38. *
  39. * `timer` returns an Observable that emits an infinite sequence of ascending
  40. * integers, with a constant interval of time, `period` of your choosing
  41. * between those emissions. The first emission happens after the specified
  42. * `initialDelay`. The initial delay may be a {@link Date}. By default, this
  43. * operator uses the `async` IScheduler to provide a notion of time, but you
  44. * may pass any IScheduler to it. If `period` is not specified, the output
  45. * Observable emits only one value, `0`. Otherwise, it emits an infinite
  46. * sequence.
  47. *
  48. * @example <caption>Emits ascending numbers, one every second (1000ms), starting after 3 seconds</caption>
  49. * var numbers = Rx.Observable.timer(3000, 1000);
  50. * numbers.subscribe(x => console.log(x));
  51. *
  52. * @example <caption>Emits one number after five seconds</caption>
  53. * var numbers = Rx.Observable.timer(5000);
  54. * numbers.subscribe(x => console.log(x));
  55. *
  56. * @see {@link interval}
  57. * @see {@link delay}
  58. *
  59. * @param {number|Date} initialDelay The initial delay time to wait before
  60. * emitting the first value of `0`.
  61. * @param {number} [period] The period of time between emissions of the
  62. * subsequent numbers.
  63. * @param {Scheduler} [scheduler=async] The IScheduler to use for scheduling
  64. * the emission of values, and providing a notion of "time".
  65. * @return {Observable} An Observable that emits a `0` after the
  66. * `initialDelay` and ever increasing numbers after each `period` of time
  67. * thereafter.
  68. * @static true
  69. * @name timer
  70. * @owner Observable
  71. */
  72. static create(initialDelay = 0, period, scheduler) {
  73. return new TimerObservable(initialDelay, period, scheduler);
  74. }
  75. static dispatch(state) {
  76. const { index, period, subscriber } = state;
  77. const action = this;
  78. subscriber.next(index);
  79. if (subscriber.closed) {
  80. return;
  81. }
  82. else if (period === -1) {
  83. return subscriber.complete();
  84. }
  85. state.index = index + 1;
  86. action.schedule(state, period);
  87. }
  88. /** @deprecated internal use only */ _subscribe(subscriber) {
  89. const index = 0;
  90. const { period, dueTime, scheduler } = this;
  91. return scheduler.schedule(TimerObservable.dispatch, dueTime, {
  92. index, period, subscriber
  93. });
  94. }
  95. }
  96. //# sourceMappingURL=TimerObservable.js.map