From 70a142c203e4e88d3e26b36ec0ee152ac0c7e6ae Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 14 Oct 2019 11:18:34 +0800 Subject: [PATCH] refactor(runtime-core): programmatically create lifecycle APIs for DRYness (#246) --- packages/runtime-core/src/apiLifecycle.ts | 90 +++++++---------------- 1 file changed, 27 insertions(+), 63 deletions(-) diff --git a/packages/runtime-core/src/apiLifecycle.ts b/packages/runtime-core/src/apiLifecycle.ts index 833d902c..f8303616 100644 --- a/packages/runtime-core/src/apiLifecycle.ts +++ b/packages/runtime-core/src/apiLifecycle.ts @@ -8,7 +8,7 @@ import { ComponentPublicInstance } from './componentProxy' 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, @@ -48,69 +48,33 @@ function injectHook( } } -export function onBeforeMount( - hook: Function, +const createHook = any>( + lifecycle: LifecycleHooks +) => ( + hook: T, target: ComponentInternalInstance | null = currentInstance -) { - injectHook(LifecycleHooks.BEFORE_MOUNT, hook, target) -} +) => injectHook(lifecycle, hook, target) -export function onMounted( - hook: Function, - target: ComponentInternalInstance | null = currentInstance -) { - injectHook(LifecycleHooks.MOUNTED, 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 onBeforeUpdate( - hook: Function, - target: ComponentInternalInstance | null = currentInstance -) { - injectHook(LifecycleHooks.BEFORE_UPDATE, hook, target) -} +type DebuggerHook = (e: DebuggerEvent) => void +export const onRenderTriggered = createHook( + LifecycleHooks.RENDER_TRIGGERED +) +export const onRenderTracked = createHook( + LifecycleHooks.RENDER_TRACKED +) -export function onUpdated( - hook: Function, - target: ComponentInternalInstance | null = currentInstance -) { - injectHook(LifecycleHooks.UPDATED, hook, target) -} - -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( + LifecycleHooks.ERROR_CAPTURED +)