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
26 changes: 20 additions & 6 deletions packages/interactivity/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ interface Flusher {
readonly dispose: () => void;
}

declare global {
interface Window {
scheduler: {
readonly yield: () => void;
westonruter marked this conversation as resolved.
Show resolved Hide resolved
};
}
}

/**
* Executes a callback function after the next frame is rendered.
*
Expand All @@ -48,12 +56,18 @@ const afterNextFrame = ( callback: () => void ) => {
*
* @return Promise
*/
export const splitTask = () => {
return new Promise( ( resolve ) => {
// TODO: Use scheduler.yield() when available.
setTimeout( resolve, 0 );
} );
};
export const splitTask =
'scheduler' in window &&
westonruter marked this conversation as resolved.
Show resolved Hide resolved
typeof window.scheduler === 'object' &&
null !== window.scheduler &&
'yield' in window.scheduler &&
typeof window.scheduler.yield === 'function'
? window.scheduler.yield
: () => {
return new Promise( async ( resolve ) => {
westonruter marked this conversation as resolved.
Show resolved Hide resolved
setTimeout( resolve, 0 );
} );
};

/**
* Creates a Flusher object that can be used to flush computed values and notify listeners.
Expand Down
Loading