File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change 1
1
import { watchBuildLogsByBuildId } from "api/api" ;
2
2
import { ProvisionerJobLog } from "api/typesGenerated" ;
3
3
import { displayError } from "components/GlobalSnackbar/utils" ;
4
- import { useState , useEffect } from "react" ;
4
+ import { useState , useEffect , useRef } from "react" ;
5
5
6
6
export const useWorkspaceBuildLogs = (
7
7
// buildId is optional because sometimes the build is not loaded yet
8
8
buildId : string | undefined ,
9
9
enabled : boolean = true ,
10
10
) => {
11
11
const [ logs , setLogs ] = useState < ProvisionerJobLog [ ] > ( ) ;
12
+ const socket = useRef < WebSocket > ( ) ;
13
+
12
14
useEffect ( ( ) => {
13
15
if ( ! buildId || ! enabled ) {
16
+ socket . current ?. close ( ) ;
14
17
return ;
15
18
}
16
19
17
20
// Every time this hook is called reset the values
18
21
setLogs ( undefined ) ;
19
22
20
- const socket = watchBuildLogsByBuildId ( buildId , {
23
+ socket . current = watchBuildLogsByBuildId ( buildId , {
21
24
// Retrieve all the logs
22
25
after : - 1 ,
23
26
onMessage : ( log ) => {
@@ -34,9 +37,9 @@ export const useWorkspaceBuildLogs = (
34
37
} ) ;
35
38
36
39
return ( ) => {
37
- socket . close ( ) ;
40
+ socket . current ?. close ( ) ;
38
41
} ;
39
- } , [ buildId ] ) ;
42
+ } , [ buildId , enabled ] ) ;
40
43
41
44
return logs ;
42
45
} ;
You can’t perform that action at this time.
0 commit comments