Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Created August 11, 2020 16:51
Show Gist options
  • Save stevewithington/32ad085d23e43548c03b2615cf236540 to your computer and use it in GitHub Desktop.
Save stevewithington/32ad085d23e43548c03b2615cf236540 to your computer and use it in GitHub Desktop.
TypeScript Passive Support Detection
/**
* 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