Front end of the Slack clone application.

delay.js 5.2KB

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