Skip to content

Commit

Permalink
🎨 (core): Reviews ble transport
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed Oct 18, 2024
1 parent 90a4f1e commit 82460bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type WebBleInternalDevice = {

@injectable()
export class WebBleTransport implements Transport {
private _connectedDevices: Array<BluetoothDevice>;
private _internalDevicesById: Map<DeviceId, WebBleInternalDevice>;
private readonly _connectedDevices: Array<BluetoothDevice>;
private readonly _internalDevicesById: Map<DeviceId, WebBleInternalDevice>;
private _deviceConnectionById: Map<DeviceId, BleDeviceConnection>;
private _disconnectionHandlersById: Map<DeviceId, () => void>;
private _logger: LoggerPublisherService;
Expand Down Expand Up @@ -148,28 +148,28 @@ export class WebBleTransport implements Transport {
*
* @private
*/
private async promptDeviceAccess(): Promise<
Either<PromptDeviceAccessError, BluetoothDevice>
private promptDeviceAccess(): EitherAsync<
PromptDeviceAccessError,
BluetoothDevice
> {
return EitherAsync.liftEither(this.getBluetoothApi())
.map(async (bluetoothApi) => {
let bleDevice: BluetoothDevice;
return EitherAsync(async ({ liftEither, throwE }) => {
const bluetoothApi = await liftEither(this.getBluetoothApi());
let bleDevice: BluetoothDevice;

try {
bleDevice = await bluetoothApi.requestDevice({
filters: this._deviceModelDataSource
.getBluetoothServices()
.map((serviceUuid) => ({
services: [serviceUuid],
})),
});
} catch (error) {
throw new NoAccessibleDeviceError(error);
}
try {
bleDevice = await bluetoothApi.requestDevice({
filters: this._deviceModelDataSource
.getBluetoothServices()
.map((serviceUuid) => ({
services: [serviceUuid],
})),
});
} catch (error) {
return throwE(new NoAccessibleDeviceError(error));
}

return bleDevice;
})
.run();
return bleDevice;
});
}

/**
Expand Down
65 changes: 0 additions & 65 deletions packages/core/src/internal/transport/model/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,119 +26,69 @@ export class DeviceNotRecognizedError extends GeneralSdkError {
override readonly _tag = "DeviceNotRecognizedError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class NoAccessibleDeviceError extends GeneralSdkError {
override readonly _tag = "NoAccessibleDeviceError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class OpeningConnectionError extends GeneralSdkError {
override readonly _tag = "ConnectionOpeningError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class UnknownDeviceError extends GeneralSdkError {
override readonly _tag = "UnknownDeviceError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class TransportNotSupportedError extends GeneralSdkError {
override readonly _tag = "TransportNotSupportedError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class BleTransportNotSupportedError extends GeneralSdkError {
override readonly _tag = "BleTransportNotSupportedError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class UsbHidTransportNotSupportedError extends GeneralSdkError {
override readonly _tag = "UsbHidTransportNotSupportedError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class SendApduConcurrencyError extends GeneralSdkError {
override readonly _tag = "SendApduConcurrencyError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class DisconnectError extends GeneralSdkError {
override readonly _tag = "DisconnectError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class ReconnectionFailedError extends GeneralSdkError {
override readonly _tag = "ReconnectionFailedError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

Expand All @@ -153,34 +103,19 @@ export class DeviceNotInitializedError extends GeneralSdkError {
override readonly _tag = "DeviceNotInitializedError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class BleDeviceGattServerError extends GeneralSdkError {
override readonly _tag = "BleDeviceGattServerError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

export class DeviceAlreadyConnectedError extends GeneralSdkError {
override readonly _tag = "DeviceAlreadyDiscoveredError";
constructor(readonly err?: unknown) {
super(err);
if (err instanceof Error) {
this.originalError = err;
} else if (err !== undefined) {
this.originalError = new Error(String(err));
}
}
}

0 comments on commit 82460bf

Please sign in to comment.