Skip to content

Commit

Permalink
fix(ReplaySubject): don't buffer next if stopped (#5696)
Browse files Browse the repository at this point in the history
* test(ReplaySubject): add failing tests

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

* chore: remove VSCode-added lodash import

Oh, FFS, now I've done it.
  • Loading branch information
cartant authored Sep 6, 2020
1 parent 0e0dbe3 commit a08232b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
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

0 comments on commit a08232b

Please sign in to comment.