change: improving adb connection

Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
pakintada@gmail.com 2026-07-02 16:54:25 +07:00
parent fbbf0c12f4
commit d0a3b553a5
10 changed files with 320 additions and 56 deletions

View file

@ -14,6 +14,7 @@ import { addNotification } from '../stores/noti';
import { handleAdbPayload } from '../handlers/adbPayloadHandler';
import { GlobalEventBus } from '../utils/eventBus';
import { adbWriter } from '../stores/adbWriter';
import { adbReconnect } from '../stores/adbReconnectStore';
import { WritableStream } from '@yume-chan/stream-extra';
import { env } from '$env/dynamic/public';
import { get } from 'svelte/store';
@ -340,6 +341,7 @@ export async function connectRecipeMenuDeviceByCred(
}
export async function reconnectAndroidRecipeMenuServer() {
adbWriter.set(null);
await connectToAndroidRecipeMenuServer(true);
}
@ -665,6 +667,9 @@ export async function pushBinary(
// NOTE: adb reverse is not work by unavailable features support
export async function reconnectAndroidServer() {
// Clear any stale writer before reconnecting so the new stream
// doesn't conflict with a dead one still referenced in the store.
adbWriter.set(null);
await connectToAndroidServer();
}
@ -707,13 +712,23 @@ async function connectToAndroidServer(maxRetries = 5) {
addNotification('INFO:Enable Brewing Mode T on machine');
(async () => {
while (true) {
const { value, done } = await reader.read();
if (done) break;
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
// Transfer ONLY the buffer, not the stream
// Type assertion (as any) fixes the overload error
(worker as any).postMessage({ type: 'CHUNK', payload: value }, [value.buffer]);
// Transfer ONLY the buffer, not the stream
// Type assertion (as any) fixes the overload error
(worker as any).postMessage({ type: 'CHUNK', payload: value }, [value.buffer]);
}
} catch (e) {
logger.error('Android server read error', e);
} finally {
adbWriter.set(null);
addNotification('WARN:Android server channel offline ...');
reader.cancel().catch(() => {});
// Prompt the user to reconnect instead of auto-reconnecting
adbReconnect.requestReconnect('Android server disconnected');
}
})();
return;
@ -827,18 +842,16 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
adbWriter.set(null);
addNotification('WARN:Android recipe menu channel offline ...');
reader.cancel().catch(() => {});
if (retryOnFailure) {
scheduleRecipeMenuAndroidServerReconnect();
}
// Prompt the user to reconnect instead of auto-scheduling
adbReconnect.requestReconnect('Android recipe menu disconnected');
}
})();
} catch (err) {
logger.error('Recipe menu connection failed. Suspect java running or not', err);
adbWriter.set(null);
if (notifyFailure) addNotification('ERR:Fail to enable Android recipe menu channel');
if (retryOnFailure) {
scheduleRecipeMenuAndroidServerReconnect();
}
// Prompt the user to reconnect instead of auto-scheduling
adbReconnect.requestReconnect('Android recipe menu connection failed');
}
}