refactor: call instead of wrap
This commit is contained in:
parent
3d681f8bcd
commit
7fe82398f7
@ -4,7 +4,7 @@ import {
|
|||||||
currentInstance,
|
currentInstance,
|
||||||
setCurrentInstance
|
setCurrentInstance
|
||||||
} from './component'
|
} from './component'
|
||||||
import { applyErrorHandling, ErrorTypeStrings } from './errorHandling'
|
import { callUserFnWithErrorHandling, ErrorTypeStrings } from './errorHandling'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
import { capitalize } from '@vue/shared'
|
import { capitalize } from '@vue/shared'
|
||||||
|
|
||||||
@ -14,14 +14,12 @@ function injectHook(
|
|||||||
target: ComponentInstance | null = currentInstance
|
target: ComponentInstance | null = currentInstance
|
||||||
) {
|
) {
|
||||||
if (target) {
|
if (target) {
|
||||||
// wrap user hook with error handling logic
|
|
||||||
const withErrorHandling = applyErrorHandling(hook, target, type)
|
|
||||||
;(target[type] || (target[type] = [])).push((...args: any[]) => {
|
;(target[type] || (target[type] = [])).push((...args: any[]) => {
|
||||||
// Set currentInstance during hook invocation.
|
// Set currentInstance during hook invocation.
|
||||||
// This assumes the hook does not synchronously trigger other hooks, which
|
// This assumes the hook does not synchronously trigger other hooks, which
|
||||||
// can only be false when the user does something really funky.
|
// can only be false when the user does something really funky.
|
||||||
setCurrentInstance(target)
|
setCurrentInstance(target)
|
||||||
const res = withErrorHandling(...args)
|
const res = callUserFnWithErrorHandling(hook, target, type, args)
|
||||||
setCurrentInstance(null)
|
setCurrentInstance(null)
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
|
@ -37,17 +37,15 @@ export const ErrorTypeStrings: Record<number | string, string> = {
|
|||||||
|
|
||||||
type ErrorTypes = LifecycleHooks | UserExecutionContexts
|
type ErrorTypes = LifecycleHooks | UserExecutionContexts
|
||||||
|
|
||||||
// takes a user-provided function and returns a verison that handles potential
|
export function callUserFnWithErrorHandling(
|
||||||
// errors (including async)
|
fn: Function,
|
||||||
export function applyErrorHandling<T extends Function>(
|
|
||||||
fn: T,
|
|
||||||
instance: ComponentInstance | null,
|
instance: ComponentInstance | null,
|
||||||
type: ErrorTypes
|
type: ErrorTypes,
|
||||||
): T {
|
args?: any[]
|
||||||
return function errorHandlingWrapper(...args: any[]) {
|
) {
|
||||||
let res: any
|
let res: any
|
||||||
try {
|
try {
|
||||||
res = fn(...args)
|
res = args ? fn(...args) : fn()
|
||||||
if (res && !res._isVue && typeof res.then === 'function') {
|
if (res && !res._isVue && typeof res.then === 'function') {
|
||||||
;(res as Promise<any>).catch(err => {
|
;(res as Promise<any>).catch(err => {
|
||||||
handleError(err, instance, type)
|
handleError(err, instance, type)
|
||||||
@ -57,7 +55,6 @@ export function applyErrorHandling<T extends Function>(
|
|||||||
handleError(err, instance, type)
|
handleError(err, instance, type)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
} as any
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function handleError(
|
export function handleError(
|
||||||
|
Loading…
Reference in New Issue
Block a user