Skip to content

Commit

Permalink
fix(delay): fix memory leak (#3605)
Browse files Browse the repository at this point in the history
* test(delay): failing test

* fix(delay): fix memory leak
  • Loading branch information
ubnt-michals authored and benlesh committed May 3, 2018
1 parent 8c5d680 commit 96f05b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
38 changes: 35 additions & 3 deletions spec/operators/delay-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as Rx from 'rxjs/Rx';
import { Observable } from 'rxjs';
import { delay, repeatWhen, skip, take, tap } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import * as sinon from 'sinon';
import { expect } from 'chai';
import { hot, cold, expectObservable, expectSubscriptions, time } from '../helpers/marble-testing';

declare const asDiagram: Function;
declare const rxTestScheduler: Rx.TestScheduler;
const Observable = Rx.Observable;
declare const rxTestScheduler: TestScheduler;

/** @test {delay} */
describe('Observable.prototype.delay', () => {
Expand Down Expand Up @@ -143,4 +146,33 @@ describe('Observable.prototype.delay', () => {

expectObservable(result).toBe(expected);
});

it('should unsubscribe scheduled actions after execution', () => {
let subscribeSpy: any = null;
const counts: number[] = [];

const e1 = cold('a|');
const expected = '--a-(a|)';
const duration = time('-|');
const result = e1.pipe(
repeatWhen(notifications => {
const delayed = notifications.pipe(delay(duration, rxTestScheduler));
subscribeSpy = sinon.spy(delayed['source'], 'subscribe');
return delayed;
}),
skip(1),
take(2),
tap({
next() {
const [[subscriber]] = subscribeSpy.args;
counts.push(subscriber._subscriptions.length);
},
complete() {
expect(counts).to.deep.equal([1, 1]);
}
})
);

expectObservable(result).toBe(expected);
});
});
1 change: 1 addition & 0 deletions src/internal/operators/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class DelaySubscriber<T> extends Subscriber<T> {
const delay = Math.max(0, queue[0].time - scheduler.now());
this.schedule(state, delay);
} else {
this.unsubscribe();
source.active = false;
}
}
Expand Down

0 comments on commit 96f05b0

Please sign in to comment.