1 | import { Subject } from './Subject';
|
---|
2 | import { queue } from './scheduler/queue';
|
---|
3 | import { Subscription } from './Subscription';
|
---|
4 | import { ObserveOnSubscriber } from './operators/observeOn';
|
---|
5 | import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
|
---|
6 | import { SubjectSubscription } from './SubjectSubscription';
|
---|
7 | export class ReplaySubject extends Subject {
|
---|
8 | constructor(bufferSize = Number.POSITIVE_INFINITY, windowTime = Number.POSITIVE_INFINITY, scheduler) {
|
---|
9 | super();
|
---|
10 | this.scheduler = scheduler;
|
---|
11 | this._events = [];
|
---|
12 | this._infiniteTimeWindow = false;
|
---|
13 | this._bufferSize = bufferSize < 1 ? 1 : bufferSize;
|
---|
14 | this._windowTime = windowTime < 1 ? 1 : windowTime;
|
---|
15 | if (windowTime === Number.POSITIVE_INFINITY) {
|
---|
16 | this._infiniteTimeWindow = true;
|
---|
17 | this.next = this.nextInfiniteTimeWindow;
|
---|
18 | }
|
---|
19 | else {
|
---|
20 | this.next = this.nextTimeWindow;
|
---|
21 | }
|
---|
22 | }
|
---|
23 | nextInfiniteTimeWindow(value) {
|
---|
24 | if (!this.isStopped) {
|
---|
25 | const _events = this._events;
|
---|
26 | _events.push(value);
|
---|
27 | if (_events.length > this._bufferSize) {
|
---|
28 | _events.shift();
|
---|
29 | }
|
---|
30 | }
|
---|
31 | super.next(value);
|
---|
32 | }
|
---|
33 | nextTimeWindow(value) {
|
---|
34 | if (!this.isStopped) {
|
---|
35 | this._events.push(new ReplayEvent(this._getNow(), value));
|
---|
36 | this._trimBufferThenGetEvents();
|
---|
37 | }
|
---|
38 | super.next(value);
|
---|
39 | }
|
---|
40 | _subscribe(subscriber) {
|
---|
41 | const _infiniteTimeWindow = this._infiniteTimeWindow;
|
---|
42 | const _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
|
---|
43 | const scheduler = this.scheduler;
|
---|
44 | const len = _events.length;
|
---|
45 | let subscription;
|
---|
46 | if (this.closed) {
|
---|
47 | throw new ObjectUnsubscribedError();
|
---|
48 | }
|
---|
49 | else if (this.isStopped || this.hasError) {
|
---|
50 | subscription = Subscription.EMPTY;
|
---|
51 | }
|
---|
52 | else {
|
---|
53 | this.observers.push(subscriber);
|
---|
54 | subscription = new SubjectSubscription(this, subscriber);
|
---|
55 | }
|
---|
56 | if (scheduler) {
|
---|
57 | subscriber.add(subscriber = new ObserveOnSubscriber(subscriber, scheduler));
|
---|
58 | }
|
---|
59 | if (_infiniteTimeWindow) {
|
---|
60 | for (let i = 0; i < len && !subscriber.closed; i++) {
|
---|
61 | subscriber.next(_events[i]);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | else {
|
---|
65 | for (let i = 0; i < len && !subscriber.closed; i++) {
|
---|
66 | subscriber.next(_events[i].value);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | if (this.hasError) {
|
---|
70 | subscriber.error(this.thrownError);
|
---|
71 | }
|
---|
72 | else if (this.isStopped) {
|
---|
73 | subscriber.complete();
|
---|
74 | }
|
---|
75 | return subscription;
|
---|
76 | }
|
---|
77 | _getNow() {
|
---|
78 | return (this.scheduler || queue).now();
|
---|
79 | }
|
---|
80 | _trimBufferThenGetEvents() {
|
---|
81 | const now = this._getNow();
|
---|
82 | const _bufferSize = this._bufferSize;
|
---|
83 | const _windowTime = this._windowTime;
|
---|
84 | const _events = this._events;
|
---|
85 | const eventsCount = _events.length;
|
---|
86 | let spliceCount = 0;
|
---|
87 | while (spliceCount < eventsCount) {
|
---|
88 | if ((now - _events[spliceCount].time) < _windowTime) {
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | spliceCount++;
|
---|
92 | }
|
---|
93 | if (eventsCount > _bufferSize) {
|
---|
94 | spliceCount = Math.max(spliceCount, eventsCount - _bufferSize);
|
---|
95 | }
|
---|
96 | if (spliceCount > 0) {
|
---|
97 | _events.splice(0, spliceCount);
|
---|
98 | }
|
---|
99 | return _events;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | class ReplayEvent {
|
---|
103 | constructor(time, value) {
|
---|
104 | this.time = time;
|
---|
105 | this.value = value;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | //# sourceMappingURL=ReplaySubject.js.map |
---|