@@ -23,13 +23,20 @@ import {
23
23
ViewChild ,
24
24
ViewEncapsulation ,
25
25
} from '@angular/core' ;
26
- import { animationFrameScheduler , asapScheduler , Observable , Subject , Observer } from 'rxjs' ;
26
+ import {
27
+ animationFrameScheduler ,
28
+ asapScheduler ,
29
+ Observable ,
30
+ Subject ,
31
+ Observer ,
32
+ Subscription ,
33
+ } from 'rxjs' ;
27
34
import { auditTime , startWith , takeUntil } from 'rxjs/operators' ;
28
35
import { ScrollDispatcher } from './scroll-dispatcher' ;
29
36
import { CdkScrollable , ExtendedScrollToOptions } from './scrollable' ;
30
37
import { CdkVirtualForOf } from './virtual-for-of' ;
31
38
import { VIRTUAL_SCROLL_STRATEGY , VirtualScrollStrategy } from './virtual-scroll-strategy' ;
32
-
39
+ import { ViewportRuler } from './viewport-ruler' ;
33
40
34
41
/** Checks if the given ranges are equal. */
35
42
function rangesEqual ( r1 : ListRange , r2 : ListRange ) : boolean {
@@ -142,18 +149,33 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
142
149
/** A list of functions to run after the next change detection cycle. */
143
150
private _runAfterChangeDetection : Function [ ] = [ ] ;
144
151
152
+ /** Subscription to changes in the viewport size. */
153
+ private _viewportChanges = Subscription . EMPTY ;
154
+
145
155
constructor ( public elementRef : ElementRef < HTMLElement > ,
146
156
private _changeDetectorRef : ChangeDetectorRef ,
147
157
ngZone : NgZone ,
148
158
@Optional ( ) @Inject ( VIRTUAL_SCROLL_STRATEGY )
149
159
private _scrollStrategy : VirtualScrollStrategy ,
150
160
@Optional ( ) dir : Directionality ,
151
- scrollDispatcher : ScrollDispatcher ) {
161
+ scrollDispatcher : ScrollDispatcher ,
162
+ /**
163
+ * @deprecated `viewportRuler` parameter to become required.
164
+ * @breaking -change 11.0.0
165
+ */
166
+ @Optional ( ) viewportRuler ?: ViewportRuler ) {
152
167
super ( elementRef , scrollDispatcher , ngZone , dir ) ;
153
168
154
169
if ( ! _scrollStrategy ) {
155
170
throw Error ( 'Error: cdk-virtual-scroll-viewport requires the "itemSize" property to be set.' ) ;
156
171
}
172
+
173
+ // @breaking -change 11.0.0 Remove null check for `viewportRuler`.
174
+ if ( viewportRuler ) {
175
+ this . _viewportChanges = viewportRuler . change ( ) . subscribe ( ( ) => {
176
+ this . checkViewportSize ( ) ;
177
+ } ) ;
178
+ }
157
179
}
158
180
159
181
ngOnInit ( ) {
@@ -188,6 +210,7 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
188
210
// Complete all subjects
189
211
this . _renderedRangeSubject . complete ( ) ;
190
212
this . _detachedSubject . complete ( ) ;
213
+ this . _viewportChanges . unsubscribe ( ) ;
191
214
192
215
super . ngOnDestroy ( ) ;
193
216
}
0 commit comments