File tree Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,31 @@ export function createAdbClient() {
16
16
} ) ;
17
17
}
18
18
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 ) => {
20
44
try {
21
45
const devices = await adbClient . listDevices ( ) ;
22
46
return devices
@@ -34,7 +58,7 @@ export async function getConnectedDevices(adbClient: adb.AdbClient) {
34
58
throw e ;
35
59
}
36
60
}
37
- }
61
+ } ) ;
38
62
39
63
export function stringAsStream ( input : string ) {
40
64
const contentStream = new stream . Readable ( ) ;
You can’t perform that action at this time.
0 commit comments