IntervalObservable.js 3.6 KB

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