Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/common/node/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class PromiseUtil {
interval: number = 1000,
timeout?: number,
): Promise<T | null> {
return new Promise(async resolve => {
return new Promise(async (resolve, reject) => {
let rejectTimeout: NodeJS.Timeout | undefined;
// eslint-disable-next-line prefer-const
let сheckInterval: NodeJS.Timeout | undefined;
Expand All @@ -89,12 +89,18 @@ export class PromiseUtil {
};

const tryToResolve = async (): Promise<boolean> => {
const result = await condition();
if (result) {
try {
const result = await condition();
if (result) {
cleanup();
resolve(result);
}
return !!result;
} catch (err) {
cleanup();
resolve(result);
reject(err);
return false;
}
return !!result;
};

const resolved = await tryToResolve();
Expand Down
57 changes: 33 additions & 24 deletions src/extension/android/androidTargetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export class AndroidTargetManager extends MobileTargetManager {

protected async launchSimulator(emulatorTarget: IMobileTarget): Promise<AndroidTarget> {
return new Promise<AndroidTarget>((resolve, reject) => {
let emulatorLaunchFailed = false;
const emulatorProcess = this.childProcess.spawn(
AndroidTargetManager.EMULATOR_COMMAND,
[AndroidTargetManager.EMULATOR_AVD_START_COMMAND, emulatorTarget.name as string],
Expand All @@ -150,6 +151,7 @@ export class AndroidTargetManager extends MobileTargetManager {
true,
);
emulatorProcess.outcome.catch(error => {
emulatorLaunchFailed = true;
if (
process.platform == "win32" &&
process.env.SESSIONNAME &&
Expand All @@ -169,6 +171,8 @@ export class AndroidTargetManager extends MobileTargetManager {
emulatorProcess.spawnedProcess.unref();

const condition = async () => {
if (emulatorLaunchFailed)
throw new Error("Android simulator launch failed unexpectedly");
const connectedDevices = await this.adbHelper.getOnlineTargets();
for (const target of connectedDevices) {
const onlineAvdName = await this.adbHelper.getAvdNameById(target.id);
Expand All @@ -183,31 +187,36 @@ export class AndroidTargetManager extends MobileTargetManager {
condition,
1000,
AndroidTargetManager.EMULATOR_START_TIMEOUT * 1000,
).then(emulatorId => {
if (emulatorId) {
emulatorTarget.id = emulatorId;
emulatorTarget.isOnline = true;
this.logger.info(
localize(
"EmulatorLaunched",
"Launched Android emulator {0}",
emulatorTarget.name,
),
);
resolve(AndroidTarget.fromInterface(<IDebuggableMobileTarget>emulatorTarget));
} else {
reject(
new Error(
`Virtual device launch finished with an exception: ${localize(
"EmulatorStartWarning",
"Could not start the emulator {0} within {1} seconds.",
).then(
emulatorId => {
if (emulatorId) {
emulatorTarget.id = emulatorId;
emulatorTarget.isOnline = true;
this.logger.info(
localize(
"EmulatorLaunched",
"Launched Android emulator {0}",
emulatorTarget.name,
AndroidTargetManager.EMULATOR_START_TIMEOUT,
)}`,
),
);
}
});
),
);
resolve(
AndroidTarget.fromInterface(<IDebuggableMobileTarget>emulatorTarget),
);
} else {
reject(
new Error(
`Virtual device launch finished with an exception: ${localize(
"EmulatorStartWarning",
"Could not start the emulator {0} within {1} seconds.",
emulatorTarget.name,
AndroidTargetManager.EMULATOR_START_TIMEOUT,
)}`,
),
);
}
},
() => {},
);
});
}
}
49 changes: 30 additions & 19 deletions src/extension/ios/iOSTargetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export class IOSTargetManager extends MobileTargetManager {
protected async launchSimulator(
virtualTarget: IDebuggableIOSTarget,
): Promise<IOSTarget | undefined> {
let emulatorLaunchFailed = false;
return new Promise<IOSTarget | undefined>((resolve, reject) => {
const emulatorProcess = this.childProcess.spawn(
IOSTargetManager.XCRUN_COMMAND,
Expand All @@ -228,6 +229,7 @@ export class IOSTargetManager extends MobileTargetManager {
);
emulatorProcess.spawnedProcess.unref();
emulatorProcess.outcome.catch(e => {
emulatorLaunchFailed = true;
this.logger.error(
localize(
"ErrorWhileLaunchingSimulator",
Expand All @@ -240,6 +242,8 @@ export class IOSTargetManager extends MobileTargetManager {
});

const condition = async () => {
if (emulatorLaunchFailed)
throw new Error("iOS simulator launch failed unexpectedly");
await this.collectTargets(TargetType.Simulator);
const onlineTarget = (await this.getTargetList()).find(
target => target.id === virtualTarget.id && target.isOnline,
Expand All @@ -251,26 +255,33 @@ export class IOSTargetManager extends MobileTargetManager {
condition,
1000,
IOSTargetManager.SIMULATOR_START_TIMEOUT * 1000,
).then(isBooted => {
if (isBooted) {
virtualTarget.isOnline = true;
this.logger.info(
localize("SimulatorLaunched", "Launched simulator {0}", virtualTarget.name),
);
resolve(IOSTarget.fromInterface(virtualTarget));
} else {
reject(
new Error(
`Virtual device launch finished with an exception: ${localize(
"SimulatorStartWarning",
"Could not start the simulator {0} within {1} seconds.",
).then(
isBooted => {
if (isBooted) {
virtualTarget.isOnline = true;
this.logger.info(
localize(
"SimulatorLaunched",
"Launched simulator {0}",
virtualTarget.name,
IOSTargetManager.SIMULATOR_START_TIMEOUT,
)}`,
),
);
}
});
),
);
resolve(IOSTarget.fromInterface(virtualTarget));
} else {
reject(
new Error(
`Virtual device launch finished with an exception: ${localize(
"SimulatorStartWarning",
"Could not start the simulator {0} within {1} seconds.",
virtualTarget.name,
IOSTargetManager.SIMULATOR_START_TIMEOUT,
)}`,
),
);
}
},
() => {},
);
});
}
}