Skip to content
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
4 changes: 3 additions & 1 deletion packages/uni-mp-core/src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ref,
} from 'vue'

import { initBaseInstance } from './componentInstance'
import { callHook, hasHook, initBaseInstance } from './componentInstance'
import { initHooks, initUnknownHooks } from './componentHooks'
import { getLocaleLanguage } from '../runtime/util'

Expand Down Expand Up @@ -123,6 +123,8 @@ export function initCreateSubpackageApp(parseAppOptions?: ParseAppOptions) {
})
if (!app) return
;(vm.$ as any).ctx.$scope = app
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning '$hasHook' and '$callHook' synchronously resolves the async race condition that previously resulted in 't.$callHook is not a function'. Consider adding a comment explaining why these assignments are done here for future maintainability.

Suggested change
;(vm.$ as any).ctx.$scope = app
;(vm.$ as any).ctx.$scope = app
// Assigning `$hasHook` and `$callHook` synchronously is critical to prevent
// an async race condition that could result in `t.$callHook is not a function`.
// Do not modify the order or timing of these assignments without understanding
// the potential impact on app lifecycle hooks.

Copilot uses AI. Check for mistakes.

;(vm.$ as any).ctx.$hasHook = hasHook
;(vm.$ as any).ctx.$callHook = callHook
const globalData = app.globalData
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
Expand Down
8 changes: 6 additions & 2 deletions packages/uni-mp-core/src/runtime/componentInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,19 @@ export function initMocks(
})
}

function hasHook(this: ComponentPublicInstance, name: string) {
export function hasHook(this: ComponentPublicInstance, name: string) {
const hooks = (this.$ as any)[name]
if (hooks && hooks.length) {
return true
}
return false
}

function callHook(this: ComponentPublicInstance, name: string, args?: unknown) {
export function callHook(
this: ComponentPublicInstance,
name: string,
args?: unknown
) {
if (name === 'mounted') {
callHook.call(this, 'bm') // beforeMount
this.$.isMounted = true
Expand Down
Loading