@@ -2,15 +2,35 @@ import { EventData, Observable } from '../data/observable';
22import { Screen } from '../platform/screen' ;
33import { getApplicationProperties , toggleApplicationEventListeners } from '../application/helpers-common' ;
44import type { ApplicationEventData } from '../application/application-interfaces' ;
5- import { matchQuery , MediaQueryType } from '../css-mediaquery' ;
5+ import { isEvaluableMediaQuery , matchQuery , MediaQueryType } from '../css-mediaquery' ;
66import { Trace } from '../trace' ;
77
88const mediaQueryLists : MediaQueryListImpl [ ] = [ ] ;
99
1010// In browser, developers cannot create MediaQueryList instances without calling matchMedia
1111let isMediaInitializationEnabled : boolean = false ;
1212
13+ // Query results only change with orientation, appearance or screen metrics.
14+ const matchResultCache = new Map < string , boolean > ( ) ;
15+ let isCacheInvalidationRegistered = false ;
16+
17+ function clearMatchResultCache ( ) {
18+ matchResultCache . clear ( ) ;
19+ }
20+
21+ function registerCacheInvalidation ( ) {
22+ if ( isCacheInvalidationRegistered ) {
23+ return ;
24+ }
25+
26+ isCacheInvalidationRegistered = true ;
27+ toggleApplicationEventListeners ( true , clearMatchResultCache ) ;
28+ }
29+
1330function onDeviceChange ( args : ApplicationEventData ) {
31+ // Listener order is not guaranteed, so never re-evaluate against stale results
32+ clearMatchResultCache ( ) ;
33+
1434 for ( const mql of mediaQueryLists ) {
1535 const matches = checkIfMediaQueryMatches ( mql . media ) ;
1636 if ( mql . matches !== matches ) {
@@ -27,26 +47,55 @@ function onDeviceChange(args: ApplicationEventData) {
2747}
2848
2949function checkIfMediaQueryMatches ( mediaQueryString : string ) : boolean {
30- const { widthPixels, heightPixels } = Screen . mainScreen ;
50+ const cachedResult = matchResultCache . get ( mediaQueryString ) ;
51+ if ( cachedResult !== undefined ) {
52+ return cachedResult ;
53+ }
3154
3255 let matches : boolean ;
3356
34- try {
35- const appProperties = getApplicationProperties ( ) ;
36- matches = matchQuery ( mediaQueryString , {
37- type : MediaQueryType . screen ,
38- width : widthPixels ,
39- height : heightPixels ,
40- 'device-width' : widthPixels ,
41- 'device-height' : heightPixels ,
42- orientation : appProperties . orientation ,
43- 'prefers-color-scheme' : appProperties . systemAppearance ,
44- } ) ;
45- } catch ( err ) {
57+ if ( isEvaluableMediaQuery ( mediaQueryString ) ) {
58+ registerCacheInvalidation ( ) ;
59+
60+ // Native reads are deferred until a feature in the query asks for them
61+ let screenPixels : { width : number ; height : number } ;
62+ let appProperties : ReturnType < typeof getApplicationProperties > ;
63+ const getScreenPixels = ( ) => ( screenPixels ??= { width : Screen . mainScreen . widthPixels , height : Screen . mainScreen . heightPixels } ) ;
64+ const getAppProperties = ( ) => ( appProperties ??= getApplicationProperties ( ) ) ;
65+
66+ try {
67+ matches = matchQuery ( mediaQueryString , {
68+ type : MediaQueryType . screen ,
69+ get width ( ) {
70+ return getScreenPixels ( ) . width ;
71+ } ,
72+ get height ( ) {
73+ return getScreenPixels ( ) . height ;
74+ } ,
75+ get 'device-width' ( ) {
76+ return getScreenPixels ( ) . width ;
77+ } ,
78+ get 'device-height' ( ) {
79+ return getScreenPixels ( ) . height ;
80+ } ,
81+ get orientation ( ) {
82+ return getAppProperties ( ) . orientation ;
83+ } ,
84+ get 'prefers-color-scheme' ( ) {
85+ return getAppProperties ( ) . systemAppearance ;
86+ } ,
87+ } ) ;
88+ } catch ( err ) {
89+ matches = false ;
90+ Trace . write ( err , Trace . categories . MediaQuery , Trace . messageType . error ) ;
91+ }
92+ } else {
4693 matches = false ;
47- Trace . write ( err , Trace . categories . MediaQuery , Trace . messageType . error ) ;
94+ Trace . write ( `Media query ' ${ mediaQueryString } ' cannot be evaluated and will never match` , Trace . categories . MediaQuery , Trace . messageType . warn ) ;
4895 }
4996
97+ matchResultCache . set ( mediaQueryString , matches ) ;
98+
5099 return matches ;
51100}
52101
0 commit comments