Skip to content

Commit 636d27e

Browse files
crisbetojelbourn
authored andcommitted
refactor(collections): allow ReadonlyArray as data type (#12723)
Allows a `ReadonlyArray`, in addition to a regular one, to be passed into the data source. Fixes #12720.
1 parent 9c184ea commit 636d27e

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

src/cdk/collections/array-data-source.ts

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

1313
/** DataSource wrapper for a native array. */
1414
export class ArrayDataSource<T> extends DataSource<T> {
15-
constructor(private _data: T[] | Observable<T[]>) {
15+
constructor(private _data: T[] | ReadonlyArray<T> | Observable<T[] | ReadonlyArray<T>>) {
1616
super();
1717
}
1818

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

src/cdk/collections/data-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export abstract class DataSource<T> {
1818
* data source.
1919
* @returns Observable that emits a new value when the data changes.
2020
*/
21-
abstract connect(collectionViewer: CollectionViewer): Observable<T[]>;
21+
abstract connect(collectionViewer: CollectionViewer): Observable<T[] | ReadonlyArray<T>>;
2222

2323
/**
2424
* Disconnects a collection viewer (such as a data-table) from this data source. Can be used

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
121121
@Input() cdkVirtualForTemplateCacheSize: number = 20;
122122

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

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

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

256256
/** Swap out one `DataSource` for another. */
257-
private _changeDataSource(oldDs: DataSource<T> | null, newDs: DataSource<T>): Observable<T[]> {
257+
private _changeDataSource(oldDs: DataSource<T> | null, newDs: DataSource<T>):
258+
Observable<T[] | ReadonlyArray<T>> {
259+
258260
if (oldDs) {
259261
oldDs.disconnect(this);
260262
}
263+
261264
this._needsUpdate = true;
262265
return newDs.connect(this);
263266
}

src/cdk/table/table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export interface RenderRow<T> {
149149
})
150150
export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
151151
/** Latest data provided by the data source. */
152-
protected _data: T[];
152+
protected _data: T[] | ReadonlyArray<T>;
153153

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

772-
let dataStream: Observable<T[]> | undefined;
772+
let dataStream: Observable<T[] | ReadonlyArray<T>> | undefined;
773773

774774
// Check if the datasource is a DataSource object by observing if it has a connect function.
775775
// Cannot check this.dataSource['connect'] due to potential property renaming, nor can it

src/cdk/tree/tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class CdkTree<T>
183183

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

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

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

0 commit comments

Comments
 (0)