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

Leverage scheduler.yield in splitTask when available #66001

Merged
merged 9 commits into from
Oct 16, 2024
16 changes: 13 additions & 3 deletions packages/interactivity/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,19 @@ const afterNextFrame = ( callback: () => void ) => {
* @return Promise
*/
export const splitTask = () => {
return new Promise( ( resolve ) => {
// TODO: Use scheduler.yield() when available.
setTimeout( resolve, 0 );
return new Promise( async ( resolve ) => {
if (
'scheduler' in window &&
typeof window.scheduler === 'object' &&
null !== window.scheduler &&
'yield' in window.scheduler &&
typeof window.scheduler.yield === 'function'
) {
await window.scheduler.yield();
resolve( undefined );
} else {
setTimeout( resolve, 0 );
}
} );
Copy link
Member

Choose a reason for hiding this comment

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

This is more readable IMO and avoids an async function in the promise body:

export const splitTask = () => {
	if (
		'scheduler' in window &&
		typeof window.scheduler === 'object' &&
		null !== window.scheduler &&
		'yield' in window.scheduler &&
		typeof window.scheduler.yield === 'function'
	) {
		return window.scheduler.yield();
	}
	
	return new Promise( ( resolve ) => {
		setTimeout( resolve, 0 );
	} );
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, perfect. I spaced on this. I got the same feedback from @LeszekSwirski.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've applied that in 70077b8 and then further improved it in d5c51a6 by only doing the check for scheduler.yield() once.

Copy link
Member Author

Choose a reason for hiding this comment

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

And improved yet further in 2516cd4 by removing the wrapper function around scheduler.yield(). So when scheduler.yield() is available, splitTask is just a reference to that function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Humm. Actually this results in an error for some reason:

image

I guess it's because yield here doesn't have scheduler as its context object.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in a0933f4 via bind().

Copy link
Member Author

Choose a reason for hiding this comment

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

There's probably zero difference in the end between:

window.scheduler.yield.bind( window.scheduler )

And:

() => {
    return window.scheduler.yield();
}

Since in both cases a new function is involved. The former seems to be syntactic sugar for the latter.

Copy link
Member

Choose a reason for hiding this comment

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

But why use bind over the latter? I find the latter more intuitive to understand. Also, I'm not familiar enough with bind to be confident so that is the same. Since you also say "probably", I'd suggest we go with the latter.

We have to use a function anyway for the fallback of returning a promise, so we might as well wrap the entire code in a function.

Choose a reason for hiding this comment

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

"bind" is very very slightly preferable for the JS engine, since it can represent the result as a "bound function" that forwards directly to the function it binds already while resolving the call, and doesn't need to parse it, compile it, interpret bytecode, etc.

};

Expand Down
Loading