vue3-yuanma/packages/runtime-core/src/apiLifecycle.ts

101 lines
3.3 KiB
TypeScript
Raw Normal View History

import {
2019-09-07 00:58:31 +08:00
ComponentInternalInstance,
LifecycleHooks,
currentInstance,
setCurrentInstance
} from './component'
import { ComponentPublicInstance } from './componentProxy'
import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
import { warn } from './warning'
import { capitalize } from '@vue/shared'
import { pauseTracking, resumeTracking, DebuggerEvent } from '@vue/reactivity'
2019-10-30 10:28:38 +08:00
import { registerKeepAliveHook } from './keepAlive'
2019-05-28 19:36:15 +08:00
2019-10-30 10:28:38 +08:00
export function injectHook(
type: LifecycleHooks,
2019-08-29 00:13:36 +08:00
hook: 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] = [])
const wrappedHook = (...args: unknown[]) => {
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()
// 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)
2019-09-05 06:20:47 +08:00
resumeTracking()
return res
2019-10-30 10:28:38 +08:00
}
if (prepend) {
hooks.unshift(wrappedHook)
} else {
hooks.push(wrappedHook)
}
} else if (__DEV__) {
const apiName = `on${capitalize(
2019-09-04 10:25:38 +08:00
ErrorTypeStrings[type].replace(/ hook$/, '')
)}`
warn(
`${apiName} is called when there is no active component instance to be ` +
`associated with. ` +
`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-05-28 19:36:15 +08:00
}
}
2019-10-30 10:28:38 +08:00
export const createHook = <T extends Function = () => any>(
lifecycle: LifecycleHooks
) => (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
injectHook(lifecycle, hook, target)
2019-05-28 19:36:15 +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
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 = (
err: Error,
instance: ComponentPublicInstance | null,
info: string
) => boolean | void
export const onErrorCaptured = createHook<ErrorCapturedHook>(
LifecycleHooks.ERROR_CAPTURED
)
2019-10-30 10:28:38 +08:00
export function onActivated(
hook: Function,
target?: ComponentInternalInstance | null
) {
registerKeepAliveHook(hook, LifecycleHooks.ACTIVATED, target)
}
export function onDeactivated(
hook: Function,
target?: ComponentInternalInstance | null
) {
registerKeepAliveHook(hook, LifecycleHooks.DEACTIVATED, target)
}