66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { Injectable , Optional , SkipSelf , NgZone } from '@angular/core' ;
9+ import { Injectable , Optional , SkipSelf , NgZone , OnDestroy } from '@angular/core' ;
1010import { Platform } from '@angular/cdk/platform' ;
1111import { ScrollDispatcher } from './scroll-dispatcher' ;
1212import { Observable } from 'rxjs/Observable' ;
1313import { Subject } from 'rxjs/Subject' ;
1414import { fromEvent } from 'rxjs/observable/fromEvent' ;
1515import { merge } from 'rxjs/observable/merge' ;
1616import { auditTime } from 'rxjs/operator/auditTime' ;
17+ import { Subscription } from 'rxjs/Subscription' ;
1718
1819/** Time in ms to throttle the resize events by default. */
1920export const DEFAULT_RESIZE_TIME = 20 ;
@@ -23,27 +24,33 @@ export const DEFAULT_RESIZE_TIME = 20;
2324 * @docs -private
2425 */
2526@Injectable ( )
26- export class ViewportRuler {
27+ export class ViewportRuler implements OnDestroy {
2728
2829 /** Cached document client rectangle. */
2930 private _documentRect ?: ClientRect ;
3031
3132 /** Stream of viewport change events. */
32- private _changed = new Subject < string > ( ) ;
33+ private _changed = new Subject < void > ( ) ;
34+
35+ /** Subscriptions to streams that invalidate the cached viewport dimensions. */
36+ private _invalidateCacheSubscriptions : Subscription [ ] ;
3337
3438 constructor ( platform : Platform , ngZone : NgZone , scrollDispatcher : ScrollDispatcher ) {
3539 if ( platform . isBrowser ) {
36- ngZone . runOutsideAngular ( ( ) => {
37- merge < Event > (
38- fromEvent ( window , 'resize' ) ,
39- fromEvent ( window , 'orientationchange' )
40- ) . subscribe ( event => this . _changed . next ( event . type ) ) ;
40+ this . _changed = ngZone . runOutsideAngular ( ( ) => {
41+ return merge < Event > ( fromEvent ( window , 'resize' ) , fromEvent ( window , 'orientationchange' ) ) ;
4142 } ) ;
4243 }
4344
4445 // Subscribe to scroll and resize events and update the document rectangle on changes.
45- scrollDispatcher . scrolled ( 0 , ( ) => this . _cacheViewportGeometry ( ) ) ;
46- this . change ( ) . subscribe ( ( ) => this . _cacheViewportGeometry ( ) ) ;
46+ this . _invalidateCacheSubscriptions = [
47+ scrollDispatcher . scrolled ( 0 , ( ) => this . _cacheViewportGeometry ( ) ) ,
48+ this . change ( ) . subscribe ( ( ) => this . _cacheViewportGeometry ( ) )
49+ ] ;
50+ }
51+
52+ ngOnDestroy ( ) {
53+ this . _invalidateCacheSubscriptions . forEach ( subscription => subscription . unsubscribe ( ) ) ;
4754 }
4855
4956 /** Gets a ClientRect for the viewport's bounds. */
0 commit comments