-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2066 from filrak/lh-imp
LH improvements
- Loading branch information
Showing
22 changed files
with
162 additions
and
295 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const eventListenerOptionsSupported = () => { | ||
let supported = false | ||
|
||
try { | ||
const opts = Object.defineProperty({}, 'passive', { | ||
get () { | ||
supported = true | ||
} | ||
}) | ||
|
||
window.addEventListener('test', null, opts) | ||
window.removeEventListener('test', null, opts) | ||
} catch (e) {} | ||
|
||
return supported | ||
} | ||
|
||
const defaultOptions = { | ||
passive: true, | ||
capture: false | ||
} | ||
const supportedPassiveTypes = [ | ||
'scroll', 'wheel', | ||
'touchstart', 'touchmove', 'touchenter', 'touchend', 'touchleave', | ||
'mouseout', 'mouseleave', 'mouseup', 'mousedown', 'mousemove', 'mouseenter', 'mousewheel', 'mouseover' | ||
] | ||
const getDefaultPassiveOption = (passive, eventName) => { | ||
if (passive !== undefined) return passive | ||
|
||
return supportedPassiveTypes.indexOf(eventName) === -1 ? false : defaultOptions.passive | ||
} | ||
|
||
const getWritableOptions = (options) => { | ||
const passiveDescriptor = Object.getOwnPropertyDescriptor(options, 'passive') | ||
|
||
return passiveDescriptor && passiveDescriptor.writable !== true && passiveDescriptor.set === undefined ? Object.assign({}, options) : options | ||
} | ||
|
||
const overwriteAddEvent = (superMethod) => { | ||
EventTarget.prototype.addEventListener = function (type, listener, options) { | ||
const usesListenerOptions = typeof options === 'object' && options !== null | ||
const useCapture = usesListenerOptions ? options.capture : options | ||
|
||
options = usesListenerOptions ? getWritableOptions(options) : {} | ||
options.passive = getDefaultPassiveOption(options.passive, type) | ||
options.capture = useCapture === undefined ? defaultOptions.capture : useCapture | ||
|
||
superMethod.call(this, type, listener, options) | ||
} | ||
|
||
EventTarget.prototype.addEventListener._original = superMethod | ||
} | ||
|
||
const supportsPassive = eventListenerOptionsSupported() | ||
|
||
if (supportsPassive) { | ||
const addEvent = EventTarget.prototype.addEventListener | ||
overwriteAddEvent(addEvent) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.