Strict null check ScreenDprMonitor

This commit is contained in:
Daniel Imms
2019-04-28 02:16:28 +00:00
parent ebfa57b71c
commit 88d5cf46f8
2 changed files with 19 additions and 11 deletions
+17 -10
View File
@@ -18,10 +18,10 @@ export type ScreenDprListener = (newDevicePixelRatio?: number, oldDevicePixelRat
* monitor with a different DPI. * monitor with a different DPI.
*/ */
export class ScreenDprMonitor extends Disposable { export class ScreenDprMonitor extends Disposable {
private _currentDevicePixelRatio: number; private _currentDevicePixelRatio: number = window.devicePixelRatio;
private _outerListener: (this: MediaQueryList, ev: MediaQueryListEvent) => any; private _outerListener: ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | undefined;
private _listener: ScreenDprListener; private _listener: ScreenDprListener | undefined;
private _resolutionMediaMatchList: MediaQueryList; private _resolutionMediaMatchList: MediaQueryList | undefined;
public setListener(listener: ScreenDprListener): void { public setListener(listener: ScreenDprListener): void {
if (this._listener) { if (this._listener) {
@@ -29,6 +29,9 @@ export class ScreenDprMonitor extends Disposable {
} }
this._listener = listener; this._listener = listener;
this._outerListener = () => { this._outerListener = () => {
if (!this._listener) {
return;
}
this._listener(window.devicePixelRatio, this._currentDevicePixelRatio); this._listener(window.devicePixelRatio, this._currentDevicePixelRatio);
this._updateDpr(); this._updateDpr();
}; };
@@ -41,10 +44,13 @@ export class ScreenDprMonitor extends Disposable {
} }
private _updateDpr(): void { private _updateDpr(): void {
// Clear listeners for old DPR if (!this._resolutionMediaMatchList || !this._outerListener) {
if (this._resolutionMediaMatchList) { return;
this._resolutionMediaMatchList.removeListener(this._outerListener);
} }
// Clear listeners for old DPR
this._resolutionMediaMatchList.removeListener(this._outerListener);
// Add listeners for new DPR // Add listeners for new DPR
this._currentDevicePixelRatio = window.devicePixelRatio; this._currentDevicePixelRatio = window.devicePixelRatio;
this._resolutionMediaMatchList = window.matchMedia(`screen and (resolution: ${window.devicePixelRatio}dppx)`); this._resolutionMediaMatchList = window.matchMedia(`screen and (resolution: ${window.devicePixelRatio}dppx)`);
@@ -52,11 +58,12 @@ export class ScreenDprMonitor extends Disposable {
} }
public clearListener(): void { public clearListener(): void {
if (!this._listener) { if (!this._resolutionMediaMatchList || !this._listener || !this._outerListener) {
return; return;
} }
this._resolutionMediaMatchList.removeListener(this._outerListener); this._resolutionMediaMatchList.removeListener(this._outerListener);
this._listener = null; this._resolutionMediaMatchList = undefined;
this._outerListener = null; this._listener = undefined;
this._outerListener = undefined;
} }
} }
+2 -1
View File
@@ -12,7 +12,8 @@
}, },
"include": [ "include": [
"./Lifecycle.ts", "./Lifecycle.ts",
"./RenderDebouncer.ts" "./RenderDebouncer.ts",
"./ScreenDprMonitor.ts"
], ],
"references": [ "references": [
{ "path": "../common" } { "path": "../common" }