Skip to content

Commit

Permalink
refactor(cdk/tree): move coerceObservable to cdk/coercion/private
Browse files Browse the repository at this point in the history
  • Loading branch information
BobobUnicorn committed May 21, 2024
1 parent cea66ae commit 23bed7d
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/cdk/a11y/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ng_module(
deps = [
"//src:dev_mode_types",
"//src/cdk/coercion",
"//src/cdk/coercion/private",
"//src/cdk/keycodes",
"//src/cdk/layout",
"//src/cdk/observers",
Expand Down
8 changes: 1 addition & 7 deletions src/cdk/a11y/key-manager/tree-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {InjectionToken, QueryList} from '@angular/core';
import {coerceObservable} from '@angular/cdk/coercion/private';
import {Observable, Subject, Subscription, isObservable, of as observableOf} from 'rxjs';
import {take} from 'rxjs/operators';
import {
Expand All @@ -17,13 +18,6 @@ import {
} from './tree-key-manager-strategy';
import {Typeahead} from './typeahead';

function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
if (!isObservable(data)) {
return observableOf(data);
}
return data;
}

/**
* This class manages keyboard events for trees. If you pass it a QueryList or other list of tree
* items, it will set the active item, focus, handle expansion and typeahead correctly when
Expand Down
45 changes: 45 additions & 0 deletions src/cdk/coercion/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
load(
"//tools:defaults.bzl",
"ng_module",
"ng_test_library",
"ng_web_test_suite",
)

package(default_visibility = ["//visibility:public"])

ng_module(
name = "private",
srcs = glob(
["**/*.ts"],
exclude = ["**/*.spec.ts"],
),
deps = [
"//src:dev_mode_types",
"@npm//@angular/core",
"@npm//rxjs",
],
)

ng_test_library(
name = "private_tests_lib",
srcs = glob(
["**/*.spec.ts"],
exclude = ["**/*.e2e.spec.ts"],
),
deps = [
":private",
"@npm//rxjs",
],
)

ng_web_test_suite(
name = "unit_tests",
deps = [
":private_tests_lib",
],
)

filegroup(
name = "source-files",
srcs = glob(["**/*.ts"]),
)
9 changes: 9 additions & 0 deletions src/cdk/coercion/private/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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
*/

export * from './observable';
24 changes: 24 additions & 0 deletions src/cdk/coercion/private/observable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {Observable, ReplaySubject} from 'rxjs';
import {coerceObservable} from './observable';
import {fakeAsync} from '@angular/core/testing';

describe('coerceObservable', () => {
it('should return the Observable, if an Observable is passed in', () => {
const observable = new Observable();
expect(coerceObservable(observable)).toBe(observable);
});

it('should return subclasses of Observables', () => {
const observable = new ReplaySubject(1);
expect(coerceObservable(observable)).toBe(observable);
});

it('should wrap non-Observables in Observables', fakeAsync(() => {
const observable = coerceObservable(3);
let emittedValue = 0;
observable.subscribe(value => {
emittedValue = value;
});
expect(emittedValue).toBe(3);
}));
});
19 changes: 19 additions & 0 deletions src/cdk/coercion/private/observable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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, isObservable, of as observableOf} from 'rxjs';

/**
* Given either an Observable or non-Observable value, returns either the original
* Observable, or wraps it in an Observable that emits the non-Observable value.
*/
export function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
if (!isObservable(data)) {
return observableOf(data);
}
return data;
}
1 change: 1 addition & 0 deletions src/cdk/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CDK_ENTRYPOINTS = [
"bidi",
"clipboard",
"coercion",
"coercion/private",
"collections",
"dialog",
"drag-drop",
Expand Down
1 change: 1 addition & 0 deletions src/cdk/tree/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ng_module(
"//src:dev_mode_types",
"//src/cdk/a11y",
"//src/cdk/bidi",
"//src/cdk/coercion/private",
"//src/cdk/collections",
"//src/cdk/keycodes",
"@npm//@angular/core",
Expand Down
8 changes: 1 addition & 7 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
inject,
booleanAttribute,
} from '@angular/core';
import {coerceObservable} from '@angular/cdk/coercion/private';
import {
BehaviorSubject,
combineLatest,
Expand All @@ -70,13 +71,6 @@ import {
getTreeNoValidDataSourceError,
} from './tree-errors';

function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
if (!isObservable(data)) {
return observableOf(data);
}
return data;
}

type RenderingData<T> =
| {
flattenedNodes: null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ChangeDetectionStrategy, Component, QueryList} from '@angular/core';
import {ArrayDataSource} from '@angular/cdk/collections';
import {coerceObservable} from '@angular/cdk/coercion/private';
import {FlatTreeControl, CdkTreeModule} from '@angular/cdk/tree';
import {MatIconModule} from '@angular/material/icon';
import {MatButtonModule} from '@angular/material/button';
Expand All @@ -24,7 +25,7 @@ import {
TAB,
UP_ARROW,
} from '@angular/cdk/keycodes';
import {of as observableOf, Subject, isObservable, Observable} from 'rxjs';
import {Subject, isObservable, Observable} from 'rxjs';
import {take} from 'rxjs/operators';

const TREE_DATA: ExampleFlatNode[] = [
Expand Down Expand Up @@ -93,13 +94,6 @@ interface ExampleFlatNode {
isExpanded?: boolean;
}

function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
if (!isObservable(data)) {
return observableOf(data);
}
return data;
}

/**
* This class manages keyboard events for trees. If you pass it a QueryList or other list of tree
* items, it will set the active item, focus, handle expansion and typeahead correctly when
Expand Down

0 comments on commit 23bed7d

Please sign in to comment.