Skip to content

Commit

Permalink
♻️ (core) [NO-ISSUE]: Method sendCommand now returns a promise (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman authored Jun 12, 2024
2 parents 0ae7c6b + 6b17607 commit fe0a537
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-glasses-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-sdk-core": patch
---

Change sendCommand signature to directly return a Promise instead of a new function
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("SendCommandUseCase", () => {

jest
.spyOn(deviceSession, "sendCommand")
.mockReturnValue(async () => Promise.resolve({ status: "success" }));
.mockResolvedValue({ status: "success" });

const response = await useCase.execute<{ status: string }>({
sessionId: fakeSessionId,
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/api/command/use-case/SendCommandUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ export class SendCommandUseCase {

return deviceSessionOrError.caseOf({
// Case device session found
Right: async (deviceSession) => {
const deviceModelId = deviceSession.connectedDevice.deviceModel.id;
const action = deviceSession.sendCommand<Response, Args>(command);
return await action(deviceModelId);
},
Right: async (deviceSession) =>
await deviceSession.sendCommand<Response, Args>(command),
// Case device session not found
Left: (error) => {
this._logger.error("Error getting session", {
Expand Down
26 changes: 13 additions & 13 deletions packages/core/src/internal/device-session/model/DeviceSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { v4 as uuidv4 } from "uuid";

import { Command } from "@api/command/Command";
import { CommandUtils } from "@api/command/utils/CommandUtils";
import { DeviceModelId } from "@api/device/DeviceModel";
import { DeviceStatus } from "@api/device/DeviceStatus";
import {
DeviceSessionState,
Expand Down Expand Up @@ -97,18 +96,19 @@ export class DeviceSession {
});
}

sendCommand<Response, Args>(command: Command<Response, Args>) {
return async (deviceModelId: DeviceModelId): Promise<Response> => {
const apdu = command.getApdu();
const response = await this.sendApdu(apdu.getRawApdu());

return response.caseOf({
Left: (err) => {
throw err;
},
Right: (r) => command.parseResponse(r, deviceModelId),
});
};
async sendCommand<Response, Args>(
command: Command<Response, Args>,
): Promise<Response> {
const apdu = command.getApdu();
const response = await this.sendApdu(apdu.getRawApdu());

return response.caseOf({
Left: (err) => {
throw err;
},
Right: (r) =>
command.parseResponse(r, this._connectedDevice.deviceModel.id),
});
}

close() {
Expand Down

0 comments on commit fe0a537

Please sign in to comment.