Skip to content

Commit

Permalink
Clear timing events and event listeners before component unmounts (#4000
Browse files Browse the repository at this point in the history
)

* Clear timing events and event listeners before component unmounts
- The following components clear timeouts before unmounted so that timeout callbacks
  won't access unmounted component instances:
    - `Autocomplete`
    - `ColorpickerHSLRepresentationSquare`
    - `ColorpickerHSLRepresentationTriangle`
    - `Dropdown`
    - `Slider`
    - `Taginput`
    - `Tooltip`

* feat(lib): revert unnecessary clearTimeout

- Reverts unnecessary `clearTimeout` calls in:
    - `Dialog`: #4000 (comment)
    - `Loading`: #4000 (comment)
    - `Modal`: #4000 (comment)
    - `NotificationNotice`: #4000 (comment)

---------

Co-authored-by: Kikuo Emoto <kemoto@codemonger.io>
  • Loading branch information
Arooba-git and kikuomax committed Feb 9, 2024
1 parent b9df92c commit a13d844
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/components/autocomplete/Autocomplete.vue
Expand Up @@ -173,7 +173,8 @@ export default {
style: {},
_isAutocomplete: true,
_elementRef: 'input',
_bodyEl: undefined // Used to append to body
_bodyEl: undefined, // Used to append to body
timeOutID: null
}
},
computed: {
Expand Down Expand Up @@ -311,7 +312,7 @@ export default {
this.calcDropdownInViewportVertical()
} else {
// Timeout to wait for the animation to finish before recalculating
setTimeout(() => {
this.timeOutID = setTimeout(() => {
this.calcDropdownInViewportVertical()
}, 100)
}
Expand Down Expand Up @@ -729,6 +730,7 @@ export default {
if (this.appendToBody) {
removeElement(this.$data._bodyEl)
}
clearTimeout(this.timeOutID)
}
}
</script>
Expand Up @@ -361,6 +361,8 @@ export default {
window.removeEventListener('touchmove', this.trackMouse)
window.removeEventListener('mouseup', this.stopMouseCapture)
window.removeEventListener('touchend', this.stopMouseCapture)
clearTimeout(this.debounce)
}
}
</script>
Expand Up @@ -439,6 +439,8 @@ export default {
window.removeEventListener('touchmove', this.trackMouse)
window.removeEventListener('mouseup', this.stopMouseCapture)
window.removeEventListener('touchend', this.stopMouseCapture)
clearTimeout(this.debounce)
}
}
</script>
10 changes: 7 additions & 3 deletions src/components/dropdown/Dropdown.vue
Expand Up @@ -142,7 +142,9 @@ export default {
isHoverable: false,
maybeTap: false,
isTouchEnabled: false,
_bodyEl: undefined // Used to append to body
_bodyEl: undefined, // Used to append to body
timeOutID: null,
timeOutID2: null
}
},
computed: {
Expand Down Expand Up @@ -198,7 +200,7 @@ export default {
// also takes care of chattering, e.g., repeated quick taps,
// otherwise the flag may become inconsistent with the actual
// state of the dropdown menu
setTimeout(() => {
this.timeOutID = setTimeout(() => {
if (!this.isActive) {
this.isTouchEnabled = false
}
Expand Down Expand Up @@ -368,7 +370,7 @@ export default {
const value = !this.isActive
this.isActive = value
// Vue 2.6.x ???
setTimeout(() => (this.isActive = value))
this.timeOutID2 = setTimeout(() => (this.isActive = value))
})
} else {
this.isActive = !this.isActive
Expand Down Expand Up @@ -447,6 +449,8 @@ export default {
if (this.appendToBody) {
removeElement(this.$data._bodyEl)
}
clearTimeout(this.timeOutID)
clearTimeout(this.timeOutID2)
}
}
</script>
9 changes: 7 additions & 2 deletions src/components/slider/Slider.vue
Expand Up @@ -151,7 +151,8 @@ export default {
value2: null,
dragging: false,
isRange: false,
_isSlider: true // Used by Thumb and Tick
_isSlider: true, // Used by Thumb and Tick
timeOutID: null
}
},
computed: {
Expand Down Expand Up @@ -288,7 +289,7 @@ export default {
},
onDragEnd() {
this.isTrackClickDisabled = true
setTimeout(() => {
this.timeOutID = setTimeout(() => {
// avoid triggering onSliderClick after dragend
this.isTrackClickDisabled = false
}, 0)
Expand All @@ -308,6 +309,10 @@ export default {
this.isThumbReversed = false
this.isTrackClickDisabled = false
this.setValues(this.value)
},
beforeDestroy() {
clearTimeout(this.timeOutID)
}
}
</script>
9 changes: 7 additions & 2 deletions src/components/taginput/Taginput.vue
Expand Up @@ -196,7 +196,8 @@ export default {
newTag: '',
isComposing: false,
_elementRef: 'autocomplete',
_isTaginput: true
_isTaginput: true,
requestID: null
}
},
computed: {
Expand Down Expand Up @@ -301,7 +302,7 @@ export default {
}
// after autocomplete events
requestAnimationFrame(() => {
this.requestID = requestAnimationFrame(() => {
this.newTag = ''
this.$emit('typing', '')
})
Expand Down Expand Up @@ -372,6 +373,10 @@ export default {
emitInfiniteScroll() {
this.$emit('infinite-scroll')
}
},
beforeDestroy() {
cancelAnimationFrame(this.requestID)
}
}
</script>
13 changes: 10 additions & 3 deletions src/components/tooltip/Tooltip.vue
Expand Up @@ -96,7 +96,8 @@ export default {
timer: null,
_bodyEl: undefined, // Used to append to body
resizeObserver: undefined,
resizeListener: undefined
resizeListener: undefined,
timeOutID: null
}
},
computed: {
Expand Down Expand Up @@ -199,7 +200,7 @@ export default {
// if not active, toggle after clickOutside event
// this fixes toggling programmatic
this.$nextTick(() => {
setTimeout(() => this.open())
this.timeOutID = setTimeout(() => this.open())
})
},
onHover() {
Expand Down Expand Up @@ -284,6 +285,7 @@ export default {
}
},
mounted() {
this.controller = new window.AbortController()
if (this.appendToBody && typeof window !== 'undefined') {
this.$data._bodyEl = createAbsoluteElement(this.$refs.content)
this.updateAppendToBody()
Expand All @@ -295,7 +297,9 @@ export default {
this.updateAppendToBody()
animation.removeEventListener('transitionend', listener)
}
animation.addEventListener('transitionend', listener)
animation.addEventListener('transitionend', listener, {
signal: this.controller.signal
})
}
// observes changes in the window size
this.resizeListener = () => this.updateAppendToBody()
Expand Down Expand Up @@ -327,6 +331,9 @@ export default {
if (this.appendToBody) {
removeElement(this.$data._bodyEl)
}
this.controller.abort()
clearTimeout(this.timer)
clearTimeout(this.timeOutID)
}
}
</script>

0 comments on commit a13d844

Please sign in to comment.