ReplaySubject.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.ReplaySubject = void 0;
  19. var Subject_1 = require("./Subject");
  20. var dateTimestampProvider_1 = require("./scheduler/dateTimestampProvider");
  21. var ReplaySubject = (function (_super) {
  22. __extends(ReplaySubject, _super);
  23. function ReplaySubject(_bufferSize, _windowTime, _timestampProvider) {
  24. if (_bufferSize === void 0) { _bufferSize = Infinity; }
  25. if (_windowTime === void 0) { _windowTime = Infinity; }
  26. if (_timestampProvider === void 0) { _timestampProvider = dateTimestampProvider_1.dateTimestampProvider; }
  27. var _this = _super.call(this) || this;
  28. _this._bufferSize = _bufferSize;
  29. _this._windowTime = _windowTime;
  30. _this._timestampProvider = _timestampProvider;
  31. _this._buffer = [];
  32. _this._infiniteTimeWindow = true;
  33. _this._infiniteTimeWindow = _windowTime === Infinity;
  34. _this._bufferSize = Math.max(1, _bufferSize);
  35. _this._windowTime = Math.max(1, _windowTime);
  36. return _this;
  37. }
  38. ReplaySubject.prototype.next = function (value) {
  39. var _a = this, isStopped = _a.isStopped, _buffer = _a._buffer, _infiniteTimeWindow = _a._infiniteTimeWindow, _timestampProvider = _a._timestampProvider, _windowTime = _a._windowTime;
  40. if (!isStopped) {
  41. _buffer.push(value);
  42. !_infiniteTimeWindow && _buffer.push(_timestampProvider.now() + _windowTime);
  43. }
  44. this._trimBuffer();
  45. _super.prototype.next.call(this, value);
  46. };
  47. ReplaySubject.prototype._subscribe = function (subscriber) {
  48. this._throwIfClosed();
  49. this._trimBuffer();
  50. var subscription = this._innerSubscribe(subscriber);
  51. var _a = this, _infiniteTimeWindow = _a._infiniteTimeWindow, _buffer = _a._buffer;
  52. var copy = _buffer.slice();
  53. for (var i = 0; i < copy.length && !subscriber.closed; i += _infiniteTimeWindow ? 1 : 2) {
  54. subscriber.next(copy[i]);
  55. }
  56. this._checkFinalizedStatuses(subscriber);
  57. return subscription;
  58. };
  59. ReplaySubject.prototype._trimBuffer = function () {
  60. var _a = this, _bufferSize = _a._bufferSize, _timestampProvider = _a._timestampProvider, _buffer = _a._buffer, _infiniteTimeWindow = _a._infiniteTimeWindow;
  61. var adjustedBufferSize = (_infiniteTimeWindow ? 1 : 2) * _bufferSize;
  62. _bufferSize < Infinity && adjustedBufferSize < _buffer.length && _buffer.splice(0, _buffer.length - adjustedBufferSize);
  63. if (!_infiniteTimeWindow) {
  64. var now = _timestampProvider.now();
  65. var last = 0;
  66. for (var i = 1; i < _buffer.length && _buffer[i] <= now; i += 2) {
  67. last = i;
  68. }
  69. last && _buffer.splice(0, last + 1);
  70. }
  71. };
  72. return ReplaySubject;
  73. }(Subject_1.Subject));
  74. exports.ReplaySubject = ReplaySubject;
  75. //# sourceMappingURL=ReplaySubject.js.map