-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
base: next
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
;(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.
我试了一下这个确实能解决问题,但是为什么官方不合并 |
这个修复也不对吧, |
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
518d7cb
to
3b42482
Compare
@viccici 我调整了代码,你再看下 |
close #4781
close #4422
当发行为混合分包的时候,uniapp 会调用 initCreateSubpackageApp 方法
initCreateSubpackageApp 里调用 parseApp ,并在 onLaunch 时候进行 initBaseInstance
initCreateSubpackageApp 里调用 parseApp 后同步执行
vm.$.ctx.$scope = app;
initBaseInstance 在 onLaunch 会进行
if (this.$vm && ctx.$scope) {return;}
阻断,如果通过则执行ctx.$hasHook = hasHook; ctx.$callHook = callHook;
问题出在 onLaunch 是异步的,导致 4 的流程阻断,没有执行 $callHook 赋值,最终导致
initAppLifecycle
中的vm.$callHook
为 undefined