Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ReplaySubject): don't buffer next if stopped #5696

Merged
merged 3 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions spec/subjects/ReplaySubject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,32 @@ describe('ReplaySubject', () => {

expect(results).to.deep.equal([3, 4, 5, 'done']);
});

it('should not buffer nexted values after complete', () => {
const results: (number | string)[] = [];
const subject = new ReplaySubject<number>();
subject.next(1);
subject.next(2);
subject.complete();
subject.next(3);
subject.subscribe({
next: value => results.push(value),
complete: () => results.push('C'),
});
expect(results).to.deep.equal([1, 2, 'C']);
});

it('should not buffer nexted values after error', () => {
const results: (number | string)[] = [];
const subject = new ReplaySubject<number>();
subject.next(1);
subject.next(2);
subject.error(new Error('Boom!'));
subject.next(3);
subject.subscribe({
next: value => results.push(value),
error: () => results.push('E'),
});
expect(results).to.deep.equal([1, 2, 'E']);
});
});
22 changes: 12 additions & 10 deletions src/internal/ReplaySubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,23 @@ export class ReplaySubject<T> extends Subject<T> {
}

private nextInfiniteTimeWindow(value: T): void {
const _events = this._events;
_events.push(value);
// Since this method is invoked in every next() call than the buffer
// can overgrow the max size only by one item
if (_events.length > this._bufferSize) {
_events.shift();
if (!this.isStopped) {
const _events = this._events;
_events.push(value);
// Since this method is invoked in every next() call than the buffer
// can overgrow the max size only by one item
if (_events.length > this._bufferSize) {
_events.shift();
}
}

super.next(value);
}

private nextTimeWindow(value: T): void {
this._events.push({ time: this._getNow(), value });
this._trimBufferThenGetEvents();

if (!this.isStopped) {
this._events.push({ time: this._getNow(), value });
this._trimBufferThenGetEvents();
}
super.next(value);
}

Expand Down