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,
|
2019-09-06 23:19:22 +08:00
|
|
|
setCurrentInstance
|
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'
|
2019-09-05 06:20:47 +08:00
|
|
|
import { pauseTracking, resumeTracking } from '@vue/reactivity'
|
2019-05-28 19:36:15 +08:00
|
|
|
|
|
|
|
function injectHook(
|
2019-08-31 00:16:09 +08:00
|
|
|
type: LifecycleHooks,
|
2019-08-29 00:13:36 +08:00
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null
|
2019-05-28 19:36:15 +08:00
|
|
|
) {
|
|
|
|
if (target) {
|
2019-08-31 00:16:09 +08:00
|
|
|
;(target[type] || (target[type] = [])).push((...args: any[]) => {
|
2019-09-11 21:07:29 +08:00
|
|
|
if (target.isUnmounted) {
|
|
|
|
return
|
|
|
|
}
|
2019-09-05 06:20:47 +08:00
|
|
|
// disable tracking inside all lifecycle hooks
|
|
|
|
// since they can potentially be called inside effects.
|
|
|
|
pauseTracking()
|
2019-08-31 00:16:09 +08:00
|
|
|
// 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)
|
2019-08-31 03:05:39 +08:00
|
|
|
const res = callWithAsyncErrorHandling(hook, target, type, args)
|
2019-08-31 00:16:09 +08:00
|
|
|
setCurrentInstance(null)
|
2019-09-05 06:20:47 +08:00
|
|
|
resumeTracking()
|
2019-08-31 00:16:09 +08:00
|
|
|
return res
|
|
|
|
})
|
|
|
|
} 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-08-31 00:16:09 +08:00
|
|
|
export function onBeforeMount(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.BEFORE_MOUNT, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onMounted(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.MOUNTED, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onBeforeUpdate(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.BEFORE_UPDATE, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onUpdated(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.UPDATED, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onBeforeUnmount(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.BEFORE_UNMOUNT, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onUnmounted(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.UNMOUNTED, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onRenderTriggered(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.RENDER_TRIGGERED, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onRenderTracked(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.RENDER_TRACKED, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export function onErrorCaptured(
|
2019-08-31 04:08:10 +08:00
|
|
|
hook: (
|
|
|
|
err: Error,
|
2019-09-07 00:58:31 +08:00
|
|
|
instance: ComponentPublicInstance | null,
|
2019-08-31 04:08:10 +08:00
|
|
|
info: string
|
|
|
|
) => boolean | void,
|
2019-09-07 00:58:31 +08:00
|
|
|
target: ComponentInternalInstance | null = currentInstance
|
2019-08-31 00:16:09 +08:00
|
|
|
) {
|
|
|
|
injectHook(LifecycleHooks.ERROR_CAPTURED, hook, target)
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|