Skip to content

Commit

Permalink
♻️ (chore): Use EitherAsync instead of multiple caseOf in bleTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed Oct 14, 2024
1 parent 5a85fae commit 2fb8880
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ export class BleDeviceConnection implements DeviceConnection {
});
},
);
const frames = this._apduSender.caseOf({
Just: (apduSender) => apduSender.getFrames(apdu),
Nothing: () => [],
});
const frames = this._apduSender.mapOrDefault(
(apduSender) => apduSender.getFrames(apdu),
[],
);
for (const frame of frames) {
try {
this._logger.debug("Sending Frame", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,47 +224,37 @@ export class WebBleTransport implements Transport {

return from(this.promptDeviceAccess()).pipe(
switchMap(async (errorOrBleDevice) =>
errorOrBleDevice.caseOf({
Right: async (bleDevice) => {
// ble connect here as gatt server needs to be opened to fetch gatt service
if (bleDevice.gatt) {
try {
await bleDevice.gatt.connect();
} catch (error) {
throw new OpeningConnectionError(error);
}
EitherAsync(async ({ liftEither, fromPromise }) => {
const bleDevice = await liftEither(errorOrBleDevice);
if (bleDevice.gatt) {
try {
await bleDevice.gatt.connect();
} catch (error) {
throw new OpeningConnectionError(error);
}
const errorOrBleGattService =
await this.getBleGattService(bleDevice);
return errorOrBleGattService.caseOf({
Right: (bleGattService) => {
const errorOrBleDeviceInfos =
this.getBleDeviceInfos(bleGattService);
return errorOrBleDeviceInfos.caseOf({
Right: (bleDeviceInfos) => {
const discoveredDevice =
this.getDiscoveredDeviceFrom(bleDeviceInfos);
this.setInternalDeviceFrom(
discoveredDevice,
bleDevice,
bleDeviceInfos,
bleGattService,
);
return discoveredDevice;
},

Left: (error) => {
bleDevice.forget();
throw error;
},
});
},
Left: (error) => {
bleDevice.forget();
throw error;
},
});
},
}
try {
const bleGattService = await fromPromise(
this.getBleGattService(bleDevice),
);
const bleDeviceInfos = await liftEither(
this.getBleDeviceInfos(bleGattService),
);
const discoveredDevice =
this.getDiscoveredDeviceFrom(bleDeviceInfos);
this.setInternalDeviceFrom(
discoveredDevice,
bleDevice,
bleDeviceInfos,
bleGattService,
);
return discoveredDevice;
} catch (error) {
await bleDevice.forget();
throw error;
}
}).caseOf({
Right: (discoveredDevice) => discoveredDevice,
Left: (error) => {
this._logger.error("Error while getting accessible device", {
data: { error },
Expand Down

0 comments on commit 2fb8880

Please sign in to comment.