Skip to content

Commit

Permalink
perf(ReplaySubject): fix memory leak of growing buffer
Browse files Browse the repository at this point in the history
Fix ReplaySubject to attempt to trim its own buffer of events whenever a new event is fed through
next().

Resolves issue #578.
  • Loading branch information
staltz authored and kwonoj committed Nov 30, 2015
1 parent 280f7ed commit 0a73b4d
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/subjects/ReplaySubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export class ReplaySubject<T> extends Subject<T> {
_next(value: T): void {
const now = this._getNow();
this.events.push(new ReplayEvent(now, value));
this._trimBufferThenGetEvents(now);
super._next(value);
}

_subscribe(subscriber: Subscriber<any>): Subscription<T> {
const events = this._getEvents(this._getNow());
const events = this._trimBufferThenGetEvents(this._getNow());
let index = -1;
const len = events.length;
while (!subscriber.isUnsubscribed && ++index < len) {
Expand All @@ -39,7 +40,7 @@ export class ReplaySubject<T> extends Subject<T> {
return (this.scheduler || immediate).now();
}

private _getEvents(now): ReplayEvent<T>[] {
private _trimBufferThenGetEvents(now): ReplayEvent<T>[] {
const bufferSize = this.bufferSize;
const _windowTime = this._windowTime;
const events = this.events;
Expand Down

0 comments on commit 0a73b4d

Please sign in to comment.