TimerObservable.js 4.5KB

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