import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
import { warn } from './warning'
import { capitalize } from '@vue/shared'
-import { pauseTracking, resumeTracking } from '@vue/reactivity'
+import { pauseTracking, resumeTracking, DebuggerEvent } from '@vue/reactivity'
function injectHook(
type: LifecycleHooks,
}
}
-export function onBeforeMount(
- hook: Function,
- target: ComponentInternalInstance | null = currentInstance
-) {
- injectHook(LifecycleHooks.BEFORE_MOUNT, hook, target)
-}
-
-export function onMounted(
- hook: Function,
+const createHook = <T extends Function = () => any>(
+ lifecycle: LifecycleHooks
+) => (
+ hook: T,
target: ComponentInternalInstance | null = currentInstance
-) {
- injectHook(LifecycleHooks.MOUNTED, hook, target)
-}
+) => injectHook(lifecycle, hook, target)
-export function onBeforeUpdate(
- hook: Function,
- target: ComponentInternalInstance | null = currentInstance
-) {
- injectHook(LifecycleHooks.BEFORE_UPDATE, hook, target)
-}
+export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
+export const onMounted = createHook(LifecycleHooks.MOUNTED)
+export const onBeforeUpdate = createHook(LifecycleHooks.BEFORE_UPDATE)
+export const onUpdated = createHook(LifecycleHooks.UPDATED)
+export const onBeforeUnmount = createHook(LifecycleHooks.BEFORE_UNMOUNT)
+export const onUnmounted = createHook(LifecycleHooks.UNMOUNTED)
-export function onUpdated(
- hook: Function,
- target: ComponentInternalInstance | null = currentInstance
-) {
- injectHook(LifecycleHooks.UPDATED, hook, target)
-}
+type DebuggerHook = (e: DebuggerEvent) => void
+export const onRenderTriggered = createHook<DebuggerHook>(
+ LifecycleHooks.RENDER_TRIGGERED
+)
+export const onRenderTracked = createHook<DebuggerHook>(
+ LifecycleHooks.RENDER_TRACKED
+)
-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)
-}
+type ErrorCapturedHook = (
+ err: Error,
+ instance: ComponentPublicInstance | null,
+ info: string
+) => boolean | void
+export const onErrorCaptured = createHook<ErrorCapturedHook>(
+ LifecycleHooks.ERROR_CAPTURED
+)