| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import {
- ComponentInternalInstance,
- LifecycleHooks,
- currentInstance,
- setCurrentInstance,
- isInSSRComponentSetup
- } from './component'
- import { ComponentPublicInstance } from './componentProxy'
- import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
- import { warn } from './warning'
- import { capitalize } from '@vue/shared'
- import { pauseTracking, resetTracking, DebuggerEvent } from '@vue/reactivity'
- export { onActivated, onDeactivated } from './components/KeepAlive'
- export function injectHook(
- type: LifecycleHooks,
- hook: Function & { __weh?: Function },
- target: ComponentInternalInstance | null = currentInstance,
- prepend: boolean = false
- ) {
- if (target) {
- const hooks = target[type] || (target[type] = [])
- // 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)
- resetTracking()
- 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) =>
- // post-create lifecycle registrations are noops during SSR
- !isInSSRComponentSetup && 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 = (
- hook: ErrorCapturedHook,
- target: ComponentInternalInstance | null = currentInstance
- ) => {
- injectHook(LifecycleHooks.ERROR_CAPTURED, hook, target)
- }
|