Created
August 11, 2020 16:51
-
-
Save stevewithington/32ad085d23e43548c03b2615cf236540 to your computer and use it in GitHub Desktop.
TypeScript Passive Support Detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Safety detection for passive support | |
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support | |
*/ | |
initPassiveSupport = (): boolean => { | |
let supported = false; | |
let options = {}; | |
try { | |
options = { | |
get passive() { | |
supported = true; | |
return null; | |
} | |
}; | |
const callback = () => { | |
return null; | |
}; | |
window.addEventListener('TEST', callback, options); | |
window.removeEventListener('TEST', callback, options); | |
} catch (err) { | |
supported = false; | |
}; | |
return supported; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment