Skip to content

Commit

Permalink
chore: use 2 effects instead
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 15, 2019
1 parent 5445038 commit 80b1f9f
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Reporter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ const RunningTests: React.FC<{
);
};

const Exiter: React.FC<{ done: boolean }> = ({ done }) => {
const { exit } = useApp();

const [shouldExit, setShouldExit] = React.useState(false);

// use a separate effect to ensure output is properly flushed. This _might_ be a bug in Ink, not sure
React.useEffect(() => {
if (done) {
setShouldExit(true);
}
}, [done, exit]);

React.useEffect(() => {
if (shouldExit) {
exit();
}
}, [exit, shouldExit]);

return null;
};

const Reporter: React.FC<Props> = ({
register,
globalConfig,
Expand Down Expand Up @@ -246,13 +267,6 @@ const Reporter: React.FC<Props> = ({
} = state;
const { estimatedTime = 0 } = options;

const { exit } = useApp();
React.useEffect(() => {
if (done) {
setImmediate(exit);
}
}, [done, exit]);

const summary = (
<Summary
aggregatedResults={aggregatedResults}
Expand All @@ -277,6 +291,7 @@ const Reporter: React.FC<Props> = ({
/>
<RunningTests tests={currentTests} width={width} />
{done ? null : summary}
<Exiter done={done} />
</Box>
);
};
Expand Down

0 comments on commit 80b1f9f

Please sign in to comment.