Skip to content

Commit 67eb29f

Browse files
jo0geryyx990803
authored andcommitted
refactor(errorHandlling): handle array in callWithAsyncErrorHandling (vuejs#332)
1 parent 74d8c59 commit 67eb29f

File tree

4 files changed

+32
-60
lines changed

4 files changed

+32
-60
lines changed

packages/runtime-core/src/component.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
isFunction,
2121
capitalize,
2222
NOOP,
23-
isArray,
2423
isObject,
2524
NO,
2625
makeMap,
@@ -196,23 +195,12 @@ export function createComponentInstance(
196195
const props = instance.vnode.props || EMPTY_OBJ
197196
const handler = props[`on${event}`] || props[`on${capitalize(event)}`]
198197
if (handler) {
199-
if (isArray(handler)) {
200-
for (let i = 0; i < handler.length; i++) {
201-
callWithAsyncErrorHandling(
202-
handler[i],
203-
instance,
204-
ErrorCodes.COMPONENT_EVENT_HANDLER,
205-
args
206-
)
207-
}
208-
} else {
209-
callWithAsyncErrorHandling(
210-
handler,
211-
instance,
212-
ErrorCodes.COMPONENT_EVENT_HANDLER,
213-
args
214-
)
215-
}
198+
callWithAsyncErrorHandling(
199+
handler,
200+
instance,
201+
ErrorCodes.COMPONENT_EVENT_HANDLER,
202+
args
203+
)
216204
}
217205
}
218206
}

packages/runtime-core/src/directives.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ return withDirectives(h(comp), [
1212
*/
1313

1414
import { VNode } from './vnode'
15-
import { isArray, isFunction, EMPTY_OBJ, makeMap } from '@vue/shared'
15+
import { isFunction, EMPTY_OBJ, makeMap } from '@vue/shared'
1616
import { warn } from './warning'
1717
import { ComponentInternalInstance } from './component'
1818
import { currentRenderingInstance } from './componentRenderUtils'
@@ -140,17 +140,8 @@ export function invokeDirectiveHook(
140140
vnode: VNode,
141141
prevVNode: VNode | null = null
142142
) {
143-
const args = [vnode, prevVNode]
144-
if (isArray(hook)) {
145-
for (let i = 0; i < hook.length; i++) {
146-
callWithAsyncErrorHandling(
147-
hook[i],
148-
instance,
149-
ErrorCodes.DIRECTIVE_HOOK,
150-
args
151-
)
152-
}
153-
} else if (isFunction(hook)) {
154-
callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, args)
155-
}
143+
callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, [
144+
vnode,
145+
prevVNode
146+
])
156147
}

packages/runtime-core/src/errorHandling.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { VNode } from './vnode'
22
import { ComponentInternalInstance, LifecycleHooks } from './component'
33
import { warn, pushWarningContext, popWarningContext } from './warning'
4-
import { isPromise } from '@vue/shared'
4+
import { isPromise, isFunction } from '@vue/shared'
55

66
// contexts where user provided function may be executed, in addition to
77
// lifecycle hooks.
@@ -66,18 +66,24 @@ export function callWithErrorHandling(
6666
}
6767

6868
export function callWithAsyncErrorHandling(
69-
fn: Function,
69+
fn: Function | Function[],
7070
instance: ComponentInternalInstance | null,
7171
type: ErrorTypes,
7272
args?: any[]
7373
) {
74-
const res = callWithErrorHandling(fn, instance, type, args)
75-
if (res != null && !res._isVue && isPromise(res)) {
76-
res.catch((err: Error) => {
77-
handleError(err, instance, type)
78-
})
74+
if (isFunction(fn)) {
75+
const res = callWithErrorHandling(fn, instance, type, args)
76+
if (res != null && !res._isVue && isPromise(res)) {
77+
res.catch((err: Error) => {
78+
handleError(err, instance, type)
79+
})
80+
}
81+
return res
82+
}
83+
84+
for (let i = 0; i < fn.length; i++) {
85+
callWithAsyncErrorHandling(fn[i], instance, type, args)
7986
}
80-
return res
8187
}
8288

8389
export function handleError(

packages/runtime-dom/src/modules/events.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isArray, EMPTY_OBJ } from '@vue/shared'
1+
import { EMPTY_OBJ } from '@vue/shared'
22
import {
33
ComponentInternalInstance,
44
callWithAsyncErrorHandling
@@ -128,25 +128,12 @@ function createInvoker(
128128
// and the handler would only fire if the event passed to it was fired
129129
// AFTER it was attached.
130130
if (e.timeStamp >= invoker.lastUpdated - 1) {
131-
const args = [e]
132-
const value = invoker.value
133-
if (isArray(value)) {
134-
for (let i = 0; i < value.length; i++) {
135-
callWithAsyncErrorHandling(
136-
value[i],
137-
instance,
138-
ErrorCodes.NATIVE_EVENT_HANDLER,
139-
args
140-
)
141-
}
142-
} else {
143-
callWithAsyncErrorHandling(
144-
value,
145-
instance,
146-
ErrorCodes.NATIVE_EVENT_HANDLER,
147-
args
148-
)
149-
}
131+
callWithAsyncErrorHandling(
132+
invoker.value,
133+
instance,
134+
ErrorCodes.NATIVE_EVENT_HANDLER,
135+
[e]
136+
)
150137
}
151138
}
152139
invoker.value = initialValue

0 commit comments

Comments
 (0)