refactor(runtime-core): programmatically create lifecycle APIs for DRYness (#246)
This commit is contained in:
parent
1baa19aea5
commit
70a142c203
@ -8,7 +8,7 @@ import { ComponentPublicInstance } from './componentProxy'
|
|||||||
import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
|
import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
import { capitalize } from '@vue/shared'
|
import { capitalize } from '@vue/shared'
|
||||||
import { pauseTracking, resumeTracking } from '@vue/reactivity'
|
import { pauseTracking, resumeTracking, DebuggerEvent } from '@vue/reactivity'
|
||||||
|
|
||||||
function injectHook(
|
function injectHook(
|
||||||
type: LifecycleHooks,
|
type: LifecycleHooks,
|
||||||
@ -48,69 +48,33 @@ function injectHook(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onBeforeMount(
|
const createHook = <T extends Function = () => any>(
|
||||||
hook: Function,
|
lifecycle: LifecycleHooks
|
||||||
|
) => (
|
||||||
|
hook: T,
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
target: ComponentInternalInstance | null = currentInstance
|
||||||
) {
|
) => injectHook(lifecycle, hook, target)
|
||||||
injectHook(LifecycleHooks.BEFORE_MOUNT, hook, target)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onMounted(
|
export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
|
||||||
hook: Function,
|
export const onMounted = createHook(LifecycleHooks.MOUNTED)
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
export const onBeforeUpdate = createHook(LifecycleHooks.BEFORE_UPDATE)
|
||||||
) {
|
export const onUpdated = createHook(LifecycleHooks.UPDATED)
|
||||||
injectHook(LifecycleHooks.MOUNTED, hook, target)
|
export const onBeforeUnmount = createHook(LifecycleHooks.BEFORE_UNMOUNT)
|
||||||
}
|
export const onUnmounted = createHook(LifecycleHooks.UNMOUNTED)
|
||||||
|
|
||||||
export function onBeforeUpdate(
|
type DebuggerHook = (e: DebuggerEvent) => void
|
||||||
hook: Function,
|
export const onRenderTriggered = createHook<DebuggerHook>(
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
LifecycleHooks.RENDER_TRIGGERED
|
||||||
) {
|
)
|
||||||
injectHook(LifecycleHooks.BEFORE_UPDATE, hook, target)
|
export const onRenderTracked = createHook<DebuggerHook>(
|
||||||
}
|
LifecycleHooks.RENDER_TRACKED
|
||||||
|
)
|
||||||
|
|
||||||
export function onUpdated(
|
type ErrorCapturedHook = (
|
||||||
hook: Function,
|
err: Error,
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
instance: ComponentPublicInstance | null,
|
||||||
) {
|
info: string
|
||||||
injectHook(LifecycleHooks.UPDATED, hook, target)
|
) => boolean | void
|
||||||
}
|
export const onErrorCaptured = createHook<ErrorCapturedHook>(
|
||||||
|
LifecycleHooks.ERROR_CAPTURED
|
||||||
export function onBeforeUnmount(
|
)
|
||||||
hook: Function,
|
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
|
||||||
) {
|
|
||||||
injectHook(LifecycleHooks.BEFORE_UNMOUNT, hook, target)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onUnmounted(
|
|
||||||
hook: Function,
|
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
|
||||||
) {
|
|
||||||
injectHook(LifecycleHooks.UNMOUNTED, hook, target)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onRenderTriggered(
|
|
||||||
hook: Function,
|
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
|
||||||
) {
|
|
||||||
injectHook(LifecycleHooks.RENDER_TRIGGERED, hook, target)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onRenderTracked(
|
|
||||||
hook: Function,
|
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
|
||||||
) {
|
|
||||||
injectHook(LifecycleHooks.RENDER_TRACKED, hook, target)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onErrorCaptured(
|
|
||||||
hook: (
|
|
||||||
err: Error,
|
|
||||||
instance: ComponentPublicInstance | null,
|
|
||||||
info: string
|
|
||||||
) => boolean | void,
|
|
||||||
target: ComponentInternalInstance | null = currentInstance
|
|
||||||
) {
|
|
||||||
injectHook(LifecycleHooks.ERROR_CAPTURED, hook, target)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user