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

[WIP] Add requestIdleCallback polyfill package #12253

Closed
wants to merge 14 commits into from

Conversation

aweary
Copy link
Contributor

@aweary aweary commented Feb 20, 2018

Adds a new package, request-idle-callback-polyfill (final name pending) that's based on the polyfill inside of ReactDOMFrameScheduling. Adds support for scheduling multiple callbacks. It also schedules a setTimeout call to ensure that callbacks with timeouts get called.

TODO

  • create two entry points, one that writes to global and one that doesn't
  • See if we can detect native rIC implementations and warn when people do the wrong thing
  • figure out the actual name for the package
  • figure out the correct build config for rollup/bundles.js (probably don't need prod/dev builds?)
  • determine the final API 1
  • remove the previous polyfill from ReactDOMFrameScheduling and import this one
    • Depending on this answer, potentially re-export the native requestIdleCallback if it exists

1: For example, should it export requestIdleCallback or write to window?

Brandon Dail added 9 commits February 20, 2018 09:59
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
@acdlite
Copy link
Collaborator

acdlite commented Feb 20, 2018

(probably don't need prod/dev builds?)

Why not?

@pull-bot
Copy link

pull-bot commented Feb 20, 2018

Details of bundled changes.

Comparing: 48ffbf0...b3a5223

request-idle-callback-polyfill

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
request-idle-callback-polyfill.development.js +100% +100% 0 B 6.88 KB 0 B 2.5 KB UMD_DEV
request-idle-callback-polyfill.production.min.js 🔺+100% 🔺+100% 0 B 1.59 KB 0 B 907 B UMD_PROD
request-idle-callback-polyfill.development.js +100% +100% 0 B 6.68 KB 0 B 2.45 KB NODE_DEV
request-idle-callback-polyfill.production.min.js 🔺+100% 🔺+100% 0 B 1.42 KB 0 B 839 B NODE_PROD

Generated by 🚫 dangerJS

'use strict';

// @TODO figure out if we need a prod/dev build for this?
// if (process.env.NODE_ENV === 'production') {
Copy link
Collaborator

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?

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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?

Copy link
Collaborator

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

Copy link
Collaborator

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)

Copy link
Collaborator

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.

Copy link
Collaborator

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);
Copy link
Collaborator

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:

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.

Copy link
Contributor Author

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 👌

Copy link
Collaborator

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/"],
Copy link
Contributor

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?

Brandon Dail added 2 commits February 21, 2018 14:28
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.
@acdlite
Copy link
Collaborator

acdlite commented Feb 23, 2018

I'm curious to see what your test plan will be

@aweary
Copy link
Contributor Author

aweary commented Feb 23, 2018

@acdlite I was going to start with the expiration fixture. Any other recommendations?

@acdlite
Copy link
Collaborator

acdlite commented Feb 24, 2018

Yeah some sort of fixture seems like the way to go

Copy link
Collaborator

@sebmarkbage sebmarkbage left a 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.

@EECOLOR
Copy link

EECOLOR commented Mar 26, 2018

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 ReactDOMFrameScheduling has some great methods when you are in a browser. My original question was to make it publicly available (probably as part of react-dom). It seems this thread makes the original request obsolete.

If my assumption is correct you can consider this a use case to support moving the logic to a separate package.

@aweary
Copy link
Contributor Author

aweary commented Mar 27, 2018

@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.

@aweary
Copy link
Contributor Author

aweary commented Apr 17, 2018

Superseded by the much cooler sounding React Scheduler #12624

@aweary aweary closed this Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants