change: improving adb connection
Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
parent
fbbf0c12f4
commit
d0a3b553a5
10 changed files with 320 additions and 56 deletions
|
|
@ -22,6 +22,7 @@
|
|||
import { deviceCredentialManager } from '$lib/core/adb/deviceCredManager';
|
||||
import { browser } from '$app/environment';
|
||||
import { onMount } from 'svelte';
|
||||
import AdbReconnectBanner from '$lib/components/adb-reconnect-banner.svelte';
|
||||
|
||||
let { children } = $props();
|
||||
let websocketConnectedForUid = $state('');
|
||||
|
|
@ -144,3 +145,5 @@
|
|||
<svelte:component this={TerminalDrawerComponent} />
|
||||
{/if}
|
||||
</Sidebar.Provider>
|
||||
|
||||
<AdbReconnectBanner />
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@
|
|||
import { deviceCredentialManager } from '$lib/core/adb/deviceCredManager';
|
||||
import { afterNavigate, goto } from '$app/navigation';
|
||||
import { env } from '$env/dynamic/public';
|
||||
import { adbWriter, isAdbWriterAvailable, sendToAndroid } from '$lib/core/stores/adbWriter';
|
||||
import { adbWriter, isAdbWriterAlive, wasAndroidSocketEverAlive, sendToAndroid } from '$lib/core/stores/adbWriter';
|
||||
import { adbReconnect } from '$lib/core/stores/adbReconnectStore';
|
||||
import { AdbInstance } from '../../../state.svelte';
|
||||
import {
|
||||
setOnMenuSavedCallback,
|
||||
|
|
@ -68,7 +69,7 @@
|
|||
let recipeLoading = $state(false);
|
||||
let recipeAutoLoadAttempted = $state(false);
|
||||
let isAdbConnected = $derived(Boolean(AdbInstance.instance));
|
||||
let isAndroidSocketConnected = $derived(Boolean($adbWriter));
|
||||
let isAndroidSocketConnected = $derived(isAdbWriterAlive());
|
||||
let isRecipeLoaded = $derived(Boolean(devRecipe));
|
||||
|
||||
// clear out event
|
||||
|
|
@ -208,7 +209,7 @@
|
|||
async function connectAdb() {
|
||||
try {
|
||||
if (adb.getAdbInstance()) {
|
||||
if (!isAdbWriterAvailable()) {
|
||||
if (!isAdbWriterAlive()) {
|
||||
await adb.reconnectAndroidServer();
|
||||
}
|
||||
await loadBrewDataFromConnectedAdb();
|
||||
|
|
@ -240,7 +241,7 @@
|
|||
async function tryAutoConnect() {
|
||||
try {
|
||||
if (adb.getAdbInstance()) {
|
||||
if (!isAdbWriterAvailable()) {
|
||||
if (!isAdbWriterAlive()) {
|
||||
await adb.reconnectAndroidServer();
|
||||
}
|
||||
return true;
|
||||
|
|
@ -335,7 +336,8 @@
|
|||
}
|
||||
|
||||
async function ensureAndroidSocket() {
|
||||
if (isAdbWriterAvailable()) return true;
|
||||
// Use the liveness check, not just "is object in store"
|
||||
if (isAdbWriterAlive()) return true;
|
||||
|
||||
if (!adb.getAdbInstance()) {
|
||||
addNotification('ERR:ADB is not connected');
|
||||
|
|
@ -344,7 +346,7 @@
|
|||
|
||||
await adb.reconnectAndroidServer();
|
||||
|
||||
if (!isAdbWriterAvailable()) {
|
||||
if (!isAdbWriterAlive()) {
|
||||
addNotification('ERR:Android socket is not connected');
|
||||
return false;
|
||||
}
|
||||
|
|
@ -354,7 +356,9 @@
|
|||
|
||||
afterNavigate(async () => {
|
||||
logger.info('after navigate brew');
|
||||
await startFetchRecipeFromMachine();
|
||||
// Data loading is handled by the $effect below — no need to
|
||||
// duplicate it here. This hook is kept for future navigation-
|
||||
// specific side effects only.
|
||||
});
|
||||
|
||||
function getMenuStatus(ms: number): RecipeOverview['status'] {
|
||||
|
|
@ -838,14 +842,40 @@
|
|||
|
||||
$effect(() => {
|
||||
if (!isAdbConnected) {
|
||||
recipeAutoLoadAttempted = false;
|
||||
// Don't reset recipeAutoLoadAttempted here — that causes
|
||||
// duplicate loads on back-and-forth navigation.
|
||||
return;
|
||||
}
|
||||
|
||||
if (isRecipeLoaded || recipeLoading || recipeAutoLoadAttempted) return;
|
||||
|
||||
recipeAutoLoadAttempted = true;
|
||||
void loadBrewDataFromConnectedAdb();
|
||||
|
||||
void (async () => {
|
||||
if (!isAdbWriterAlive()) {
|
||||
// Writer is null or stale. Decide what to do:
|
||||
if (wasAndroidSocketEverAlive()) {
|
||||
// Socket was alive before and died → show reconnect
|
||||
// banner. isAdbWriterAlive() already triggered it
|
||||
// if the writer was stale.
|
||||
adbReconnect.requestReconnect('Android socket disconnected');
|
||||
return;
|
||||
}
|
||||
// First visit — auto-connect the Android server.
|
||||
if (adb.getAdbInstance()) {
|
||||
try {
|
||||
await adb.reconnectAndroidServer();
|
||||
} catch {
|
||||
// Connection failed — user can retry manually.
|
||||
}
|
||||
}
|
||||
// Re-check after connect attempt
|
||||
if (!isAdbWriterAlive()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
await loadBrewDataFromConnectedAdb();
|
||||
})();
|
||||
});
|
||||
</script>
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
import { addNotification } from '$lib/core/stores/noti';
|
||||
import { referenceFromPage } from '$lib/core/stores/recipeStore';
|
||||
import { env } from '$env/dynamic/public';
|
||||
import { adbWriter, isAdbWriterAvailable } from '$lib/core/stores/adbWriter';
|
||||
import { adbWriter, isAdbWriterAlive, wasAndroidSocketEverAlive } from '$lib/core/stores/adbWriter';
|
||||
import { AdbInstance } from '../../../state.svelte';
|
||||
import {
|
||||
categoryOptions,
|
||||
|
|
@ -61,7 +61,7 @@
|
|||
|
||||
// ADB connection state
|
||||
let isAdbConnected = $derived(Boolean(AdbInstance.instance));
|
||||
let isAndroidSocketConnected = $derived(Boolean($adbWriter));
|
||||
let isAndroidSocketConnected = $derived(isAdbWriterAlive());
|
||||
let isRecipeLoaded = $derived(Boolean(devRecipe));
|
||||
|
||||
// Setup popup state
|
||||
|
|
@ -523,7 +523,7 @@
|
|||
try {
|
||||
const connected = await adb.ensureAdbConnection();
|
||||
if (connected) {
|
||||
if (!isAdbWriterAvailable()) {
|
||||
if (!isAdbWriterAlive()) {
|
||||
await adb.reconnectAndroidRecipeMenuServer();
|
||||
}
|
||||
await loadRecipeFromMachine();
|
||||
|
|
@ -546,7 +546,7 @@
|
|||
async function reconnectAndroidSocket() {
|
||||
try {
|
||||
await adb.reconnectAndroidRecipeMenuServer();
|
||||
if (isAdbWriterAvailable()) {
|
||||
if (isAdbWriterAlive()) {
|
||||
addNotification('INFO:Android socket connected');
|
||||
} else {
|
||||
addNotification('WARN:Android socket not connected');
|
||||
|
|
@ -622,11 +622,11 @@
|
|||
}
|
||||
|
||||
async function ensureAndroidSocket(): Promise<boolean> {
|
||||
if (isAdbWriterAvailable()) return true;
|
||||
if (isAdbWriterAlive()) return true;
|
||||
|
||||
try {
|
||||
await adb.reconnectAndroidRecipeMenuServer();
|
||||
if (isAdbWriterAvailable()) return true;
|
||||
if (isAdbWriterAlive()) return true;
|
||||
} catch {}
|
||||
|
||||
addNotification('WARN:Android socket not connected');
|
||||
|
|
@ -1447,7 +1447,8 @@
|
|||
// Auto-load when ADB is connected
|
||||
$effect(() => {
|
||||
if (!isAdbConnected) {
|
||||
recipeAutoLoadAttempted = false;
|
||||
// Don't reset recipeAutoLoadAttempted — prevents duplicate loads
|
||||
// on back-and-forth navigation.
|
||||
detectedCountry = '';
|
||||
return;
|
||||
}
|
||||
|
|
@ -1458,8 +1459,23 @@
|
|||
|
||||
// Auto-load recipe data and country
|
||||
(async () => {
|
||||
if (!isAdbWriterAvailable()) {
|
||||
await adb.reconnectAndroidRecipeMenuServer();
|
||||
if (!isAdbWriterAlive()) {
|
||||
if (wasAndroidSocketEverAlive()) {
|
||||
// Socket was alive before and died → reconnect banner
|
||||
// already triggered by isAdbWriterAlive() if stale.
|
||||
return;
|
||||
}
|
||||
// First visit — auto-connect the Android recipe menu server.
|
||||
if (adb.getAdbInstance()) {
|
||||
try {
|
||||
await adb.reconnectAndroidRecipeMenuServer();
|
||||
} catch {
|
||||
// Connection failed — user can retry manually.
|
||||
}
|
||||
}
|
||||
if (!isAdbWriterAlive()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
await loadCountryFromMachine();
|
||||
await loadStagedMenusFromAndroid();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue