-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
refactor(runtime): nextTick适配小程序setData回调 #5978
Conversation
if (initRender) { | ||
perf.stop(PAGE_INIT) | ||
} | ||
}) | ||
} | ||
}, 0) | ||
} | ||
|
||
public enqueueUpdateCallbak (cb: Function, ctx?: Record<string, any>) { |
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.
这应该是一个私有方法?
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.
这个方法会在nextTick里被外部调用
|
||
public enqueueUpdateCallbak (cb: Function, ctx?: Record<string, any>) { | ||
this.updateCallbacks.push(() => { | ||
if (cb) { |
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.
这个判断没有必要,因为 cb 一定是一个函数,要么就在 Taro.nextTick 那里判断。
|
||
export const nextTick = (cb: Function, ctx?: Record<string, any>) => { | ||
const hasSetDataCallbacks = ['weapp', 'swan', 'qq', 'alipay', 'tt'] | ||
if (~hasSetDataCallbacks.indexOf(process.env.TARO_ENV || '')) { |
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.
这里在 env.ts 有一个更简单的判断 isBrowser
,未来还会添加新的 env,先用 isBrowser
吧。
类似于这样的判断其实最好在编译时或代码代码加载后就应该求值,且只求值一次。
if (~hasSetDataCallbacks.indexOf(process.env.TARO_ENV || '')) { | ||
let pageElement: TaroRootElement | null = null | ||
const router = Current.router | ||
if (router) { |
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.
直接判断它是不是 null 效率高一些
const path = getPath(removeLeadingSlash(router.path), router.params) | ||
pageElement = document.getElementById<TaroRootElement>(path) | ||
ensure(pageElement !== null, '没有找到页面实例。') | ||
if (pageElement) { |
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.
如果 ensure 函数已经判断过了,这个判断就没必要了,不过我的理解是这个不应该用 ensure,应该直接判断是否为 null,如果为真,直接走 setTimeout,根据调用时机的不同还是有可能找不到 pageElement 的(例如在页面切换的时候)
这个 PR 做了什么? (简要描述所做更改)
需求背景:
Taro.nextTick
目前触发时机是js线程下一个event loop,在小程序运行环境中,这个时机不能确保视图数据渲染完成;场景:在渲染某个元素之后,需要使用Taro.createSelectorQuery()
去获取元素的样式信息时,目前触发时元素可能还没渲染完成,无法准确获取;修改点:
在小程序环境且支持渲染回调下,调用
Taro.nextTick
,则回调在setData
渲染完成之后执行;在其他环境,则是下一个event loop;这个 PR 是什么类型? (至少选择一个)
这个 PR 满足以下需求:
这个 PR 涉及以下平台:
其它需要 Reviewer 或社区知晓的内容: