Skip to content

Commit

Permalink
Fix(@inquirer/core): Delay showing any loader by 300ms to avoid spinn…
Browse files Browse the repository at this point in the history
…er flickering. Ref #1407
  • Loading branch information
SBoudrias committed Jun 19, 2024
1 parent 3d15d50 commit c7583fc
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/core/src/lib/use-prefix.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ export function usePrefix({

useEffect((): void | (() => unknown) => {
if (isLoading) {
let tickInterval: NodeJS.Timeout | undefined;
let inc = -1;
const interval = setInterval(
AsyncResource.bind(() => {
inc = inc + 1;
setTick(inc % spinner.frames.length);
}),
spinner.interval,
);
// Delay displaying spinner by 300ms, to avoid flickering
const delayTimeout = setTimeout(() => {
tickInterval = setInterval(
AsyncResource.bind(() => {
inc = inc + 1;
setTick(inc % spinner.frames.length);
}),
spinner.interval,
);
}, 300);

return () => clearInterval(interval);
return () => {
clearTimeout(delayTimeout);
clearInterval(tickInterval);
};
}
}, [isLoading]);

Expand Down

0 comments on commit c7583fc

Please sign in to comment.