Skip to content

Commit 638e408

Browse files
committed
Don't auto-query devices until you call getDevices()
We could, but getDevices is fast and effective anyway (so no major need to pre-empty imo) and there are now plausible cases (tunneling/querying data only) where you would actively _not_ want a device monitoring connection open unnecesarily.
1 parent 4481e37 commit 638e408

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ export class UsbmuxClient {
158158

159159
constructor(
160160
private connectionOptions: net.NetConnectOpts = DEFAULT_ADDRESS
161-
) {
162-
this.startListeningForDevices().catch(() => {});
163-
}
161+
) {}
164162

165163
deviceMonitorConnection: net.Socket | Promise<net.Socket> | undefined;
166164

test/unit-tests.spec.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,26 @@ describe("Usbmux-client unit tests", () => {
7575

7676
it("should connect & report no connected devices initially", async () => {
7777
client = new UsbmuxClient({ port: mockServerPort! });
78+
const devicesPromise = client.getDevices();
7879
const socket = await waitForSocket();
7980

8081
await expectMessage(socket, 'LISTEN_REQUEST');
8182
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
8283
;
83-
const devices = await client.getDevices();
84+
const devices = await devicesPromise;
8485
expect(Object.keys(devices)).to.have.length(0);
8586
});
8687

8788
it("should connect & report a connected device after one appears", async () => {
8889
client = new UsbmuxClient({ port: mockServerPort! });
90+
const devicesPromise = client.getDevices();
8991
const socket = await waitForSocket();
9092

9193
await expectMessage(socket, 'LISTEN_REQUEST');
9294
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
9395

94-
expect(Object.keys(await client.getDevices())).to.have.length(0);
96+
const devices = await devicesPromise;
97+
expect(Object.keys(devices)).to.have.length(0);
9598

9699
socket.write(Buffer.from(MESSAGES.DEVICE_ATTACHED_EVENT, 'base64'));
97100
await delay(10);
@@ -100,25 +103,26 @@ describe("Usbmux-client unit tests", () => {
100103

101104
it("should handle reconnecting after disconnection", async () => {
102105
client = new UsbmuxClient({ port: mockServerPort! });
103-
let socket = await waitForSocket();
106+
const devicesPromise = client.getDevices();
104107

108+
let socket = await waitForSocket();
105109
await expectMessage(socket, 'LISTEN_REQUEST');
106110
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
107111

108-
expect(Object.keys(await client.getDevices())).to.have.length(0);
112+
expect(Object.keys(await devicesPromise)).to.have.length(0);
109113

110114
socket.destroy();
111115
serverSocket = undefined;
112116
await delay(10);
113117

114-
const deviceQuery = client.getDevices();
118+
const device2ndQuery = client.getDevices();
115119

116120
socket = await waitForSocket();
117121
await expectMessage(socket, 'LISTEN_REQUEST');
118122
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
119123
socket.write(Buffer.from(MESSAGES.DEVICE_ATTACHED_EVENT, 'base64'));
120124

121-
expect(Object.keys(await deviceQuery)).to.have.length(1);
125+
expect(Object.keys(await device2ndQuery)).to.have.length(1);
122126
});
123127

124128
it("should handle reconnecting to an initially unresponsive server", async () => {
@@ -127,7 +131,7 @@ describe("Usbmux-client unit tests", () => {
127131

128132
client = new UsbmuxClient({ port });
129133

130-
const deviceQueryResult = await client.getDevices().catch(e => e);;
134+
const deviceQueryResult = await client.getDevices().catch(e => e);
131135
expect(deviceQueryResult).to.be.instanceOf(Error);
132136
expect(deviceQueryResult.message).to.contain("ECONNREFUSED");
133137

0 commit comments

Comments
 (0)