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

refactor(runtime): nextTick适配小程序setData回调 #5978

Merged
merged 2 commits into from
Apr 16, 2020

Conversation

alexloading
Copy link
Contributor

@alexloading alexloading commented Apr 14, 2020

这个 PR 做了什么? (简要描述所做更改)
需求背景:
Taro.nextTick目前触发时机是js线程下一个event loop,在小程序运行环境中,这个时机不能确保视图数据渲染完成;场景:在渲染某个元素之后,需要使用Taro.createSelectorQuery()去获取元素的样式信息时,目前触发时元素可能还没渲染完成,无法准确获取;

修改点:
在小程序环境且支持渲染回调下,调用Taro.nextTick,则回调在setData渲染完成之后执行;在其他环境,则是下一个event loop;

这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix) issue id #
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 满足以下需求:

  • 提交到 master 分支
  • Commit 信息遵循 Angular Style Commit Message Conventions
  • 所有测试用例已经通过
  • 代码遵循相关包中的 .eslintrc, .tslintrc, .stylelintrc 所规定的规范
  • 在本地测试可用,不会影响到其它功能

这个 PR 涉及以下平台:

  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 头条小程序
  • QQ 轻应用
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)

其它需要 Reviewer 或社区知晓的内容:

@yuche yuche self-requested a review April 15, 2020 07:21
if (initRender) {
perf.stop(PAGE_INIT)
}
})
}
}, 0)
}

public enqueueUpdateCallbak (cb: Function, ctx?: Record<string, any>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

这应该是一个私有方法?

Copy link
Contributor Author

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) {
Copy link
Contributor

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 || '')) {
Copy link
Contributor

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) {
Copy link
Contributor

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) {
Copy link
Contributor

@yuche yuche Apr 15, 2020

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 的(例如在页面切换的时候)

@alexloading alexloading requested a review from yuche April 15, 2020 14:16
@yuche yuche changed the title feat(runtime): nextTick适配小程序setData回调 refactor(runtime): nextTick适配小程序setData回调 Apr 16, 2020
@yuche yuche merged commit f401065 into NervJS:next Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants