2019-08-31 00:16:09 +08:00
|
|
|
import {
|
2019-09-07 00:58:31 +08:00
|
|
|
ComponentInternalInstance,
|
2019-08-31 00:16:09 +08:00
|
|
|
LifecycleHooks,
|
|
|
|
currentInstance,
|
2020-01-29 22:49:17 +08:00
|
|
|
setCurrentInstance,
|
|
|
|
isInSSRComponentSetup
|
2019-08-31 00:16:09 +08:00
|
|
|
} from './component'
|
2019-10-02 22:03:43 +08:00
|
|
|
import { ComponentPublicInstance } from './componentProxy'
|
2019-08-31 03:05:39 +08:00
|
|
|
import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
|
2019-08-31 00:16:09 +08:00
|
|
|
import { warn } from './warning'
|
|
|
|
import { capitalize } from '@vue/shared'
|
2020-02-18 12:14:07 +08:00
|
|
|
import { pauseTracking, resetTracking, DebuggerEvent } from '@vue/reactivity'
|
2019-10-31 09:41:28 +08:00
|
|
|
|
2019-11-03 00:18:35 +08:00
|
|
|
export { onActivated, onDeactivated } from './components/KeepAlive'
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-10-30 10:28:38 +08:00
|
|
|
export function injectHook(
|
2019-08-31 00:16:09 +08:00
|
|
|
type: LifecycleHooks,
|
2019-10-31 09:41:28 +08:00
|
|
|
hook: Function & { __weh?: Function },
|
2019-10-30 10:28:38 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance,
|
|
|
|
prepend: boolean = false
|
2019-05-28 19:36:15 +08:00
|
|
|
) {
|
|
|
|
if (target) {
|
2019-10-30 10:28:38 +08:00
|
|
|
const hooks = target[type] || (target[type] = [])
|
2019-10-31 09:41:28 +08:00
|
|
|
// cache the error handling wrapper for injected hooks so the same hook
|
|
|
|
// can be properly deduped by the scheduler. "__weh" stands for "with error
|
|
|
|
// handling".
|
|
|
|
const wrappedHook =
|
|
|
|
hook.__weh ||
|
|
|
|
(hook.__weh = (...args: unknown[]) => {
|
|
|
|
if (target.isUnmounted) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// disable tracking inside all lifecycle hooks
|
|
|
|
// since they can potentially be called inside effects.
|
|
|
|
pauseTracking()
|
|
|
|
// Set currentInstance during hook invocation.
|
|
|
|
// This assumes the hook does not synchronously trigger other hooks, which
|
|
|
|
// can only be false when the user does something really funky.
|
|
|
|
setCurrentInstance(target)
|
|
|
|
const res = callWithAsyncErrorHandling(hook, target, type, args)
|
|
|
|
setCurrentInstance(null)
|
2020-02-18 12:14:07 +08:00
|
|
|
resetTracking()
|
2019-10-31 09:41:28 +08:00
|
|
|
return res
|
|
|
|
})
|
2019-10-30 10:28:38 +08:00
|
|
|
if (prepend) {
|
|
|
|
hooks.unshift(wrappedHook)
|
|
|
|
} else {
|
|
|
|
hooks.push(wrappedHook)
|
|
|
|
}
|
2019-08-31 00:16:09 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
const apiName = `on${capitalize(
|
2019-09-04 10:25:38 +08:00
|
|
|
ErrorTypeStrings[type].replace(/ hook$/, '')
|
2019-08-31 00:16:09 +08:00
|
|
|
)}`
|
|
|
|
warn(
|
|
|
|
`${apiName} is called when there is no active component instance to be ` +
|
|
|
|
`associated with. ` +
|
2019-09-11 22:09:00 +08:00
|
|
|
`Lifecycle injection APIs can only be used during execution of setup().` +
|
|
|
|
(__FEATURE_SUSPENSE__
|
|
|
|
? ` If you are using async setup(), make sure to register lifecycle ` +
|
|
|
|
`hooks before the first await statement.`
|
|
|
|
: ``)
|
2019-08-31 00:16:09 +08:00
|
|
|
)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:28:38 +08:00
|
|
|
export const createHook = <T extends Function = () => any>(
|
2019-10-14 11:18:34 +08:00
|
|
|
lifecycle: LifecycleHooks
|
2019-10-15 03:07:43 +08:00
|
|
|
) => (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
|
2020-01-25 00:39:52 +08:00
|
|
|
// post-create lifecycle registrations are noops during SSR
|
2020-01-29 22:49:17 +08:00
|
|
|
!isInSSRComponentSetup && injectHook(lifecycle, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-10-14 11:18:34 +08:00
|
|
|
export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
|
|
|
|
export const onMounted = createHook(LifecycleHooks.MOUNTED)
|
|
|
|
export const onBeforeUpdate = createHook(LifecycleHooks.BEFORE_UPDATE)
|
|
|
|
export const onUpdated = createHook(LifecycleHooks.UPDATED)
|
|
|
|
export const onBeforeUnmount = createHook(LifecycleHooks.BEFORE_UNMOUNT)
|
|
|
|
export const onUnmounted = createHook(LifecycleHooks.UNMOUNTED)
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-10-15 11:15:09 +08:00
|
|
|
export type DebuggerHook = (e: DebuggerEvent) => void
|
2019-10-14 11:18:34 +08:00
|
|
|
export const onRenderTriggered = createHook<DebuggerHook>(
|
|
|
|
LifecycleHooks.RENDER_TRIGGERED
|
|
|
|
)
|
|
|
|
export const onRenderTracked = createHook<DebuggerHook>(
|
|
|
|
LifecycleHooks.RENDER_TRACKED
|
|
|
|
)
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-10-15 11:15:09 +08:00
|
|
|
export type ErrorCapturedHook = (
|
2020-03-10 03:58:52 +08:00
|
|
|
err: unknown,
|
2019-10-14 11:18:34 +08:00
|
|
|
instance: ComponentPublicInstance | null,
|
|
|
|
info: string
|
|
|
|
) => boolean | void
|
2020-01-25 00:39:52 +08:00
|
|
|
|
|
|
|
export const onErrorCaptured = (
|
|
|
|
hook: ErrorCapturedHook,
|
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
|
|
|
) => {
|
|
|
|
injectHook(LifecycleHooks.ERROR_CAPTURED, hook, target)
|
|
|
|
}
|