TimerObservable.js 4.4 KB

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