diff --git a/apps/sample/src/components/MainView/index.tsx b/apps/sample/src/components/MainView/index.tsx index 641333eb3..9dbe2d5ab 100644 --- a/apps/sample/src/components/MainView/index.tsx +++ b/apps/sample/src/components/MainView/index.tsx @@ -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]); diff --git a/packages/core/src/internal/discovery/use-case/ConnectUseCase.test.ts b/packages/core/src/internal/discovery/use-case/ConnectUseCase.test.ts index 6a040aadb..33d94bfa1 100644 --- a/packages/core/src/internal/discovery/use-case/ConnectUseCase.test.ts +++ b/packages/core/src/internal/discovery/use-case/ConnectUseCase.test.ts @@ -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); }); }); diff --git a/packages/core/src/internal/discovery/use-case/ConnectUseCase.ts b/packages/core/src/internal/discovery/use-case/ConnectUseCase.ts index 64cc98f62..a21836a0c 100644 --- a/packages/core/src/internal/discovery/use-case/ConnectUseCase.ts +++ b/packages/core/src/internal/discovery/use-case/ConnectUseCase.ts @@ -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"; @@ -21,18 +20,13 @@ export class ConnectUseCase { // Later: @inject(usbDiTypes.BleTransport) private bleTransport: BleTransport, ) {} - execute({ deviceId }: ConnectUseCaseArgs): Observable { - 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 { + const either = await this.usbHidTransport.connect({ deviceId }); + return either.caseOf({ + Left: (error) => { + throw error; + }, + Right: (connectedDevice) => connectedDevice, + }); } }