ReplaySubject.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { __extends } from "tslib";
  2. import { Subject } from './Subject';
  3. import { dateTimestampProvider } from './scheduler/dateTimestampProvider';
  4. var ReplaySubject = (function (_super) {
  5. __extends(ReplaySubject, _super);
  6. function ReplaySubject(_bufferSize, _windowTime, _timestampProvider) {
  7. if (_bufferSize === void 0) { _bufferSize = Infinity; }
  8. if (_windowTime === void 0) { _windowTime = Infinity; }
  9. if (_timestampProvider === void 0) { _timestampProvider = dateTimestampProvider; }
  10. var _this = _super.call(this) || this;
  11. _this._bufferSize = _bufferSize;
  12. _this._windowTime = _windowTime;
  13. _this._timestampProvider = _timestampProvider;
  14. _this._buffer = [];
  15. _this._infiniteTimeWindow = true;
  16. _this._infiniteTimeWindow = _windowTime === Infinity;
  17. _this._bufferSize = Math.max(1, _bufferSize);
  18. _this._windowTime = Math.max(1, _windowTime);
  19. return _this;
  20. }
  21. ReplaySubject.prototype.next = function (value) {
  22. var _a = this, isStopped = _a.isStopped, _buffer = _a._buffer, _infiniteTimeWindow = _a._infiniteTimeWindow, _timestampProvider = _a._timestampProvider, _windowTime = _a._windowTime;
  23. if (!isStopped) {
  24. _buffer.push(value);
  25. !_infiniteTimeWindow && _buffer.push(_timestampProvider.now() + _windowTime);
  26. }
  27. this._trimBuffer();
  28. _super.prototype.next.call(this, value);
  29. };
  30. ReplaySubject.prototype._subscribe = function (subscriber) {
  31. this._throwIfClosed();
  32. this._trimBuffer();
  33. var subscription = this._innerSubscribe(subscriber);
  34. var _a = this, _infiniteTimeWindow = _a._infiniteTimeWindow, _buffer = _a._buffer;
  35. var copy = _buffer.slice();
  36. for (var i = 0; i < copy.length && !subscriber.closed; i += _infiniteTimeWindow ? 1 : 2) {
  37. subscriber.next(copy[i]);
  38. }
  39. this._checkFinalizedStatuses(subscriber);
  40. return subscription;
  41. };
  42. ReplaySubject.prototype._trimBuffer = function () {
  43. var _a = this, _bufferSize = _a._bufferSize, _timestampProvider = _a._timestampProvider, _buffer = _a._buffer, _infiniteTimeWindow = _a._infiniteTimeWindow;
  44. var adjustedBufferSize = (_infiniteTimeWindow ? 1 : 2) * _bufferSize;
  45. _bufferSize < Infinity && adjustedBufferSize < _buffer.length && _buffer.splice(0, _buffer.length - adjustedBufferSize);
  46. if (!_infiniteTimeWindow) {
  47. var now = _timestampProvider.now();
  48. var last = 0;
  49. for (var i = 1; i < _buffer.length && _buffer[i] <= now; i += 2) {
  50. last = i;
  51. }
  52. last && _buffer.splice(0, last + 1);
  53. }
  54. };
  55. return ReplaySubject;
  56. }(Subject));
  57. export { ReplaySubject };
  58. //# sourceMappingURL=ReplaySubject.js.map