Skip to content
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

fix: [plugin-framework-react] 支付宝小程序 app.onLaunch 生命周期执行滞后比 page.onload 晚 #15886

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Open
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
24 changes: 14 additions & 10 deletions packages/taro-plugin-react/src/runtime/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export function createReactApp (
let appWrapper: AppWrapper
let appWrapperResolver: (value: AppWrapper) => void
const appWrapperPromise = new Promise<AppWrapper>(resolve => (appWrapperResolver = resolve))
const waitAppWrapperCallbacks: Array<() => void> = []

setReconciler(ReactDOM)

Expand All @@ -189,12 +190,17 @@ export function createReactApp (
}

function waitAppWrapper (cb: () => void) {
/**
* 当同个事件触发多次时,waitAppWrapper 会出现同步和异步任务的执行顺序问题,
* 导致某些场景下 onShow 会优于 onLaunch 执行
*/
appWrapperPromise.then(() => cb())
// appWrapper ? cb() : appWrapperPromise.then(() => cb())
if (appWrapper) return cb()
waitAppWrapperCallbacks.push(cb)
}

function initializeAppWrapper (wrapper: AppWrapper) {
appWrapper = wrapper
appWrapperResolver(wrapper)
while (waitAppWrapperCallbacks.length) {
const callback = waitAppWrapperCallbacks.shift()
if (callback) callback()
}
}

function renderReactRoot () {
Expand All @@ -220,10 +226,8 @@ export function createReactApp (
private pages: Array<() => PageComponent> = []
private elements: Array<PageComponent> = []

constructor (props) {
super(props)
appWrapper = this
appWrapperResolver(this)
componentDidMount () {
initializeAppWrapper(this)
}

public mount (pageComponent: ReactPageComponent, id: string, cb: () => void) {
Expand Down
Loading