perf: remove passive event polyfill (#9590)

This commit is contained in:
neverland 2021-09-29 17:36:50 +08:00 committed by GitHub
parent 7aba794d80
commit ec5067ce7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 58 deletions

View File

@ -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);

View File

@ -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<EventTarget | undefined>;
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;
}
};

View File

@ -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);