Skip to content

Commit

Permalink
Avoid needlessly re-checking whether scheduler.yield() is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Oct 10, 2024
1 parent 70077b8 commit d5c51a6
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 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;
};
}
}

/**
* Executes a callback function after the next frame is rendered.
*
Expand All @@ -48,20 +56,20 @@ const afterNextFrame = ( callback: () => void ) => {
*
* @return Promise
*/
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( async ( resolve ) => {
setTimeout( resolve, 0 );
} );
};
export const splitTask =
'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( async ( 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 d5c51a6

Please sign in to comment.