(
hook: Function & { __wdc?: Function },
type: LifecycleHooks,
target: ComponentInternalInstance | null = currentInstance,
)
| 420 | } |
| 421 | |
| 422 | function 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 | |
| 460 | function injectToKeepAliveRoot( |
| 461 | hook: Function & { __weh?: Function }, |
no test coverage detected