Skip to content

feat: add new lifecycle onAfterUnmount #8762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function injectHook(
const wrappedHook =
hook.__weh ||
(hook.__weh = (...args: unknown[]) => {
if (target.isUnmounted) {
if (target.isUnmounted && type !== LifecycleHooks.AFTER_UNMOUNT) {
return
}
// disable tracking inside all lifecycle hooks
Expand Down Expand Up @@ -76,6 +76,7 @@ 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 const onAfterUnmounted = createHook(LifecycleHooks.AFTER_UNMOUNT)
export const onServerPrefetch = createHook(LifecycleHooks.SERVER_PREFETCH)

export type DebuggerHook = (e: DebuggerEvent) => void
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ export interface ComponentInternalInstance {
* @internal
*/
[LifecycleHooks.UNMOUNTED]: LifecycleHook
/**
* @internal
*/
[LifecycleHooks.AFTER_UNMOUNT]: LifecycleHook
/**
* @internal
*/
Expand Down Expand Up @@ -561,6 +565,7 @@ export function createComponentInstance(
bu: null,
u: null,
um: null,
au: null,
bum: null,
da: null,
a: null,
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import {
onRenderTriggered,
DebuggerHook,
ErrorCapturedHook,
onServerPrefetch
onServerPrefetch,
onAfterUnmounted
} from './apiLifecycle'
import {
reactive,
Expand Down Expand Up @@ -641,6 +642,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
beforeUnmount,
destroyed,
unmounted,
afterUnmount,
render,
renderTracked,
renderTriggered,
Expand Down Expand Up @@ -824,8 +826,8 @@ export function applyOptions(instance: ComponentInternalInstance) {
registerLifecycleHook(onRenderTriggered, renderTriggered)
registerLifecycleHook(onBeforeUnmount, beforeUnmount)
registerLifecycleHook(onUnmounted, unmounted)
registerLifecycleHook(onAfterUnmounted, afterUnmount)
registerLifecycleHook(onServerPrefetch, serverPrefetch)

if (__COMPAT__) {
if (
beforeDestroy &&
Expand Down Expand Up @@ -1066,6 +1068,7 @@ export const internalOptionMergeStrats: Record<string, Function> = {
updated: mergeAsArray,
beforeDestroy: mergeAsArray,
beforeUnmount: mergeAsArray,
afterUnmount: mergeAsArray,
destroyed: mergeAsArray,
unmounted: mergeAsArray,
activated: mergeAsArray,
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const enum LifecycleHooks {
UPDATED = 'u',
BEFORE_UNMOUNT = 'bum',
UNMOUNTED = 'um',
AFTER_UNMOUNT = 'au',
DEACTIVATED = 'da',
ACTIVATED = 'a',
RENDER_TRIGGERED = 'rtg',
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/errorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const ErrorTypeStrings: Record<LifecycleHooks | ErrorCodes, string> = {
[LifecycleHooks.UPDATED]: 'updated',
[LifecycleHooks.BEFORE_UNMOUNT]: 'beforeUnmount hook',
[LifecycleHooks.UNMOUNTED]: 'unmounted hook',
[LifecycleHooks.AFTER_UNMOUNT]: 'afterUnmounted hook',
[LifecycleHooks.ACTIVATED]: 'activated hook',
[LifecycleHooks.DEACTIVATED]: 'deactivated hook',
[LifecycleHooks.ERROR_CAPTURED]: 'errorCaptured hook',
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export {
onActivated,
onDeactivated,
onRenderTracked,
onAfterUnmounted,
onRenderTriggered,
onErrorCaptured,
onServerPrefetch
Expand Down
12 changes: 11 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
NOOP,
invokeArrayFns,
isArray,
getGlobalThis
getGlobalThis,
observeDomRemoval
} from '@vue/shared'
import {
queueJob,
Expand Down Expand Up @@ -1255,6 +1256,7 @@ function baseCreateRenderer(
popWarningContext()
endMeasure(instance, `mount`)
}
afterUnmount(instance)
}

const updateComponent = (n1: VNode, n2: VNode, optimized: boolean) => {
Expand Down Expand Up @@ -2157,6 +2159,14 @@ function baseCreateRenderer(
}
}

const afterUnmount = (instance: ComponentInternalInstance) => {
const { vnode, au } = instance
const { el } = vnode
if (au && el) {
observeDomRemoval(el as Node, au)
}
}

const remove: RemoveFn = vnode => {
const { type, el, anchor, transition } = vnode
if (type === Fragment) {
Expand Down
47 changes: 47 additions & 0 deletions packages/shared/src/domUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { isArray } from '.'
const targetMap = new Map<Node, Set<Function>>()

let observer: MutationObserver | null = null

/**
* Observe DOM removal of a target element
* @param target
* @param callback
*/
export function observeDomRemoval(
target: Node,
callback: Function | Function[]
) {
let cbSet = targetMap.get(target)

if (!cbSet) {
cbSet = new Set()
targetMap.set(target, cbSet)
}
if (isArray(callback)) {
callback.forEach(cb => cbSet!.add(cb))
} else {
cbSet.add(callback)
}

if (observer) return
observer = new MutationObserver(mutationsList => {
if (targetMap.size === 0) return
for (const mutation of mutationsList) {
const removedNodes = mutation.removedNodes
if (mutation.type === 'childList' && removedNodes.length > 0) {
for (let index = 0; index < removedNodes.length; index++) {
const removedNode = removedNodes[index]
for (const [target, cbSet] of targetMap) {
if (removedNode.contains(target)) {
cbSet.forEach(cb => cb())
targetMap.delete(target)
return
}
}
}
}
}
})
observer.observe(document, { childList: true, subtree: true })
}
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './escapeHtml'
export * from './looseEqual'
export * from './toDisplayString'
export * from './typeUtils'
export * from './domUtils'