Skip to content

fix(runtime-core): global mixin lifecyclehooks execute after component lifecyclehooks when use both Composition API and Options API #5927

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ export function createAppAPI<HostElement>(
mixin(mixin: ComponentOptions) {
if (__FEATURE_OPTIONS_API__) {
if (!context.mixins.includes(mixin)) {
for (let m in mixin) {
if (isFunction(mixin[m])) {
// mark globalMixin
mixin[m]._isGlobalMixin = true
}
}
context.mixins.push(mixin)
} else if (__DEV__) {
warn(
Expand Down
47 changes: 36 additions & 11 deletions packages/runtime-core/src/apiLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ import { DebuggerEvent, pauseTracking, resetTracking } from '@vue/reactivity'

export { onActivated, onDeactivated } from './components/KeepAlive'

export type WRAPPEDHOOK = {
(hook: Function & { __weh?: WRAPPEDHOOK; _isGlobalMixin?: boolean }): void
_isGlobalMixin?: boolean
}

export function injectHook(
type: LifecycleHooks,
hook: Function & { __weh?: Function },
hook: Function & { __weh?: WRAPPEDHOOK; _isGlobalMixin?: boolean },
target: ComponentInternalInstance | null = currentInstance,
prepend: boolean = false
): Function | undefined {
): WRAPPEDHOOK | undefined {
if (target) {
const hooks = target[type] || (target[type] = [])
// cache the error handling wrapper for injected hooks so the same hook
// can be properly deduped by the scheduler. "__weh" stands for "with error
// handling".
const wrappedHook =
const wrappedHook: WRAPPEDHOOK =
hook.__weh ||
(hook.__weh = (...args: unknown[]) => {
if (target.isUnmounted) {
Expand All @@ -43,8 +48,22 @@ export function injectHook(
resetTracking()
return res
})
if (hook._isGlobalMixin) {
wrappedHook._isGlobalMixin = hook._isGlobalMixin
}
if (prepend) {
hooks.unshift(wrappedHook)
let index = -1
for (let i = hooks.length - 1; i >= 0; i--) {
if ((hooks[i] as WRAPPEDHOOK)._isGlobalMixin) {
index = i
break
}
}
if (index === -1) {
hooks.unshift(wrappedHook)
} else {
hooks.splice(index + 1, 0, wrappedHook)
}
} else {
hooks.push(wrappedHook)
}
Expand All @@ -63,12 +82,17 @@ export function injectHook(
}
}

export interface HOOK {
(e: DebuggerEvent): void
_isGlobalMixin?: boolean
}

export const createHook =
<T extends Function = () => any>(lifecycle: LifecycleHooks) =>
<T extends HOOK>(lifecycle: LifecycleHooks) =>
(hook: T, target: ComponentInternalInstance | null = currentInstance) =>
// post-create lifecycle registrations are noops during SSR (except for serverPrefetch)
(!isInSSRComponentSetup || lifecycle === LifecycleHooks.SERVER_PREFETCH) &&
injectHook(lifecycle, hook, target)
injectHook(lifecycle, hook, target, hook._isGlobalMixin ? true : false)

export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
export const onMounted = createHook(LifecycleHooks.MOUNTED)
Expand All @@ -86,11 +110,12 @@ export const onRenderTracked = createHook<DebuggerHook>(
LifecycleHooks.RENDER_TRACKED
)

export type ErrorCapturedHook<TError = unknown> = (
err: TError,
instance: ComponentPublicInstance | null,
info: string
) => boolean | void
export type ErrorCapturedHook<TError = unknown> = {
(err: TError, instance: ComponentPublicInstance | null, info: string):
| boolean
| void
_isGlobalMixin?: boolean
}

export function onErrorCaptured<TError = Error>(
hook: ErrorCapturedHook<TError>,
Expand Down
19 changes: 15 additions & 4 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import {
onRenderTriggered,
DebuggerHook,
ErrorCapturedHook,
onServerPrefetch
onServerPrefetch,
HOOK
} from './apiLifecycle'
import {
reactive,
Expand Down Expand Up @@ -756,12 +757,22 @@ export function applyOptions(instance: ComponentInternalInstance) {

function registerLifecycleHook(
register: Function,
hook?: Function | Function[]
hook?: HOOK | HOOK[] | ErrorCapturedHook | ErrorCapturedHook[]
) {
if (isArray(hook)) {
hook.forEach(_hook => register(_hook.bind(publicThis)))
hook.forEach(_hook => {
const bind = _hook.bind(publicThis)
if (_hook._isGlobalMixin) {
bind._isGlobalMixin = true
}
register(bind)
})
} else if (hook) {
register((hook as Function).bind(publicThis))
const bind = hook.bind(publicThis)
if (hook._isGlobalMixin) {
bind._isGlobalMixin = true
}
register(bind)
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
injectHook,
onUnmounted,
onMounted,
onUpdated
onUpdated,
WRAPPEDHOOK
} from '../apiLifecycle'
import {
isString,
Expand Down Expand Up @@ -407,7 +408,7 @@ function registerKeepAliveHook(
}

function injectToKeepAliveRoot(
hook: Function & { __weh?: Function },
hook: Function & { __weh?: WRAPPEDHOOK; _isGlobalMixin?: boolean },
type: LifecycleHooks,
target: ComponentInternalInstance,
keepAliveRoot: ComponentInternalInstance
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy