6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
+ import { coerceArray } from '@angular/cdk/coercion' ;
9
10
import { Injectable , NgZone , OnDestroy } from '@angular/core' ;
10
11
import { MediaMatcher } from './media-matcher' ;
11
12
import { combineLatest , concat , Observable , Subject , Observer } from 'rxjs' ;
12
13
import { debounceTime , map , skip , startWith , take , takeUntil } from 'rxjs/operators' ;
13
- import { coerceArray } from '@angular/cdk/coercion' ;
14
14
15
15
16
16
/** The current state of a layout breakpoint. */
@@ -60,7 +60,7 @@ export class BreakpointObserver implements OnDestroy {
60
60
* @param value One or more media queries to check.
61
61
* @returns Whether any of the media queries match.
62
62
*/
63
- isMatched ( value : string | string [ ] ) : boolean {
63
+ isMatched ( value : string | string [ ] ) : boolean {
64
64
const queries = splitQueries ( coerceArray ( value ) ) ;
65
65
return queries . some ( mediaQuery => this . _registerQuery ( mediaQuery ) . mql . matches ) ;
66
66
}
@@ -71,7 +71,7 @@ export class BreakpointObserver implements OnDestroy {
71
71
* @param value One or more media queries to check.
72
72
* @returns A stream of matches for the given queries.
73
73
*/
74
- observe ( value : string | string [ ] ) : Observable < BreakpointState > {
74
+ observe ( value : string | string [ ] ) : Observable < BreakpointState > {
75
75
const queries = splitQueries ( coerceArray ( value ) ) ;
76
76
const observables = queries . map ( query => this . _registerQuery ( query ) . observable ) ;
77
77
@@ -103,23 +103,24 @@ export class BreakpointObserver implements OnDestroy {
103
103
const mql : MediaQueryList = this . _mediaMatcher . matchMedia ( query ) ;
104
104
105
105
// Create callback for match changes and add it is as a listener.
106
- const queryObservable = new Observable < MediaQueryList > ( ( observer : Observer < MediaQueryList > ) => {
107
- // Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be placed
108
- // back into the zone because matchMedia is only included in Zone.js by loading the
109
- // webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do not
110
- // have MediaQueryList inherit from EventTarget, which causes inconsistencies in how Zone.js
111
- // patches it.
112
- const handler = ( e : any ) => this . _zone . run ( ( ) => observer . next ( e ) ) ;
113
- mql . addListener ( handler ) ;
114
-
115
- return ( ) => {
116
- mql . removeListener ( handler ) ;
117
- } ;
118
- } ) . pipe (
119
- startWith ( mql ) ,
120
- map ( ( nextMql : MediaQueryList ) => ( { query, matches : nextMql . matches } ) ) ,
121
- takeUntil ( this . _destroySubject )
122
- ) ;
106
+ const queryObservable =
107
+ new Observable < MediaQueryListEvent > ( ( observer : Observer < MediaQueryListEvent > ) => {
108
+ // Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be
109
+ // placed back into the zone because matchMedia is only included in Zone.js by loading the
110
+ // webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do
111
+ // not have MediaQueryList inherit from EventTarget, which causes inconsistencies in how
112
+ // Zone.js patches it.
113
+ const handler : ( event : MediaQueryListEvent ) => void = e =>
114
+ this . _zone . run ( ( ) => observer . next ( e ) ) ;
115
+ mql . addListener ( handler ) ;
116
+
117
+ return ( ) => {
118
+ mql . removeListener ( handler ) ;
119
+ } ;
120
+ } )
121
+ . pipe (
122
+ map ( ( nextMql : MediaQueryListEvent ) => ( { query, matches : nextMql . matches } ) ) ,
123
+ startWith ( { query, matches : mql . matches } ) , takeUntil ( this . _destroySubject ) ) ;
123
124
124
125
// Add the MediaQueryList to the set of queries.
125
126
const output = { observable : queryObservable , mql} ;
@@ -134,6 +135,6 @@ export class BreakpointObserver implements OnDestroy {
134
135
*/
135
136
function splitQueries ( queries : string [ ] ) : string [ ] {
136
137
return queries . map ( ( query : string ) => query . split ( ',' ) )
137
- . reduce ( ( a1 : string [ ] , a2 : string [ ] ) => a1 . concat ( a2 ) )
138
- . map ( query => query . trim ( ) ) ;
138
+ . reduce ( ( a1 : string [ ] , a2 : string [ ] ) => a1 . concat ( a2 ) )
139
+ . map ( query => query . trim ( ) ) ;
139
140
}
0 commit comments