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