Front end of the Slack clone application.

ReplaySubject.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 Subject_1 = require('./Subject');
  8. var queue_1 = require('./scheduler/queue');
  9. var Subscription_1 = require('./Subscription');
  10. var observeOn_1 = require('./operators/observeOn');
  11. var ObjectUnsubscribedError_1 = require('./util/ObjectUnsubscribedError');
  12. var SubjectSubscription_1 = require('./SubjectSubscription');
  13. /**
  14. * @class ReplaySubject<T>
  15. */
  16. var ReplaySubject = (function (_super) {
  17. __extends(ReplaySubject, _super);
  18. function ReplaySubject(bufferSize, windowTime, scheduler) {
  19. if (bufferSize === void 0) { bufferSize = Number.POSITIVE_INFINITY; }
  20. if (windowTime === void 0) { windowTime = Number.POSITIVE_INFINITY; }
  21. _super.call(this);
  22. this.scheduler = scheduler;
  23. this._events = [];
  24. this._bufferSize = bufferSize < 1 ? 1 : bufferSize;
  25. this._windowTime = windowTime < 1 ? 1 : windowTime;
  26. }
  27. ReplaySubject.prototype.next = function (value) {
  28. var now = this._getNow();
  29. this._events.push(new ReplayEvent(now, value));
  30. this._trimBufferThenGetEvents();
  31. _super.prototype.next.call(this, value);
  32. };
  33. /** @deprecated internal use only */ ReplaySubject.prototype._subscribe = function (subscriber) {
  34. var _events = this._trimBufferThenGetEvents();
  35. var scheduler = this.scheduler;
  36. var subscription;
  37. if (this.closed) {
  38. throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
  39. }
  40. else if (this.hasError) {
  41. subscription = Subscription_1.Subscription.EMPTY;
  42. }
  43. else if (this.isStopped) {
  44. subscription = Subscription_1.Subscription.EMPTY;
  45. }
  46. else {
  47. this.observers.push(subscriber);
  48. subscription = new SubjectSubscription_1.SubjectSubscription(this, subscriber);
  49. }
  50. if (scheduler) {
  51. subscriber.add(subscriber = new observeOn_1.ObserveOnSubscriber(subscriber, scheduler));
  52. }
  53. var len = _events.length;
  54. for (var i = 0; i < len && !subscriber.closed; i++) {
  55. subscriber.next(_events[i].value);
  56. }
  57. if (this.hasError) {
  58. subscriber.error(this.thrownError);
  59. }
  60. else if (this.isStopped) {
  61. subscriber.complete();
  62. }
  63. return subscription;
  64. };
  65. ReplaySubject.prototype._getNow = function () {
  66. return (this.scheduler || queue_1.queue).now();
  67. };
  68. ReplaySubject.prototype._trimBufferThenGetEvents = function () {
  69. var now = this._getNow();
  70. var _bufferSize = this._bufferSize;
  71. var _windowTime = this._windowTime;
  72. var _events = this._events;
  73. var eventsCount = _events.length;
  74. var spliceCount = 0;
  75. // Trim events that fall out of the time window.
  76. // Start at the front of the list. Break early once
  77. // we encounter an event that falls within the window.
  78. while (spliceCount < eventsCount) {
  79. if ((now - _events[spliceCount].time) < _windowTime) {
  80. break;
  81. }
  82. spliceCount++;
  83. }
  84. if (eventsCount > _bufferSize) {
  85. spliceCount = Math.max(spliceCount, eventsCount - _bufferSize);
  86. }
  87. if (spliceCount > 0) {
  88. _events.splice(0, spliceCount);
  89. }
  90. return _events;
  91. };
  92. return ReplaySubject;
  93. }(Subject_1.Subject));
  94. exports.ReplaySubject = ReplaySubject;
  95. var ReplayEvent = (function () {
  96. function ReplayEvent(time, value) {
  97. this.time = time;
  98. this.value = value;
  99. }
  100. return ReplayEvent;
  101. }());
  102. //# sourceMappingURL=ReplaySubject.js.map