MCPcopy
hub / github.com/vuejs/core / injectHook

Function injectHook

packages/runtime-core/src/apiLifecycle.ts:20–64  ·  view source on GitHub ↗
(
  type: LifecycleHooks,
  hook: Function & { __weh?: Function },
  target: ComponentInternalInstance | null = currentInstance,
  prepend: boolean = false,
)

Source from the content-addressed store, hash-verified

18export { onActivated, onDeactivated } from './components/KeepAlive'
19
20export 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
66const createHook =
67 <T extends Function = () => any>(lifecycle: LifecycleHooks) =>

Callers 4

registerKeepAliveHookFunction · 0.90
injectToKeepAliveRootFunction · 0.90
createHookFunction · 0.85
onErrorCapturedFunction · 0.85

Calls 7

pauseTrackingFunction · 0.90
setCurrentInstanceFunction · 0.90
resetTrackingFunction · 0.90
warnFunction · 0.90
pushMethod · 0.65
resetFunction · 0.50

Tested by

no test coverage detected