UNPKG

4.96 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 if (!this.isStopped) {
44 var _events = this._events;
45 _events.push(value);
46 if (_events.length > this._bufferSize) {
47 _events.shift();
48 }
49 }
50 _super.prototype.next.call(this, value);
51 };
52 ReplaySubject.prototype.nextTimeWindow = function (value) {
53 if (!this.isStopped) {
54 this._events.push(new ReplayEvent(this._getNow(), value));
55 this._trimBufferThenGetEvents();
56 }
57 _super.prototype.next.call(this, value);
58 };
59 ReplaySubject.prototype._subscribe = function (subscriber) {
60 var _infiniteTimeWindow = this._infiniteTimeWindow;
61 var _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
62 var scheduler = this.scheduler;
63 var len = _events.length;
64 var subscription;
65 if (this.closed) {
66 throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
67 }
68 else if (this.isStopped || this.hasError) {
69 subscription = Subscription_1.Subscription.EMPTY;
70 }
71 else {
72 this.observers.push(subscriber);
73 subscription = new SubjectSubscription_1.SubjectSubscription(this, subscriber);
74 }
75 if (scheduler) {
76 subscriber.add(subscriber = new observeOn_1.ObserveOnSubscriber(subscriber, scheduler));
77 }
78 if (_infiniteTimeWindow) {
79 for (var i = 0; i < len && !subscriber.closed; i++) {
80 subscriber.next(_events[i]);
81 }
82 }
83 else {
84 for (var i = 0; i < len && !subscriber.closed; i++) {
85 subscriber.next(_events[i].value);
86 }
87 }
88 if (this.hasError) {
89 subscriber.error(this.thrownError);
90 }
91 else if (this.isStopped) {
92 subscriber.complete();
93 }
94 return subscription;
95 };
96 ReplaySubject.prototype._getNow = function () {
97 return (this.scheduler || queue_1.queue).now();
98 };
99 ReplaySubject.prototype._trimBufferThenGetEvents = function () {
100 var now = this._getNow();
101 var _bufferSize = this._bufferSize;
102 var _windowTime = this._windowTime;
103 var _events = this._events;
104 var eventsCount = _events.length;
105 var spliceCount = 0;
106 while (spliceCount < eventsCount) {
107 if ((now - _events[spliceCount].time) < _windowTime) {
108 break;
109 }
110 spliceCount++;
111 }
112 if (eventsCount > _bufferSize) {
113 spliceCount = Math.max(spliceCount, eventsCount - _bufferSize);
114 }
115 if (spliceCount > 0) {
116 _events.splice(0, spliceCount);
117 }
118 return _events;
119 };
120 return ReplaySubject;
121}(Subject_1.Subject));
122exports.ReplaySubject = ReplaySubject;
123var ReplayEvent = (function () {
124 function ReplayEvent(time, value) {
125 this.time = time;
126 this.value = value;
127 }
128 return ReplayEvent;
129}());
130//# sourceMappingURL=ReplaySubject.js.map
\No newline at end of file