@@ -43,6 +43,7 @@ export interface ElementMatcher {
4343 */
4444@Injectable ( { providedIn : 'root' } )
4545export class MediaMarshaller {
46+ private _useFallbacks = true ;
4647 private activatedBreakpoints : BreakPoint [ ] = [ ] ;
4748 private elementMap : ElementMap = new Map ( ) ;
4849 private elementKeyMap : ElementKeyMap = new WeakMap ( ) ;
@@ -53,7 +54,11 @@ export class MediaMarshaller {
5354 private subject : Subject < ElementMatcher > = new Subject ( ) ;
5455
5556 get activatedAlias ( ) : string {
56- return this . activatedBreakpoints [ 0 ] ? this . activatedBreakpoints [ 0 ] . alias : '' ;
57+ return this . activatedBreakpoints [ 0 ] ?. alias ?? '' ;
58+ }
59+
60+ set useFallbacks ( value : boolean ) {
61+ this . _useFallbacks = value ;
5762 }
5863
5964 constructor ( protected matchMedia : MatchMedia ,
@@ -68,18 +73,20 @@ export class MediaMarshaller {
6873 */
6974 onMediaChange ( mc : MediaChange ) {
7075 const bp : BreakPoint | null = this . findByQuery ( mc . mediaQuery ) ;
76+
7177 if ( bp ) {
7278 mc = mergeAlias ( mc , bp ) ;
7379
74- if ( mc . matches && this . activatedBreakpoints . indexOf ( bp ) === - 1 ) {
80+ const bpIndex = this . activatedBreakpoints . indexOf ( bp ) ;
81+
82+ if ( mc . matches && bpIndex === - 1 ) {
7583 this . activatedBreakpoints . push ( bp ) ;
7684 this . activatedBreakpoints . sort ( sortDescendingPriority ) ;
7785
7886 this . updateStyles ( ) ;
79-
80- } else if ( ! mc . matches && this . activatedBreakpoints . indexOf ( bp ) !== - 1 ) {
87+ } else if ( ! mc . matches && bpIndex !== - 1 ) {
8188 // Remove the breakpoint when it's deactivated
82- this . activatedBreakpoints . splice ( this . activatedBreakpoints . indexOf ( bp ) , 1 ) ;
89+ this . activatedBreakpoints . splice ( bpIndex , 1 ) ;
8390 this . activatedBreakpoints . sort ( sortDescendingPriority ) ;
8491
8592 this . updateStyles ( ) ;
@@ -193,7 +200,6 @@ export class MediaMarshaller {
193200 this . clearElement ( el , k ) ;
194201 }
195202 } ) ;
196-
197203 } ) ;
198204 }
199205
@@ -204,6 +210,7 @@ export class MediaMarshaller {
204210 */
205211 clearElement ( element : HTMLElement , key : string ) : void {
206212 const builders = this . clearMap . get ( element ) ;
213+
207214 if ( builders ) {
208215 const clearFn : ClearCallback = builders . get ( key ) as ClearCallback ;
209216 if ( ! ! clearFn ) {
@@ -316,12 +323,20 @@ export class MediaMarshaller {
316323 for ( let i = 0 ; i < this . activatedBreakpoints . length ; i ++ ) {
317324 const activatedBp = this . activatedBreakpoints [ i ] ;
318325 const valueMap = bpMap . get ( activatedBp . alias ) ;
326+
319327 if ( valueMap ) {
320328 if ( key === undefined || ( valueMap . has ( key ) && valueMap . get ( key ) != null ) ) {
321329 return valueMap ;
322330 }
323331 }
324332 }
333+
334+ // On the server, we explicitly have an "all" section filled in to begin with.
335+ // So we don't need to aggressively find a fallback if no explicit value exists.
336+ if ( ! this . _useFallbacks ) {
337+ return undefined ;
338+ }
339+
325340 const lastHope = bpMap . get ( '' ) ;
326341 return ( key === undefined || lastHope && lastHope . has ( key ) ) ? lastHope : undefined ;
327342 }
@@ -347,14 +362,11 @@ export class MediaMarshaller {
347362function initBuilderMap ( map : BuilderMap ,
348363 element : HTMLElement ,
349364 key : string ,
350- input ?: UpdateCallback | ClearCallback ) : void {
365+ input ?: Builder ) : void {
351366 if ( input !== undefined ) {
352- let oldMap = map . get ( element ) ;
353- if ( ! oldMap ) {
354- oldMap = new Map ( ) ;
355- map . set ( element , oldMap ) ;
356- }
367+ const oldMap = map . get ( element ) ?? new Map ( ) ;
357368 oldMap . set ( key , input ) ;
369+ map . set ( element , oldMap ) ;
358370 }
359371}
360372
0 commit comments