Skip to content

refactor(collections): allow ReadonlyArray as data type in data source #12723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cdk/collections/array-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {DataSource} from './data-source';

/** DataSource wrapper for a native array. */
export class ArrayDataSource<T> extends DataSource<T> {
constructor(private _data: T[] | Observable<T[]>) {
constructor(private _data: T[] | ReadonlyArray<T> | Observable<T[] | ReadonlyArray<T>>) {
super();
}

connect(): Observable<T[]> {
connect(): Observable<T[] | ReadonlyArray<T>> {
return this._data instanceof Observable ? this._data : observableOf(this._data);
}

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/collections/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class DataSource<T> {
* data source.
* @returns Observable that emits a new value when the data changes.
*/
abstract connect(collectionViewer: CollectionViewer): Observable<T[]>;
abstract connect(collectionViewer: CollectionViewer): Observable<T[] | ReadonlyArray<T>>;

/**
* Disconnects a collection viewer (such as a data-table) from this data source. Can be used
Expand Down
9 changes: 6 additions & 3 deletions src/cdk/scrolling/virtual-for-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
@Input() cdkVirtualForTemplateCacheSize: number = 20;

/** Emits whenever the data in the current DataSource changes. */
dataStream: Observable<T[]> = this._dataSourceChanges
dataStream: Observable<T[] | ReadonlyArray<T>> = this._dataSourceChanges
.pipe(
// Start off with null `DataSource`.
startWith(null!),
Expand All @@ -138,7 +138,7 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
private _differ: IterableDiffer<T> | null = null;

/** The most recent data emitted from the DataSource. */
private _data: T[];
private _data: T[] | ReadonlyArray<T>;

/** The currently rendered items. */
private _renderedItems: T[];
Expand Down Expand Up @@ -254,10 +254,13 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
}

/** Swap out one `DataSource` for another. */
private _changeDataSource(oldDs: DataSource<T> | null, newDs: DataSource<T>): Observable<T[]> {
private _changeDataSource(oldDs: DataSource<T> | null, newDs: DataSource<T>):
Observable<T[] | ReadonlyArray<T>> {

if (oldDs) {
oldDs.disconnect(this);
}

this._needsUpdate = true;
return newDs.connect(this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export interface RenderRow<T> {
})
export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
/** Latest data provided by the data source. */
protected _data: T[];
protected _data: T[] | ReadonlyArray<T>;

/** Subject that emits when the component has been destroyed. */
private _onDestroy = new Subject<void>();
Expand Down Expand Up @@ -769,7 +769,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
// If no data source has been set, there is nothing to observe for changes.
if (!this.dataSource) { return; }

let dataStream: Observable<T[]> | undefined;
let dataStream: Observable<T[] | ReadonlyArray<T>> | undefined;

// Check if the datasource is a DataSource object by observing if it has a connect function.
// Cannot check this.dataSource['connect'] due to potential property renaming, nor can it
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class CdkTree<T>

/** Set up a subscription for the data provided by the data source. */
private _observeRenderChanges() {
let dataStream: Observable<T[]> | undefined;
let dataStream: Observable<T[] | ReadonlyArray<T>> | undefined;

// Cannot use `instanceof DataSource` since the data source could be a literal with
// `connect` function and may not extends DataSource.
Expand All @@ -204,7 +204,7 @@ export class CdkTree<T>
}

/** Check for changes made in the data and render each change (node added/removed/moved). */
renderNodeChanges(data: T[], dataDiffer: IterableDiffer<T> = this._dataDiffer,
renderNodeChanges(data: T[] | ReadonlyArray<T>, dataDiffer: IterableDiffer<T> = this._dataDiffer,
viewContainer: ViewContainerRef = this._nodeOutlet.viewContainer,
parentData?: T) {
const changes = dataDiffer.diff(data);
Expand Down