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 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() {
|
||||
configSaving = true;
|
||||
configError = null;
|
||||
@@ -227,16 +218,39 @@
|
||||
}
|
||||
|
||||
// ── Load data ────────────────────────────────────────────────────────────────
|
||||
let loadDataInFlight = false;
|
||||
async function loadData() {
|
||||
const [res, gres] = await Promise.all([
|
||||
fetch(`${api}/units`),
|
||||
fetch(`${api}/groups`)
|
||||
]);
|
||||
if (!res.ok) return;
|
||||
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)]);
|
||||
if (loadDataInFlight) return;
|
||||
loadDataInFlight = true;
|
||||
try {
|
||||
const [res, gres] = await Promise.all([
|
||||
fetch(`${api}/units`),
|
||||
fetch(`${api}/groups`)
|
||||
]);
|
||||
if (!res.ok) return;
|
||||
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(); });
|
||||
|
||||
Reference in New Issue
Block a user