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 and benlesh committed Sep 6, 2020
1 parent 193e519 commit 47d9e33
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
8 changes: 8 additions & 0 deletions ajax/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "rxjs/ajax",
"typings": "../dist/types/ajax/index.d.ts",
"main": "../dist/cjs/ajax/index.js",
"module": "../dist/esm5/ajax/index.js",
"es2015": "../dist/esm/ajax/index.js",
"sideEffects": false
}
8 changes: 8 additions & 0 deletions fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "rxjs/fetch",
"typings": "../dist/types/fetch/index.d.ts",
"main": "../dist/cjs/fetch/index.js",
"module": "../dist/esm5/fetch/index.js",
"es2015": "../dist/esm/fetch/index.js",
"sideEffects": false
}
8 changes: 8 additions & 0 deletions operators/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "rxjs/operators",
"typings": "../dist/types/operators/index.d.ts",
"main": "../dist/cjs/operators/index.js",
"module": "../dist/esm5/operators/index.js",
"es2015": "../dist/esm/operators/index.js",
"sideEffects": false
}
28 changes: 28 additions & 0 deletions spec/subjects/ReplaySubject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,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 @@ -35,21 +35,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(new ReplayEvent(this._getNow(), value));
this._trimBufferThenGetEvents();

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

Expand Down
8 changes: 8 additions & 0 deletions testing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "rxjs/testing",
"typings": "../dist/types/testing/index.d.ts",
"main": "../dist/cjs/testing/index.js",
"module": "../dist/esm5/testing/index.js",
"es2015": "../dist/esm/testing/index.js",
"sideEffects": false
}
8 changes: 8 additions & 0 deletions webSocket/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "rxjs/webSocket",
"typings": "../dist/types/webSocket/index.d.ts",
"main": "../dist/cjs/webSocket/index.js",
"module": "../dist/esm5/webSocket/index.js",
"es2015": "../dist/esm/webSocket/index.js",
"sideEffects": false
}

0 comments on commit 47d9e33

Please sign in to comment.