123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /** PURE_IMPORTS_START .._scheduler_async,.._util_isDate,.._Subscriber,.._Notification 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 { async } from '../scheduler/async';
  10. import { isDate } from '../util/isDate';
  11. import { Subscriber } from '../Subscriber';
  12. import { Notification } from '../Notification';
  13. /**
  14. * Delays the emission of items from the source Observable by a given timeout or
  15. * until a given Date.
  16. *
  17. * <span class="informal">Time shifts each item by some specified amount of
  18. * milliseconds.</span>
  19. *
  20. * <img src="./img/delay.png" width="100%">
  21. *
  22. * If the delay argument is a Number, this operator time shifts the source
  23. * Observable by that amount of time expressed in milliseconds. The relative
  24. * time intervals between the values are preserved.
  25. *
  26. * If the delay argument is a Date, this operator time shifts the start of the
  27. * Observable execution until the given date occurs.
  28. *
  29. * @example <caption>Delay each click by one second</caption>
  30. * var clicks = Rx.Observable.fromEvent(document, 'click');
  31. * var delayedClicks = clicks.delay(1000); // each click emitted after 1 second
  32. * delayedClicks.subscribe(x => console.log(x));
  33. *
  34. * @example <caption>Delay all clicks until a future date happens</caption>
  35. * var clicks = Rx.Observable.fromEvent(document, 'click');
  36. * var date = new Date('March 15, 2050 12:00:00'); // in the future
  37. * var delayedClicks = clicks.delay(date); // click emitted only after that date
  38. * delayedClicks.subscribe(x => console.log(x));
  39. *
  40. * @see {@link debounceTime}
  41. * @see {@link delayWhen}
  42. *
  43. * @param {number|Date} delay The delay duration in milliseconds (a `number`) or
  44. * a `Date` until which the emission of the source items is delayed.
  45. * @param {Scheduler} [scheduler=async] The IScheduler to use for
  46. * managing the timers that handle the time-shift for each item.
  47. * @return {Observable} An Observable that delays the emissions of the source
  48. * Observable by the specified timeout or Date.
  49. * @method delay
  50. * @owner Observable
  51. */
  52. export function delay(delay, scheduler) {
  53. if (scheduler === void 0) {
  54. scheduler = async;
  55. }
  56. var absoluteDelay = isDate(delay);
  57. var delayFor = absoluteDelay ? (+delay - scheduler.now()) : Math.abs(delay);
  58. return function (source) { return source.lift(new DelayOperator(delayFor, scheduler)); };
  59. }
  60. var DelayOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  61. function DelayOperator(delay, scheduler) {
  62. this.delay = delay;
  63. this.scheduler = scheduler;
  64. }
  65. DelayOperator.prototype.call = function (subscriber, source) {
  66. return source.subscribe(new DelaySubscriber(subscriber, this.delay, this.scheduler));
  67. };
  68. return DelayOperator;
  69. }());
  70. /**
  71. * We need this JSDoc comment for affecting ESDoc.
  72. * @ignore
  73. * @extends {Ignored}
  74. */
  75. var DelaySubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  76. __extends(DelaySubscriber, _super);
  77. function DelaySubscriber(destination, delay, scheduler) {
  78. _super.call(this, destination);
  79. this.delay = delay;
  80. this.scheduler = scheduler;
  81. this.queue = [];
  82. this.active = false;
  83. this.errored = false;
  84. }
  85. DelaySubscriber.dispatch = function (state) {
  86. var source = state.source;
  87. var queue = source.queue;
  88. var scheduler = state.scheduler;
  89. var destination = state.destination;
  90. while (queue.length > 0 && (queue[0].time - scheduler.now()) <= 0) {
  91. queue.shift().notification.observe(destination);
  92. }
  93. if (queue.length > 0) {
  94. var delay_1 = Math.max(0, queue[0].time - scheduler.now());
  95. this.schedule(state, delay_1);
  96. }
  97. else {
  98. this.unsubscribe();
  99. source.active = false;
  100. }
  101. };
  102. DelaySubscriber.prototype._schedule = function (scheduler) {
  103. this.active = true;
  104. this.add(scheduler.schedule(DelaySubscriber.dispatch, this.delay, {
  105. source: this, destination: this.destination, scheduler: scheduler
  106. }));
  107. };
  108. DelaySubscriber.prototype.scheduleNotification = function (notification) {
  109. if (this.errored === true) {
  110. return;
  111. }
  112. var scheduler = this.scheduler;
  113. var message = new DelayMessage(scheduler.now() + this.delay, notification);
  114. this.queue.push(message);
  115. if (this.active === false) {
  116. this._schedule(scheduler);
  117. }
  118. };
  119. DelaySubscriber.prototype._next = function (value) {
  120. this.scheduleNotification(Notification.createNext(value));
  121. };
  122. DelaySubscriber.prototype._error = function (err) {
  123. this.errored = true;
  124. this.queue = [];
  125. this.destination.error(err);
  126. };
  127. DelaySubscriber.prototype._complete = function () {
  128. this.scheduleNotification(Notification.createComplete());
  129. };
  130. return DelaySubscriber;
  131. }(Subscriber));
  132. var DelayMessage = /*@__PURE__*/ (/*@__PURE__*/ function () {
  133. function DelayMessage(time, notification) {
  134. this.time = time;
  135. this.notification = notification;
  136. }
  137. return DelayMessage;
  138. }());
  139. //# sourceMappingURL=delay.js.map