]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: call instead of wrap
authorEvan You <yyx990803@gmail.com>
Fri, 30 Aug 2019 16:22:41 +0000 (12:22 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 30 Aug 2019 16:22:41 +0000 (12:22 -0400)
packages/runtime-core/src/apiLifecycle.ts
packages/runtime-core/src/errorHandling.ts

index d1e95cfc7cc2a36bf78cf00ad3eaa0de197aef70..ed4a4e4fc8c3b1bf691c9d642362a4b675d61210 100644 (file)
@@ -4,7 +4,7 @@ import {
   currentInstance,
   setCurrentInstance
 } from './component'
-import { applyErrorHandling, ErrorTypeStrings } from './errorHandling'
+import { callUserFnWithErrorHandling, ErrorTypeStrings } from './errorHandling'
 import { warn } from './warning'
 import { capitalize } from '@vue/shared'
 
@@ -14,14 +14,12 @@ function injectHook(
   target: ComponentInstance | null = currentInstance
 ) {
   if (target) {
-    // wrap user hook with error handling logic
-    const withErrorHandling = applyErrorHandling(hook, target, type)
     ;(target[type] || (target[type] = [])).push((...args: any[]) => {
       // Set currentInstance during hook invocation.
       // This assumes the hook does not synchronously trigger other hooks, which
       // can only be false when the user does something really funky.
       setCurrentInstance(target)
-      const res = withErrorHandling(...args)
+      const res = callUserFnWithErrorHandling(hook, target, type, args)
       setCurrentInstance(null)
       return res
     })
index 202051328861daf731fb631dad2b36acfa3093b2..703384b2debc08de962d3faf0aee9272c672edd2 100644 (file)
@@ -37,27 +37,24 @@ export const ErrorTypeStrings: Record<number | string, string> = {
 
 type ErrorTypes = LifecycleHooks | UserExecutionContexts
 
-// takes a user-provided function and returns a verison that handles potential
-// errors (including async)
-export function applyErrorHandling<T extends Function>(
-  fn: T,
+export function callUserFnWithErrorHandling(
+  fn: Function,
   instance: ComponentInstance | null,
-  type: ErrorTypes
-): T {
-  return function errorHandlingWrapper(...args: any[]) {
-    let res: any
-    try {
-      res = fn(...args)
-      if (res && !res._isVue && typeof res.then === 'function') {
-        ;(res as Promise<any>).catch(err => {
-          handleError(err, instance, type)
-        })
-      }
-    } catch (err) {
-      handleError(err, instance, type)
+  type: ErrorTypes,
+  args?: any[]
+) {
+  let res: any
+  try {
+    res = args ? fn(...args) : fn()
+    if (res && !res._isVue && typeof res.then === 'function') {
+      ;(res as Promise<any>).catch(err => {
+        handleError(err, instance, type)
+      })
     }
-    return res
-  } as any
+  } catch (err) {
+    handleError(err, instance, type)
+  }
+  return res
 }
 
 export function handleError(