Skip to content

Commit ec6ddb9

Browse files
committed
Support for multiple simulators
1 parent 0df90aa commit ec6ddb9

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

lib/ios-sim.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ Object.defineProperty(publicApi, "getRunningSimulator", {
3131
}
3232
});
3333

34+
Object.defineProperty(publicApi, "getRunningSimulators", {
35+
get: () => {
36+
return (...args: any[]) => {
37+
let isResolved = false;
38+
39+
return new Promise<any>((resolve, reject) => {
40+
const libraryPath = require("./iphone-simulator-xcode-simctl");
41+
const simulator = new libraryPath.XCodeSimctlSimulator();
42+
let repeatCount = 30;
43+
const timer = setInterval(() => {
44+
const result = simulator.getBootedDevices.apply(simulator, args);
45+
if ((result || !repeatCount) && !isResolved) {
46+
isResolved = true;
47+
clearInterval(timer);
48+
resolve(result);
49+
}
50+
repeatCount--;
51+
}, 500);
52+
});
53+
}
54+
}
55+
});
56+
3457
Object.defineProperty(publicApi, "getApplicationPath", {
3558
get: () => {
3659
return (...args: any[]) => {

lib/iphone-simulator-xcode-simctl.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
220220
return _.find(devices, device => this.isDeviceBooted(device));
221221
}
222222

223+
private getBootedDevices(): IDevice[] {
224+
const devices = this.simctl.getDevices();
225+
return _.filter(devices, device => this.isDeviceBooted(device));
226+
}
227+
223228
public startSimulator(device?: IDevice): void {
224229
device = device || this.getDeviceToRun();
225230

@@ -229,15 +234,17 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
229234
}
230235

231236
if (!this.isDeviceBooted(device)) {
232-
let bootedDevice = this.getBootedDevice();
233-
if (bootedDevice && bootedDevice.id !== device.id) {
234-
this.killSimulator();
235-
}
236-
237-
// In case user closes simulator window but simulator app is still alive
238-
if (!bootedDevice && this.isSimulatorAppRunning()) {
239-
const defaultDevice = _.find(this.simctl.getDevices(), device => device.name === this.defaultDeviceIdentifier);
240-
this.simctl.boot(defaultDevice.id);
237+
const isSimulatorAppRunning = this.isSimulatorAppRunning();
238+
const haveBootedDevices = this.haveBootedDevices();
239+
240+
if (isSimulatorAppRunning) {
241+
// In case user closes simulator window but simulator app is still alive
242+
if (!haveBootedDevices) {
243+
device = this.getDeviceToRun();
244+
}
245+
this.simctl.boot(device.id);
246+
} else {
247+
common.startSimulator(device.id);
241248
}
242249

243250
common.startSimulator(device.id);
@@ -248,6 +255,11 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
248255
}
249256
}
250257

258+
private haveBootedDevices(): boolean {
259+
const bootedDevices = this.getBootedDevices();
260+
return bootedDevices && bootedDevices.length > 0;
261+
}
262+
251263
private isSimulatorAppRunning(): boolean {
252264
const simulatorAppName = "Simulator";
253265

0 commit comments

Comments
 (0)