Skip to content

Commit

Permalink
fix(VImg): stop timers and events when unmounted
Browse files Browse the repository at this point in the history
fixes #18651
closes #18616
  • Loading branch information
KaelWD committed Nov 14, 2023
1 parent f5b13c1 commit e5f09a4
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/vuetify/src/components/VImg/VImg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
computed,
nextTick,
onBeforeMount,
onBeforeUnmount,
ref,
shallowRef,
vShow,
Expand All @@ -25,6 +26,7 @@ import {
import {
convertToUnit,
genericComponent,
getCurrentInstance,
propsFactory,
SUPPORTS_INTERSECTION,
useRender,
Expand Down Expand Up @@ -106,6 +108,8 @@ export const VImg = genericComponent<VImgSlots>()({
},

setup (props, { emit, slots }) {
const vm = getCurrentInstance('VImg')

const currentSrc = shallowRef('') // Set from srcset
const image = ref<HTMLImageElement>()
const state = shallowRef<'idle' | 'loading' | 'loaded' | 'error'>(props.eager ? 'loading' : 'idle')
Expand Down Expand Up @@ -165,6 +169,8 @@ export const VImg = genericComponent<VImgSlots>()({
emit('loadstart', image.value?.currentSrc || normalisedSrc.value.src)

setTimeout(() => {
if (vm.isUnmounted) return

if (image.value?.complete) {
if (!image.value.naturalWidth) {
onError()
Expand All @@ -183,13 +189,17 @@ export const VImg = genericComponent<VImgSlots>()({
}

function onLoad () {
if (vm.isUnmounted) return

getSrc()
pollForSize(image.value!)
state.value = 'loaded'
emit('load', image.value?.currentSrc || normalisedSrc.value.src)
}

function onError () {
if (vm.isUnmounted) return

state.value = 'error'
emit('error', image.value?.currentSrc || normalisedSrc.value.src)
}
Expand All @@ -200,9 +210,16 @@ export const VImg = genericComponent<VImgSlots>()({
}

let timer = -1

onBeforeUnmount(() => {
clearTimeout(timer)
})

function pollForSize (img: HTMLImageElement, timeout: number | null = 100) {
const poll = () => {
clearTimeout(timer)
if (vm.isUnmounted) return

const { naturalHeight: imgHeight, naturalWidth: imgWidth } = img

if (imgHeight || imgWidth) {
Expand Down

0 comments on commit e5f09a4

Please sign in to comment.