Improve mode stop responsiveness and timeout handling

This commit is contained in:
Smittix
2026-02-23 17:53:50 +00:00
parent 7241dbed35
commit c31ed14041
5 changed files with 371 additions and 238 deletions

View File

@@ -944,21 +944,36 @@ const BluetoothMode = (function() {
}
}
async function stopScan() {
const isAgentMode = typeof currentAgent !== 'undefined' && currentAgent !== 'local';
try {
if (isAgentMode) {
await fetch(`/controller/agents/${currentAgent}/bluetooth/stop`, { method: 'POST' });
} else {
await fetch('/api/bluetooth/scan/stop', { method: 'POST' });
}
setScanning(false);
stopEventStream();
} catch (err) {
console.error('Failed to stop scan:', err);
}
}
async function stopScan() {
const isAgentMode = typeof currentAgent !== 'undefined' && currentAgent !== 'local';
const timeoutMs = isAgentMode ? 8000 : 2200;
const controller = (typeof AbortController !== 'undefined') ? new AbortController() : null;
const timeoutId = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
// Optimistic UI teardown keeps mode changes responsive.
setScanning(false);
stopEventStream();
try {
if (isAgentMode) {
await fetch(`/controller/agents/${currentAgent}/bluetooth/stop`, {
method: 'POST',
...(controller ? { signal: controller.signal } : {}),
});
} else {
await fetch('/api/bluetooth/scan/stop', {
method: 'POST',
...(controller ? { signal: controller.signal } : {}),
});
}
} catch (err) {
console.error('Failed to stop scan:', err);
} finally {
if (timeoutId) {
clearTimeout(timeoutId);
}
}
}
function setScanning(scanning) {
isScanning = scanning;