Skip to content

Commit

Permalink
fix: unsubscribe all observables when the component is destroyed
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
Matt Lewis committed Nov 19, 2016
1 parent 178f09b commit d5ea756
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/resizable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
Input,
EventEmitter,
ContentChildren,
QueryList
QueryList,
OnDestroy
} from '@angular/core';
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';
Expand Down Expand Up @@ -226,7 +227,7 @@ export class ResizeHandle {
@Directive({
selector: '[mwlResizable]'
})
export class Resizable implements OnInit, AfterViewInit {
export class Resizable implements OnInit, OnDestroy, AfterViewInit {

/**
* A function that will be called before each resize event. Return `true` to allow the resize event to propagate or `false` to cancel it
Expand Down Expand Up @@ -474,6 +475,15 @@ export class Resizable implements OnInit, AfterViewInit {
});
}

/**
* @private
*/
ngOnDestroy(): void {
this.mousedown.complete();
this.mouseup.complete();
this.mousemove.complete();
}

/**
* @private
*/
Expand Down
33 changes: 33 additions & 0 deletions test/resizable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,4 +972,37 @@ describe('resizable directive', () => {

});

it('should end the mouseup observable when the component is destroyed', () => {

const fixture: ComponentFixture<TestCmp> = createComponent();
fixture.detectChanges();
const onComplete: sinon.SinonSpy = sinon.spy();
fixture.componentInstance.resizable.mouseup.subscribe(() => '', () => '', onComplete);
fixture.destroy();
expect(onComplete).to.have.been.calledOnce;

});

it('should end the mousedown observable when the component is destroyed', () => {

const fixture: ComponentFixture<TestCmp> = createComponent();
fixture.detectChanges();
const onComplete: sinon.SinonSpy = sinon.spy();
fixture.componentInstance.resizable.mousedown.subscribe(() => '', () => '', onComplete);
fixture.destroy();
expect(onComplete).to.have.been.calledOnce;

});

it('should end the mousemove observable when the component is destroyed', () => {

const fixture: ComponentFixture<TestCmp> = createComponent();
fixture.detectChanges();
const onComplete: sinon.SinonSpy = sinon.spy();
fixture.componentInstance.resizable.mousemove.subscribe(() => '', () => '', onComplete);
fixture.destroy();
expect(onComplete).to.have.been.calledOnce;

});

});

0 comments on commit d5ea756

Please sign in to comment.