Skip to content

Commit 69ec88a

Browse files
committed
virtual-scroll: allow user to pass Observable<T[]>
1 parent 234414c commit 69ec88a

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

src/cdk-experimental/scrolling/virtual-for-of.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {CollectionViewer, DataSource, Range, StaticArrayDataSource} from '@angular/cdk/collections';
9+
import {ArrayDataSource, CollectionViewer, DataSource, Range} from '@angular/cdk/collections';
1010
import {
1111
Directive,
1212
DoCheck,
@@ -35,7 +35,7 @@ import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
3535
/** The context for an item rendered by `CdkVirtualForOf` */
3636
export type CdkVirtualForOfContext<T> = {
3737
$implicit: T;
38-
cdkVirtualForOf: NgIterable<T> | DataSource<T>;
38+
cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;
3939
index: number;
4040
count: number;
4141
first: boolean;
@@ -61,15 +61,18 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
6161

6262
/** The DataSource to display. */
6363
@Input()
64-
get cdkVirtualForOf(): NgIterable<T> | DataSource<T> { return this._cdkVirtualForOf; }
65-
set cdkVirtualForOf(value: NgIterable<T> | DataSource<T>) {
64+
get cdkVirtualForOf(): DataSource<T> | Observable<T[]> | NgIterable<T> {
65+
return this._cdkVirtualForOf;
66+
}
67+
set cdkVirtualForOf(value: DataSource<T> | Observable<T[]> | NgIterable<T>) {
6668
this._cdkVirtualForOf = value;
6769
const ds = value instanceof DataSource ? value :
68-
// Slice the value since NgIterable may be array-like rather than an array.
69-
new StaticArrayDataSource<T>(Array.prototype.slice.call(value));
70+
// Slice the value if its an NgIterable to ensure we're working with an array.
71+
new ArrayDataSource<T>(
72+
value instanceof Observable ? value : Array.prototype.slice.call(value));
7073
this._dataSourceChanges.next(ds);
7174
}
72-
_cdkVirtualForOf: NgIterable<T> | DataSource<T>;
75+
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;
7376

7477
/**
7578
* The `TrackByFunction` to use for tracking changes. The `TrackByFunction` takes the index and

src/cdk/collections/static-array-data-source.ts renamed to src/cdk/collections/array-data-source.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {DataSource} from './data-source';
1212

1313

1414
/** DataSource wrapper for a native array. */
15-
export class StaticArrayDataSource<T> implements DataSource<T> {
16-
constructor(private _data: T[]) {}
15+
export class ArrayDataSource<T> implements DataSource<T> {
16+
constructor(private _data: T[] | Observable<T[]>) {}
1717

1818
connect(): Observable<T[]> {
19-
return observableOf(this._data);
19+
return this._data instanceof Observable ? this._data : observableOf(this._data);
2020
}
2121

2222
disconnect() {}

src/cdk/collections/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
export * from './array-data-source';
910
export * from './collection-viewer';
1011
export * from './data-source';
1112
export * from './selection';
12-
export * from './static-array-data-source';
1313
export {
1414
UniqueSelectionDispatcher,
1515
UniqueSelectionDispatcherListener,

tools/package-tools/rollup-globals.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export const rollupGlobals = {
7171
'rxjs/observable/of': 'Rx.Observable',
7272
'rxjs/observable/throw': 'Rx.Observable',
7373

74+
'rxjs/scheduler/animationFrame': 'Rx.Scheduler',
75+
7476
'rxjs/operators/auditTime': 'Rx.operators',
7577
'rxjs/operators/catchError': 'Rx.operators',
7678
'rxjs/operators/combineLatest': 'Rx.operators',
@@ -88,4 +90,5 @@ export const rollupGlobals = {
8890
'rxjs/operators/take': 'Rx.operators',
8991
'rxjs/operators/takeUntil': 'Rx.operators',
9092
'rxjs/operators/tap': 'Rx.operators',
93+
'rxjs/operators/throttleTime': 'Rx.operators',
9194
};

0 commit comments

Comments
 (0)