From 2854ab422b13630fcc8b1e1cd13332301b4d1cd7 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sun, 24 May 2020 19:56:16 -0500 Subject: [PATCH] Rename BLE connect actions This matches the naming patter used in other files. --- src/actions/ble.ts | 76 ++++++++++++++++++++++++++++----------------- src/epics/ble.ts | 4 +-- src/reducers/ble.ts | 8 ++--- 3 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/actions/ble.ts b/src/actions/ble.ts index 80c3eb65..fe9e63e1 100644 --- a/src/actions/ble.ts +++ b/src/actions/ble.ts @@ -20,27 +20,61 @@ const bleNusMaxSize = 20; let device: BluetoothDevice | undefined; let rxChar: PolyfillBluetoothRemoteGATTCharacteristic | undefined; +/** + * Bluetooth low energy connection action types. + */ export enum BLEConnectActionType { /** - * Begin async connect. + * Connecting to a device has been requested. */ - BeginConnect = 'ble.connect.begin', + WillConnect = 'ble.action.will.connect', /** - * End async connect (success). + * The connection completed successfully. */ - EndConnect = 'ble.connect.end', + DidConnect = 'ble.action.did.connect', /** - * Begin async disconnect. + * Disconnecting from a device has been requested. */ - BeginDisconnect = 'ble.disconnect.begin', + WillDisconnect = 'ble.action.will.disconnect', /** * End async disconnect (can be sent without sending BeginDisconnect first). */ - EndDisconnect = 'ble.disconnect.end', + DidDisconnect = 'ble.action.did.disconnect', } +/** + * Common type for all BLE connection actions. + */ type BLEConnectAction = Action; +/** + * Creates an action that indicates connecting has been requested. + */ +function willConnect(): BLEConnectAction { + return { type: BLEConnectActionType.WillConnect }; +} + +/** + * Creates an action that indicates a device was connected. + */ +function didConnect(): BLEConnectAction { + return { type: BLEConnectActionType.DidConnect }; +} + +/** + * Creates an action that indicates disconnecting was requested. + */ +function willDisconnect(): BLEConnectAction { + return { type: BLEConnectActionType.WillDisconnect }; +} + +/** + * Creates an action that indicates a device was disconnected. + */ +function didDisconnect(): BLEConnectAction { + return { type: BLEConnectActionType.DidDisconnect }; +} + export enum BLEDataActionType { /** * Send data. @@ -58,22 +92,6 @@ export interface BLEDataAction extends Action { export type BLEThunkAction = ThunkAction, {}, {}, Action>; -function beginConnect(): BLEConnectAction { - return { type: BLEConnectActionType.BeginConnect }; -} - -function endConnect(): BLEConnectAction { - return { type: BLEConnectActionType.EndConnect }; -} - -function beginDisconnect(): BLEConnectAction { - return { type: BLEConnectActionType.BeginDisconnect }; -} - -function endDisconnect(): BLEConnectAction { - return { type: BLEConnectActionType.EndDisconnect }; -} - export function connect(): BLEThunkAction { return async function (dispatch): Promise { if (device !== undefined) { @@ -91,7 +109,7 @@ export function connect(): BLEThunkAction { return; } // TODO: check navigator.bluetooth.getAvailability() - dispatch(beginConnect()); + dispatch(willConnect()); try { device = await navigator.bluetooth.requestDevice({ filters: [{ services: [pybricksServiceUUID] }], @@ -113,18 +131,18 @@ export function connect(): BLEThunkAction { ), ); } - dispatch(endDisconnect()); + dispatch(didDisconnect()); return; } if (device.gatt === undefined) { dispatch(notification.add('error', 'Device does not support GATT.')); - dispatch(endDisconnect()); + dispatch(didDisconnect()); return; } device.addEventListener('gattserverdisconnected', () => { device = undefined; rxChar = undefined; - dispatch(endDisconnect()); + dispatch(didDisconnect()); }); const server = await device.gatt.connect(); try { @@ -146,13 +164,13 @@ export function connect(): BLEThunkAction { device.gatt.disconnect(); return; } - dispatch(endConnect()); + dispatch(didConnect()); }; } export function disconnect(): BLEThunkAction { return async function (dispatch): Promise { - dispatch(beginDisconnect()); + dispatch(willDisconnect()); device?.gatt?.disconnect(); }; } diff --git a/src/epics/ble.ts b/src/epics/ble.ts index 05c2f6b5..9ba7c9fd 100644 --- a/src/epics/ble.ts +++ b/src/epics/ble.ts @@ -14,13 +14,13 @@ const decoder = new TextDecoder(); const connect: Epic = (action$) => action$.pipe( - ofType(BLEConnectActionType.EndConnect), + ofType(BLEConnectActionType.DidConnect), map(() => updateStatus(HubRuntimeStatusType.Idle)), ); const disconnect: Epic = (action$) => action$.pipe( - ofType(BLEConnectActionType.EndDisconnect), + ofType(BLEConnectActionType.DidDisconnect), map(() => updateStatus(HubRuntimeStatusType.Disconnected)), ); diff --git a/src/reducers/ble.ts b/src/reducers/ble.ts index 1b7cc67d..45891d4c 100644 --- a/src/reducers/ble.ts +++ b/src/reducers/ble.ts @@ -31,13 +31,13 @@ const connection: Reducer = ( action, ) => { switch (action.type) { - case BLEConnectActionType.BeginConnect: + case BLEConnectActionType.WillConnect: return BLEConnectionState.Connecting; - case BLEConnectActionType.EndConnect: + case BLEConnectActionType.DidConnect: return BLEConnectionState.Connected; - case BLEConnectActionType.BeginDisconnect: + case BLEConnectActionType.WillDisconnect: return BLEConnectionState.Disconnecting; - case BLEConnectActionType.EndDisconnect: + case BLEConnectActionType.DidDisconnect: return BLEConnectionState.Disconnected; default: return state;