feat(backtests): separate saving from retry-safe run submission
This commit is contained in:
parent
5eb0985057
commit
9b9403e36b
18 changed files with 1200 additions and 226 deletions
|
|
@ -24,9 +24,21 @@ export function useBacktestV2() {
|
|||
const [isRunLoading, setIsRunLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [runError, setRunError] = useState<string | null>(null);
|
||||
const [submissionError, setSubmissionError] = useState<string | null>(null);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [cancelRequested, setCancelRequested] = useState(false);
|
||||
const mounted = useRef(true);
|
||||
const backtestGeneration = useRef(0);
|
||||
const loadedBacktestId = useRef<string | null>(null);
|
||||
const selectionGeneration = useRef(0);
|
||||
// The intent survives an ambiguous response and configuration edits. Only an
|
||||
// acknowledged submission (or leaving this definition) releases its key.
|
||||
const submission = useRef<{
|
||||
backtestId: string;
|
||||
key: string;
|
||||
body: { speedMultiplier: number | null };
|
||||
pending: boolean;
|
||||
} | null>(null);
|
||||
const selection = useRef<RunSelection | null>(null);
|
||||
const cancellation = useRef<RunSelection | null>(null);
|
||||
|
||||
|
|
@ -36,6 +48,7 @@ export function useBacktestV2() {
|
|||
);
|
||||
|
||||
const stopMonitoring = useCallback(() => {
|
||||
selectionGeneration.current++;
|
||||
const selected = selection.current;
|
||||
selection.current = null;
|
||||
cancellation.current = null;
|
||||
|
|
@ -165,6 +178,12 @@ export function useBacktestV2() {
|
|||
|
||||
const loadBacktest = useCallback(async (id: string) => {
|
||||
const generation = ++backtestGeneration.current;
|
||||
if (loadedBacktestId.current !== id) {
|
||||
submission.current = null;
|
||||
setSubmissionError(null);
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
loadedBacktestId.current = id;
|
||||
setIsBacktestLoading(true);
|
||||
setError(null);
|
||||
setBacktest(null);
|
||||
|
|
@ -205,6 +224,10 @@ export function useBacktestV2() {
|
|||
try {
|
||||
const created = await backtestApiV2.createBacktest(request);
|
||||
if (mounted.current && generation === backtestGeneration.current) {
|
||||
loadedBacktestId.current = created.id;
|
||||
submission.current = null;
|
||||
setSubmissionError(null);
|
||||
setIsSubmitting(false);
|
||||
setBacktest(created);
|
||||
setRuns([]);
|
||||
await selectRun(undefined);
|
||||
|
|
@ -256,6 +279,10 @@ export function useBacktestV2() {
|
|||
try {
|
||||
await backtestApiV2.deleteBacktest(id);
|
||||
if (mounted.current && generation === backtestGeneration.current) {
|
||||
loadedBacktestId.current = null;
|
||||
submission.current = null;
|
||||
setSubmissionError(null);
|
||||
setIsSubmitting(false);
|
||||
setBacktest(null);
|
||||
setRuns([]);
|
||||
await selectRun(undefined);
|
||||
|
|
@ -276,29 +303,44 @@ export function useBacktestV2() {
|
|||
|
||||
const createRun = useCallback(
|
||||
async (speedMultiplier: number | null = null) => {
|
||||
if (!backtest) {
|
||||
if (!backtest || backtest.id !== loadedBacktestId.current || submission.current?.pending) {
|
||||
return undefined;
|
||||
}
|
||||
const generation = backtestGeneration.current;
|
||||
const selected = selection.current;
|
||||
setIsBacktestLoading(true);
|
||||
setError(null);
|
||||
const intent = (submission.current ??= {
|
||||
backtestId: backtest.id,
|
||||
key: crypto.randomUUID(),
|
||||
body: { speedMultiplier },
|
||||
pending: false,
|
||||
});
|
||||
intent.pending = true;
|
||||
const selectedGeneration = selectionGeneration.current;
|
||||
const ownsSubmission = () => mounted.current && submission.current === intent;
|
||||
const ownsSelection = () =>
|
||||
ownsSubmission() && selectionGeneration.current === selectedGeneration;
|
||||
setIsSubmitting(true);
|
||||
setSubmissionError(null);
|
||||
try {
|
||||
const created = await backtestApiV2.createRun(backtest.id, { speedMultiplier });
|
||||
if (!mounted.current || generation !== backtestGeneration.current) {
|
||||
const created = await backtestApiV2.createRun(intent.backtestId, intent.body, intent.key);
|
||||
if (!ownsSubmission()) {
|
||||
return undefined;
|
||||
}
|
||||
setRuns(previous => [created, ...previous]);
|
||||
// The route owns selection. Never navigate away from a newer user selection.
|
||||
return selection.current === selected ? created : undefined;
|
||||
const selected = ownsSelection();
|
||||
submission.current = null;
|
||||
setIsSubmitting(false);
|
||||
if (!selected) {
|
||||
return undefined;
|
||||
}
|
||||
setRuns(previous => [created, ...previous.filter(run => run.id !== created.id)]);
|
||||
return created;
|
||||
} catch (err) {
|
||||
if (mounted.current && generation === backtestGeneration.current) {
|
||||
setError(messageOf(err, 'Failed to create run'));
|
||||
if (ownsSubmission()) {
|
||||
setSubmissionError(messageOf(err, 'Failed to submit run'));
|
||||
}
|
||||
return undefined;
|
||||
} finally {
|
||||
if (mounted.current && generation === backtestGeneration.current) {
|
||||
setIsBacktestLoading(false);
|
||||
intent.pending = false;
|
||||
if (ownsSubmission()) {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
@ -349,6 +391,9 @@ export function useBacktestV2() {
|
|||
runResults,
|
||||
isLoading: isBacktestLoading || isRunLoading,
|
||||
error: runError ?? error,
|
||||
submissionError,
|
||||
isSubmitting,
|
||||
hasPendingSubmission: submission.current !== null,
|
||||
cancelRequested,
|
||||
loadBacktest,
|
||||
createBacktest,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue