Avoid try/catch, DOM and workers

This commit is contained in:
Andreas Hocevar
2020-03-21 15:49:01 +01:00
parent 6dcc54bfb8
commit 3f7f999db0
6 changed files with 85 additions and 100 deletions
+4 -16
View File
@@ -37,27 +37,15 @@ export const MAC = ua.indexOf('macintosh') !== -1;
* @type {number}
* @api
*/
export const DEVICE_PIXEL_RATIO = (function() {
try {
return self.devicePixelRatio;
} catch (e) {
return window.devicePixelRatio || 1;
}
})();
export const DEVICE_PIXEL_RATIO = (typeof self !== 'undefined' ? self.devicePixelRatio : window.devicePixelRatio) || 1;
/**
* The execution context is a window.
* The execution context is a worker with OffscreenCanvas available.
* @const
* @type {boolean}
*/
export const WINDOW = (function() {
try {
return 'document' in self;
} catch (e) {
// ancient browsers don't have `self`
return true;
}
})();
export const WORKER_OFFSCREEN_CANVAS = typeof WorkerGlobalScope !== 'undefined' && typeof OffscreenCanvas !== 'undefined' &&
self instanceof WorkerGlobalScope; //eslint-disable-line
/**
* Image.prototype.decode() is supported.