Skip to content

Commit 00c782e

Browse files
rafaelss95wagnermaciel
authored andcommitted
fix(cdk/layout): support readonly arrays for public methods (#20252)
* fix(cdk/layout): support readonly arrays for public methods * refactor(cdk/layout): remove unnecessary type annotations (cherry picked from commit e3699dc)
1 parent dce7115 commit 00c782e

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/cdk/layout/breakpoints-observer.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {coerceArray} from '@angular/cdk/coercion';
910
import {Injectable, NgZone, OnDestroy} from '@angular/core';
10-
import {MediaMatcher} from './media-matcher';
11-
import {combineLatest, concat, Observable, Subject, Observer} from 'rxjs';
11+
import {combineLatest, concat, Observable, Observer, Subject} from 'rxjs';
1212
import {debounceTime, map, skip, startWith, take, takeUntil} from 'rxjs/operators';
13-
import {coerceArray} from '@angular/cdk/coercion';
13+
import {MediaMatcher} from './media-matcher';
1414

1515

1616
/** The current state of a layout breakpoint. */
@@ -60,7 +60,7 @@ export class BreakpointObserver implements OnDestroy {
6060
* @param value One or more media queries to check.
6161
* @returns Whether any of the media queries match.
6262
*/
63-
isMatched(value: string | string[]): boolean {
63+
isMatched(value: string | readonly string[]): boolean {
6464
const queries = splitQueries(coerceArray(value));
6565
return queries.some(mediaQuery => this._registerQuery(mediaQuery).mql.matches);
6666
}
@@ -71,7 +71,7 @@ export class BreakpointObserver implements OnDestroy {
7171
* @param value One or more media queries to check.
7272
* @returns A stream of matches for the given queries.
7373
*/
74-
observe(value: string | string[]): Observable<BreakpointState> {
74+
observe(value: string | readonly string[]): Observable<BreakpointState> {
7575
const queries = splitQueries(coerceArray(value));
7676
const observables = queries.map(query => this._registerQuery(query).observable);
7777

@@ -80,14 +80,14 @@ export class BreakpointObserver implements OnDestroy {
8080
stateObservable = concat(
8181
stateObservable.pipe(take(1)),
8282
stateObservable.pipe(skip(1), debounceTime(0)));
83-
return stateObservable.pipe(map((breakpointStates: InternalBreakpointState[]) => {
83+
return stateObservable.pipe(map(breakpointStates => {
8484
const response: BreakpointState = {
8585
matches: false,
8686
breakpoints: {},
8787
};
88-
breakpointStates.forEach((state: InternalBreakpointState) => {
89-
response.matches = response.matches || state.matches;
90-
response.breakpoints[state.query] = state.matches;
88+
breakpointStates.forEach(({matches, query}) => {
89+
response.matches = response.matches || matches;
90+
response.breakpoints[query] = matches;
9191
});
9292
return response;
9393
}));
@@ -100,10 +100,10 @@ export class BreakpointObserver implements OnDestroy {
100100
return this._queries.get(query)!;
101101
}
102102

103-
const mql: MediaQueryList = this._mediaMatcher.matchMedia(query);
103+
const mql = this._mediaMatcher.matchMedia(query);
104104

105105
// Create callback for match changes and add it is as a listener.
106-
const queryObservable = new Observable<MediaQueryList>((observer: Observer<MediaQueryList>) => {
106+
const queryObservable = new Observable((observer: Observer<MediaQueryList>) => {
107107
// Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be placed
108108
// back into the zone because matchMedia is only included in Zone.js by loading the
109109
// webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do not
@@ -117,7 +117,7 @@ export class BreakpointObserver implements OnDestroy {
117117
};
118118
}).pipe(
119119
startWith(mql),
120-
map((nextMql: MediaQueryList) => ({query, matches: nextMql.matches})),
120+
map(({matches}) => ({query, matches})),
121121
takeUntil(this._destroySubject)
122122
);
123123

@@ -132,8 +132,8 @@ export class BreakpointObserver implements OnDestroy {
132132
* Split each query string into separate query strings if two queries are provided as comma
133133
* separated.
134134
*/
135-
function splitQueries(queries: string[]): string[] {
136-
return queries.map((query: string) => query.split(','))
137-
.reduce((a1: string[], a2: string[]) => a1.concat(a2))
135+
function splitQueries(queries: readonly string[]): readonly string[] {
136+
return queries.map(query => query.split(','))
137+
.reduce((a1, a2) => a1.concat(a2))
138138
.map(query => query.trim());
139139
}

tools/public_api_guard/cdk/layout.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export declare class BreakpointObserver implements OnDestroy {
22
constructor(_mediaMatcher: MediaMatcher, _zone: NgZone);
3-
isMatched(value: string | string[]): boolean;
3+
isMatched(value: string | readonly string[]): boolean;
44
ngOnDestroy(): void;
5-
observe(value: string | string[]): Observable<BreakpointState>;
5+
observe(value: string | readonly string[]): Observable<BreakpointState>;
66
static ɵfac: i0.ɵɵFactoryDef<BreakpointObserver, never>;
77
static ɵprov: i0.ɵɵInjectableDef<BreakpointObserver>;
88
}

0 commit comments

Comments
 (0)