Skip to content

fix(directionality): complete dir change observable #8874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/cdk/bidi/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
Directive,
Output,
Input,
EventEmitter
EventEmitter,
AfterContentInit,
OnDestroy,
} from '@angular/core';

import {Direction, Directionality} from './directionality';
Expand All @@ -27,7 +29,7 @@ import {Direction, Directionality} from './directionality';
host: {'[dir]': 'dir'},
exportAs: 'dir',
})
export class Dir implements Directionality {
export class Dir implements Directionality, AfterContentInit, OnDestroy {
_dir: Direction = 'ltr';

/** Whether the `value` has been set to its initial value. */
Expand All @@ -40,7 +42,7 @@ export class Dir implements Directionality {
@Input('dir')
get dir(): Direction { return this._dir; }
set dir(v: Direction) {
let old = this._dir;
const old = this._dir;
this._dir = v;
if (old !== this._dir && this._isInitialized) {
this.change.emit(this._dir);
Expand All @@ -54,5 +56,9 @@ export class Dir implements Directionality {
ngAfterContentInit() {
this._isInitialized = true;
}

ngOnDestroy() {
this.change.complete();
}
}

13 changes: 13 additions & 0 deletions src/cdk/bidi/directionality.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ describe('Directionality', () => {
expect(injectedDirectionality.value).toBe('ltr');
expect(fixture.componentInstance.changeCount).toBe(1);
}));

it('should complete the change stream on destroy', fakeAsync(() => {
const fixture = TestBed.createComponent(ElementWithDir);
const dir =
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;
const spy = jasmine.createSpy('complete spy');
const subscription = dir.change.subscribe(undefined, undefined, spy);

fixture.destroy();
expect(spy).toHaveBeenCalled();
subscription.unsubscribe();
}));

});
});

Expand Down