From 6a9e867fe978218c48f3dec162c222a7af0747ad Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 23 Dec 2021 16:36:29 -0600 Subject: [PATCH] ble-pybricks-service: rename statusReportEvent New name is didReceiveStatusReport which is more consistent with existing naming patterns. --- src/ble-pybricks-service/actions.ts | 13 ++++++++----- src/ble-pybricks-service/sagas.test.ts | 4 ++-- src/ble-pybricks-service/sagas.ts | 4 ++-- src/hub/reducers.test.ts | 18 ++++++++++-------- src/hub/reducers.ts | 2 +- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/ble-pybricks-service/actions.ts b/src/ble-pybricks-service/actions.ts index 56748e13..33d1b2e6 100644 --- a/src/ble-pybricks-service/actions.ts +++ b/src/ble-pybricks-service/actions.ts @@ -187,8 +187,8 @@ export type BlePybricksServiceCommandAction = /** Action types for events received from the Pybricks service control characteristic. */ export enum BlePybricksServiceEventActionType { - /** A status report event. */ - StatusReport = 'blePybricksServiceEvent.action.statusReport', + /** A status report event was received. */ + DidReceiveStatusReport = 'blePybricksServiceEvent.action.didReceiveStatusReport', /** A pseudo-event indicating there was a protocol error (not directly received from the hub). */ ProtocolError = 'blePybricksServiceEvent.action.protocolError', } @@ -197,7 +197,7 @@ export enum BlePybricksServiceEventActionType { * Action that represents a status report event received from the hub. */ export type BlePybricksServiceEventStatusReportAction = - Action & { + Action & { statusFlags: number; }; @@ -205,10 +205,13 @@ export type BlePybricksServiceEventStatusReportAction = * Action that represents a status report event received from the hub. * @param statusFlags The status flags. */ -export function statusReportEvent( +export function didReceiveStatusReport( statusFlags: number, ): BlePybricksServiceEventStatusReportAction { - return { type: BlePybricksServiceEventActionType.StatusReport, statusFlags }; + return { + type: BlePybricksServiceEventActionType.DidReceiveStatusReport, + statusFlags, + }; } /** diff --git a/src/ble-pybricks-service/sagas.test.ts b/src/ble-pybricks-service/sagas.test.ts index 8487e067..ec44e30e 100644 --- a/src/ble-pybricks-service/sagas.test.ts +++ b/src/ble-pybricks-service/sagas.test.ts @@ -6,11 +6,11 @@ import { didFailToSendCommand, didFailToWriteCommand, didNotifyEvent, + didReceiveStatusReport, didSendCommand, didWriteCommand, eventProtocolError, sendStopUserProgramCommand, - statusReportEvent, writeCommand, } from './actions'; import { CommandType, ProtocolError } from './protocol'; @@ -101,7 +101,7 @@ describe('event decoder', () => { 0x00, // . 0x00, // flags count MSB ], - statusReportEvent(0x00000001), + didReceiveStatusReport(0x00000001), ], ])('decode %s event', async (_n, message, expected) => { const saga = new AsyncSaga(blePybricksService); diff --git a/src/ble-pybricks-service/sagas.ts b/src/ble-pybricks-service/sagas.ts index ece27928..2f4b81cc 100644 --- a/src/ble-pybricks-service/sagas.ts +++ b/src/ble-pybricks-service/sagas.ts @@ -21,9 +21,9 @@ import { BlePybricksServiceDidNotifyEventAction, BlePybricksServiceDidWriteCommandAction, didFailToSendCommand, + didReceiveStatusReport, didSendCommand, eventProtocolError, - statusReportEvent, writeCommand, } from './actions'; import { @@ -92,7 +92,7 @@ function* decodeResponse(action: BlePybricksServiceDidNotifyEventAction): Genera const responseType = getEventType(action.value); switch (responseType) { case EventType.StatusReport: - yield* put(statusReportEvent(parseStatusReport(action.value))); + yield* put(didReceiveStatusReport(parseStatusReport(action.value))); break; default: throw new ProtocolError( diff --git a/src/hub/reducers.test.ts b/src/hub/reducers.test.ts index ba2153b7..aef743d0 100644 --- a/src/hub/reducers.test.ts +++ b/src/hub/reducers.test.ts @@ -3,7 +3,7 @@ import { firmwareVersion } from '@pybricks/firmware'; import { Action } from '../actions'; -import { statusReportEvent } from '../ble-pybricks-service/actions'; +import { didReceiveStatusReport } from '../ble-pybricks-service/actions'; import { Status, statusToFlag } from '../ble-pybricks-service/protocol'; import { didConnect, didDisconnect } from '../ble/actions'; import { @@ -108,13 +108,13 @@ describe('runtime', () => { ).toBe(HubRuntimeState.Idle); }); - test('statusReportEvent', () => { + test('didReceiveStatusReport', () => { // don't ever expect this to happen in practice since we can't receive // updates while disconnected expect( reducers( { runtime: HubRuntimeState.Disconnected } as State, - statusReportEvent(statusToFlag(Status.UserProgramRunning)), + didReceiveStatusReport(statusToFlag(Status.UserProgramRunning)), ).runtime, ).toBe(HubRuntimeState.Disconnected); @@ -122,7 +122,7 @@ describe('runtime', () => { expect( reducers( { runtime: HubRuntimeState.Loading } as State, - statusReportEvent(statusToFlag(Status.UserProgramRunning)), + didReceiveStatusReport(statusToFlag(Status.UserProgramRunning)), ).runtime, ).toBe(HubRuntimeState.Loading); @@ -130,21 +130,23 @@ describe('runtime', () => { expect( reducers( { runtime: HubRuntimeState.Loaded } as State, - statusReportEvent(statusToFlag(Status.UserProgramRunning)), + didReceiveStatusReport(statusToFlag(Status.UserProgramRunning)), ).runtime, ).toBe(HubRuntimeState.Running); // really short program run finished before receiving download finished expect( - reducers({ runtime: HubRuntimeState.Loaded } as State, statusReportEvent(0)) - .runtime, + reducers( + { runtime: HubRuntimeState.Loaded } as State, + didReceiveStatusReport(0), + ).runtime, ).toBe(HubRuntimeState.Idle); // normal operation - user program stopped expect( reducers( { runtime: HubRuntimeState.Running } as State, - statusReportEvent(0), + didReceiveStatusReport(0), ).runtime, ).toBe(HubRuntimeState.Idle); }); diff --git a/src/hub/reducers.ts b/src/hub/reducers.ts index 934e3d03..d0e88036 100644 --- a/src/hub/reducers.ts +++ b/src/hub/reducers.ts @@ -65,7 +65,7 @@ const runtime: Reducer = ( return state; } return HubRuntimeState.Idle; - case BlePybricksServiceEventActionType.StatusReport: + case BlePybricksServiceEventActionType.DidReceiveStatusReport: // The loading state is determined solely by the IDE, so we can't // let the hub status interfere with it. if (