Skip to content

Commit d87444d

Browse files
committed
fix(ios): don't end device discovery on the wrong device
With 100 ms polling, an unrelated device ended the wait almost immediately. A requested device now only ends it by being found, and otherwise the 2 seconds the old polling allowed still apply, so device indexes stay stable.
1 parent a92b8d0 commit d87444d

2 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎lib/common/mobile/ios/device/ios-device-operations.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ import {
1111
} from "../../../declarations";
1212
import { injector } from "../../../yok";
1313

14+
// Other devices can still be arriving by then, and device indexes follow the
15+
// order they were discovered in.
16+
const MIN_DISCOVERY_WAIT_MS = 2000;
17+
18+
export function isDeviceDiscoveryDone(
19+
foundDevice: boolean,
20+
waitedMs: number,
21+
options?: Mobile.IDeviceLookingOptions,
22+
): boolean {
23+
if (options?.isRequestedDeviceFound) {
24+
return options.isRequestedDeviceFound();
25+
}
26+
27+
return foundDevice && !options?.fullDiscovery && waitedMs >= MIN_DISCOVERY_WAIT_MS;
28+
}
29+
1430
export class IOSDeviceOperations
1531
extends EventEmitter
1632
implements IIOSDeviceOperations, IDisposable, IShouldDispose
@@ -91,16 +107,11 @@ export class IOSDeviceOperations
91107
let waited = 0;
92108

93109
const intervalHandle: NodeJS.Timer = setInterval(() => {
110+
waited += pollInterval;
94111
if (
95-
(foundDevice && !options.fullDiscovery) ||
96-
options.isRequestedDeviceFound?.()
112+
waited >= maxWait ||
113+
isDeviceDiscoveryDone(foundDevice, waited, options)
97114
) {
98-
resolve();
99-
return clearInterval(intervalHandle as unknown as number);
100-
}
101-
102-
waited += pollInterval;
103-
if (waited >= maxWait) {
104115
clearInterval(intervalHandle as unknown as number);
105116
return resolve();
106117
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { assert } from "chai";
2+
import { isDeviceDiscoveryDone } from "../../../mobile/ios/device/ios-device-operations";
3+
4+
describe("isDeviceDiscoveryDone", () => {
5+
const options = (extra: object = {}) => <any>{ ...extra };
6+
7+
it("ends as soon as the requested device is found", () => {
8+
const isRequestedDeviceFound = () => true;
9+
assert.isTrue(
10+
isDeviceDiscoveryDone(false, 100, options({ isRequestedDeviceFound })),
11+
);
12+
});
13+
14+
it("keeps waiting for the requested device when another one shows up", () => {
15+
const isRequestedDeviceFound = () => false;
16+
assert.isFalse(
17+
isDeviceDiscoveryDone(true, 5000, options({ isRequestedDeviceFound })),
18+
);
19+
});
20+
21+
it("gives other devices time to arrive when none was requested", () => {
22+
assert.isFalse(isDeviceDiscoveryDone(true, 100, options()));
23+
assert.isTrue(isDeviceDiscoveryDone(true, 2000, options()));
24+
});
25+
26+
it("keeps waiting while no device was found", () => {
27+
assert.isFalse(isDeviceDiscoveryDone(false, 5000, options()));
28+
});
29+
30+
it("doesn't end early on a full discovery", () => {
31+
assert.isFalse(
32+
isDeviceDiscoveryDone(true, 5000, options({ fullDiscovery: true })),
33+
);
34+
});
35+
36+
it("works without options", () => {
37+
assert.isTrue(isDeviceDiscoveryDone(true, 2000));
38+
});
39+
});

0 commit comments

Comments
 (0)