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 <[email protected]>

* 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 <[email protected]>

* Define scheduler and scheduler.yield as optional

Co-authored-by: Brendan Kenny <[email protected]>

---------


Unlinked contributors: brendankenny, LeszekSwirski.
Co-authored-by: felixarntz <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent 26e108d commit cd8d4dc
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 cd8d4dc

Please sign in to comment.