@@ -11,7 +11,6 @@ import {
11
11
ChangeDetectionStrategy ,
12
12
ChangeDetectorRef ,
13
13
Component ,
14
- DoCheck ,
15
14
ElementRef ,
16
15
Inject ,
17
16
Input ,
@@ -24,11 +23,19 @@ import {
24
23
import { Observable } from 'rxjs/Observable' ;
25
24
import { fromEvent } from 'rxjs/observable/fromEvent' ;
26
25
import { takeUntil } from 'rxjs/operators/takeUntil' ;
26
+ import { throttleTime } from 'rxjs/operators/throttleTime' ;
27
+ import { animationFrame } from 'rxjs/scheduler/animationFrame' ;
27
28
import { Subject } from 'rxjs/Subject' ;
28
29
import { CdkVirtualForOf } from './virtual-for-of' ;
29
30
import { VIRTUAL_SCROLL_STRATEGY , VirtualScrollStrategy } from './virtual-scroll-strategy' ;
30
31
31
32
33
+ /** Checks if the given ranges are equal. */
34
+ function rangesEqual ( r1 : Range , r2 : Range ) : boolean {
35
+ return r1 . start == r2 . start && r1 . end == r2 . end ;
36
+ }
37
+
38
+
32
39
/** A viewport that virtualizes it's scrolling with the help of `CdkVirtualForOf`. */
33
40
@Component ( {
34
41
moduleId : module . id ,
@@ -42,7 +49,7 @@ import {VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy} from './virtual-scroll-s
42
49
changeDetection : ChangeDetectionStrategy . OnPush ,
43
50
preserveWhitespaces : false ,
44
51
} )
45
- export class CdkVirtualScrollViewport implements OnInit , DoCheck , OnDestroy {
52
+ export class CdkVirtualScrollViewport implements OnInit , OnDestroy {
46
53
/** Emits when the viewport is detached from a CdkVirtualForOf. */
47
54
private _detachedSubject = new Subject < void > ( ) ;
48
55
@@ -78,14 +85,6 @@ export class CdkVirtualScrollViewport implements OnInit, DoCheck, OnDestroy {
78
85
/** Whether this viewport is attached to a CdkVirtualForOf. */
79
86
private _isAttached = false ;
80
87
81
- /**
82
- * The scroll handling status.
83
- * needed - The scroll state needs to be updated, but a check hasn't yet been scheduled.
84
- * pending - The scroll state needs to be updated, and an update has already been scheduled.
85
- * done - The scroll state does not need to be updated.
86
- */
87
- private _scrollHandledStatus : 'needed' | 'pending' | 'done' = 'done' ;
88
-
89
88
constructor ( public elementRef : ElementRef , private _changeDetectorRef : ChangeDetectorRef ,
90
89
private _ngZone : NgZone ,
91
90
@Inject ( VIRTUAL_SCROLL_STRATEGY ) private _scrollStrategy : VirtualScrollStrategy ) { }
@@ -118,7 +117,7 @@ export class CdkVirtualScrollViewport implements OnInit, DoCheck, OnDestroy {
118
117
119
118
/** Sets the currently rendered range of indices. */
120
119
setRenderedRange ( range : Range ) {
121
- if ( ! this . _rangesEqual ( this . _renderedRange , range ) ) {
120
+ if ( ! rangesEqual ( this . _renderedRange , range ) ) {
122
121
// Re-enter the Angular zone so we can mark for change detection.
123
122
this . _ngZone . run ( ( ) => {
124
123
this . _renderedRangeSubject . next ( this . _renderedRange = range ) ;
@@ -174,25 +173,16 @@ export class CdkVirtualScrollViewport implements OnInit, DoCheck, OnDestroy {
174
173
Promise . resolve ( ) . then ( ( ) => {
175
174
this . _viewportSize = this . orientation === 'horizontal' ?
176
175
this . elementRef . nativeElement . clientWidth : this . elementRef . nativeElement . clientHeight ;
176
+ this . _scrollStrategy . attach ( this ) ;
177
+
177
178
this . _ngZone . runOutsideAngular ( ( ) => {
178
- fromEvent ( this . elementRef . nativeElement , 'scroll' ) . subscribe ( ( ) => {
179
- this . _markScrolled ( ) ;
180
- } ) ;
179
+ fromEvent ( this . elementRef . nativeElement , 'scroll' )
180
+ . pipe ( throttleTime ( 0 , animationFrame ) )
181
+ . subscribe ( ( ) => this . _scrollStrategy . onContentScrolled ( ) ) ;
181
182
} ) ;
182
- this . _scrollStrategy . attach ( this ) ;
183
183
} ) ;
184
184
}
185
185
186
- ngDoCheck ( ) {
187
- if ( this . _scrollHandledStatus === 'needed' ) {
188
- this . _scrollHandledStatus = 'pending' ;
189
- this . _ngZone . runOutsideAngular ( ( ) => requestAnimationFrame ( ( ) => {
190
- this . _scrollHandledStatus = 'done' ;
191
- this . _scrollStrategy . onContentScrolled ( ) ;
192
- } ) ) ;
193
- }
194
- }
195
-
196
186
ngOnDestroy ( ) {
197
187
this . detach ( ) ;
198
188
this . _scrollStrategy . detach ( ) ;
@@ -201,20 +191,4 @@ export class CdkVirtualScrollViewport implements OnInit, DoCheck, OnDestroy {
201
191
this . _detachedSubject . complete ( ) ;
202
192
this . _renderedRangeSubject . complete ( ) ;
203
193
}
204
-
205
- /** Marks that a scroll event happened and that the scroll state should be checked. */
206
- private _markScrolled ( ) {
207
- if ( this . _scrollHandledStatus === 'done' ) {
208
- // Re-enter the Angular zone so we can mark for change detection.
209
- this . _ngZone . run ( ( ) => {
210
- this . _scrollHandledStatus = 'needed' ;
211
- this . _changeDetectorRef . markForCheck ( ) ;
212
- } ) ;
213
- }
214
- }
215
-
216
- /** Checks if the given ranges are equal. */
217
- private _rangesEqual ( r1 : Range , r2 : Range ) : boolean {
218
- return r1 . start == r2 . start && r1 . end == r2 . end ;
219
- }
220
194
}
0 commit comments