Skip to content

Commit 0d9e605

Browse files
committed
Update disconnect detection logic
1 parent c85d40c commit 0d9e605

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

mbf-site/src/App.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { OpenLogsButton } from './components/OpenLogsButton';
1717
import { isViewingOnIos, isViewingOnMobile, isViewingOnWindows, usingOculusBrowser } from './platformDetection';
1818
import { SourceUrl } from '.';
1919
import { AdbServerWebSocketConnector, bridgeData, checkForBridge } from './AdbServerWebSocketConnector';
20+
import { waitForDisconnect } from "./waitForDisconnect";
2021

2122
type NoDeviceCause = "NoDeviceSelected" | "DeviceInUse";
2223

@@ -145,7 +146,7 @@ function ChooseDevice() {
145146
if (!areDevicesEqual(devices, adbDevices)) {
146147
setAdbDevices(devices);
147148
if (devices.length == 1) {
148-
setChosenDevice(await connectAdbDevice(bridgeClient, devices[0]));
149+
await connectDevice(await connectAdbDevice(bridgeClient, devices[0]));
149150
}
150151
}
151152
} catch (err) {
@@ -169,23 +170,7 @@ function ChooseDevice() {
169170
setAuthing(false);
170171
setChosenDevice(device);
171172

172-
// Track if the transport disconnects early
173-
let disconnectedEarly = true;
174-
setTimeout(() => disconnectedEarly = false, 1000);
175-
176-
// Wait for the transport to determine disconnect
177-
await device.transport.disconnected;
178-
179-
// Old adb server versions don't support the wait-for-any-disconnect feautre
180-
// so if the transport disconnects within 1 second, we spawn a process that
181-
// never exits and await it instead.
182-
if (disconnectedEarly) {
183-
try {
184-
await device.subprocess.spawnAndWait("read");
185-
} catch (error) {
186-
Log.error("ADB server process exited: " + error, error);
187-
}
188-
}
173+
await waitForDisconnect(device);
189174

190175
setChosenDevice(null);
191176
}

mbf-site/src/components/ModManager.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import useFileDropper from "../hooks/useFileDropper";
1515
import { Log } from "../Logging";
1616
import { useSetWorking, useSyncStore, wrapOperation } from "../SyncStore";
1717
import { ModRepoMod } from "../ModsRepo";
18+
import { waitForDisconnect } from "../waitForDisconnect";
1819

1920
interface ModManagerProps {
2021
gameVersion: string,
@@ -279,7 +280,7 @@ function AddModsMenu(props: ModMenuProps) {
279280
isProcessingQueue = true;
280281

281282
let disconnected = false;
282-
device.disconnected.then(() => disconnected = true);
283+
waitForDisconnect(device).then(() => disconnected = true);
283284
const setWorking = useSetWorking("Importing");
284285
const { setStatusText } = useSyncStore.getState();
285286

mbf-site/src/waitForDisconnect.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { Adb } from "@yume-chan/adb";
2+
import { PromiseResolver } from "@yume-chan/async";
3+
import { Log } from "./Logging";
4+
5+
const disconnectPromises = new Map<string, Promise<void>>();
6+
export async function waitForDisconnect(device: Adb) {
7+
if (disconnectPromises.has(device.serial)) {
8+
Log.debug(`Already waiting for ${device.serial} to disconnect`, device);
9+
return await disconnectPromises.get(device.serial);
10+
}
11+
12+
Log.debug(`Waiting for ${device.serial} to disconnect`, device);
13+
var resolver = new PromiseResolver<void>();
14+
15+
disconnectPromises.set(device.serial, resolver.promise);
16+
17+
// Track if the transport disconnects early
18+
let disconnectedEarly = true;
19+
setTimeout(() => disconnectedEarly = false, 1000);
20+
21+
// Wait for the transport to determine disconnect
22+
await device.transport.disconnected;
23+
24+
// Old adb server versions don't support the wait-for-any-disconnect feautre
25+
// so if the transport disconnects within 1 second, we spawn a process that
26+
// never exits and await it instead.
27+
if (disconnectedEarly) {
28+
try {
29+
Log.debug(`Waiting for ${device.serial} to disconnect using subprocess`, device);
30+
await device.subprocess.spawnAndWait("read");
31+
} catch (error) {
32+
console.error("ADB server process exited: " + error, error);
33+
}
34+
}
35+
36+
Log.debug(`Devoce ${device.serial} disconnected`, device);
37+
resolver.resolve();
38+
disconnectPromises.delete(device.serial);
39+
}

0 commit comments

Comments
 (0)