perf(mixin/placeable): register listeners on demand

This commit is contained in:
07akioni 2020-05-19 00:10:51 +08:00
parent 61ea1063bb
commit bd0584f319

View File

@ -1,6 +1,5 @@
import scrollDelegate from '../_utils/delegate/scrollDelegate' import scrollDelegate from '../_utils/delegate/scrollDelegate'
import resizeDelegate from '../_utils/delegate/resizeDelegate' import resizeDelegate from '../_utils/delegate/resizeDelegate'
import getParentNode from '../_utils/dom/getParentNode'
import getScrollParent from '../_utils/dom/getScrollParent' import getScrollParent from '../_utils/dom/getScrollParent'
import { import {
getAdjustedPlacementOfTrackingElement, getAdjustedPlacementOfTrackingElement,
@ -169,6 +168,11 @@ export default {
watch: { watch: {
active (value) { active (value) {
if (value) { if (value) {
if (this.listenersRegistered) {
this.registerScrollListeners()
this.registerResizeListener()
this.listenersRegistered = true
}
this.$nextTick().then(this.updatePosition) this.$nextTick().then(this.updatePosition)
} }
}, },
@ -184,17 +188,23 @@ export default {
trackingElement: null, trackingElement: null,
trackedElement: null, trackedElement: null,
scrollListeners: [], scrollListeners: [],
adjustedPlacement: this.placement adjustedPlacement: this.placement,
listenersRegistered: false
} }
}, },
mounted () { mounted () {
this.registerScrollListeners() if (this.active) {
this.registerResizeListener() this.registerScrollListeners()
this.registerResizeListener()
this.listenersRegistered = true
}
this.updatePosition() this.updatePosition()
}, },
beforeDestroy () { beforeDestroy () {
this.unregisterScrollListeners() if (this.listenersRegistered) {
this.unregisterResizeListener() this.unregisterScrollListeners()
this.unregisterResizeListener()
}
}, },
methods: { methods: {
_getTrackingElement () { _getTrackingElement () {