element-plus/packages/utils/after-leave.ts
2020-07-29 11:44:12 +08:00

25 lines
804 B
TypeScript

import { ComponentPublicInstance } from 'vue'
/**
* Bind after-leave event for vue instance. Make sure after-leave is called in any browsers.
*/
export default function(instance: ComponentPublicInstance, callback: (...args: unknown[]) => unknown, speed = 300): void {
if (!instance || !callback) throw new Error('instance & callback is required')
let called = false
const afterLeaveCallback = function(...args: unknown[]) {
if (called) return
called = true
if (callback) {
callback(args)
}
}
// TODO: migrate to [mitt](https://github.com/developit/mitt)
// if (once) {
// instance.$once('after-leave', afterLeaveCallback)
// } else {
// instance.$on('after-leave', afterLeaveCallback)
// }
setTimeout(() => {
afterLeaveCallback()
}, speed + 100)
}