perf: virtual list remove eventListener

pull/2930/head^2
tanjinzhou 2020-09-29 16:11:05 +08:00
parent c579bb0ad5
commit b9f9dad4c7
2 changed files with 22 additions and 13 deletions

View File

@ -7,7 +7,7 @@ import useFrameWheel from './hooks/useFrameWheel';
import useMobileTouchMove from './hooks/useMobileTouchMove'; import useMobileTouchMove from './hooks/useMobileTouchMove';
import useOriginScroll from './hooks/useOriginScroll'; import useOriginScroll from './hooks/useOriginScroll';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { computed, nextTick, reactive, watchEffect } from 'vue'; import { computed, nextTick, onBeforeUnmount, reactive, watchEffect } from 'vue';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import createRef from '../_util/createRef'; import createRef from '../_util/createRef';
@ -212,18 +212,23 @@ const List = {
onRawWheel({ preventDefault() {}, deltaY }); onRawWheel({ preventDefault() {}, deltaY });
return true; return true;
}); });
watchEffect(() => {
nextTick(() => {
if (componentRef.current) {
// Firefox only // Firefox only
function onMozMousePixelScroll(e) { function onMozMousePixelScroll(e) {
if (inVirtual.value) { if (inVirtual.value) {
e.preventDefault(); e.preventDefault();
} }
} }
const removeEventListener = () => {
if (componentRef.current) {
componentRef.current.removeEventListener('wheel', onRawWheel); componentRef.current.removeEventListener('wheel', onRawWheel);
componentRef.current.removeEventListener('DOMMouseScroll', onFireFoxScroll); componentRef.current.removeEventListener('DOMMouseScroll', onFireFoxScroll);
componentRef.current.removeEventListener('MozMousePixelScroll', onMozMousePixelScroll); componentRef.current.removeEventListener('MozMousePixelScroll', onMozMousePixelScroll);
}
};
watchEffect(() => {
nextTick(() => {
if (componentRef.current) {
removeEventListener();
componentRef.current.addEventListener('wheel', onRawWheel); componentRef.current.addEventListener('wheel', onRawWheel);
componentRef.current.addEventListener('DOMMouseScroll', onFireFoxScroll); componentRef.current.addEventListener('DOMMouseScroll', onFireFoxScroll);
componentRef.current.addEventListener('MozMousePixelScroll', onMozMousePixelScroll); componentRef.current.addEventListener('MozMousePixelScroll', onMozMousePixelScroll);
@ -231,6 +236,10 @@ const List = {
}); });
}); });
onBeforeUnmount(() => {
removeEventListener();
});
// ================================= Ref ================================== // ================================= Ref ==================================
const scrollTo = useScrollTo( const scrollTo = useScrollTo(
componentRef, componentRef,

View File

@ -1,3 +1,4 @@
import { reactive } from 'vue';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import createRef from '../_util/createRef'; import createRef from '../_util/createRef';
import raf from '../_util/raf'; import raf from '../_util/raf';
@ -46,12 +47,12 @@ export default {
scrollbarRef: createRef(), scrollbarRef: createRef(),
thumbRef: createRef(), thumbRef: createRef(),
visibleTimeout: null, visibleTimeout: null,
state: { state: reactive({
dragging: false, dragging: false,
pageY: null, pageY: null,
startTop: null, startTop: null,
visible: false, visible: false,
}, }),
}; };
}, },
watch: { watch: {
@ -79,7 +80,6 @@ export default {
this.visibleTimeout = setTimeout(() => { this.visibleTimeout = setTimeout(() => {
this.state.visible = false; this.state.visible = false;
this.$forceUpdate(); // why ?
}, 2000); }, 2000);
}, },