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

Bind rAF and cAF to window (#16606) #16709

Closed
wants to merge 4 commits into from
Closed

Conversation

liady
Copy link
Contributor

@liady liady commented Sep 9, 2019

When capturing local references of requestAnimationFrame and cancelAnimationFrame - bind them to the window object.
Fixes #16606

When capturing local references of requestAnimationFrame and cancelAnimationFrame - bind them to the window object.
Fixes facebook#16606
@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@sizebot
Copy link

sizebot commented Sep 9, 2019

React: size: 🔺+1.1%, gzip: 🔺+0.3%

Details of bundled changes.

Comparing: 9444c87...0562fb5

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js +0.3% +0.2% 117.47 KB 117.77 KB 29.82 KB 29.89 KB UMD_DEV
react.production.min.js 🔺+1.1% 🔺+0.3% 12.43 KB 12.56 KB 4.9 KB 4.91 KB UMD_PROD
react.profiling.min.js +0.9% +0.3% 15.83 KB 15.96 KB 5.9 KB 5.91 KB UMD_PROFILING
react.development.js 0.0% -0.0% 73.12 KB 73.12 KB 19.25 KB 19.25 KB NODE_DEV
react.production.min.js 0.0% -0.0% 6.69 KB 6.69 KB 2.78 KB 2.78 KB NODE_PROD
React-dev.js 0.0% -0.0% 71.18 KB 71.18 KB 18.31 KB 18.31 KB FB_WWW_DEV
React-prod.js 0.0% 0.0% 17.41 KB 17.41 KB 4.55 KB 4.56 KB FB_WWW_PROD
React-profiling.js 0.0% 0.0% 17.41 KB 17.41 KB 4.55 KB 4.56 KB FB_WWW_PROFILING

scheduler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
scheduler-unstable_mock.development.js 0.0% 0.0% 22.12 KB 22.12 KB 5.13 KB 5.13 KB UMD_DEV
scheduler-tracing.profiling.min.js 0.0% -0.1% 3.25 KB 3.25 KB 994 B 993 B NODE_PROFILING
scheduler-unstable_mock.production.min.js 0.0% -0.0% 4.73 KB 4.73 KB 1.98 KB 1.98 KB UMD_PROD
Scheduler-dev.js +1.1% +1.3% 29.62 KB 29.94 KB 7.52 KB 7.62 KB FB_WWW_DEV
Scheduler-prod.js 🔺+1.5% 🔺+1.1% 12.61 KB 12.8 KB 3.06 KB 3.09 KB FB_WWW_PROD
Scheduler-profiling.js +1.2% +0.8% 16.24 KB 16.43 KB 3.76 KB 3.79 KB FB_WWW_PROFILING
scheduler-tracing.development.js 0.0% -0.0% 11.72 KB 11.72 KB 3.03 KB 3.03 KB NODE_DEV
scheduler-tracing.production.min.js 0.0% -1.0% 728 B 728 B 387 B 383 B NODE_PROD
scheduler-unstable_mock.development.js 0.0% -0.0% 21.93 KB 21.93 KB 5.07 KB 5.07 KB NODE_DEV
scheduler.development.js +1.0% +1.2% 29.16 KB 29.46 KB 7.43 KB 7.53 KB NODE_DEV
scheduler.production.min.js 🔺+2.7% 🔺+1.0% 5 KB 5.14 KB 2 KB 2.02 KB NODE_PROD

Generated by 🚫 dangerJS against 0562fb5

@gaearon
Copy link
Collaborator

gaearon commented Sep 10, 2019

  1. Please add a comment explaining why this was necessary.
  2. Has someone verified this indeed fixes the reported issue?

@liady
Copy link
Contributor Author

liady commented Sep 10, 2019

This issue occured when running in environments where window exists but the global this does not equal window.
In those environments - dereferencing the functions (requestAnimationFrame = window.requestAnimationFrame) loses the binding to window and since the global this is different it throws an error.
By explicitly binding the derefernced function to window - this error is avoided.

@gaearon
Copy link
Collaborator

gaearon commented Sep 10, 2019

I understand the reasoning. I’d just like someone to manually verify that this fixes the observable issue by doing a build and checking that. Could you please do it?

@gaearon
Copy link
Collaborator

gaearon commented Sep 10, 2019

Sorry I didn’t clarify. By “add a comment” I meant please add a comment in the source code. Just a couple of lines.

Add a comment to clarify why we explicitly bind the functions to window.
Check specifically that requestAnimationFrame and cancelAnimationFrame are functions, and not just that they are truthy.
@liady
Copy link
Contributor Author

liady commented Sep 10, 2019

@gaearon I checked it.

With a very minimal web-extension content script (repo here):

const container = document.createElement('div');
container.id = "raf-test";
document.body.appendChild(container);

ReactDOM.render(React.createElement('div', null, `Hello rAF`), container);

I Managed to reproduce it without the fix:

Notok

And with the fix it works:

ok2

Copy link
Collaborator

@acdlite acdlite left a comment

Choose a reason for hiding this comment

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

There could be other problems with using requestAnimationFrame in a background or extension script. The browser could choose to throttle requestAnimationFrame since the script is not associated with visible content, for instance. So I don't think binding to window is a reasonable solution. We should try to find a more comprehensive solution for when Scheduler runs in the background.

(However, we'll probably be switching away from requestAnimationFrame to MessageChannel, so this might not matter: #16408)

@liady
Copy link
Contributor Author

liady commented Sep 10, 2019

@acdlite do you think this change should be reverted? Or should it remain there to at least fix this specific issue (when trying to run React inside a web extension)?

@phaistonian
Copy link

@liady Did you manage to fix this issue in your app/extension? Without using this PR, that is.

@liady
Copy link
Contributor Author

liady commented Nov 1, 2019

@phaistonian no, still waiting for the final verdict on this PR...

@stale
Copy link

stale bot commented Jan 30, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jan 30, 2020
@stale
Copy link

stale bot commented Feb 6, 2020

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@stale stale bot closed this Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Resolution: Stale Automatically closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: "'requestAnimationFrame' called on an object that does not implement interface Window."
7 participants