Skip to content

Commit

Permalink
fix(transition): avoid invoking stale transition end callbacks
Browse files Browse the repository at this point in the history
fix #2482
  • Loading branch information
yyx990803 committed Dec 2, 2020
1 parent 5ad6ed3 commit eaf8a67
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions packages/runtime-dom/src/components/Transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ export function resolveTransitionProps(
removeTransitionClass(el, isAppear ? appearFromClass : enterFromClass)
addTransitionClass(el, isAppear ? appearToClass : enterToClass)
if (!(hook && hook.length > 1)) {
if (enterDuration) {
setTimeout(resolve, enterDuration)
} else {
whenTransitionEnds(el, type, resolve)
}
whenTransitionEnds(el, type, enterDuration, resolve)
}
})
}
Expand All @@ -157,11 +153,7 @@ export function resolveTransitionProps(
removeTransitionClass(el, leaveFromClass)
addTransitionClass(el, leaveToClass)
if (!(onLeave && onLeave.length > 1)) {
if (leaveDuration) {
setTimeout(resolve, leaveDuration)
} else {
whenTransitionEnds(el, type, resolve)
}
whenTransitionEnds(el, type, leaveDuration, resolve)
}
})
onLeave && onLeave(el, resolve)
Expand Down Expand Up @@ -247,27 +239,39 @@ function nextFrame(cb: () => void) {
})
}

let endId = 0

function whenTransitionEnds(
el: Element,
el: Element & { _endId?: number },
expectedType: TransitionProps['type'] | undefined,
cb: () => void
explicitTimeout: number | null,
resolve: () => void
) {
const id = (el._endId = ++endId)
const resolveIfNotStale = () => {
if (id === el._endId) {
resolve()
}
}

if (explicitTimeout) {
return setTimeout(resolveIfNotStale, explicitTimeout)
}

const { type, timeout, propCount } = getTransitionInfo(el, expectedType)
if (!type) {
return cb()
return resolve()
}

const endEvent = type + 'end'
let ended = 0
const end = () => {
el.removeEventListener(endEvent, onEnd)
cb()
resolveIfNotStale()
}
const onEnd = (e: Event) => {
if (e.target === el) {
if (++ended >= propCount) {
end()
}
if (e.target === el && ++ended >= propCount) {
end()
}
}
setTimeout(() => {
Expand Down

0 comments on commit eaf8a67

Please sign in to comment.