File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -2,16 +2,34 @@ import { useEffect, useState } from "react";
22
33import { useTasksApi } from "./useTasksApi" ;
44
5+ /**
6+ * Subscribes to workspace log lines pushed from the extension.
7+ * Batches updates per animation frame to avoid excessive re-renders
8+ * when many lines arrive in quick succession.
9+ */
510export function useWorkspaceLogs ( ) : string [ ] {
611 const { onWorkspaceLogsAppend, closeWorkspaceLogs } = useTasksApi ( ) ;
712 const [ lines , setLines ] = useState < string [ ] > ( [ ] ) ;
813
914 useEffect ( ( ) => {
15+ let pending : string [ ] = [ ] ;
16+ let frame = 0 ;
17+
1018 const unsubscribe = onWorkspaceLogsAppend ( ( newLines ) => {
11- setLines ( ( prev ) => [ ...prev , ...newLines ] ) ;
19+ pending . push ( ...newLines ) ;
20+ if ( frame === 0 ) {
21+ frame = requestAnimationFrame ( ( ) => {
22+ const batch = pending ;
23+ pending = [ ] ;
24+ frame = 0 ;
25+ setLines ( ( prev ) => prev . concat ( batch ) ) ;
26+ } ) ;
27+ }
1228 } ) ;
29+
1330 return ( ) => {
1431 unsubscribe ( ) ;
32+ cancelAnimationFrame ( frame ) ;
1533 closeWorkspaceLogs ( ) ;
1634 } ;
1735 } , [ closeWorkspaceLogs , onWorkspaceLogsAppend ] ) ;
You can’t perform that action at this time.
0 commit comments