UNPKG

4.84 kBJavaScriptView Raw
1"use strict";
2var __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 (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var Subject_1 = require("./Subject");
17var queue_1 = require("./scheduler/queue");
18var Subscription_1 = require("./Subscription");
19var observeOn_1 = require("./operators/observeOn");
20var ObjectUnsubscribedError_1 = require("./util/ObjectUnsubscribedError");
21var SubjectSubscription_1 = require("./SubjectSubscription");
22var ReplaySubject = (function (_super) {
23 __extends(ReplaySubject, _super);
24 function ReplaySubject(bufferSize, windowTime, scheduler) {
25 if (bufferSize === void 0) { bufferSize = Number.POSITIVE_INFINITY; }
26 if (windowTime === void 0) { windowTime = Number.POSITIVE_INFINITY; }
27 var _this = _super.call(this) || this;
28 _this.scheduler = scheduler;
29 _this._events = [];
30 _this._infiniteTimeWindow = false;
31 _this._bufferSize = bufferSize < 1 ? 1 : bufferSize;
32 _this._windowTime = windowTime < 1 ? 1 : windowTime;
33 if (windowTime === Number.POSITIVE_INFINITY) {
34 _this._infiniteTimeWindow = true;
35 _this.next = _this.nextInfiniteTimeWindow;
36 }
37 else {
38 _this.next = _this.nextTimeWindow;
39 }
40 return _this;
41 }
42 ReplaySubject.prototype.nextInfiniteTimeWindow = function (value) {
43 var _events = this._events;
44 _events.push(value);
45 if (_events.length > this._bufferSize) {
46 _events.shift();
47 }
48 _super.prototype.next.call(this, value);
49 };
50 ReplaySubject.prototype.nextTimeWindow = function (value) {
51 this._events.push(new ReplayEvent(this._getNow(), value));
52 this._trimBufferThenGetEvents();
53 _super.prototype.next.call(this, value);
54 };
55 ReplaySubject.prototype._subscribe = function (subscriber) {
56 var _infiniteTimeWindow = this._infiniteTimeWindow;
57 var _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
58 var scheduler = this.scheduler;
59 var len = _events.length;
60 var subscription;
61 if (this.closed) {
62 throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
63 }
64 else if (this.isStopped || this.hasError) {
65 subscription = Subscription_1.Subscription.EMPTY;
66 }
67 else {
68 this.observers.push(subscriber);
69 subscription = new SubjectSubscription_1.SubjectSubscription(this, subscriber);
70 }
71 if (scheduler) {
72 subscriber.add(subscriber = new observeOn_1.ObserveOnSubscriber(subscriber, scheduler));
73 }
74 if (_infiniteTimeWindow) {
75 for (var i = 0; i < len && !subscriber.closed; i++) {
76 subscriber.next(_events[i]);
77 }
78 }
79 else {
80 for (var i = 0; i < len && !subscriber.closed; i++) {
81 subscriber.next(_events[i].value);
82 }
83 }
84 if (this.hasError) {
85 subscriber.error(this.thrownError);
86 }
87 else if (this.isStopped) {
88 subscriber.complete();
89 }
90 return subscription;
91 };
92 ReplaySubject.prototype._getNow = function () {
93 return (this.scheduler || queue_1.queue).now();
94 };
95 ReplaySubject.prototype._trimBufferThenGetEvents = function () {
96 var now = this._getNow();
97 var _bufferSize = this._bufferSize;
98 var _windowTime = this._windowTime;
99 var _events = this._events;
100 var eventsCount = _events.length;
101 var spliceCount = 0;
102 while (spliceCount < eventsCount) {
103 if ((now - _events[spliceCount].time) < _windowTime) {
104 break;
105 }
106 spliceCount++;
107 }
108 if (eventsCount > _bufferSize) {
109 spliceCount = Math.max(spliceCount, eventsCount - _bufferSize);
110 }
111 if (spliceCount > 0) {
112 _events.splice(0, spliceCount);
113 }
114 return _events;
115 };
116 return ReplaySubject;
117}(Subject_1.Subject));
118exports.ReplaySubject = ReplaySubject;
119var ReplayEvent = (function () {
120 function ReplayEvent(time, value) {
121 this.time = time;
122 this.value = value;
123 }
124 return ReplayEvent;
125}());
126//# sourceMappingURL=ReplaySubject.js.map
\No newline at end of file