refactor: extract updateList into callback

This commit is contained in:
Simon Knott 2024-07-24 15:38:49 +02:00
parent fdf39a2970
commit 01a0024ce7
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -93,6 +93,7 @@ export const UIModeView: React.FC<{}> = ({
const [isDisconnected, setIsDisconnected] = React.useState(false); const [isDisconnected, setIsDisconnected] = React.useState(false);
const [hasBrowsers, setHasBrowsers] = React.useState(true); const [hasBrowsers, setHasBrowsers] = React.useState(true);
const [testServerConnection, setTestServerConnection] = React.useState<TestServerConnection>(); const [testServerConnection, setTestServerConnection] = React.useState<TestServerConnection>();
const [teleSuiteUpdater, setTeleSuiteUpdater] = React.useState<TeleSuiteUpdater>();
const inputRef = React.useRef<HTMLInputElement>(null); const inputRef = React.useRef<HTMLInputElement>(null);
@ -157,20 +158,7 @@ export const UIModeView: React.FC<{}> = ({
pathSeparator, pathSeparator,
}); });
const updateList = () => { setTeleSuiteUpdater(teleSuiteUpdater);
commandQueue.current = commandQueue.current.then(async () => {
setIsLoading(true);
try {
const result = await testServerConnection.listTests({ projects: queryParams.projects, locations: queryParams.args });
teleSuiteUpdater.processListReport(result.report);
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
} finally {
setIsLoading(false);
}
});
};
setTestModel(undefined); setTestModel(undefined);
setIsLoading(true); setIsLoading(true);
@ -189,7 +177,6 @@ export const UIModeView: React.FC<{}> = ({
const result = await testServerConnection.listTests({ projects: queryParams.projects, locations: queryParams.args }); const result = await testServerConnection.listTests({ projects: queryParams.projects, locations: queryParams.args });
teleSuiteUpdater.processListReport(result.report); teleSuiteUpdater.processListReport(result.report);
testServerConnection.onListChanged(updateList);
testServerConnection.onReport(params => { testServerConnection.onReport(params => {
teleSuiteUpdater.processTestReportEvent(params); teleSuiteUpdater.processTestReportEvent(params);
}); });
@ -205,6 +192,29 @@ export const UIModeView: React.FC<{}> = ({
}; };
}, [testServerConnection]); }, [testServerConnection]);
const updateList = React.useCallback(async () => {
if (!testServerConnection || !teleSuiteUpdater)
return;
commandQueue.current = commandQueue.current.then(async () => {
setIsLoading(true);
try {
const result = await testServerConnection.listTests({ projects: queryParams.projects, locations: queryParams.args });
teleSuiteUpdater.processListReport(result.report);
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
} finally {
setIsLoading(false);
}
});
await commandQueue.current;
}, [testServerConnection, teleSuiteUpdater]);
React.useEffect(() => {
testServerConnection?.onListChanged(updateList);
}, [testServerConnection, updateList]);
// Update project filter default values. // Update project filter default values.
React.useEffect(() => { React.useEffect(() => {
if (!testModel) if (!testModel)