diff --git a/src/ble/actions.ts b/src/ble/actions.ts index 6cf0f3e8..71c7204a 100644 --- a/src/ble/actions.ts +++ b/src/ble/actions.ts @@ -20,61 +20,12 @@ export const bleDidConnectPybricks = createAction((id: string, name: string) => name, })); -export enum BleDeviceFailToConnectReasonType { - NoWebBluetooth = 'ble.device.didFailToConnect.noWebBluetooth', - NoBluetooth = 'ble.device.didFailToConnect.noBluetooth', - Canceled = 'ble.device.didFailToConnect.canceled', - NoGatt = 'ble.device.didFailToConnect.noGatt', - NoDeviceInfoService = 'ble.device.didFailToConnect.noDeviceInfoService', - NoPybricksService = 'ble.device.didFailToConnect.noPybricksService', - Unknown = 'ble.device.didFailToConnect.unknown', -} - -type Reason = { - reason: T; -}; - -export type BleDeviceFailToConnectNoWebBluetoothReason = - Reason; - -export type BleDeviceFailToConnectNoBluetoothReason = - Reason; - -export type BleDeviceFailToConnectCanceledReason = - Reason; - -export type BleDeviceFailToConnectNoGattReason = - Reason; - -export type BleDeviceFailToConnectNoDeviceInfoServiceReason = - Reason; - -export type BleDeviceFailToConnectNoPybricksServiceReason = - Reason; - -export type BleDeviceFailToConnectUnknownReason = - Reason & { - err: Error; - }; - -export type BleDeviceDidFailToConnectReason = - | BleDeviceFailToConnectNoWebBluetoothReason - | BleDeviceFailToConnectNoBluetoothReason - | BleDeviceFailToConnectCanceledReason - | BleDeviceFailToConnectNoGattReason - | BleDeviceFailToConnectNoDeviceInfoServiceReason - | BleDeviceFailToConnectNoPybricksServiceReason - | BleDeviceFailToConnectUnknownReason; - /** * Response that indicates {@link bleConnectPybricks} failed. */ -export const bleDidFailToConnectPybricks = createAction( - (reason: BleDeviceDidFailToConnectReason) => ({ - type: 'ble.action.didFailToConnectPybricks', - ...reason, - }), -); +export const bleDidFailToConnectPybricks = createAction(() => ({ + type: 'ble.action.didFailToConnectPybricks', +})); /** * Creates an action to request disconnecting a hub running Pybricks firmware. diff --git a/src/ble/reducers.test.ts b/src/ble/reducers.test.ts index 4c043cb3..3fc80dbb 100644 --- a/src/ble/reducers.test.ts +++ b/src/ble/reducers.test.ts @@ -11,7 +11,6 @@ import { HubType, LegoCompanyId } from '../ble-lwp3-service/protocol'; import { didReceiveStatusReport } from '../ble-pybricks-service/actions'; import { Status, statusToFlag } from '../ble-pybricks-service/protocol'; import { - BleDeviceDidFailToConnectReason, bleConnectPybricks, bleDidConnectPybricks, bleDidDisconnectPybricks, @@ -52,7 +51,7 @@ test('connection', () => { expect( reducers( { connection: BleConnectionState.Connecting } as State, - bleDidFailToConnectPybricks({} as BleDeviceDidFailToConnectReason), + bleDidFailToConnectPybricks(), ).connection, ).toBe(BleConnectionState.Disconnected); expect( diff --git a/src/ble/sagas.test.ts b/src/ble/sagas.test.ts index 7389def0..096009c7 100644 --- a/src/ble/sagas.test.ts +++ b/src/ble/sagas.test.ts @@ -27,7 +27,6 @@ import { pybricksServiceUUID, } from '../ble-pybricks-service/protocol'; import { - BleDeviceFailToConnectReasonType, bleConnectPybricks, bleDidConnectPybricks, bleDidDisconnectPybricks, @@ -247,11 +246,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('ble', 'noWebBluetooth'), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.NoWebBluetooth, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); }); describe('has web bluetooth', () => { @@ -269,11 +264,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('ble', 'bluetoothNotAvailable'), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.NoBluetooth, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); }); it('should fail if user canceled requestDevice', async () => { @@ -283,11 +274,7 @@ describe('connect action is dispatched', () => { await runConnectUntil(saga, ConnectRunPoint.Connect); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Canceled, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); }); it('should fail on other exception in requestDevice', async () => { @@ -301,12 +288,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); }); it('should fail if device has no gatt property', async () => { @@ -317,11 +299,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('ble', 'noGatt'), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.NoGatt, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); }); it('should fail if gatt connect fails', async () => { @@ -333,12 +311,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); }); it('should fail if device does not have device info service', async () => { @@ -355,11 +328,7 @@ describe('connect action is dispatched', () => { hubName: 'test name', }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.NoDeviceInfoService, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -375,12 +344,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -394,12 +358,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -415,12 +374,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -434,12 +388,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -466,12 +415,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -490,11 +434,7 @@ describe('connect action is dispatched', () => { hubName: 'test name', }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.NoPybricksService, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -510,12 +450,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -529,12 +464,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -548,12 +478,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -572,12 +497,7 @@ describe('connect action is dispatched', () => { hubName: 'test name', }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - // FIXME: this is wrong error - reason: BleDeviceFailToConnectReasonType.NoPybricksService, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -593,12 +513,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -614,12 +529,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -633,12 +543,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); @@ -652,12 +557,7 @@ describe('connect action is dispatched', () => { await expect(saga.take()).resolves.toEqual( alertsShowAlert('alerts', 'unexpectedError', { error: testError }), ); - await expect(saga.take()).resolves.toEqual( - bleDidFailToConnectPybricks({ - reason: BleDeviceFailToConnectReasonType.Unknown, - err: testError, - }), - ); + await expect(saga.take()).resolves.toEqual(bleDidFailToConnectPybricks()); expect(mocks.gatt.disconnect).toHaveBeenCalled(); }); diff --git a/src/ble/sagas.ts b/src/ble/sagas.ts index 57bd88b8..6f8a39d7 100644 --- a/src/ble/sagas.ts +++ b/src/ble/sagas.ts @@ -54,7 +54,6 @@ import { import { RootState } from '../reducers'; import { ensureError } from '../utils'; import { - BleDeviceFailToConnectReasonType as Reason, bleConnectPybricks as bleConnectPybricks, bleDidConnectPybricks, bleDidDisconnectPybricks, @@ -101,14 +100,14 @@ function* handleWriteUart( function* handleBleConnectPybricks(): Generator { if (navigator.bluetooth === undefined) { yield* put(alertsShowAlert('ble', 'noWebBluetooth')); - yield* put(bleDidFailToConnectPybricks({ reason: Reason.NoWebBluetooth })); + yield* put(bleDidFailToConnectPybricks()); return; } const available = yield* call(() => navigator.bluetooth.getAvailability()); if (!available) { yield* put(alertsShowAlert('ble', 'bluetoothNotAvailable')); - yield* put(bleDidFailToConnectPybricks({ reason: Reason.NoBluetooth })); + yield* put(bleDidFailToConnectPybricks()); return; } @@ -142,7 +141,7 @@ function* handleBleConnectPybricks(): Generator { ); if (!device) { - yield* put(bleDidFailToConnectPybricks({ reason: Reason.Canceled })); + yield* put(bleDidFailToConnectPybricks()); return; } @@ -150,7 +149,7 @@ function* handleBleConnectPybricks(): Generator { if (!gatt) { yield* put(alertsShowAlert('ble', 'noGatt')); - yield* put(bleDidFailToConnectPybricks({ reason: Reason.NoGatt })); + yield* put(bleDidFailToConnectPybricks()); return; } @@ -192,9 +191,7 @@ function* handleBleConnectPybricks(): Generator { hubName: device.name || 'Pybricks Hub', }), ); - yield* put( - bleDidFailToConnectPybricks({ reason: Reason.NoDeviceInfoService }), - ); + yield* put(bleDidFailToConnectPybricks()); return; } @@ -261,11 +258,7 @@ function* handleBleConnectPybricks(): Generator { hubName: device.name || 'Pybricks Hub', }), ); - yield* put( - bleDidFailToConnectPybricks({ - reason: Reason.NoPybricksService, - }), - ); + yield* put(bleDidFailToConnectPybricks()); return; } @@ -330,11 +323,7 @@ function* handleBleConnectPybricks(): Generator { hubName: device.name || 'Pybricks Hub', }), ); - yield* put( - bleDidFailToConnectPybricks({ - reason: Reason.NoPybricksService, - }), - ); + yield* put(bleDidFailToConnectPybricks()); return; } @@ -396,12 +385,7 @@ function* handleBleConnectPybricks(): Generator { error: ensureError(err), }), ); - yield* put( - bleDidFailToConnectPybricks({ - reason: Reason.Unknown, - err: ensureError(err), - }), - ); + yield* put(bleDidFailToConnectPybricks()); } finally { yield* cancel(tasks);