-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
[WIP] Add requestIdleCallback polyfill package #12253
Conversation
Sets up the basic structure for the new package along with some empty tests. Pulls out the relevant code from ReactDOMFrameScheduling as a starting point for the polyfill.
This makes it a little easier understand when trying to implement spec-compliant behavior
This is a prerequisite of supporting multiple scheduled callbacks.
idleTick calculates whether a callback is timed out, but this still depends on the callback queue being processed. Use setTimeout as a fallback so that callbacks with timeouts definitely get called within a reasonable timeframe.
This was used when only a single callback was supported
Why not? |
Details of bundled changes.Comparing: 48ffbf0...b3a5223 request-idle-callback-polyfill
Generated by 🚫 dangerJS |
'use strict'; | ||
|
||
// @TODO figure out if we need a prod/dev build for this? | ||
// if (process.env.NODE_ENV === 'production') { |
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.
Yes :D I'm curious why you thought perhaps we don't need separate builds?
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.
What would be the advantage of a development build? There aren't any warnings or DEV-only checks as of now. I guess just providing a non-minified build in DEV is a fine enough reason?
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.
I see. Yeah, let's have both, for debugging. And since it's possible we may add warnings.
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.
This may limit adoption and someone might publish one that just has the one. The prod/dev build thing is still somewhat controversial.
The whole point of this is that we would like this particular implementation to be canonical and gain adoption so a complex build works against that.
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.
Why do we care about anyone who isn’t also using React, with which they have to deal with this anyway?
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.
I get that you’re saying we can get more adoption within the React community if it becomes widely adopted outside of it but idk if I buy that scenario
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.
In my view the main argument for a DEV build is so we can warn, e.g. if we detect the presence of a non-native global requestIdleCallback implementation, which isn’t something we’d want to do in prod (I’m assuming)
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.
Polyfills are not always part of the same build system neither since they can often just be concatenated or loaded from a polyfill CDN service. Even if you're using React you'd hit this.
I just don't see a reason for it.
The scenario you're describing is just a warning about two polyfills existing which you shouldn't do anyway. Even if we warn, there might be another one after that overrides.
We can still warn for a non-native global requestIdleCallback implementation in React itself. That's where it matters.
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.
Warning in ReactDOM makes sense.
|
||
it('executes callbacks that timeout', () => { | ||
const callback = jest.fn(deadline => { | ||
expect(deadline.didTimeout).toBe(true); |
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.
Our typical strategy is to never put assertions inside of callbacks. It's too easy to neglect to call the callback. Instead, we push into a log and make assertions on it in the main function. See one of our async tests for an example:
react/packages/react-reconciler/src/__tests__/ReactIncremental-test.internal.js
Lines 75 to 140 in 48ffbf0
it('updates a previous render', () => { | |
let ops = []; | |
function Header() { | |
ops.push('Header'); | |
return <h1>Hi</h1>; | |
} | |
function Content(props) { | |
ops.push('Content'); | |
return <div>{props.children}</div>; | |
} | |
function Footer() { | |
ops.push('Footer'); | |
return <footer>Bye</footer>; | |
} | |
const header = <Header />; | |
const footer = <Footer />; | |
function Foo(props) { | |
ops.push('Foo'); | |
return ( | |
<div> | |
{header} | |
<Content>{props.text}</Content> | |
{footer} | |
</div> | |
); | |
} | |
ReactNoop.render(<Foo text="foo" />, () => | |
ops.push('renderCallbackCalled'), | |
); | |
ReactNoop.flush(); | |
expect(ops).toEqual([ | |
'Foo', | |
'Header', | |
'Content', | |
'Footer', | |
'renderCallbackCalled', | |
]); | |
ops = []; | |
ReactNoop.render(<Foo text="bar" />, () => | |
ops.push('firstRenderCallbackCalled'), | |
); | |
ReactNoop.render(<Foo text="bar" />, () => | |
ops.push('secondRenderCallbackCalled'), | |
); | |
ReactNoop.flush(); | |
// TODO: Test bail out of host components. This is currently unobservable. | |
// Since this is an update, it should bail out and reuse the work from | |
// Header and Content. | |
expect(ops).toEqual([ | |
'Foo', | |
'Content', | |
'firstRenderCallbackCalled', | |
'secondRenderCallbackCalled', | |
]); | |
}); |
Then you can get rid of suppressErrorLogging = false
.
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.
It does assert that the callback is called, but this looks much better than hacking suppressErrorLogging
👌
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.
Another benefit of a log is the test will fail if there are multiple invocations.
"README.md", | ||
"index.js" | ||
], | ||
"files": ["LICENSE", "README.md", "index.js", "cjs/"], |
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.
Is it ok to format package.json?
While it's reasonable for the polyfill to assume a DOM environment, ReactDOM itself must be able to run in a Node environment. These polyfills won't actually be called if it is, so making any DOM-specific initialization lazy protects us from breaking Node.
I'm curious to see what your test plan will be |
@acdlite I was going to start with the expiration fixture. Any other recommendations? |
Yeah some sort of fixture seems like the way to go |
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.
We might want to externalize but if we do, it'll be under a different name and not as a polyfill.
Not sure if this is related. I have searched, but could not find a good place to comment. My hope is that this proposal actually answers my question. I am working on a custom renderer and noticed If my assumption is correct you can consider this a use case to support moving the logic to a separate package. |
@sebmarkbage so what does that mean for this PR? I'm happy to restart the effort with a different approach if this isn't what we want to do. |
Superseded by the much cooler sounding React Scheduler #12624 |
Adds a new package,
request-idle-callback-polyfill
(final name pending) that's based on the polyfill inside ofReactDOMFrameScheduling
. Adds support for scheduling multiple callbacks. It also schedules asetTimeout
call to ensure that callbacks with timeouts get called.TODO
rollup/bundles.js
(probably don't need prod/dev builds?)ReactDOMFrameScheduling
and import this onerequestIdleCallback
if it exists1: For example, should it export
requestIdleCallback
or write towindow
?