Skip to content

Commit

Permalink
Leverage scheduler.yield in splitTask when available (WordPress#66001)
Browse files Browse the repository at this point in the history
* Leverage scheduler.yield in splitTask when available

* Eliminate extra Promise when using scheduler.yield()

Co-authored-by: swissspidy <swissspidy@git.wordpress.org>

* Avoid needlessly re-checking whether scheduler.yield() is defined

* Remove needless function wrapper

* Ensure that scheduler is context object to yield function

* Remove needless async

* Reduce verbosity of scheduler.yield() existence check

* Fix return type for scheduler.yield()

Co-authored-by: Brendan Kenny <bckenny@gmail.com>

* Define scheduler and scheduler.yield as optional

Co-authored-by: Brendan Kenny <bckenny@gmail.com>

---------


Unlinked contributors: brendankenny, LeszekSwirski.
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
  • Loading branch information
3 people authored and karthick-murugan committed Nov 13, 2024
1 parent 7b9b4ab commit a630c40
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 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?: () => Promise< void >;
};
}
}

/**
* Executes a callback function after the next frame is rendered.
*
Expand All @@ -48,12 +56,14 @@ 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 =
typeof window.scheduler?.yield === 'function'
? window.scheduler.yield.bind( window.scheduler )
: () => {
return new Promise( ( resolve ) => {
setTimeout( resolve, 0 );
} );
};

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

0 comments on commit a630c40

Please sign in to comment.