-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Use Yield within the Scheduler API to break up long JavaScript tasks [🟥 awaiting full support] #2295
Comments
Quick notes, the function that this is within must be an if ( "scheduler" in window && "yield" in scheduler ){
await scheduler.yield();
} |
Great article on this: https://www.debugbear.com/blog/scheduler-yield |
Added comments throughout Nebula for where I intend to place these when available.
These are placed in functions that are already async as well as functions that are not. Would need to test if changing non-async functions into async (and enabling the yield) breaks any functionality. |
Could use this function in the meantime... //A function for shimming scheduler.yield with no fallback:
function yieldToMain () {
//Use scheduler.yield if it exists
if ( 'scheduler' in window && 'yield' in scheduler ){
return scheduler.yield();
}
return; //Fall back to nothing (no yielding)
}
// Example usage:
async function doWork(){
// Do some work:
// ...
await yieldToMain();
// Do some other work:
// ...
} |
Ok I have grown on the idea of using a shim function in the meantime while we wait for browser support. //Yield back to the main thread when supported by the browser. Otherwise, do nothing.
nebula.yield = function(){
//Use scheduler.yield if it exists
if ( 'scheduler' in window && 'yield' in scheduler ){
return scheduler.yield();
}
return; //Fall back to nothing (no yielding)
}; Use it like this: await nebula.yield(); This is now used in several places throughout Nebula and can also be used in the child theme. Just remember that the function needs to be async which means that it returns a promise! Anything calling an async function must expect a promise (and not a specific value) returned. So they can either use |
I also wrote a |
https://developer.chrome.com/blog/introducing-scheduler-yield-origin-trial
As of Chrome 129, functions can yield back to the main thread by using
await scheduler.yield();
Google recommends using it liberally throughout the codebase, but I want to start with just the biggest functionality first.
Edit: Now on CanIUse: https://caniuse.com/mdn-api_scheduler_yield
The text was updated successfully, but these errors were encountered: