mirror of
https://github.com/coder/coder.git
synced 2026-09-21 20:51:01 +08:00
chore: add cleanup callbacks to some useEffect calls (#13444)
This commit is contained in:
@@ -225,6 +225,10 @@ export const useProxyLatency = (
|
||||
// Local storage cleanup
|
||||
garbageCollectStoredLatencies(proxies, maxStoredLatencies);
|
||||
});
|
||||
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
};
|
||||
}, [proxies, latestFetchRequest, maxStoredLatencies]);
|
||||
|
||||
return {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user