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() {
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
3963export function stringAsStream ( input : string ) {
4064 const contentStream = new stream . Readable ( ) ;
You can’t perform that action at this time.
0 commit comments