1
+ import * as constants from "../../common/constants" ;
2
+
1
3
export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
2
4
constructor ( private $errors : IErrors ,
3
5
private $iOSNotification : IiOSNotification ,
4
6
private $iOSNotificationService : IiOSNotificationService ,
5
7
private $logger : ILogger ) { }
6
8
7
9
public async executeAttachRequest ( device : Mobile . IiOSDevice , timeout : number , projectId : string ) : Promise < void > {
10
+ const deviceIdentifier = device . deviceInfo . identifier ;
8
11
9
- let data = [ this . $iOSNotification . getAlreadyConnected ( projectId ) , this . $iOSNotification . getReadyForAttach ( projectId ) , this . $iOSNotification . getAttachAvailable ( projectId ) ]
10
- . map ( ( notification ) => this . $iOSNotificationService . awaitNotification ( device . deviceInfo . identifier , notification , timeout ) ) ,
11
- alreadyConnected = data [ 0 ] ,
12
- readyForAttach = data [ 1 ] ,
13
- attachAvailable = data [ 2 ] ;
12
+ const observeNotificationPromises = [
13
+ await this . $iOSNotificationService . subscribeForNotification ( deviceIdentifier , this . $iOSNotification . alreadyConnected , timeout ) ,
14
+ await this . $iOSNotificationService . subscribeForNotification ( deviceIdentifier , this . $iOSNotification . readyForAttach , timeout ) ,
15
+ await this . $iOSNotificationService . subscribeForNotification ( deviceIdentifier , this . $iOSNotification . attachAvailable , timeout )
16
+ ] ;
14
17
15
- await this . $iOSNotificationService . postNotification ( device . deviceInfo . identifier , this . $iOSNotification . getAttachAvailabilityQuery ( projectId ) ) ;
18
+ // Trigger the notifications update.
19
+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . attachAvailabilityQuery , constants . IOS_POST_NOTIFICATION_COMMAND_TYPE ) ;
16
20
17
21
let receivedNotification : string ;
18
22
try {
19
- receivedNotification = await Promise . race ( [ alreadyConnected , readyForAttach , attachAvailable ] ) ;
23
+ receivedNotification = await Promise . race ( observeNotificationPromises ) ;
20
24
} catch ( e ) {
21
25
this . $errors . failWithoutHelp ( `The application ${ projectId } does not appear to be running on ${ device . deviceInfo . displayName } or is not built with debugging enabled.` ) ;
22
26
}
@@ -26,24 +30,29 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
26
30
this . $errors . failWithoutHelp ( "A client is already connected." ) ;
27
31
break ;
28
32
case this . $iOSNotification . getAttachAvailable ( projectId ) :
29
- await this . executeAttachAvailable ( device . deviceInfo . identifier , timeout ) ;
33
+ await this . executeAttachAvailable ( deviceIdentifier , timeout ) ;
30
34
break ;
31
35
case this . $iOSNotification . getReadyForAttach ( projectId ) :
32
36
break ;
37
+ default :
38
+ this . $logger . trace ( "Response from attach availability query:" ) ;
39
+ this . $logger . trace ( receivedNotification ) ;
40
+ this . $errors . failWithoutHelp ( "No notification received while executing attach request." ) ;
33
41
}
34
42
}
35
43
36
44
public async executeLaunchRequest ( deviceIdentifier : string , timeout : number , readyForAttachTimeout : number , shouldBreak ?: boolean ) : Promise < void > {
37
45
try {
38
- await this . $iOSNotificationService . awaitNotification ( deviceIdentifier , this . $iOSNotification . appLaunching , timeout ) ;
46
+ const appLaunchingPromise = await this . $iOSNotificationService . subscribeForNotification ( deviceIdentifier , this . $iOSNotification . appLaunching , timeout ) ;
47
+ await appLaunchingPromise ;
39
48
if ( shouldBreak ) {
40
- await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . waitForDebug ) ;
49
+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . waitForDebug , constants . IOS_POST_NOTIFICATION_COMMAND_TYPE ) ;
41
50
}
42
51
43
52
// We need to send the ObserveNotification ReadyForAttach before we post the AttachRequest.
44
- const readyForAttachPromise = this . $iOSNotificationService . awaitNotification ( deviceIdentifier , this . $iOSNotification . readyForAttach , readyForAttachTimeout ) ;
53
+ const readyForAttachPromise = await this . $iOSNotificationService . subscribeForNotification ( deviceIdentifier , this . $iOSNotification . readyForAttach , readyForAttachTimeout ) ;
45
54
46
- await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . attachRequest ) ;
55
+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . attachRequest , constants . IOS_POST_NOTIFICATION_COMMAND_TYPE ) ;
47
56
await readyForAttachPromise ;
48
57
} catch ( e ) {
49
58
this . $logger . trace ( "Launch request error:" ) ;
@@ -56,7 +65,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
56
65
try {
57
66
// We should create this promise here because we need to send the ObserveNotification on the device
58
67
// before we send the PostNotification.
59
- const readyForAttachPromise = this . $iOSNotificationService . awaitNotification ( deviceIdentifier , this . $iOSNotification . readyForAttach , timeout ) ;
68
+ const readyForAttachPromise = await this . $iOSNotificationService . subscribeForNotification ( deviceIdentifier , this . $iOSNotification . readyForAttach , timeout ) ;
60
69
await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . attachRequest ) ;
61
70
await readyForAttachPromise ;
62
71
} catch ( e ) {
0 commit comments