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

Function registerKeepAliveHook

packages/runtime-core/src/components/KeepAlive.ts:422–458  ·  view source on GitHub ↗
(
  hook: Function & { __wdc?: Function },
  type: LifecycleHooks,
  target: ComponentInternalInstance | null = currentInstance,
)

Source from the content-addressed store, hash-verified

420}
421
422function registerKeepAliveHook(
423 hook: Function & { __wdc?: Function },
424 type: LifecycleHooks,
425 target: ComponentInternalInstance | null = currentInstance,
426) {
427 // cache the deactivate branch check wrapper for injected hooks so the same
428 // hook can be properly deduped by the scheduler. "__wdc" stands for "with
429 // deactivation check".
430 const wrappedHook =
431 hook.__wdc ||
432 (hook.__wdc = () => {
433 // only fire the hook if the target instance is NOT in a deactivated branch.
434 let current: ComponentInternalInstance | null = target
435 while (current) {
436 if (current.isDeactivated) {
437 return
438 }
439 current = current.parent
440 }
441 return hook()
442 })
443 injectHook(type, wrappedHook, target)
444 // In addition to registering it on the target instance, we walk up the parent
445 // chain and register it on all ancestor instances that are keep-alive roots.
446 // This avoids the need to walk the entire component tree when invoking these
447 // hooks, and more importantly, avoids the need to track child components in
448 // arrays.
449 if (target) {
450 let current = target.parent
451 while (current && current.parent) {
452 if (isKeepAlive(current.parent.vnode)) {
453 injectToKeepAliveRoot(wrappedHook, type, target, current)
454 }
455 current = current.parent
456 }
457 }
458}
459
460function injectToKeepAliveRoot(
461 hook: Function & { __weh?: Function },

Callers 2

onActivatedFunction · 0.85
onDeactivatedFunction · 0.85

Calls 3

injectHookFunction · 0.90
isKeepAliveFunction · 0.85
injectToKeepAliveRootFunction · 0.85

Tested by

no test coverage detected