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(statusbaritem): render before linting #2

Merged
merged 1 commit into from
Oct 27, 2022
Merged

fix(statusbaritem): render before linting #2

merged 1 commit into from
Oct 27, 2022

Conversation

lvjiaxuan
Copy link
Owner

@lvjiaxuan lvjiaxuan commented Oct 27, 2022

当前,首次使用 ctrl+alt+d 命令,底部状态栏的 loading 效果不会立即被渲染,而是 ESLint linting 结束后才一闪而过。

statusBarItem.text = '$(loading~spin) Start linting hole file content...'
statusBarItem.show() // 查看源码,show() 最终被放到 setTimeout,然后渲染也是个异步任务。

const results = await eslint.lintFiles(activeTextEditor.document.uri.fsPath) // 经调试,这里实际上是同步代码,所以导致 `statusBarItem.show` 无法立即渲染显示。

像下面代码:

async function test () {
  setTimeout(() => {
    console.log('show')
    setTimeout(() => console.log('render'))
  })

  await new Promise(resolve => {
    console.log('linting')
    resolve()
  })

  console.log('code')
}
// test()
// linting code show render

所以需要把 render 放到 linting的前面,或者说把 linting 放到 render 的后面:

async function test () {
  setTimeout(() => {
    console.log('show')
    setTimeout(() => console.log('render'))
  })

  await new Promise(resolve => setTimeout(resolve))

  await new Promise(resolve => {
    console.log('linting')
    resolve()
  })

  console.log('code')
}
// test()
// show linting code render

一个 setTimeout 居然不够用,分析一下事件循环模式😂,要再加一个:

async function test () {
  setTimeout(() => {
    console.log('show')
    setTimeout(() => console.log('render'))
  })

  await new Promise(resolve => setTimeout(() => setTimeout(resolve)))

  await new Promise(resolve => {
    console.log('linting')
    resolve()
  })

  console.log('code')
}
// test()
// show render linting code

可以了。

这里渲染任务无论是宏还是微,都需要两个 setTimeout 。
可以换成这样自己试一试 Promise.resolve().then(() => console.log('render'))

@lvjiaxuan lvjiaxuan marked this pull request as ready for review October 27, 2022 10:03
Copy link
Owner Author

@lvjiaxuan lvjiaxuan left a comment

Choose a reason for hiding this comment

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

Finsh my review.

@lvjiaxuan lvjiaxuan merged commit f16ab48 into main Oct 27, 2022
@lvjiaxuan lvjiaxuan deleted the patch branch October 27, 2022 10:06
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.

None yet

1 participant