Skip to content

Commit 684b98a

Browse files
committed
refactor(cdk/tree): move coerceObservable to cdk/coercion/private
1 parent df57528 commit 684b98a

File tree

10 files changed

+104
-22
lines changed

10 files changed

+104
-22
lines changed

src/cdk/a11y/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ng_module(
1919
deps = [
2020
"//src:dev_mode_types",
2121
"//src/cdk/coercion",
22+
"//src/cdk/coercion/private",
2223
"//src/cdk/keycodes",
2324
"//src/cdk/layout",
2425
"//src/cdk/observers",

src/cdk/a11y/key-manager/tree-key-manager.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

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

20-
function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
21-
if (!isObservable(data)) {
22-
return observableOf(data);
23-
}
24-
return data;
25-
}
26-
2721
/**
2822
* This class manages keyboard events for trees. If you pass it a QueryList or other list of tree
2923
* items, it will set the active item, focus, handle expansion and typeahead correctly when

src/cdk/coercion/private/BUILD.bazel

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
load(
2+
"//tools:defaults.bzl",
3+
"ng_module",
4+
"ng_test_library",
5+
"ng_web_test_suite",
6+
)
7+
8+
package(default_visibility = ["//visibility:public"])
9+
10+
ng_module(
11+
name = "private",
12+
srcs = glob(
13+
["**/*.ts"],
14+
exclude = ["**/*.spec.ts"],
15+
),
16+
deps = [
17+
"//src:dev_mode_types",
18+
"@npm//@angular/core",
19+
"@npm//rxjs",
20+
],
21+
)
22+
23+
ng_test_library(
24+
name = "private_tests_lib",
25+
srcs = glob(
26+
["**/*.spec.ts"],
27+
exclude = ["**/*.e2e.spec.ts"],
28+
),
29+
deps = [
30+
":private",
31+
"@npm//rxjs",
32+
],
33+
)
34+
35+
ng_web_test_suite(
36+
name = "unit_tests",
37+
deps = [
38+
":private_tests_lib",
39+
],
40+
)
41+
42+
filegroup(
43+
name = "source-files",
44+
srcs = glob(["**/*.ts"]),
45+
)

src/cdk/coercion/private/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './observable';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Observable, ReplaySubject} from 'rxjs';
2+
import {coerceObservable} from './observable';
3+
import {fakeAsync} from '@angular/core/testing';
4+
5+
describe('coerceObservable', () => {
6+
it('should return the Observable, if an Observable is passed in', () => {
7+
const observable = new Observable();
8+
expect(coerceObservable(observable)).toBe(observable);
9+
});
10+
11+
it('should return subclasses of Observables', () => {
12+
const observable = new ReplaySubject(1);
13+
expect(coerceObservable(observable)).toBe(observable);
14+
});
15+
16+
it('should wrap non-Observables in Observables', fakeAsync(() => {
17+
const observable = coerceObservable(3);
18+
let emittedValue = 0;
19+
observable.subscribe(value => {
20+
emittedValue = value;
21+
});
22+
expect(emittedValue).toBe(3);
23+
}));
24+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {Observable, isObservable, of as observableOf} from 'rxjs';
9+
10+
/**
11+
* Given either an Observable or non-Observable value, returns either the original
12+
* Observable, or wraps it in an Observable that emits the non-Observable value.
13+
*/
14+
export function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
15+
if (!isObservable(data)) {
16+
return observableOf(data);
17+
}
18+
return data;
19+
}

src/cdk/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CDK_ENTRYPOINTS = [
55
"bidi",
66
"clipboard",
77
"coercion",
8+
"coercion/private",
89
"collections",
910
"dialog",
1011
"drag-drop",

src/cdk/tree/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ng_module(
1818
"//src:dev_mode_types",
1919
"//src/cdk/a11y",
2020
"//src/cdk/bidi",
21+
"//src/cdk/coercion/private",
2122
"//src/cdk/collections",
2223
"//src/cdk/keycodes",
2324
"@npm//@angular/core",

src/cdk/tree/tree.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
inject,
4848
booleanAttribute,
4949
} from '@angular/core';
50+
import {coerceObservable} from '@angular/cdk/coercion/private';
5051
import {
5152
BehaviorSubject,
5253
combineLatest,
@@ -70,13 +71,6 @@ import {
7071
getTreeNoValidDataSourceError,
7172
} from './tree-errors';
7273

73-
function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
74-
if (!isObservable(data)) {
75-
return observableOf(data);
76-
}
77-
return data;
78-
}
79-
8074
type RenderingData<T> =
8175
| {
8276
flattenedNodes: null;

src/components-examples/cdk/tree/cdk-tree-custom-key-manager/cdk-tree-custom-key-manager-example.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {ChangeDetectionStrategy, Component, QueryList} from '@angular/core';
22
import {ArrayDataSource} from '@angular/cdk/collections';
3+
import {coerceObservable} from '@angular/cdk/coercion/private';
34
import {FlatTreeControl, CdkTreeModule} from '@angular/cdk/tree';
45
import {MatIconModule} from '@angular/material/icon';
56
import {MatButtonModule} from '@angular/material/button';
@@ -24,7 +25,7 @@ import {
2425
TAB,
2526
UP_ARROW,
2627
} from '@angular/cdk/keycodes';
27-
import {of as observableOf, Subject, isObservable, Observable} from 'rxjs';
28+
import {Subject, isObservable, Observable} from 'rxjs';
2829
import {take} from 'rxjs/operators';
2930

3031
const TREE_DATA: ExampleFlatNode[] = [
@@ -93,13 +94,6 @@ interface ExampleFlatNode {
9394
isExpanded?: boolean;
9495
}
9596

96-
function coerceObservable<T>(data: T | Observable<T>): Observable<T> {
97-
if (!isObservable(data)) {
98-
return observableOf(data);
99-
}
100-
return data;
101-
}
102-
10397
/**
10498
* This class manages keyboard events for trees. If you pass it a QueryList or other list of tree
10599
* items, it will set the active item, focus, handle expansion and typeahead correctly when

0 commit comments

Comments
 (0)