Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 3555e14

Browse files
fix(MatchMediaObservable): register breakpoints so observable announces properly
Breakpoints are registered in the BreakPointRegistry. But the MatchMediaObservable does not iterate that registry to configure the default listeners. This means only the "all" breakpoint is announced when using the observable. Fixes #65. Closes #64.
1 parent ad58e11 commit 3555e14

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/lib/media-query/mock/mock-match-media.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import {BreakPointRegistry} from '../breakpoints/break-point-registry';
1010
@Injectable()
1111
export class MockMatchMedia extends MatchMedia {
1212

13+
/**
14+
* Special flag used to test BreakPoint registrations with MatchMedia
15+
*/
16+
public autoRegisterQueries : boolean = true;
17+
1318
constructor(_zone:NgZone, private _breakpoints:BreakPointRegistry ){
1419
super(_zone);
1520
this._actives = [ ];
@@ -119,7 +124,7 @@ export class MockMatchMedia extends MatchMedia {
119124
* Insure the mediaQuery is registered with MatchMedia
120125
*/
121126
private _registerMediaQuery(mediaQuery) {
122-
if ( !this._registry.has(mediaQuery) ) {
127+
if ( !this._registry.has(mediaQuery) && this.autoRegisterQueries ) {
123128
this.registerQuery(mediaQuery);
124129
}
125130
}

src/lib/media-query/providers/match-media-observable-provider.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,42 @@ describe('match-media-observable-provider', () => {
3535
});
3636
});
3737

38+
it('can can observe built-in mediaQueries', async(inject(
39+
[MatchMediaObservable, MatchMedia],
40+
(media$, matchMedia) => {
41+
let current : MediaChange;
42+
43+
expect( media$ ).toBeDefined();
44+
45+
let subscription = media$.subscribe((change:MediaChange)=>{
46+
current = change;
47+
});
48+
49+
// Confirm initial match is for 'all'
50+
expect(current).toBeDefined();
51+
expect(current.matches).toBeTruthy();
52+
expect(current.mediaQuery).toEqual("all");
53+
54+
try {
55+
matchMedia.autoRegisterQueries = false;
56+
57+
// Activate mediaQuery associated with 'md' alias
58+
matchMedia.activate('md');
59+
expect(current.mediaQuery).toEqual(findMediaQuery('md'));
60+
61+
matchMedia.activate('gt-lg');
62+
expect(current.mediaQuery).toEqual(findMediaQuery('gt-lg'));
63+
64+
matchMedia.activate('unknown');
65+
expect(current.mediaQuery).toEqual(findMediaQuery('gt-lg'));
66+
67+
} finally {
68+
matchMedia.autoRegisterQueries = true;
69+
subscription.unsubscribe();
70+
}
71+
})));
72+
73+
3874
it('can inject a MatchMediaObservable instance', async(inject(
3975
[MatchMediaObservable, MatchMedia],
4076
(media$, matchMedia) => {

src/lib/media-query/providers/match-media-observable-provider.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ import {BreakPoint} from '../breakpoints/break-point';
1717
* notification. For custom mediaQuery notifications, alias information will not be injected and those
1818
* fields will be ''.
1919
*
20-
* !! Only activation mediaChange notifications are publised by the MatchMediaObservable
20+
* !! Only mediaChange activations (not de-activations) are announced by the MatchMediaObservable
2121
*/
2222
export function instanceOfMatchMediaObservable(mediaWatcher: MatchMedia, breakpoints: BreakPointRegistry) {
2323
let onlyActivations = function(change : MediaChange) { return change.matches === true; };
2424
let findBreakpoint = function(mediaQuery:string):BreakPoint { return breakpoints.findByQuery(mediaQuery); };
2525
let injectAlias = function(change : MediaChange) { return mergeAlias(change, findBreakpoint(change.mediaQuery)); };
2626

27+
// Register all the mediaQueries registered in the BreakPointRegistry
28+
// This is needed so subscribers can be auto-notified of all standard, registered mediaQuery activations
29+
breakpoints.items.forEach( (bp:BreakPoint) => mediaWatcher.observe(bp.mediaQuery) );
30+
2731
// Note: the raw MediaChange events [from MatchMedia] do not contain important alias information
2832
// these must be injected into the MediaChange
2933
return mediaWatcher.observe( ).filter( onlyActivations ).map( injectAlias );
30-
};
34+
}
3135

3236
/**
3337
* Provider to return observable to ALL MediaQuery events

0 commit comments

Comments
 (0)