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

react中的代码细节 #2

Open
BUPTlhuanyu opened this issue Mar 12, 2021 · 1 comment
Open

react中的代码细节 #2

BUPTlhuanyu opened this issue Mar 12, 2021 · 1 comment

Comments

@BUPTlhuanyu
Copy link
Owner

facebook/react#14226

在16.8中的scheduler中用到window.postmessage,这样会存在两个问题:

  1. 不是scheduler中的message处理函数也会被执行
  2. 不是scheduler中的postmessage会导致触发scheduler中的message处理函数。
    为了避免第二个问题,scheduler中的message处理函数会判断接收的数据中是否有messageKey,如果没有那么是不会继续执行下去的。
    但是第一个问题避免不了,每次postmessage都会执行与react-scheduler不相关的message事件处理函数。

现在最新的scheduler直接使用new MessageChannel来解决第一个问题,建立一个通信信道,利用port.postmessage代替window.postmessage,利用port.onmessage代替window.addEventListener('message', ...)。这个port实际就是这个MessageChannel专属的,不会导致信号串扰。

@BUPTlhuanyu
Copy link
Owner Author

facebook/react#16271

scheduler 中 requestAnimation 构成的循环对CPU的利用率低于 message 构成的循环

但是测试代码在哪?

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

No branches or pull requests

1 participant