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
- import { MediaMatcher } from './media-matcher' ;
11
- import { asapScheduler , combineLatest , Observable , Subject , Observer } from 'rxjs' ;
11
+ import { asapScheduler , combineLatest , Observable , Observer , Subject } from 'rxjs' ;
12
12
import { debounceTime , map , startWith , takeUntil } from 'rxjs/operators' ;
13
- import { coerceArray } from '@angular/cdk/coercion' ;
13
+
14
+ import { MediaMatcher } from './media-matcher' ;
14
15
15
16
16
17
/** The current state of a layout breakpoint. */
@@ -21,9 +22,7 @@ export interface BreakpointState {
21
22
* A key boolean pair for each query provided to the observe method,
22
23
* with its current matched state.
23
24
*/
24
- breakpoints : {
25
- [ key : string ] : boolean ;
26
- } ;
25
+ breakpoints : { [ key : string ] : boolean ; } ;
27
26
}
28
27
29
28
/** The current state of a layout breakpoint. */
@@ -60,7 +59,7 @@ export class BreakpointObserver implements OnDestroy {
60
59
* @param value One or more media queries to check.
61
60
* @returns Whether any of the media queries match.
62
61
*/
63
- isMatched ( value : string | string [ ] ) : boolean {
62
+ isMatched ( value : string | string [ ] ) : boolean {
64
63
const queries = splitQueries ( coerceArray ( value ) ) ;
65
64
return queries . some ( mediaQuery => this . _registerQuery ( mediaQuery ) . mql . matches ) ;
66
65
}
@@ -71,23 +70,22 @@ export class BreakpointObserver implements OnDestroy {
71
70
* @param value One or more media queries to check.
72
71
* @returns A stream of matches for the given queries.
73
72
*/
74
- observe ( value : string | string [ ] ) : Observable < BreakpointState > {
73
+ observe ( value : string | string [ ] ) : Observable < BreakpointState > {
75
74
const queries = splitQueries ( coerceArray ( value ) ) ;
76
75
const observables = queries . map ( query => this . _registerQuery ( query ) . observable ) ;
77
76
78
- return combineLatest ( observables ) . pipe (
79
- debounceTime ( 0 , asapScheduler ) ,
80
- map ( ( breakpointStates : InternalBreakpointState [ ] ) => {
81
- const response : BreakpointState = {
82
- matches : false ,
83
- breakpoints : { } ,
84
- } ;
85
- breakpointStates . forEach ( ( state : InternalBreakpointState ) => {
86
- response . matches = response . matches || state . matches ;
87
- response . breakpoints [ state . query ] = state . matches ;
88
- } ) ;
89
- return response ;
90
- } ) ) ;
77
+ return combineLatest ( observables )
78
+ . pipe ( debounceTime ( 0 , asapScheduler ) , map ( ( breakpointStates : InternalBreakpointState [ ] ) => {
79
+ const response : BreakpointState = {
80
+ matches : false ,
81
+ breakpoints : { } ,
82
+ } ;
83
+ breakpointStates . forEach ( ( state : InternalBreakpointState ) => {
84
+ response . matches = response . matches || state . matches ;
85
+ response . breakpoints [ state . query ] = state . matches ;
86
+ } ) ;
87
+ return response ;
88
+ } ) ) ;
91
89
}
92
90
93
91
/** Registers a specific query to be listened for. */
@@ -100,23 +98,24 @@ export class BreakpointObserver implements OnDestroy {
100
98
const mql : MediaQueryList = this . mediaMatcher . matchMedia ( query ) ;
101
99
102
100
// Create callback for match changes and add it is as a listener.
103
- const queryObservable = new Observable < MediaQueryList > ( ( observer : Observer < MediaQueryList > ) => {
104
- // Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be placed
105
- // back into the zone because matchMedia is only included in Zone.js by loading the
106
- // webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do not
107
- // have MediaQueryList inherit from EventTarget, which causes inconsistencies in how Zone.js
108
- // patches it.
109
- const handler = ( e : any ) => this . zone . run ( ( ) => observer . next ( e ) ) ;
110
- mql . addListener ( handler ) ;
111
-
112
- return ( ) => {
113
- mql . removeListener ( handler ) ;
114
- } ;
115
- } ) . pipe (
116
- startWith ( mql ) ,
117
- map ( ( nextMql : MediaQueryList ) => ( { query, matches : nextMql . matches } ) ) ,
118
- takeUntil ( this . _destroySubject )
119
- ) ;
101
+ const queryObservable =
102
+ new Observable < MediaQueryListEvent > ( ( observer : Observer < MediaQueryListEvent > ) => {
103
+ // Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be
104
+ // placed back into the zone because matchMedia is only included in Zone.js by loading the
105
+ // webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do
106
+ // not have MediaQueryList inherit from EventTarget, which causes inconsistencies in how
107
+ // Zone.js patches it.
108
+ const handler : ( event : MediaQueryListEvent ) => void = e =>
109
+ this . zone . run ( ( ) => observer . next ( e ) ) ;
110
+ mql . addListener ( handler ) ;
111
+
112
+ return ( ) => {
113
+ mql . removeListener ( handler ) ;
114
+ } ;
115
+ } )
116
+ . pipe (
117
+ map ( ( nextMql : MediaQueryListEvent ) => ( { query, matches : nextMql . matches } ) ) ,
118
+ startWith ( { query, matches : mql . matches } ) , takeUntil ( this . _destroySubject ) ) ;
120
119
121
120
// Add the MediaQueryList to the set of queries.
122
121
const output = { observable : queryObservable , mql} ;
@@ -131,6 +130,6 @@ export class BreakpointObserver implements OnDestroy {
131
130
*/
132
131
function splitQueries ( queries : string [ ] ) : string [ ] {
133
132
return queries . map ( ( query : string ) => query . split ( ',' ) )
134
- . reduce ( ( a1 : string [ ] , a2 : string [ ] ) => a1 . concat ( a2 ) )
135
- . map ( query => query . trim ( ) ) ;
133
+ . reduce ( ( a1 : string [ ] , a2 : string [ ] ) => a1 . concat ( a2 ) )
134
+ . map ( query => query . trim ( ) ) ;
136
135
}
0 commit comments