feat(runtime-core): emit now returns array of return values from all triggered handlers
close #635
This commit is contained in:
@@ -24,7 +24,8 @@ import {
|
||||
isObject,
|
||||
NO,
|
||||
makeMap,
|
||||
isPromise
|
||||
isPromise,
|
||||
isArray
|
||||
} from '@vue/shared'
|
||||
import { SuspenseBoundary } from './components/Suspense'
|
||||
import { CompilerOptions } from '@vue/compiler-core'
|
||||
@@ -70,7 +71,7 @@ export const enum LifecycleHooks {
|
||||
ERROR_CAPTURED = 'ec'
|
||||
}
|
||||
|
||||
export type Emit = (event: string, ...args: unknown[]) => void
|
||||
export type Emit = (event: string, ...args: unknown[]) => any[]
|
||||
|
||||
export interface SetupContext {
|
||||
attrs: Data
|
||||
@@ -218,16 +219,19 @@ export function defineComponentInstance(
|
||||
rtc: null,
|
||||
ec: null,
|
||||
|
||||
emit: (event, ...args) => {
|
||||
emit: (event, ...args): any[] => {
|
||||
const props = instance.vnode.props || EMPTY_OBJ
|
||||
const handler = props[`on${event}`] || props[`on${capitalize(event)}`]
|
||||
if (handler) {
|
||||
callWithAsyncErrorHandling(
|
||||
const res = callWithAsyncErrorHandling(
|
||||
handler,
|
||||
instance,
|
||||
ErrorCodes.COMPONENT_EVENT_HANDLER,
|
||||
args
|
||||
)
|
||||
return isArray(res) ? res : [res]
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ export function callWithAsyncErrorHandling(
|
||||
instance: ComponentInternalInstance | null,
|
||||
type: ErrorTypes,
|
||||
args?: unknown[]
|
||||
) {
|
||||
): any[] {
|
||||
if (isFunction(fn)) {
|
||||
const res = callWithErrorHandling(fn, instance, type, args)
|
||||
if (res != null && !res._isVue && isPromise(res)) {
|
||||
@@ -85,9 +85,11 @@ export function callWithAsyncErrorHandling(
|
||||
return res
|
||||
}
|
||||
|
||||
const values = []
|
||||
for (let i = 0; i < fn.length; i++) {
|
||||
callWithAsyncErrorHandling(fn[i], instance, type, args)
|
||||
values.push(callWithAsyncErrorHandling(fn[i], instance, type, args))
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
export function handleError(
|
||||
|
||||
Reference in New Issue
Block a user