Skip to content

Conversation

viccici
Copy link

@viccici viccici commented Apr 11, 2024

close #4781
close #4422

  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.

@Young-Spark
Copy link

我试了一下这个确实能解决问题,但是为什么官方不合并

@chouchouji
Copy link
Contributor

这个修复也不对吧,app.ts 文件没有 hasHook 函数 和 callHook 函数

viccici and others added 2 commits August 25, 2025 20:05
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
@chouchouji
Copy link
Contributor

@viccici 我调整了代码,你再看下

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
4 participants