Skip to content

fix: 【发行为混合分包】TypeError: t.$callHook is not a function #4829

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: next
Choose a base branch
from

Conversation

viccici
Copy link

@viccici viccici commented Apr 11, 2024

#4781

  1. 当发行为混合分包的时候,uniapp 会调用 initCreateSubpackageApp 方法

  2. initCreateSubpackageApp 里调用 parseApp ,并在 onLaunch 时候进行 initBaseInstance

  3. initCreateSubpackageApp 里调用 parseApp 后同步执行 vm.$.ctx.$scope = app;

  4. initBaseInstance 在 onLaunch 会进行 if (this.$vm && ctx.$scope) {return;} 阻断,如果通过则执行 ctx.$hasHook = hasHook; ctx.$callHook = callHook;

问题出在 onLaunch 是异步的,导致 4 的流程阻断,没有执行 $callHook 赋值,最终导致 initAppLifecycle 中的 vm.$callHook 为 undefined

1. 当发行为混合分包的时候,uniapp 会调用 initCreateSubpackageApp 方法

2. initCreateSubpackageApp 里调用 parseApp ,并在 onLaunch 时候进行 initBaseInstance

3. initCreateSubpackageApp 里调用 parseApp 后同步执行 `vm.$.ctx.$scope = app;`

4. initBaseInstance 在 onLaunch 会进行 `if (this.$vm && ctx.$scope) {return;}` 阻断,如果通过则执行 `ctx.$hasHook = hasHook;  ctx.$callHook = callHook;` 

问题出在 onLaunch 是异步的,导致 4 的流程阻断,没有执行 $callHook 赋值,最终导致 `initAppLifecycle` 中的 `vm.$callHook` 为 undefined
@Otto-J Otto-J self-requested a review June 20, 2024 13:10
@Otto-J Otto-J added mp-alipay 支付宝小程序 欢迎贡献 PR pr welcome labels Jun 20, 2024
@Otto-J Otto-J requested a review from Copilot May 23, 2025 11:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a race condition that resulted in a TypeError when using mixed subpackages in uniapp by synchronously assigning hook functions to the application context.

  • Synchronously assigns $hasHook and $callHook to resolve the asynchronous initialization issue.
  • Ensures hooks are available before onLaunch completes.

@@ -113,6 +113,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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mp-alipay 支付宝小程序 欢迎贡献 PR pr welcome
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants