| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import {
- 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'
- import { registerKeepAliveHook } from './keepAlive'
- export function injectHook(
- type: LifecycleHooks,
- hook: Function,
- target: ComponentInternalInstance | null = currentInstance,
- prepend: boolean = false
- ) {
- if (target) {
- const hooks = target[type] || (target[type] = [])
- const wrappedHook = (...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)
- resumeTracking()
- return res
- }
- if (prepend) {
- hooks.unshift(wrappedHook)
- } else {
- hooks.push(wrappedHook)
- }
- } else if (__DEV__) {
- const apiName = `on${capitalize(
- 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.`
- : ``)
- )
- }
- }
- export const createHook = <T extends Function = () => any>(
- lifecycle: LifecycleHooks
- ) => (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
- injectHook(lifecycle, hook, target)
- 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)
- export type DebuggerHook = (e: DebuggerEvent) => void
- export const onRenderTriggered = createHook<DebuggerHook>(
- LifecycleHooks.RENDER_TRIGGERED
- )
- export const onRenderTracked = createHook<DebuggerHook>(
- LifecycleHooks.RENDER_TRACKED
- )
- export type ErrorCapturedHook = (
- err: Error,
- instance: ComponentPublicInstance | null,
- info: string
- ) => boolean | void
- export const onErrorCaptured = createHook<ErrorCapturedHook>(
- LifecycleHooks.ERROR_CAPTURED
- )
- 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)
- }
|