Skip to content

Commit

Permalink
fix(Subscriber): handle memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Sagan - Yieldbroker committed Dec 15, 2020
1 parent 8c13e0a commit a6d0164
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions spec/operators/bufferWhen-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { of, EMPTY, Observable } from 'rxjs';
import { bufferWhen, mergeMap, takeWhile, take } from 'rxjs/operators';
import { of, throwError } from 'rxjs';
import { bufferWhen, mergeMap, takeWhile } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';

Expand Down Expand Up @@ -198,7 +198,7 @@ describe('bufferWhen operator', () => {
const result = e1.pipe(
bufferWhen(() => {
if (i === 1) {
throw 'error';
return throwError(() => 'error');
}
return closings[i++];
})
Expand Down
11 changes: 11 additions & 0 deletions src/internal/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
if (!this.closed) {
this.isStopped = true;
super.unsubscribe();
this.destination = NOOP_OBSERVER;
}
}

Expand Down Expand Up @@ -210,3 +211,13 @@ export const EMPTY_OBSERVER: Readonly<Observer<any>> & { closed: true } = {
error: defaultErrorHandler,
complete: noop,
};

/**
* The observer used as a stub for subscriptions that have already been closed.
*/
export const NOOP_OBSERVER: Readonly<Observer<any>> & { closed: true } = {
closed: true,
next: noop,
error: noop,
complete: noop,
};
1 change: 1 addition & 0 deletions src/internal/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class Subscription implements SubscriptionLike {
}

const { initialTeardown } = this;
this.initialTeardown = undefined;
if (isFunction(initialTeardown)) {
try {
initialTeardown();
Expand Down

0 comments on commit a6d0164

Please sign in to comment.