1- import * as iOSProxyServices from "../../common/mobile/ios/device/ios-proxy-services " ;
1+ import * as constants from "../../common/constants " ;
22
33export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
44 constructor ( private $errors : IErrors ,
5- private $injector : IInjector ,
65 private $iOSNotification : IiOSNotification ,
76 private $iOSNotificationService : IiOSNotificationService ,
8- private $logger : ILogger ) { }
7+ private $logger : ILogger ,
8+ private $iosDeviceOperations : IIOSDeviceOperations ) {
9+ this . $iosDeviceOperations . setShouldDispose ( false ) ;
10+ }
911
1012 public async executeAttachRequest ( device : Mobile . IiOSDevice , timeout : number , projectId : string ) : Promise < void > {
11- let npc = new iOSProxyServices . NotificationProxyClient ( device , this . $injector ) ;
13+ const deviceIdentifier = device . deviceInfo . identifier ;
14+
15+ const observeNotificationSockets = [
16+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getAlreadyConnected ( projectId ) , constants . IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE ) ,
17+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getReadyForAttach ( projectId ) , constants . IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE ) ,
18+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getAttachAvailable ( projectId ) , constants . IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE )
19+ ] ;
1220
13- let data = [ this . $iOSNotification . getAlreadyConnected ( projectId ) , this . $iOSNotification . getReadyForAttach ( projectId ) , this . $iOSNotification . getAttachAvailable ( projectId ) ]
14- . map ( ( notification ) => this . $iOSNotificationService . awaitNotification ( npc , notification , timeout ) ) ,
15- alreadyConnected = data [ 0 ] ,
16- readyForAttach = data [ 1 ] ,
17- attachAvailable = data [ 2 ] ;
21+ const observeNotificationPromises = _ ( observeNotificationSockets )
22+ . uniq ( )
23+ . map ( s => {
24+ return this . $iOSNotificationService . awaitNotification ( deviceIdentifier , + s , timeout ) ;
25+ } )
26+ . value ( ) ;
1827
19- npc . postNotificationAndAttachForData ( this . $iOSNotification . getAttachAvailabilityQuery ( projectId ) ) ;
28+ // Trigger the notifications update.
29+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getAttachAvailabilityQuery ( projectId ) ) ;
2030
2131 let receivedNotification : string ;
2232 try {
23- receivedNotification = await Promise . race ( [ alreadyConnected , readyForAttach , attachAvailable ] ) ;
33+ receivedNotification = await Promise . race ( observeNotificationPromises ) ;
2434 } catch ( e ) {
2535 this . $errors . failWithoutHelp ( `The application ${ projectId } does not appear to be running on ${ device . deviceInfo . displayName } or is not built with debugging enabled.` ) ;
2636 }
@@ -30,37 +40,47 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
3040 this . $errors . failWithoutHelp ( "A client is already connected." ) ;
3141 break ;
3242 case this . $iOSNotification . getAttachAvailable ( projectId ) :
33- await this . executeAttachAvailable ( npc , timeout , projectId ) ;
43+ await this . executeAttachAvailable ( deviceIdentifier , projectId , timeout ) ;
3444 break ;
3545 case this . $iOSNotification . getReadyForAttach ( projectId ) :
3646 break ;
47+ default :
48+ this . $logger . trace ( "Response from attach availability query:" ) ;
49+ this . $logger . trace ( receivedNotification ) ;
50+ this . $errors . failWithoutHelp ( "No notification received while executing attach request." ) ;
3751 }
3852 }
3953
40- public async executeLaunchRequest ( device : Mobile . IiOSDevice , timeout : number , readyForAttachTimeout : number , projectId : string , shouldBreak ?: boolean ) : Promise < void > {
41- let npc = new iOSProxyServices . NotificationProxyClient ( device , this . $injector ) ;
42-
54+ public async executeLaunchRequest ( deviceIdentifier : string , timeout : number , readyForAttachTimeout : number , projectId : string , shouldBreak ?: boolean ) : Promise < void > {
4355 try {
44- await this . $iOSNotificationService . awaitNotification ( npc , this . $iOSNotification . getAppLaunching ( projectId ) , timeout ) ;
45- process . nextTick ( ( ) => {
46- if ( shouldBreak ) {
47- npc . postNotificationAndAttachForData ( this . $iOSNotification . getWaitForDebug ( projectId ) ) ;
48- }
56+ const appLaunchingSocket = await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getAppLaunching ( projectId ) , constants . IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE ) ;
57+ await this . $iOSNotificationService . awaitNotification ( deviceIdentifier , + appLaunchingSocket , timeout ) ;
58+
59+ if ( shouldBreak ) {
60+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getWaitForDebug ( projectId ) ) ;
61+ }
4962
50- npc . postNotificationAndAttachForData ( this . $iOSNotification . getAttachRequest ( projectId ) ) ;
51- } ) ;
63+ // We need to send the ObserveNotification ReadyForAttach before we post the AttachRequest.
64+ const readyForAttachSocket = await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getReadyForAttach ( projectId ) , constants . IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE ) ;
65+ const readyForAttachPromise = this . $iOSNotificationService . awaitNotification ( deviceIdentifier , + readyForAttachSocket , readyForAttachTimeout ) ;
5266
53- await this . $iOSNotificationService . awaitNotification ( npc , this . $iOSNotification . getReadyForAttach ( projectId ) , readyForAttachTimeout ) ;
67+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getAttachRequest ( projectId ) ) ;
68+ await readyForAttachPromise ;
5469 } catch ( e ) {
55- this . $logger . trace ( `Timeout error: ${ e } ` ) ;
56- this . $errors . failWithoutHelp ( "Timeout waiting for response from NativeScript runtime." ) ;
70+ this . $logger . trace ( "Launch request error:" ) ;
71+ this . $logger . trace ( e ) ;
72+ this . $errors . failWithoutHelp ( "Error while waiting for response from NativeScript runtime." ) ;
5773 }
5874 }
5975
60- private async executeAttachAvailable ( npc : Mobile . INotificationProxyClient , timeout : number , projectId : string ) : Promise < void > {
61- process . nextTick ( ( ) => npc . postNotificationAndAttachForData ( this . $iOSNotification . getAttachRequest ( projectId ) ) ) ;
76+ private async executeAttachAvailable ( deviceIdentifier : string , projectId : string , timeout : number ) : Promise < void > {
6277 try {
63- await this . $iOSNotificationService . awaitNotification ( npc , this . $iOSNotification . getReadyForAttach ( projectId ) , timeout ) ;
78+ // We should create this promise here because we need to send the ObserveNotification on the device
79+ // before we send the PostNotification.
80+ const readyForAttachSocket = await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getReadyForAttach ( projectId ) , constants . IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE ) ;
81+ const readyForAttachPromise = this . $iOSNotificationService . awaitNotification ( deviceIdentifier , + readyForAttachSocket , timeout ) ;
82+ await this . $iOSNotificationService . postNotification ( deviceIdentifier , this . $iOSNotification . getAttachRequest ( projectId ) ) ;
83+ await readyForAttachPromise ;
6484 } catch ( e ) {
6585 this . $errors . failWithoutHelp ( `The application ${ projectId } timed out when performing the socket handshake.` ) ;
6686 }
0 commit comments