Skip to content

Commit a0bed5e

Browse files
Change the notification logic
1 parent 96d8b6b commit a0bed5e

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

lib/declarations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ interface IiOSNotification {
239239
}
240240

241241
interface IiOSNotificationService {
242-
awaitNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<string>;
243-
postNotification(deviceIdentifier: string, notification: string): Promise<void>;
242+
awaitNotification(deviceIdentifier: string, socket: number, timeout: number): Promise<string>;
243+
postNotification(deviceIdentifier: string, notification: string, commandType?: string): Promise<string>;
244+
subscribeForNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<Promise<string>>;
244245
}
245246

246247
interface IiOSSocketRequestExecutor {

lib/device-sockets/ios/socket-request-executor.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as constants from "../../common/constants";
2+
13
export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
24
constructor(private $errors: IErrors,
35
private $iOSNotification: IiOSNotification,
@@ -6,18 +8,20 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
68
private $projectData: IProjectData) { }
79

810
public async executeAttachRequest(device: Mobile.IiOSDevice, timeout: number): Promise<void> {
11+
const deviceIdentifier = device.deviceInfo.identifier;
912

10-
let data = [this.$iOSNotification.alreadyConnected, this.$iOSNotification.readyForAttach, this.$iOSNotification.attachAvailable]
11-
.map((notification) => this.$iOSNotificationService.awaitNotification(device.deviceInfo.identifier, notification, timeout)),
12-
alreadyConnected = data[0],
13-
readyForAttach = data[1],
14-
attachAvailable = data[2];
13+
const observeNotificationPromises = [
14+
await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.alreadyConnected, timeout),
15+
await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, timeout),
16+
await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.attachAvailable, timeout)
17+
];
1518

16-
await this.$iOSNotificationService.postNotification(device.deviceInfo.identifier, this.$iOSNotification.attachAvailabilityQuery);
19+
// Trigger the notifications update.
20+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachAvailabilityQuery, constants.IOS_POST_NOTIFICATION_COMMAND_TYPE);
1721

1822
let receivedNotification: string;
1923
try {
20-
receivedNotification = await Promise.race([alreadyConnected, readyForAttach, attachAvailable]);
24+
receivedNotification = await Promise.race(observeNotificationPromises);
2125
} catch (e) {
2226
this.$errors.failWithoutHelp(`The application ${this.$projectData.projectId} does not appear to be running on ${device.deviceInfo.displayName} or is not built with debugging enabled.`);
2327
}
@@ -27,7 +31,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
2731
this.$errors.failWithoutHelp("A client is already connected.");
2832
break;
2933
case this.$iOSNotification.attachAvailable:
30-
await this.executeAttachAvailable(device.deviceInfo.identifier, timeout);
34+
await this.executeAttachAvailable(deviceIdentifier, timeout);
3135
break;
3236
case this.$iOSNotification.readyForAttach:
3337
break;
@@ -36,15 +40,16 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
3640

3741
public async executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, shouldBreak?: boolean): Promise<void> {
3842
try {
39-
await this.$iOSNotificationService.awaitNotification(deviceIdentifier, this.$iOSNotification.appLaunching, timeout);
43+
const appLaunchingPromise = await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.appLaunching, timeout);
44+
await appLaunchingPromise;
4045
if (shouldBreak) {
41-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.waitForDebug);
46+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.waitForDebug, constants.IOS_POST_NOTIFICATION_COMMAND_TYPE);
4247
}
4348

4449
// We need to send the ObserveNotification ReadyForAttach before we post the AttachRequest.
45-
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, readyForAttachTimeout);
50+
const readyForAttachPromise = await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, readyForAttachTimeout);
4651

47-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest);
52+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest, constants.IOS_POST_NOTIFICATION_COMMAND_TYPE);
4853
await readyForAttachPromise;
4954
} catch (e) {
5055
this.$logger.trace("Launch request error:");
@@ -57,7 +62,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
5762
try {
5863
// We should create this promise here because we need to send the ObserveNotification on the device
5964
// before we send the PostNotification.
60-
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, timeout);
65+
const readyForAttachPromise = await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, timeout);
6166
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest);
6267
await readyForAttachPromise;
6368
} catch (e) {

lib/services/ios-debug-service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ class IOSDebugService implements IDebugService {
177177
};
178178

179179
const socket = await factoryPromise();
180-
const factory = () => socket;
180+
const factory = () => {
181+
process.nextTick(() => {
182+
socket.emit("connect");
183+
});
184+
return socket;
185+
};
181186

182187
if (this.$options.chrome) {
183188
this._socketProxy = this.$socketProxyFactory.createWebSocketProxy(factory);

lib/services/ios-notification-service.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import * as constants from "../common/constants";
33
export class IOSNotificationService implements IiOSNotificationService {
44
constructor(private $iosDeviceOperations: IIOSDeviceOperations) { }
55

6-
public async awaitNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<string> {
7-
const notificationResponse = await this.$iosDeviceOperations.notify([{
6+
public async awaitNotification(deviceIdentifier: string, socket: number, timeout: number): Promise<string> {
7+
const notificationResponse = await this.$iosDeviceOperations.awaitNotificationResponse([{
88
deviceId: deviceIdentifier,
9-
commandType: constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE,
10-
notificationName: notification,
11-
shouldWaitForResponse: true,
9+
socket: socket,
1210
timeout: timeout,
1311
responseCommandType: constants.IOS_RELAY_NOTIFICATION_COMMAND_TYPE,
1412
responsePropertyName: "Name"
@@ -17,8 +15,19 @@ export class IOSNotificationService implements IiOSNotificationService {
1715
return _.first(notificationResponse[deviceIdentifier]).response;
1816
}
1917

20-
public async postNotification(deviceIdentifier: string, notification: string): Promise<void> {
21-
await this.$iosDeviceOperations.notify([{ deviceId: deviceIdentifier, commandType: constants.IOS_POST_NOTIFICATION_COMMAND_TYPE, notificationName: notification, shouldWaitForResponse: false }]);
18+
public subscribeForNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<Promise<string>> {
19+
return new Promise((resolve, reject) => {
20+
this.postNotification(deviceIdentifier, notification, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE)
21+
.then((socket) => {
22+
resolve(this.awaitNotification(deviceIdentifier, +socket, timeout));
23+
});
24+
});
25+
}
26+
27+
public async postNotification(deviceIdentifier: string, notification: string, commandType?: string): Promise<string> {
28+
commandType = commandType || constants.IOS_POST_NOTIFICATION_COMMAND_TYPE;
29+
const response = await this.$iosDeviceOperations.postNotification([{ deviceId: deviceIdentifier, commandType: commandType, notificationName: notification }]);
30+
return _.first(response[deviceIdentifier]).response;
2231
}
2332
}
2433

0 commit comments

Comments
 (0)