]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(runtime-core): programmatically create lifecycle APIs for DRYness (#246)
authorfisker Cheung <lionkay@gmail.com>
Mon, 14 Oct 2019 03:18:34 +0000 (11:18 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 14 Oct 2019 03:18:34 +0000 (23:18 -0400)
packages/runtime-core/src/apiLifecycle.ts

index 833d902c00b26f30a06733d3be293be5f4502c75..f8303616bc84ad323d47594a8e8e24b0cc7ce0a4 100644 (file)
@@ -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,
-  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
+)