6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Injectable , Optional , SkipSelf , NgZone } from '@angular/core' ;
9
+ import { Injectable , Optional , SkipSelf , NgZone , OnDestroy } from '@angular/core' ;
10
10
import { Platform } from '@angular/cdk/platform' ;
11
11
import { ScrollDispatcher } from './scroll-dispatcher' ;
12
12
import { Observable } from 'rxjs/Observable' ;
13
13
import { Subject } from 'rxjs/Subject' ;
14
14
import { fromEvent } from 'rxjs/observable/fromEvent' ;
15
15
import { merge } from 'rxjs/observable/merge' ;
16
16
import { auditTime } from 'rxjs/operator/auditTime' ;
17
+ import { Subscription } from 'rxjs/Subscription' ;
17
18
18
19
/** Time in ms to throttle the resize events by default. */
19
20
export const DEFAULT_RESIZE_TIME = 20 ;
@@ -23,27 +24,33 @@ export const DEFAULT_RESIZE_TIME = 20;
23
24
* @docs -private
24
25
*/
25
26
@Injectable ( )
26
- export class ViewportRuler {
27
+ export class ViewportRuler implements OnDestroy {
27
28
28
29
/** Cached document client rectangle. */
29
30
private _documentRect ?: ClientRect ;
30
31
31
32
/** 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 [ ] ;
33
37
34
38
constructor ( platform : Platform , ngZone : NgZone , scrollDispatcher : ScrollDispatcher ) {
35
39
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' ) ) ;
41
42
} ) ;
42
43
}
43
44
44
45
// 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 ( ) ) ;
47
54
}
48
55
49
56
/** Gets a ClientRect for the viewport's bounds. */
0 commit comments