]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(errorHandlling): handle array in callWithAsyncErrorHandling (#332)
authorJooger <iamjooger@gmail.com>
Mon, 21 Oct 2019 17:59:10 +0000 (01:59 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 21 Oct 2019 17:59:10 +0000 (13:59 -0400)
packages/runtime-core/src/component.ts
packages/runtime-core/src/directives.ts
packages/runtime-core/src/errorHandling.ts
packages/runtime-dom/src/modules/events.ts

index a6540f6785dbaea3e0f549f64a7ed067db87d608..179aa866b897141c0b4436f376dfc894de2d09fe 100644 (file)
@@ -20,7 +20,6 @@ import {
   isFunction,
   capitalize,
   NOOP,
-  isArray,
   isObject,
   NO,
   makeMap,
@@ -196,23 +195,12 @@ export function createComponentInstance(
       const props = instance.vnode.props || EMPTY_OBJ
       const handler = props[`on${event}`] || props[`on${capitalize(event)}`]
       if (handler) {
-        if (isArray(handler)) {
-          for (let i = 0; i < handler.length; i++) {
-            callWithAsyncErrorHandling(
-              handler[i],
-              instance,
-              ErrorCodes.COMPONENT_EVENT_HANDLER,
-              args
-            )
-          }
-        } else {
-          callWithAsyncErrorHandling(
-            handler,
-            instance,
-            ErrorCodes.COMPONENT_EVENT_HANDLER,
-            args
-          )
-        }
+        callWithAsyncErrorHandling(
+          handler,
+          instance,
+          ErrorCodes.COMPONENT_EVENT_HANDLER,
+          args
+        )
       }
     }
   }
index a941b90d07d09275ce06332f3241dc4ba4a98167..079573eb63a13df269d6d5454fc38af54617310a 100644 (file)
@@ -12,7 +12,7 @@ return withDirectives(h(comp), [
 */
 
 import { VNode } from './vnode'
-import { isArray, isFunction, EMPTY_OBJ, makeMap } from '@vue/shared'
+import { isFunction, EMPTY_OBJ, makeMap } from '@vue/shared'
 import { warn } from './warning'
 import { ComponentInternalInstance } from './component'
 import { currentRenderingInstance } from './componentRenderUtils'
@@ -140,17 +140,8 @@ export function invokeDirectiveHook(
   vnode: VNode,
   prevVNode: VNode | null = null
 ) {
-  const args = [vnode, prevVNode]
-  if (isArray(hook)) {
-    for (let i = 0; i < hook.length; i++) {
-      callWithAsyncErrorHandling(
-        hook[i],
-        instance,
-        ErrorCodes.DIRECTIVE_HOOK,
-        args
-      )
-    }
-  } else if (isFunction(hook)) {
-    callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, args)
-  }
+  callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, [
+    vnode,
+    prevVNode
+  ])
 }
index 57ed496a7596247dcc766ddab52b8a458cc032b1..a17b05461656cef0f53124b33def1293b15564de 100644 (file)
@@ -1,7 +1,7 @@
 import { VNode } from './vnode'
 import { ComponentInternalInstance, LifecycleHooks } from './component'
 import { warn, pushWarningContext, popWarningContext } from './warning'
-import { isPromise } from '@vue/shared'
+import { isPromise, isFunction } from '@vue/shared'
 
 // contexts where user provided function may be executed, in addition to
 // lifecycle hooks.
@@ -66,18 +66,24 @@ export function callWithErrorHandling(
 }
 
 export function callWithAsyncErrorHandling(
-  fn: Function,
+  fn: Function | Function[],
   instance: ComponentInternalInstance | null,
   type: ErrorTypes,
   args?: any[]
 ) {
-  const res = callWithErrorHandling(fn, instance, type, args)
-  if (res != null && !res._isVue && isPromise(res)) {
-    res.catch((err: Error) => {
-      handleError(err, instance, type)
-    })
+  if (isFunction(fn)) {
+    const res = callWithErrorHandling(fn, instance, type, args)
+    if (res != null && !res._isVue && isPromise(res)) {
+      res.catch((err: Error) => {
+        handleError(err, instance, type)
+      })
+    }
+    return res
+  }
+
+  for (let i = 0; i < fn.length; i++) {
+    callWithAsyncErrorHandling(fn[i], instance, type, args)
   }
-  return res
 }
 
 export function handleError(
index 7de6f1add78f05605293086597467f8a7506478c..d3a5f8e46b4cfa5c4f9f6902d189311ee4b4e3d9 100644 (file)
@@ -1,4 +1,4 @@
-import { isArray, EMPTY_OBJ } from '@vue/shared'
+import { EMPTY_OBJ } from '@vue/shared'
 import {
   ComponentInternalInstance,
   callWithAsyncErrorHandling
@@ -128,25 +128,12 @@ function createInvoker(
     // and the handler would only fire if the event passed to it was fired
     // AFTER it was attached.
     if (e.timeStamp >= invoker.lastUpdated - 1) {
-      const args = [e]
-      const value = invoker.value
-      if (isArray(value)) {
-        for (let i = 0; i < value.length; i++) {
-          callWithAsyncErrorHandling(
-            value[i],
-            instance,
-            ErrorCodes.NATIVE_EVENT_HANDLER,
-            args
-          )
-        }
-      } else {
-        callWithAsyncErrorHandling(
-          value,
-          instance,
-          ErrorCodes.NATIVE_EVENT_HANDLER,
-          args
-        )
-      }
+      callWithAsyncErrorHandling(
+        invoker.value,
+        instance,
+        ErrorCodes.NATIVE_EVENT_HANDLER,
+        [e]
+      )
     }
   }
   invoker.value = initialValue