Skip to content

chore: add cleanup callbacks to some useEffect calls #13444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions site/src/contexts/useProxyLatency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ export const useProxyLatency = (
// Local storage cleanup
garbageCollectStoredLatencies(proxies, maxStoredLatencies);
});

return () => {
observer.disconnect();
};
}, [proxies, latestFetchRequest, maxStoredLatencies]);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,32 @@ const useFileTree = (templateVersion: TemplateVersion | undefined) => {
fileTree: undefined,
tarFile: undefined,
});

useEffect(() => {
let stale = false;
const initializeFileTree = async (file: ArrayBuffer) => {
const tarFile = new TarReader();
await tarFile.readFile(file);
const fileTree = await createTemplateVersionFileTree(tarFile);
setState({ fileTree, tarFile });
try {
await tarFile.readFile(file);
// Ignore stale updates if this effect has been cancelled.
if (stale) {
return;
}
const fileTree = createTemplateVersionFileTree(tarFile);
setState({ fileTree, tarFile });
} catch (error) {
console.error(error);
displayError("Error on initializing the editor");
}
};

if (fileQuery.data) {
initializeFileTree(fileQuery.data).catch((reason) => {
console.error(reason);
displayError("Error on initializing the editor");
});
void initializeFileTree(fileQuery.data);
}

return () => {
stale = true;
};
}, [fileQuery.data]);

return state;
Expand Down
5 changes: 4 additions & 1 deletion site/src/pages/WorkspacePage/WorkspaceBuildProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export const WorkspaceBuildProgress: FC<WorkspaceBuildProgressProps> = ({
setProgressValue(est);
setProgressText(text);
};
setTimeout(updateProgress, 5);
const updateTimer = requestAnimationFrame(updateProgress);
return () => {
cancelAnimationFrame(updateTimer);
};
}, [progressValue, job, transitionStats]);

// HACK: the codersdk type generator doesn't support null values, but this
Expand Down
4 changes: 2 additions & 2 deletions site/src/utils/templateVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export const getTemplateVersionFiles = async (
return files;
};

export const createTemplateVersionFileTree = async (
export const createTemplateVersionFileTree = (
tarReader: TarReader,
): Promise<FileTree> => {
): FileTree => {
let fileTree: FileTree = {};
for (const file of tarReader.fileInfo) {
fileTree = set(
Expand Down
Loading