Skip to content

Commit b1463d5

Browse files
committed
Batch calls to find ADB devices, to avoid races in setup
1 parent a7bb2cd commit b1463d5

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/interceptors/android/adb-commands.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,31 @@ export function createAdbClient() {
1616
});
1717
}
1818

19-
export async function getConnectedDevices(adbClient: adb.AdbClient) {
19+
// Batch async calls, so that all calls whilst one call is ongoing return the same result.
20+
// Always uses the arguments from the first call, so this isn't safe for some cases!
21+
const batchCalls = <A extends any[], R>(
22+
fn: (...args: A) => Promise<R>
23+
) => {
24+
let ongoingCall: Promise<R> | undefined = undefined;
25+
26+
return (...args: A) => {
27+
if (!ongoingCall) {
28+
ongoingCall = fn(...args)
29+
.then((result) => {
30+
ongoingCall = undefined;
31+
return result;
32+
})
33+
.catch((error) => {
34+
ongoingCall = undefined;
35+
throw error;
36+
});
37+
}
38+
39+
return ongoingCall;
40+
};
41+
}
42+
43+
export const getConnectedDevices = batchCalls(async (adbClient: adb.AdbClient) => {
2044
try {
2145
const devices = await adbClient.listDevices();
2246
return devices
@@ -34,7 +58,7 @@ export async function getConnectedDevices(adbClient: adb.AdbClient) {
3458
throw e;
3559
}
3660
}
37-
}
61+
});
3862

3963
export function stringAsStream(input: string) {
4064
const contentStream = new stream.Readable();

0 commit comments

Comments
 (0)