From ec5067ce7dce5da487f1b5cc10bce1dadd2ed84c Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 29 Sep 2021 17:36:50 +0800 Subject: [PATCH] perf: remove passive event polyfill (#9590) --- packages/vant-lazyload/src/util.js | 31 +++---------------- .../vant-use/src/useEventListener/index.ts | 22 +------------ .../vant/src/composables/use-lock-scroll.ts | 12 ++----- 3 files changed, 7 insertions(+), 58 deletions(-) diff --git a/packages/vant-lazyload/src/util.js b/packages/vant-lazyload/src/util.js index 704c75591..46fb4ca49 100644 --- a/packages/vant-lazyload/src/util.js +++ b/packages/vant-lazyload/src/util.js @@ -135,35 +135,12 @@ function throttle(action, delay) { }; } -function testSupportsPassive() { - if (!inBrowser) return; - let support = false; - try { - const opts = Object.defineProperty({}, 'passive', { - // eslint-disable-next-line getter-return - get() { - support = true; - }, - }); - window.addEventListener('test', null, opts); - } catch (e) { - // - } - return support; -} - -const supportsPassive = testSupportsPassive(); - const _ = { on(el, type, func, capture = false) { - if (supportsPassive) { - el.addEventListener(type, func, { - capture, - passive: true, - }); - } else { - el.addEventListener(type, func, capture); - } + el.addEventListener(type, func, { + capture, + passive: true, + }); }, off(el, type, func, capture = false) { el.removeEventListener(type, func, capture); diff --git a/packages/vant-use/src/useEventListener/index.ts b/packages/vant-use/src/useEventListener/index.ts index e4ec9f5c5..01f6d1c8e 100644 --- a/packages/vant-use/src/useEventListener/index.ts +++ b/packages/vant-use/src/useEventListener/index.ts @@ -2,22 +2,6 @@ import { Ref, watch, isRef, unref, onUnmounted, onDeactivated } from 'vue'; import { onMountedOrActivated } from '../onMountedOrActivated'; import { inBrowser } from '../utils'; -// eslint-disable-next-line -export let supportsPassive = false; - -if (inBrowser) { - try { - const opts = {}; - Object.defineProperty(opts, 'passive', { - get() { - supportsPassive = true; - }, - }); - window.addEventListener('test-passive', null as any, opts); - // eslint-disable-next-line no-empty - } catch (e) {} -} - type TargetRef = EventTarget | Ref; export type UseEventListenerOptions = { @@ -43,11 +27,7 @@ export function useEventListener( const element = unref(target); if (element && !attached) { - element.addEventListener( - type, - listener, - supportsPassive ? { capture, passive } : capture - ); + element.addEventListener(type, listener, { capture, passive }); attached = true; } }; diff --git a/packages/vant/src/composables/use-lock-scroll.ts b/packages/vant/src/composables/use-lock-scroll.ts index 642b01b44..dacc64229 100644 --- a/packages/vant/src/composables/use-lock-scroll.ts +++ b/packages/vant/src/composables/use-lock-scroll.ts @@ -1,9 +1,5 @@ import { Ref, watch, onBeforeUnmount, onDeactivated } from 'vue'; -import { - getScrollParent, - supportsPassive, - onMountedOrActivated, -} from '@vant/use'; +import { getScrollParent, onMountedOrActivated } from '@vant/use'; import { useTouch } from './use-touch'; import { preventDefault } from '../utils'; @@ -45,11 +41,7 @@ export function useLockScroll( const lock = () => { document.addEventListener('touchstart', touch.start); - document.addEventListener( - 'touchmove', - onTouchMove, - supportsPassive ? { passive: false } : false - ); + document.addEventListener('touchmove', onTouchMove, { passive: false }); if (!totalLockCount) { document.body.classList.add(BODY_LOCK_CLASS);