From 77657d012ebc8a29ef8b7fbd7f47efefc04b03d1 Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Tue, 27 Feb 2018 13:31:31 -0800 Subject: [PATCH] virtual-scroll: allow user to pass `Observable` --- .../scrolling/virtual-for-of.ts | 17 ++++++++++------- ...rray-data-source.ts => array-data-source.ts} | 6 +++--- src/cdk/collections/public-api.ts | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) rename src/cdk/collections/{static-array-data-source.ts => array-data-source.ts} (69%) diff --git a/src/cdk-experimental/scrolling/virtual-for-of.ts b/src/cdk-experimental/scrolling/virtual-for-of.ts index 04caea9d65dd..f4889b4bb117 100644 --- a/src/cdk-experimental/scrolling/virtual-for-of.ts +++ b/src/cdk-experimental/scrolling/virtual-for-of.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {CollectionViewer, DataSource, Range, StaticArrayDataSource} from '@angular/cdk/collections'; +import {ArrayDataSource, CollectionViewer, DataSource, Range} from '@angular/cdk/collections'; import { Directive, DoCheck, @@ -35,7 +35,7 @@ import {CdkVirtualScrollViewport} from './virtual-scroll-viewport'; /** The context for an item rendered by `CdkVirtualForOf` */ export type CdkVirtualForOfContext = { $implicit: T; - cdkVirtualForOf: NgIterable | DataSource; + cdkVirtualForOf: DataSource | Observable | NgIterable; index: number; count: number; first: boolean; @@ -61,15 +61,18 @@ export class CdkVirtualForOf implements CollectionViewer, DoCheck, OnDestroy /** The DataSource to display. */ @Input() - get cdkVirtualForOf(): NgIterable | DataSource { return this._cdkVirtualForOf; } - set cdkVirtualForOf(value: NgIterable | DataSource) { + get cdkVirtualForOf(): DataSource | Observable | NgIterable { + return this._cdkVirtualForOf; + } + set cdkVirtualForOf(value: DataSource | Observable | NgIterable) { this._cdkVirtualForOf = value; const ds = value instanceof DataSource ? value : - // Slice the value since NgIterable may be array-like rather than an array. - new StaticArrayDataSource(Array.prototype.slice.call(value)); + // Slice the value if its an NgIterable to ensure we're working with an array. + new ArrayDataSource( + value instanceof Observable ? value : Array.prototype.slice.call(value)); this._dataSourceChanges.next(ds); } - _cdkVirtualForOf: NgIterable | DataSource; + _cdkVirtualForOf: DataSource | Observable | NgIterable; /** * The `TrackByFunction` to use for tracking changes. The `TrackByFunction` takes the index and diff --git a/src/cdk/collections/static-array-data-source.ts b/src/cdk/collections/array-data-source.ts similarity index 69% rename from src/cdk/collections/static-array-data-source.ts rename to src/cdk/collections/array-data-source.ts index cd458033e877..91a9c1164f0e 100644 --- a/src/cdk/collections/static-array-data-source.ts +++ b/src/cdk/collections/array-data-source.ts @@ -12,11 +12,11 @@ import {DataSource} from './data-source'; /** DataSource wrapper for a native array. */ -export class StaticArrayDataSource implements DataSource { - constructor(private _data: T[]) {} +export class ArrayDataSource implements DataSource { + constructor(private _data: T[] | Observable) {} connect(): Observable { - return observableOf(this._data); + return this._data instanceof Observable ? this._data : observableOf(this._data); } disconnect() {} diff --git a/src/cdk/collections/public-api.ts b/src/cdk/collections/public-api.ts index 5dac502f6e4d..a86731c0da91 100644 --- a/src/cdk/collections/public-api.ts +++ b/src/cdk/collections/public-api.ts @@ -6,10 +6,10 @@ * found in the LICENSE file at https://angular.io/license */ +export * from './array-data-source'; export * from './collection-viewer'; export * from './data-source'; export * from './selection'; -export * from './static-array-data-source'; export { UniqueSelectionDispatcher, UniqueSelectionDispatcherListener,