IntervalObservable.js 3.5 KB

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