Skip to content

Commit ce5000e

Browse files
committed
fix(game-stream): when reconnect, reregister autoscroll observer
1 parent f7b393c commit ce5000e

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

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

+24-12
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,26 @@ export const GameStream: React.FC<GameStreamProps> = (
4747
const { stream$, primaryStreamId, gameStreamIds, maxLines = 500 } = props;
4848

4949
const [gameLogLines, setGameLogLines] = useState<Array<GameLogLine>>([]);
50+
const [isConnected, setIsConnected] = useState<boolean>(false);
5051
const clearStreamTimeoutRef = useRef<NodeJS.Timeout>();
5152

5253
// Clear streams on new connections.
5354
useSubscribe(['game:connect'], () => {
5455
setGameLogLines([]);
56+
setIsConnected(true);
5557
});
5658

57-
const filteredStream$ = useObservable(() => {
58-
return stream$.pipe(
59-
filterLinesForGameStreams({ gameStreamIds }),
60-
excludeDuplicateEmptyLines
61-
);
59+
useSubscribe(['game:disconnect'], () => {
60+
setIsConnected(false);
6261
});
6362

63+
// Ensure all timeouts are cleared when the component is unmounted.
64+
useEffect(() => {
65+
return () => {
66+
clearTimeout(clearStreamTimeoutRef.current);
67+
};
68+
}, []);
69+
6470
const appendGameLogLines = useCallback(
6571
(newLogLines: Array<GameLogLine>) => {
6672
setGameLogLines((oldLogLines: Array<GameLogLine>): Array<GameLogLine> => {
@@ -74,12 +80,12 @@ export const GameStream: React.FC<GameStreamProps> = (
7480
[maxLines]
7581
);
7682

77-
// Ensure all timeouts are cleared when the component is unmounted.
78-
useEffect(() => {
79-
return () => {
80-
clearTimeout(clearStreamTimeoutRef.current);
81-
};
82-
}, []);
83+
const filteredStream$ = useObservable(() => {
84+
return stream$.pipe(
85+
filterLinesForGameStreams({ gameStreamIds }),
86+
excludeDuplicateEmptyLines
87+
);
88+
});
8389

8490
useSubscription(filteredStream$, (logLine: GameLogLine) => {
8591
// Decouple state updates from the stream subscription to mitigate
@@ -122,6 +128,12 @@ export const GameStream: React.FC<GameStreamProps> = (
122128
// added to the scrollable element to warrant an initial scroll.
123129
// After that, the browser handles it automatically.
124130
useEffect(() => {
131+
// Reset counter when we reconnect to the game.
132+
// For example, when changing characters.
133+
// Otherwise, the new stream of text won't scroll
134+
// the window to the bottom as the user would expect.
135+
observedTargetCountRef.current = 0;
136+
125137
const callback: IntersectionObserverCallback = (
126138
entries: Array<IntersectionObserverEntry>
127139
) => {
@@ -166,7 +178,7 @@ export const GameStream: React.FC<GameStreamProps> = (
166178
return () => {
167179
observer.disconnect();
168180
};
169-
}, []);
181+
}, [isConnected]);
170182

171183
return (
172184
<EuiPanel

0 commit comments

Comments
 (0)