Skip to content

Commit b71ac1e

Browse files
crisbetojelbourn
authored andcommitted
fix(directionality): complete dir change observable (#8874)
Since it's most likely to use the `Dir.change` observable programmatically, rather than through an event binding, we should complete it in order to ensure that everything is cleaned up.
1 parent f590646 commit b71ac1e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/cdk/bidi/dir.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
Directive,
1111
Output,
1212
Input,
13-
EventEmitter
13+
EventEmitter,
14+
AfterContentInit,
15+
OnDestroy,
1416
} from '@angular/core';
1517

1618
import {Direction, Directionality} from './directionality';
@@ -27,7 +29,7 @@ import {Direction, Directionality} from './directionality';
2729
host: {'[dir]': 'dir'},
2830
exportAs: 'dir',
2931
})
30-
export class Dir implements Directionality {
32+
export class Dir implements Directionality, AfterContentInit, OnDestroy {
3133
_dir: Direction = 'ltr';
3234

3335
/** Whether the `value` has been set to its initial value. */
@@ -40,7 +42,7 @@ export class Dir implements Directionality {
4042
@Input('dir')
4143
get dir(): Direction { return this._dir; }
4244
set dir(v: Direction) {
43-
let old = this._dir;
45+
const old = this._dir;
4446
this._dir = v;
4547
if (old !== this._dir && this._isInitialized) {
4648
this.change.emit(this._dir);
@@ -54,5 +56,9 @@ export class Dir implements Directionality {
5456
ngAfterContentInit() {
5557
this._isInitialized = true;
5658
}
59+
60+
ngOnDestroy() {
61+
this.change.complete();
62+
}
5763
}
5864

src/cdk/bidi/directionality.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ describe('Directionality', () => {
7777
expect(injectedDirectionality.value).toBe('ltr');
7878
expect(fixture.componentInstance.changeCount).toBe(1);
7979
}));
80+
81+
it('should complete the change stream on destroy', fakeAsync(() => {
82+
const fixture = TestBed.createComponent(ElementWithDir);
83+
const dir =
84+
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;
85+
const spy = jasmine.createSpy('complete spy');
86+
const subscription = dir.change.subscribe(undefined, undefined, spy);
87+
88+
fixture.destroy();
89+
expect(spy).toHaveBeenCalled();
90+
subscription.unsubscribe();
91+
}));
92+
8093
});
8194
});
8295

0 commit comments

Comments
 (0)