Skip to content

Commit

Permalink
Don't auto-query devices until you call getDevices()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pimterry committed May 28, 2024
1 parent 4481e37 commit 638e408
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ export class UsbmuxClient {

constructor(
private connectionOptions: net.NetConnectOpts = DEFAULT_ADDRESS
) {
this.startListeningForDevices().catch(() => {});
}
) {}

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

Expand Down
18 changes: 11 additions & 7 deletions test/unit-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,26 @@ describe("Usbmux-client unit tests", () => {

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

await expectMessage(socket, 'LISTEN_REQUEST');
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
;
const devices = await client.getDevices();
const devices = await devicesPromise;
expect(Object.keys(devices)).to.have.length(0);
});

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

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

expect(Object.keys(await client.getDevices())).to.have.length(0);
const devices = await devicesPromise;
expect(Object.keys(devices)).to.have.length(0);

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

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

let socket = await waitForSocket();
await expectMessage(socket, 'LISTEN_REQUEST');
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));

expect(Object.keys(await client.getDevices())).to.have.length(0);
expect(Object.keys(await devicesPromise)).to.have.length(0);

socket.destroy();
serverSocket = undefined;
await delay(10);

const deviceQuery = client.getDevices();
const device2ndQuery = client.getDevices();

socket = await waitForSocket();
await expectMessage(socket, 'LISTEN_REQUEST');
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
socket.write(Buffer.from(MESSAGES.DEVICE_ATTACHED_EVENT, 'base64'));

expect(Object.keys(await deviceQuery)).to.have.length(1);
expect(Object.keys(await device2ndQuery)).to.have.length(1);
});

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

client = new UsbmuxClient({ port });

const deviceQueryResult = await client.getDevices().catch(e => e);;
const deviceQueryResult = await client.getDevices().catch(e => e);
expect(deviceQueryResult).to.be.instanceOf(Error);
expect(deviceQueryResult.message).to.contain("ECONNREFUSED");

Expand Down

0 comments on commit 638e408

Please sign in to comment.