Skip to content

Commit 7e1f1bd

Browse files
TsvetanMilanovDimitar Kerezov
authored and
Dimitar Kerezov
committed
Change the notification logic
1 parent d1c6191 commit 7e1f1bd

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

lib/declarations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,9 @@ interface IiOSNotification {
304304
}
305305

306306
interface IiOSNotificationService {
307-
awaitNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<string>;
308-
postNotification(deviceIdentifier: string, notification: string): Promise<void>;
307+
awaitNotification(deviceIdentifier: string, socket: number, timeout: number): Promise<string>;
308+
postNotification(deviceIdentifier: string, notification: string, commandType?: string): Promise<string>;
309+
subscribeForNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<Promise<string>>;
309310
}
310311

311312
interface IiOSSocketRequestExecutor {

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
import * as constants from "../../common/constants";
2+
13
export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
24
constructor(private $errors: IErrors,
35
private $iOSNotification: IiOSNotification,
46
private $iOSNotificationService: IiOSNotificationService,
57
private $logger: ILogger) { }
68

79
public async executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise<void> {
10+
const deviceIdentifier = device.deviceInfo.identifier;
811

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+
];
1417

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);
1620

1721
let receivedNotification: string;
1822
try {
19-
receivedNotification = await Promise.race([alreadyConnected, readyForAttach, attachAvailable]);
23+
receivedNotification = await Promise.race(observeNotificationPromises);
2024
} catch (e) {
2125
this.$errors.failWithoutHelp(`The application ${projectId} does not appear to be running on ${device.deviceInfo.displayName} or is not built with debugging enabled.`);
2226
}
@@ -26,24 +30,29 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
2630
this.$errors.failWithoutHelp("A client is already connected.");
2731
break;
2832
case this.$iOSNotification.getAttachAvailable(projectId):
29-
await this.executeAttachAvailable(device.deviceInfo.identifier, timeout);
33+
await this.executeAttachAvailable(deviceIdentifier, timeout);
3034
break;
3135
case this.$iOSNotification.getReadyForAttach(projectId):
3236
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.");
3341
}
3442
}
3543

3644
public async executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, shouldBreak?: boolean): Promise<void> {
3745
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;
3948
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);
4150
}
4251

4352
// 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);
4554

46-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest);
55+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest, constants.IOS_POST_NOTIFICATION_COMMAND_TYPE);
4756
await readyForAttachPromise;
4857
} catch (e) {
4958
this.$logger.trace("Launch request error:");
@@ -56,7 +65,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
5665
try {
5766
// We should create this promise here because we need to send the ObserveNotification on the device
5867
// 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);
6069
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest);
6170
await readyForAttachPromise;
6271
} catch (e) {

lib/services/ios-debug-service.ts

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

183183
const socket = await factoryPromise();
184-
const factory = () => socket;
184+
const factory = () => {
185+
process.nextTick(() => {
186+
socket.emit("connect");
187+
});
188+
return socket;
189+
};
185190

186191
if (this.$options.chrome) {
187192
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)