fix: in-flight guards on loadData/loadConfig, remove duplicate loadConfig
Concurrent calls from rapid reloads or rapid saves could pile up, each writing state on completion and potentially re-triggering more loads. Guards ensure only one load is in flight at a time; extra calls are dropped.
This commit is contained in:
@@ -40,15 +40,6 @@
|
|||||||
let configError = $state<string | null>(null);
|
let configError = $state<string | null>(null);
|
||||||
let configSuccess = $state<string | null>(null);
|
let configSuccess = $state<string | null>(null);
|
||||||
|
|
||||||
async function loadConfig() {
|
|
||||||
const res = await fetch(`${api}/config`);
|
|
||||||
if (!res.ok) return;
|
|
||||||
const c = await res.json();
|
|
||||||
configYoloLabel = c.yoloLabel ?? 'YOLO mode';
|
|
||||||
configYoloDescription = c.yoloDescription ?? '(cross-group conversions)';
|
|
||||||
configYoloVisibility = c.yoloVisibility ?? 'auto';
|
|
||||||
}
|
|
||||||
|
|
||||||
async function saveConfig() {
|
async function saveConfig() {
|
||||||
configSaving = true;
|
configSaving = true;
|
||||||
configError = null;
|
configError = null;
|
||||||
@@ -227,16 +218,39 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── Load data ────────────────────────────────────────────────────────────────
|
// ── Load data ────────────────────────────────────────────────────────────────
|
||||||
|
let loadDataInFlight = false;
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
const [res, gres] = await Promise.all([
|
if (loadDataInFlight) return;
|
||||||
fetch(`${api}/units`),
|
loadDataInFlight = true;
|
||||||
fetch(`${api}/groups`)
|
try {
|
||||||
]);
|
const [res, gres] = await Promise.all([
|
||||||
if (!res.ok) return;
|
fetch(`${api}/units`),
|
||||||
const units: Unit[] = await res.json();
|
fetch(`${api}/groups`)
|
||||||
const groups: Group[] = gres.ok ? await gres.json() : [];
|
]);
|
||||||
unitsData = { units, groups };
|
if (!res.ok) return;
|
||||||
expandedGroups = new Set([...expandedGroups, ...groups.map((g) => g.id)]);
|
const units: Unit[] = await res.json();
|
||||||
|
const groups: Group[] = gres.ok ? await gres.json() : [];
|
||||||
|
unitsData = { units, groups };
|
||||||
|
expandedGroups = new Set([...expandedGroups, ...groups.map((g) => g.id)]);
|
||||||
|
} finally {
|
||||||
|
loadDataInFlight = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let loadConfigInFlight = false;
|
||||||
|
async function loadConfig() {
|
||||||
|
if (loadConfigInFlight) return;
|
||||||
|
loadConfigInFlight = true;
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${api}/config`);
|
||||||
|
if (!res.ok) return;
|
||||||
|
const c = await res.json();
|
||||||
|
configYoloLabel = c.yoloLabel ?? 'YOLO mode';
|
||||||
|
configYoloDescription = c.yoloDescription ?? '(cross-group conversions)';
|
||||||
|
configYoloVisibility = c.yoloVisibility ?? 'auto';
|
||||||
|
} finally {
|
||||||
|
loadConfigInFlight = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => { loadData(); loadConfig(); });
|
onMount(() => { loadData(); loadConfig(); });
|
||||||
|
|||||||
Reference in New Issue
Block a user