Skip to content

Commit dd2b3a1

Browse files
committed
feat(game-steam): make scrollback buffer a cmp prop
1 parent e8eba7d commit dd2b3a1

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ export interface GameStreamProps {
2222
* Most components will only display a single stream id.
2323
*/
2424
gameStreamIds: Array<string>;
25+
/**
26+
* The maximum number of lines to keep in the game log.
27+
* The oldest lines will be dropped to stay within this limit.
28+
* Default is 500.
29+
*/
30+
maxLines?: number;
2531
}
2632

2733
export const GameStream: React.FC<GameStreamProps> = (
2834
props: GameStreamProps
2935
): ReactNode => {
30-
const { stream$, gameStreamIds } = props;
36+
const { stream$, gameStreamIds, maxLines = 500 } = props;
3137

3238
const filteredStream$ = useObservable(() => {
3339
return stream$.pipe(
@@ -39,17 +45,18 @@ export const GameStream: React.FC<GameStreamProps> = (
3945
const [gameLogLines, setGameLogLines] = useState<Array<GameLogLine>>([]);
4046
const clearStreamTimeoutRef = useRef<NodeJS.Timeout>();
4147

42-
const appendGameLogLines = useCallback((newLogLines: Array<GameLogLine>) => {
43-
// Max number of most recent lines to keep.
44-
const scrollbackBufferSize = 500;
45-
setGameLogLines((oldLogLines) => {
48+
const appendGameLogLines = useCallback(
49+
(newLogLines: Array<GameLogLine>) => {
50+
setGameLogLines((oldLogLines: Array<GameLogLine>): Array<GameLogLine> => {
4651
// Append new log line to the list.
4752
newLogLines = oldLogLines.concat(newLogLines);
4853
// Trim the back of the list to keep it within the scrollback buffer.
49-
newLogLines = newLogLines.slice(scrollbackBufferSize * -1);
54+
newLogLines = newLogLines.slice(maxLines * -1);
5055
return newLogLines;
5156
});
52-
}, []);
57+
},
58+
[maxLines]
59+
);
5360

5461
// Ensure all timeouts are cleared when the component is unmounted.
5562
useEffect(() => {
@@ -58,7 +65,7 @@ export const GameStream: React.FC<GameStreamProps> = (
5865
};
5966
}, []);
6067

61-
useSubscription(filteredStream$, (logLine) => {
68+
useSubscription(filteredStream$, (logLine: GameLogLine) => {
6269
// Decouple state updates from the stream subscription to mitigate
6370
// "Cannot update a component while rendering a different component".
6471
// This gives some control of the event loop back to react

0 commit comments

Comments
 (0)