vue3-yuanma/packages/runtime-core/src/apiLifecycle.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-05-28 19:36:15 +08:00
import { ComponentInstance, LifecycleHooks, currentInstance } from './component'
function injectHook(
name: keyof LifecycleHooks,
2019-08-29 00:13:36 +08:00
hook: Function,
2019-05-28 19:36:15 +08:00
target: ComponentInstance | null | void = currentInstance
) {
if (target) {
2019-05-28 19:59:54 +08:00
// TODO inject a error-handling wrapped version of the hook
2019-06-06 13:04:49 +08:00
// TODO also set currentInstance when calling the hook
2019-05-29 23:44:59 +08:00
;(target[name] || (target[name] = [])).push(hook)
2019-05-28 19:36:15 +08:00
} else {
// TODO warn
}
}
2019-08-29 00:13:36 +08:00
export function onBeforeMount(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('bm', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onMounted(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('m', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onBeforeUpdate(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('bu', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onUpdated(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('u', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onBeforeUnmount(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('bum', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onUnmounted(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('um', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onRenderTriggered(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('rtg', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onRenderTracked(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('rtc', hook, target)
}
2019-08-29 00:13:36 +08:00
export function onErrorCaptured(hook: Function, target?: ComponentInstance) {
2019-05-28 19:36:15 +08:00
injectHook('ec', hook, target)
}