Skip to content

Commit

Permalink
♻️ (core) [NO-ISSUE]: ConnectUseCase now returns a promise (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman authored Feb 26, 2024
2 parents b9d7c19 + aa315cb commit a2bea8c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 50 deletions.
12 changes: 6 additions & 6 deletions apps/sample/src/components/MainView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ export const MainView: React.FC = () => {

useEffect(() => {
if (discoveredDevice) {
sdk.connect({ deviceId: discoveredDevice.id }).subscribe({
next: (connectedDevice) => {
sdk
.connect({ deviceId: discoveredDevice.id })
.then((connectedDevice) => {
console.log(
`🦖 Response from connect: ${JSON.stringify(connectedDevice)} 🎉`,
);
},
error: (error) => {
})
.catch((error) => {
console.error(`Error from connection or get-version`, error);
},
});
});
}
}, [sdk, discoveredDevice]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,26 @@ describe("ConnectUseCase", () => {
jest.restoreAllMocks();
});

test("If connect use case encounter an error, return it", (done) => {
jest.spyOn(transport, "connect").mockImplementation(() => {
return Promise.resolve(Left(new UnknownDeviceError()));
});
const usecase = new ConnectUseCase(transport);
test("If connect use case encounter an error, return it", async () => {
jest
.spyOn(transport, "connect")
.mockResolvedValue(Left(new UnknownDeviceError()));

const connect = usecase.execute({ deviceId: "" });
const usecase = new ConnectUseCase(transport);

connect.subscribe({
next: (connectedDevice) => {
done(connectedDevice);
},
error: (error) => {
expect(error).toBeInstanceOf(UnknownDeviceError);
done();
},
});
await expect(usecase.execute({ deviceId: "" })).rejects.toBeInstanceOf(
UnknownDeviceError,
);
});

test("If connect is in success, return an observable connected device object", (done) => {
jest.spyOn(transport, "connect").mockImplementation(() => {
return Promise.resolve(Right(stubConnectedDevice));
});
const usecase = new ConnectUseCase(transport);
test("If connect is in success, return an observable connected device object", async () => {
jest
.spyOn(transport, "connect")
.mockResolvedValue(Promise.resolve(Right(stubConnectedDevice)));

const connect = usecase.execute({ deviceId: "" });
const usecase = new ConnectUseCase(transport);

connect.subscribe({
next: (connectedDevice) => {
expect(connectedDevice).toBe(stubConnectedDevice);
done();
},
error: (error) => {
done(error);
},
});
const connectedDevice = await usecase.execute({ deviceId: "" });
expect(connectedDevice).toBe(stubConnectedDevice);
});
});
22 changes: 8 additions & 14 deletions packages/core/src/internal/discovery/use-case/ConnectUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { inject, injectable } from "inversify";
import { from, Observable, of, switchMap } from "rxjs";

import { DeviceId } from "@internal/device-model/model/DeviceModel";
import { usbDiTypes } from "@internal/usb/di/usbDiTypes";
Expand All @@ -21,18 +20,13 @@ export class ConnectUseCase {
// Later: @inject(usbDiTypes.BleTransport) private bleTransport: BleTransport,
) {}

execute({ deviceId }: ConnectUseCaseArgs): Observable<ConnectedDevice> {
return from(this.usbHidTransport.connect({ deviceId })).pipe(
switchMap((either) => {
return either.caseOf({
Left: (error) => {
throw error;
},
Right: (connectedDevice) => {
return of(connectedDevice);
},
});
}),
);
async execute({ deviceId }: ConnectUseCaseArgs): Promise<ConnectedDevice> {
const either = await this.usbHidTransport.connect({ deviceId });
return either.caseOf({
Left: (error) => {
throw error;
},
Right: (connectedDevice) => connectedDevice,
});
}
}

0 comments on commit a2bea8c

Please sign in to comment.