From 4d4550e82e43dcdb1fbf2f3da562c1fa8159ef57 Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Sat, 5 Sep 2020 21:48:26 +1000 Subject: [PATCH 1/3] test(ReplaySubject): add failing tests --- spec/subjects/ReplaySubject-spec.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spec/subjects/ReplaySubject-spec.ts b/spec/subjects/ReplaySubject-spec.ts index 834706b017..30fe4697f1 100644 --- a/spec/subjects/ReplaySubject-spec.ts +++ b/spec/subjects/ReplaySubject-spec.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import { values } from 'lodash'; import { ReplaySubject, Subject, of } from 'rxjs'; import { mergeMapTo, tap } from 'rxjs/operators'; import { TestScheduler } from 'rxjs/testing'; @@ -351,4 +352,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(); + 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(); + 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']); + }); }); From 9f4dc8b54ff636953d6b11392487d410c18b323f Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Sat, 5 Sep 2020 21:48:59 +1000 Subject: [PATCH 2/3] fix(ReplaySubject): don't buffer next if stopped --- src/internal/ReplaySubject.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/internal/ReplaySubject.ts b/src/internal/ReplaySubject.ts index 63f35c3fc7..18214a8b34 100644 --- a/src/internal/ReplaySubject.ts +++ b/src/internal/ReplaySubject.ts @@ -65,21 +65,23 @@ export class ReplaySubject extends Subject { } 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); } From ee9d979d852b94aa5708abd89d10dd0d8f82cddf Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Sat, 5 Sep 2020 21:57:28 +1000 Subject: [PATCH 3/3] chore: remove VSCode-added lodash import Oh, FFS, now I've done it. --- spec/subjects/ReplaySubject-spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/subjects/ReplaySubject-spec.ts b/spec/subjects/ReplaySubject-spec.ts index 30fe4697f1..ba611ad649 100644 --- a/spec/subjects/ReplaySubject-spec.ts +++ b/spec/subjects/ReplaySubject-spec.ts @@ -1,5 +1,4 @@ import { expect } from 'chai'; -import { values } from 'lodash'; import { ReplaySubject, Subject, of } from 'rxjs'; import { mergeMapTo, tap } from 'rxjs/operators'; import { TestScheduler } from 'rxjs/testing';