Skip to content

refactor: use recommended approach for asserting observables #18053

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
Jan 22, 2020
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 @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Observable, of as observableOf} from 'rxjs';
import {Observable, isObservable, of as observableOf} from 'rxjs';
import {DataSource} from './data-source';


Expand All @@ -17,7 +17,7 @@ export class ArrayDataSource<T> extends DataSource<T> {
}

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

disconnect() {}
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/scrolling/virtual-for-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
TrackByFunction,
ViewContainerRef,
} from '@angular/core';
import {Observable, Subject, of as observableOf} from 'rxjs';
import {Observable, Subject, of as observableOf, isObservable} from 'rxjs';
import {pairwise, shareReplay, startWith, switchMap, takeUntil} from 'rxjs/operators';
import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';

Expand Down Expand Up @@ -93,7 +93,7 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
} else {
// Slice the value if its an NgIterable to ensure we're working with an array.
this._dataSourceChanges.next(new ArrayDataSource<T>(
value instanceof Observable ? value : Array.prototype.slice.call(value || [])));
isObservable(value) ? value : Array.prototype.slice.call(value || [])));
}
}
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T> | null | undefined;
Expand Down
11 changes: 9 additions & 2 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ import {
ViewContainerRef,
ViewEncapsulation
} from '@angular/core';
import {BehaviorSubject, Observable, of as observableOf, Subject, Subscription} from 'rxjs';
import {
BehaviorSubject,
Observable,
of as observableOf,
Subject,
Subscription,
isObservable,
} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {CdkColumnDef} from './cell';
import {
Expand Down Expand Up @@ -832,7 +839,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes

if (isDataSource(this.dataSource)) {
dataStream = this.dataSource.connect(this);
} else if (this.dataSource instanceof Observable) {
} else if (isObservable(this.dataSource)) {
dataStream = this.dataSource;
} else if (Array.isArray(this.dataSource)) {
dataStream = observableOf(this.dataSource);
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/tree/control/nested-tree-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Observable} from 'rxjs';
import {Observable, isObservable} from 'rxjs';
import {take, filter} from 'rxjs/operators';
import {BaseTreeControl} from './base-tree-control';

Expand Down Expand Up @@ -45,7 +45,7 @@ export class NestedTreeControl<T> extends BaseTreeControl<T> {
const childrenNodes = this.getChildren(dataNode);
if (Array.isArray(childrenNodes)) {
childrenNodes.forEach((child: T) => this._getDescendants(descendants, child));
} else if (childrenNodes instanceof Observable) {
} else if (isObservable(childrenNodes)) {
// TypeScript as of version 3.5 doesn't seem to treat `Boolean` like a function that
// returns a `boolean` specifically in the context of `filter`, so we manually clarify that.
childrenNodes.pipe(take(1), filter(Boolean as () => boolean))
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/tree/nested-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
OnDestroy,
QueryList,
} from '@angular/core';
import {Observable} from 'rxjs';
import {isObservable} from 'rxjs';
import {takeUntil} from 'rxjs/operators';

import {CDK_TREE_NODE_OUTLET_NODE, CdkTreeNodeOutlet} from './outlet';
Expand Down Expand Up @@ -70,7 +70,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
const childrenNodes = this._tree.treeControl.getChildren(this.data);
if (Array.isArray(childrenNodes)) {
this.updateChildrenNodes(childrenNodes as T[]);
} else if (childrenNodes instanceof Observable) {
} else if (isObservable(childrenNodes)) {
childrenNodes.pipe(takeUntil(this._destroyed))
.subscribe(result => this.updateChildrenNodes(result));
}
Expand Down
13 changes: 10 additions & 3 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ import {
ViewEncapsulation,
TrackByFunction
} from '@angular/core';
import {BehaviorSubject, Observable, of as observableOf, Subject, Subscription} from 'rxjs';
import {
BehaviorSubject,
Observable,
of as observableOf,
Subject,
Subscription,
isObservable,
} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {TreeControl} from './control/tree-control';
import {CdkTreeNodeDef, CdkTreeNodeOutletContext} from './node';
Expand Down Expand Up @@ -194,7 +201,7 @@ export class CdkTree<T> implements AfterContentChecked, CollectionViewer, OnDest

if (isDataSource(this._dataSource)) {
dataStream = this._dataSource.connect(this);
} else if (this._dataSource instanceof Observable) {
} else if (isObservable(this._dataSource)) {
dataStream = this._dataSource;
} else if (Array.isArray(this._dataSource)) {
dataStream = observableOf(this._dataSource);
Expand Down Expand Up @@ -366,7 +373,7 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
const childrenNodes = this._tree.treeControl.getChildren(this._data);
if (Array.isArray(childrenNodes)) {
this._setRoleFromChildren(childrenNodes as T[]);
} else if (childrenNodes instanceof Observable) {
} else if (isObservable(childrenNodes)) {
childrenNodes.pipe(takeUntil(this._destroyed))
.subscribe(children => this._setRoleFromChildren(children));
}
Expand Down