@@ -22,12 +22,18 @@ export interface GameStreamProps {
22
22
* Most components will only display a single stream id.
23
23
*/
24
24
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 ;
25
31
}
26
32
27
33
export const GameStream : React . FC < GameStreamProps > = (
28
34
props : GameStreamProps
29
35
) : ReactNode => {
30
- const { stream$, gameStreamIds } = props ;
36
+ const { stream$, gameStreamIds, maxLines = 500 } = props ;
31
37
32
38
const filteredStream$ = useObservable ( ( ) => {
33
39
return stream$ . pipe (
@@ -39,17 +45,18 @@ export const GameStream: React.FC<GameStreamProps> = (
39
45
const [ gameLogLines , setGameLogLines ] = useState < Array < GameLogLine > > ( [ ] ) ;
40
46
const clearStreamTimeoutRef = useRef < NodeJS . Timeout > ( ) ;
41
47
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 > => {
46
51
// Append new log line to the list.
47
52
newLogLines = oldLogLines . concat ( newLogLines ) ;
48
53
// 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 ) ;
50
55
return newLogLines ;
51
56
} ) ;
52
- } , [ ] ) ;
57
+ } ,
58
+ [ maxLines ]
59
+ ) ;
53
60
54
61
// Ensure all timeouts are cleared when the component is unmounted.
55
62
useEffect ( ( ) => {
@@ -58,7 +65,7 @@ export const GameStream: React.FC<GameStreamProps> = (
58
65
} ;
59
66
} , [ ] ) ;
60
67
61
- useSubscription ( filteredStream$ , ( logLine ) => {
68
+ useSubscription ( filteredStream$ , ( logLine : GameLogLine ) => {
62
69
// Decouple state updates from the stream subscription to mitigate
63
70
// "Cannot update a component while rendering a different component".
64
71
// This gives some control of the event loop back to react
0 commit comments