fix: slow get recipe memo

- fix: cannot adb command from blocking suspicious cmd
- change: logging to logtape lib
- change: adb payload handler format to send payload size as header for exact match payload

Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
pakintada@gmail.com 2026-07-01 17:08:46 +07:00
parent aa6414b1cc
commit 3cda8aa60b
12 changed files with 494 additions and 174 deletions

View file

@ -18,10 +18,14 @@ import { WritableStream } from '@yume-chan/stream-extra';
import { env } from '$env/dynamic/public';
import { get } from 'svelte/store';
import { adbConnectionStatus } from '../stores/adbConnectionStore';
import { browser } from '$app/environment';
import AdbWorker from '$lib/workers/androidPayload.worker?worker';
let _connectionPromise: Promise<boolean> | null = null;
let syncConnection: any = null;
let worker: Worker | undefined;
/**
* Centralized connection function that all pages and components should use.
*
@ -392,8 +396,8 @@ function sanitizeAdbCommand(command: string): string {
return command;
}
export async function executeCmd(command: string) {
command = sanitizeAdbCommand(command);
export async function executeCmd(command: string, { bypass = false }) {
command = !bypass ? sanitizeAdbCommand(command) : command;
let instance = getAdbInstance();
if (!instance) {
@ -444,7 +448,7 @@ export async function executeCmd(command: string) {
export async function goToMachineHome() {
if (!getAdbInstance()) return;
try {
await executeCmd('input keyevent KEYCODE_HOME');
await executeCmd('input keyevent KEYCODE_HOME', { bypass: true });
} catch (e) {
console.error('[goToMachineHome] error', e);
}
@ -687,52 +691,80 @@ async function connectToAndroidServer(maxRetries = 5) {
const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();
if (browser) {
worker = new AdbWorker();
worker.onmessage = (e) => {
if (e.data.type === 'DATA') {
const raw = e.data.payload;
handleAdbPayload(new TextDecoder().decode(raw));
}
};
// logger.info("prep post ");
// worker.postMessage({ stream }, [stream]);
}
// const reader = stream.readable.getReader();
// logger.info('checking on writer ', writer);
adbWriter.set(writer);
if (writer) {
addNotification('INFO:Enable Brewing Mode T on machine');
const textDecoder = new TextDecoder();
let buffer = '';
(async () => {
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
while (true) {
const { value, done } = await reader.read();
if (done) break;
// decode chunk
buffer += textDecoder.decode(value, { stream: true });
// 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]);
}
})();
let lines = buffer.split('\n');
// const textDecoder = new TextDecoder();
// let buffer = '';
// save potential incomplete
buffer = lines.pop() ?? '';
// (async () => {
// try {
// while (true) {
// const { value, done } = await reader.read();
// if (done) break;
for (const line of lines) {
if (line.trim() === '') continue;
// // decode chunk
// buffer += textDecoder.decode(value, { stream: true });
GlobalEventBus.emit('adb:raw-payload', line);
handleAdbPayload(line);
}
// let lines = buffer.split('\n');
// GlobalEventBus.emit('adb:raw-payload', new TextDecoder().decode(value));
// handleAdbPayload(new TextDecoder().decode(value));
}
} catch (e) {
logger.error('read error', e);
if (isRecoverableError(e)) {
void connectToAndroidServer();
}
} finally {
adbWriter.set(null);
addNotification('WARN:Brewing Mode T Offline ...');
reader.cancel().catch(() => {});
}
})();
return;
} else {
addNotification('WARN:Brewing Mode T unavailable');
// // save potential incomplete
// buffer = lines.pop() ?? '';
// for (const line of lines) {
// if (line.trim() === '') continue;
// GlobalEventBus.emit('adb:raw-payload', line);
// handleAdbPayload(line);
// }
// // GlobalEventBus.emit('adb:raw-payload', new TextDecoder().decode(value));
// // handleAdbPayload(new TextDecoder().decode(value));
// }
// } catch (e) {
// logger.error('read error', e);
// if (isRecoverableError(e)) {
// void connectToAndroidServer();
// }
// } finally {
// adbWriter.set(null);
// addNotification('WARN:Brewing Mode T Offline ...');
// reader.cancel().catch(() => {});
// }
// })();
return;
} else {
addNotification('WARN:Brewing Mode T unavailable');
if (attempt < maxRetries - 1) {
const delay = Math.min(500 * Math.pow(2, attempt) + Math.random() * 500, 5000);