From d974751ecbce91039bbca0784527cf03a5e75b70 Mon Sep 17 00:00:00 2001 From: mister-ben Date: Thu, 22 Jun 2017 07:10:09 +0100 Subject: [PATCH 1/3] Use passive event listenes for touchstart/touchmove --- src/js/utils/events.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/js/utils/events.js b/src/js/utils/events.js index 97e4594fcc..664b35de72 100644 --- a/src/js/utils/events.js +++ b/src/js/utils/events.js @@ -202,6 +202,23 @@ export function fixEvent(event) { return event; } +/** + * Whether passive event listeners are supported + */ +let _supportsPassive = false; + +try { + const opts = Object.defineProperty({}, 'passive', { + get() { + _supportsPassive = true; + } + }); + + window.addEventListener('test', null, opts); +} catch (e) { + // disregard +} + /** * Add an event listener to element * It stores the handler function in a separate cache object @@ -273,7 +290,13 @@ export function on(elem, type, fn) { if (data.handlers[type].length === 1) { if (elem.addEventListener) { - elem.addEventListener(type, data.dispatcher, false); + let options = false; + + if (_supportsPassive && + ['touchstart', 'touchmove'].indexOf(type) > -1) { + options = {passive: true}; + } + elem.addEventListener(type, data.dispatcher, options); } else if (elem.attachEvent) { elem.attachEvent('on' + type, data.dispatcher); } From a3a3d76f73489eb097df88d43f5f93e4fda156b4 Mon Sep 17 00:00:00 2001 From: mister-ben Date: Mon, 26 Jun 2017 11:11:47 +0100 Subject: [PATCH 2/3] Define passive events --- src/js/utils/events.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/js/utils/events.js b/src/js/utils/events.js index 664b35de72..0a40dcb3aa 100644 --- a/src/js/utils/events.js +++ b/src/js/utils/events.js @@ -219,6 +219,14 @@ try { // disregard } +/** + * Touch events Chrome expects to be passive + */ +const passiveEvents = [ + 'touchstart', + 'touchmove' +]; + /** * Add an event listener to element * It stores the handler function in a separate cache object @@ -293,7 +301,7 @@ export function on(elem, type, fn) { let options = false; if (_supportsPassive && - ['touchstart', 'touchmove'].indexOf(type) > -1) { + passiveEvents.indexOf(type) > -1) { options = {passive: true}; } elem.addEventListener(type, data.dispatcher, options); From f9888748a517699f905c4758456491293955325d Mon Sep 17 00:00:00 2001 From: mister-ben Date: Mon, 26 Jun 2017 11:53:14 +0100 Subject: [PATCH 3/3] Wrap try-catch in IIFE --- src/js/utils/events.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/js/utils/events.js b/src/js/utils/events.js index 0a40dcb3aa..a98c39fb55 100644 --- a/src/js/utils/events.js +++ b/src/js/utils/events.js @@ -207,17 +207,19 @@ export function fixEvent(event) { */ let _supportsPassive = false; -try { - const opts = Object.defineProperty({}, 'passive', { - get() { - _supportsPassive = true; - } - }); +(function() { + try { + const opts = Object.defineProperty({}, 'passive', { + get() { + _supportsPassive = true; + } + }); - window.addEventListener('test', null, opts); -} catch (e) { - // disregard -} + window.addEventListener('test', null, opts); + } catch (e) { + // disregard + } +})(); /** * Touch events Chrome expects to be passive