Skip to content

Commit be252bd

Browse files
committed
Close websocket when finishes
1 parent 68c7b3f commit be252bd

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

site/src/hooks/useWorkspaceBuildLogs.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import { watchBuildLogsByBuildId } from "api/api";
22
import { ProvisionerJobLog } from "api/typesGenerated";
33
import { displayError } from "components/GlobalSnackbar/utils";
4-
import { useState, useEffect } from "react";
4+
import { useState, useEffect, useRef } from "react";
55

66
export const useWorkspaceBuildLogs = (
77
// buildId is optional because sometimes the build is not loaded yet
88
buildId: string | undefined,
99
enabled: boolean = true,
1010
) => {
1111
const [logs, setLogs] = useState<ProvisionerJobLog[]>();
12+
const socket = useRef<WebSocket>();
13+
1214
useEffect(() => {
1315
if (!buildId || !enabled) {
16+
socket.current?.close();
1417
return;
1518
}
1619

1720
// Every time this hook is called reset the values
1821
setLogs(undefined);
1922

20-
const socket = watchBuildLogsByBuildId(buildId, {
23+
socket.current = watchBuildLogsByBuildId(buildId, {
2124
// Retrieve all the logs
2225
after: -1,
2326
onMessage: (log) => {
@@ -34,9 +37,9 @@ export const useWorkspaceBuildLogs = (
3437
});
3538

3639
return () => {
37-
socket.close();
40+
socket.current?.close();
3841
};
39-
}, [buildId]);
42+
}, [buildId, enabled]);
4043

4144
return logs;
4245
};

0 commit comments

Comments
 (0)