(
type: LifecycleHooks,
hook: Function & { __weh?: Function },
target: ComponentInternalInstance | null = currentInstance,
prepend: boolean = false,
)
| 18 | export { onActivated, onDeactivated } from './components/KeepAlive' |
| 19 | |
| 20 | export function injectHook( |
| 21 | type: LifecycleHooks, |
| 22 | hook: Function & { __weh?: Function }, |
| 23 | target: ComponentInternalInstance | null = currentInstance, |
| 24 | prepend: boolean = false, |
| 25 | ): Function | undefined { |
| 26 | if (target) { |
| 27 | const hooks = target[type] || (target[type] = []) |
| 28 | // cache the error handling wrapper for injected hooks so the same hook |
| 29 | // can be properly deduped by the scheduler. "__weh" stands for "with error |
| 30 | // handling". |
| 31 | const wrappedHook = |
| 32 | hook.__weh || |
| 33 | (hook.__weh = (...args: unknown[]) => { |
| 34 | // disable tracking inside all lifecycle hooks |
| 35 | // since they can potentially be called inside effects. |
| 36 | pauseTracking() |
| 37 | // Set currentInstance during hook invocation. |
| 38 | // This assumes the hook does not synchronously trigger other hooks, which |
| 39 | // can only be false when the user does something really funky. |
| 40 | const reset = setCurrentInstance(target) |
| 41 | const res = callWithAsyncErrorHandling(hook, target, type, args) |
| 42 | reset() |
| 43 | resetTracking() |
| 44 | return res |
| 45 | }) |
| 46 | if (prepend) { |
| 47 | hooks.unshift(wrappedHook) |
| 48 | } else { |
| 49 | hooks.push(wrappedHook) |
| 50 | } |
| 51 | return wrappedHook |
| 52 | } else if (__DEV__) { |
| 53 | const apiName = toHandlerKey(ErrorTypeStrings[type].replace(/ hook$/, '')) |
| 54 | warn( |
| 55 | `${apiName} is called when there is no active component instance to be ` + |
| 56 | `associated with. ` + |
| 57 | `Lifecycle injection APIs can only be used during execution of setup().` + |
| 58 | (__FEATURE_SUSPENSE__ |
| 59 | ? ` If you are using async setup(), make sure to register lifecycle ` + |
| 60 | `hooks before the first await statement.` |
| 61 | : ``), |
| 62 | ) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const createHook = |
| 67 | <T extends Function = () => any>(lifecycle: LifecycleHooks) => |
no test coverage detected