Skip to content

Commit 0de34fe

Browse files
committed
feat: prevent flickering when clearing stream
1 parent b379c43 commit 0de34fe

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

electron/renderer/components/game/game-content.tsx

+22-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,31 @@ export const GameContent: React.FC<GameContentProps> = (
3434
return stream$.pipe(rxjs.filter((m) => gameStreamIds.includes(m.streamId)));
3535
});
3636

37+
const clearStreamTimeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
38+
39+
// Ensure the timeout is cleared when the component is unmounted.
40+
useEffect(() => {
41+
return () => {
42+
clearTimeout(clearStreamTimeoutRef.current);
43+
};
44+
}, []);
45+
3746
useSubscription(filteredStream$, (logLine) => {
3847
if (logLine.text === '__CLEAR_STREAM__') {
39-
setGameLogLines([]);
48+
// Clear the stream after a short delay to prevent flickering.
49+
clearStreamTimeoutRef.current = setTimeout(() => {
50+
setGameLogLines([]);
51+
}, 1000);
4052
} else {
41-
appendGameLogLine(logLine);
53+
// If we receieved a new log line, cancel any pending clear stream.
54+
// Set the game log lines to the new log line to prevent flickering.
55+
if (clearStreamTimeoutRef.current) {
56+
clearTimeout(clearStreamTimeoutRef.current);
57+
clearStreamTimeoutRef.current = undefined;
58+
setGameLogLines([logLine]);
59+
} else {
60+
appendGameLogLine(logLine);
61+
}
4262
}
4363
});
4464

0 commit comments

Comments
 (0)