Skip to content

Commit 988c756

Browse files
committed
chore: improve type safety in breakpoint observer
* Fixes a workaround where we cast the MediaQueryList event listener to any because TypeScript didn't have proper types before TS 3.1. There is still some signature issue with our current TypeScript version, but at least we can now type all other declarations, and just have a missing type for `removeListener`. This has been fixed with microsoft/TypeScript@40bd7c8
1 parent 0fbe6dd commit 988c756

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

src/cdk/layout/breakpoints-observer.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,25 @@ export class BreakpointObserver implements OnDestroy {
9898
}
9999

100100
const mql: MediaQueryList = this.mediaMatcher.matchMedia(query);
101-
102-
// TODO(jelbourn): change this `any` to `MediaQueryListEvent` once Google has upgraded to
103-
// TypeScript 3.1 (the type is unavailable before then).
104-
let queryListener: any;
101+
let queryListener: (event: MediaQueryListEvent) => void;
105102

106103
// Create callback for match changes and add it is as a listener.
107-
const queryObservable = fromEventPattern<MediaQueryList>(
108-
// Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be placed
109-
// 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 not
104+
const queryObservable = fromEventPattern<MediaQueryListEvent>((listener: Function) => {
105+
// Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be
106+
// placed back into the zone because matchMedia is only included in Zone.js by loading the
107+
// webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do not
111108
// have MediaQueryList inherit from EventTarget, which causes inconsistencies in how Zone.js
112109
// patches it.
113-
(listener: Function) => {
114-
queryListener = (e: any) => this.zone.run(() => listener(e));
115-
mql.addListener(queryListener);
116-
},
117-
() => mql.removeListener(queryListener))
118-
.pipe(
119-
startWith(mql),
120-
map((nextMql: MediaQueryList) => ({query, matches: nextMql.matches})),
121-
takeUntil(this._destroySubject)
122-
);
110+
mql.addListener(queryListener = event => this.zone.run(() => listener(event)));
111+
}, () => {
112+
// TODO(devversion): This cast to "any" is just needed because our current TypeScript
113+
// version does have a wrong `removeListener` signature. Remove when updating to v3.2.1
114+
mql.removeListener(queryListener as any);
115+
}).pipe(
116+
startWith({query, matches: mql.matches}),
117+
map(nextMql => ({query, matches: nextMql.matches})),
118+
takeUntil(this._destroySubject)
119+
);
123120

124121
// Add the MediaQueryList to the set of queries.
125122
const output = {observable: queryObservable, mql};

test/browser-providers.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* - local: Launches the browser locally on the current operating system.
77
* - BS: Launches the browser within BrowserStack
88
* - SL: Launches the browser within Saucelabs
9-
*
10-
* TODO(devversion): rename this to "browserstack" and "saucelabs".
119
*/
1210
const browserConfig = {
1311
'ChromeHeadlessCI': { unitTest: {target: 'local', }},

0 commit comments

Comments
 (0)