refactor(errorHandlling): handle array in callWithAsyncErrorHandling (#332)

This commit is contained in:
Jooger 2019-10-22 01:59:10 +08:00 committed by Evan You
parent 74d8c5919d
commit 67eb29f63b
4 changed files with 32 additions and 60 deletions

View File

@ -20,7 +20,6 @@ import {
isFunction, isFunction,
capitalize, capitalize,
NOOP, NOOP,
isArray,
isObject, isObject,
NO, NO,
makeMap, makeMap,
@ -196,16 +195,6 @@ export function createComponentInstance(
const props = instance.vnode.props || EMPTY_OBJ const props = instance.vnode.props || EMPTY_OBJ
const handler = props[`on${event}`] || props[`on${capitalize(event)}`] const handler = props[`on${event}`] || props[`on${capitalize(event)}`]
if (handler) { if (handler) {
if (isArray(handler)) {
for (let i = 0; i < handler.length; i++) {
callWithAsyncErrorHandling(
handler[i],
instance,
ErrorCodes.COMPONENT_EVENT_HANDLER,
args
)
}
} else {
callWithAsyncErrorHandling( callWithAsyncErrorHandling(
handler, handler,
instance, instance,
@ -215,7 +204,6 @@ export function createComponentInstance(
} }
} }
} }
}
instance.root = parent ? parent.root : instance instance.root = parent ? parent.root : instance
return instance return instance

View File

@ -12,7 +12,7 @@ return withDirectives(h(comp), [
*/ */
import { VNode } from './vnode' 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 { warn } from './warning'
import { ComponentInternalInstance } from './component' import { ComponentInternalInstance } from './component'
import { currentRenderingInstance } from './componentRenderUtils' import { currentRenderingInstance } from './componentRenderUtils'
@ -140,17 +140,8 @@ export function invokeDirectiveHook(
vnode: VNode, vnode: VNode,
prevVNode: VNode | null = null prevVNode: VNode | null = null
) { ) {
const args = [vnode, prevVNode] callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, [
if (isArray(hook)) { vnode,
for (let i = 0; i < hook.length; i++) { prevVNode
callWithAsyncErrorHandling( ])
hook[i],
instance,
ErrorCodes.DIRECTIVE_HOOK,
args
)
}
} else if (isFunction(hook)) {
callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, args)
}
} }

View File

@ -1,7 +1,7 @@
import { VNode } from './vnode' import { VNode } from './vnode'
import { ComponentInternalInstance, LifecycleHooks } from './component' import { ComponentInternalInstance, LifecycleHooks } from './component'
import { warn, pushWarningContext, popWarningContext } from './warning' 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 // contexts where user provided function may be executed, in addition to
// lifecycle hooks. // lifecycle hooks.
@ -66,11 +66,12 @@ export function callWithErrorHandling(
} }
export function callWithAsyncErrorHandling( export function callWithAsyncErrorHandling(
fn: Function, fn: Function | Function[],
instance: ComponentInternalInstance | null, instance: ComponentInternalInstance | null,
type: ErrorTypes, type: ErrorTypes,
args?: any[] args?: any[]
) { ) {
if (isFunction(fn)) {
const res = callWithErrorHandling(fn, instance, type, args) const res = callWithErrorHandling(fn, instance, type, args)
if (res != null && !res._isVue && isPromise(res)) { if (res != null && !res._isVue && isPromise(res)) {
res.catch((err: Error) => { res.catch((err: Error) => {
@ -80,6 +81,11 @@ export function callWithAsyncErrorHandling(
return res return res
} }
for (let i = 0; i < fn.length; i++) {
callWithAsyncErrorHandling(fn[i], instance, type, args)
}
}
export function handleError( export function handleError(
err: Error, err: Error,
instance: ComponentInternalInstance | null, instance: ComponentInternalInstance | null,

View File

@ -1,4 +1,4 @@
import { isArray, EMPTY_OBJ } from '@vue/shared' import { EMPTY_OBJ } from '@vue/shared'
import { import {
ComponentInternalInstance, ComponentInternalInstance,
callWithAsyncErrorHandling callWithAsyncErrorHandling
@ -128,26 +128,13 @@ function createInvoker(
// and the handler would only fire if the event passed to it was fired // and the handler would only fire if the event passed to it was fired
// AFTER it was attached. // AFTER it was attached.
if (e.timeStamp >= invoker.lastUpdated - 1) { 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( callWithAsyncErrorHandling(
value[i], invoker.value,
instance, instance,
ErrorCodes.NATIVE_EVENT_HANDLER, ErrorCodes.NATIVE_EVENT_HANDLER,
args [e]
) )
} }
} else {
callWithAsyncErrorHandling(
value,
instance,
ErrorCodes.NATIVE_EVENT_HANDLER,
args
)
}
}
} }
invoker.value = initialValue invoker.value = initialValue
initialValue.invoker = invoker initialValue.invoker = invoker