diff --git a/src/actions/bootloader.ts b/src/actions/bootloader.ts index d003b4c4..bc9d7427 100644 --- a/src/actions/bootloader.ts +++ b/src/actions/bootloader.ts @@ -118,6 +118,20 @@ export function didDisconnect(): BootloaderConnectionDidDisconnectAction { return { type: BootloaderConnectionActionType.DidDisconnect }; } +/** + * Common type for all bootloader connection actions. + */ +export type BootloaderConnectionAction = + | BootloaderConnectionConnectAction + | BootloaderConnectionDidConnectAction + | BootloaderConnectionDidCancelAction + | BootloaderConnectionDidErrorAction + | BootloaderConnectionSendAction + | BootloaderConnectionDidSendAction + | BootloaderConnectionDidSendAction + | BootloaderConnectionDidReceiveAction + | BootloaderConnectionDidDisconnectAction; + /** * Bootloader request actions for sending commands over the connection. */ @@ -414,6 +428,18 @@ export function errorResponse(command: Command): BootloaderErrorResponseAction { return { type: BootloaderResponseActionType.Error, command }; } +/** + * Common type for all bootloader response actions. + */ +export type BootloaderResponseAction = + | BootloaderEraseResponseAction + | BootloaderProgramResponseAction + | BootloaderInitResponseAction + | BootloaderInfoResponseAction + | BootloaderChecksumResponseAction + | BootloaderStateResponseAction + | BootloaderErrorResponseAction; + /** * High-level bootloader actions. */ @@ -464,6 +490,9 @@ export function progress( return { type: BootloaderActionType.FlashProgress, complete, total }; } +/** + * Common type for all high-level bootloader actions. + */ export type BootloaderAction = | BootloaderFlashFirmwareAction | BootloaderFlashProgressAction; diff --git a/src/actions/editor.ts b/src/actions/editor.ts index bd9377d2..3c78ecda 100644 --- a/src/actions/editor.ts +++ b/src/actions/editor.ts @@ -60,3 +60,8 @@ export interface EditorOpenAction extends Action { export function open(data: ArrayBuffer): EditorOpenAction { return { type: EditorActionType.Open, data }; } + +/** + * Common type for all editor actions. + */ +export type EditorAction = CurrentEditorAction | EditorOpenAction | EditorSaveAction; diff --git a/src/actions/index.ts b/src/actions/index.ts new file mode 100644 index 00000000..4a1969a4 --- /dev/null +++ b/src/actions/index.ts @@ -0,0 +1,29 @@ +import { Dispatch as ReduxDispatch } from 'redux'; +import { + BootloaderAction, + BootloaderConnectionAction, + BootloaderDidRequestAction, + BootloaderRequestAction, + BootloaderResponseAction, +} from './bootloader'; +import { EditorAction } from './editor'; +import { NotificationAction } from './notification'; +import { TerminalDataAction } from './terminal'; + +/** + * Common type for all actions. + */ +export type Action = + | BootloaderConnectionAction + | BootloaderRequestAction + | BootloaderDidRequestAction + | BootloaderResponseAction + | BootloaderAction + | EditorAction + | NotificationAction + | TerminalDataAction; + +/** + * Dispatch function. + */ +export type Dispatch = ReduxDispatch; diff --git a/src/components/FlashButton.tsx b/src/components/FlashButton.tsx index a041aa8f..5135d0ac 100644 --- a/src/components/FlashButton.tsx +++ b/src/components/FlashButton.tsx @@ -2,7 +2,7 @@ // Copyright (c) 2020 The Pybricks Authors import { connect } from 'react-redux'; -import { Dispatch } from 'redux'; +import { Dispatch } from '../actions'; import { flashFirmware } from '../actions/bootloader'; import * as notification from '../actions/notification'; import { RootState } from '../reducers'; diff --git a/src/components/LoadButton.tsx b/src/components/LoadButton.tsx index 69f22d95..392aef63 100644 --- a/src/components/LoadButton.tsx +++ b/src/components/LoadButton.tsx @@ -2,7 +2,7 @@ // Copyright (c) 2020 The Pybricks Authors import { connect } from 'react-redux'; -import { Dispatch } from 'redux'; +import { Dispatch } from '../actions'; import * as editor from '../actions/editor'; import * as notification from '../actions/notification'; import { RootState } from '../reducers'; diff --git a/src/components/Notification.tsx b/src/components/Notification.tsx index 342cd3e7..a4352b13 100644 --- a/src/components/Notification.tsx +++ b/src/components/Notification.tsx @@ -4,7 +4,7 @@ import React from 'react'; import Toast from 'react-bootstrap/Toast'; import { connect } from 'react-redux'; -import { Dispatch } from 'redux'; +import { Dispatch } from '../actions'; import * as notification from '../actions/notification'; interface DispatchProps { diff --git a/src/components/SaveButton.tsx b/src/components/SaveButton.tsx index 7c06b509..844adb34 100644 --- a/src/components/SaveButton.tsx +++ b/src/components/SaveButton.tsx @@ -2,7 +2,7 @@ // Copyright (c) 2020 The Pybricks Authors import { connect } from 'react-redux'; -import { Dispatch } from 'redux'; +import { Dispatch } from '../actions'; import * as editor from '../actions/editor'; import { RootState } from '../reducers'; import ActionButton, { ActionButtonProps } from './ActionButton'; diff --git a/src/components/Terminal.tsx b/src/components/Terminal.tsx index 6505657b..ca370063 100644 --- a/src/components/Terminal.tsx +++ b/src/components/Terminal.tsx @@ -4,10 +4,10 @@ import React from 'react'; import { connect } from 'react-redux'; import ResizeObserver from 'react-resize-observer'; -import { Dispatch } from 'redux'; import { Subscription } from 'rxjs'; import { Terminal as XTerm } from 'xterm'; import { FitAddon } from 'xterm-addon-fit'; +import { Dispatch } from '../actions'; import { receiveData } from '../actions/terminal'; import { terminalOutput } from '../epics/terminal'; diff --git a/src/reducers/bootloader.ts b/src/reducers/bootloader.ts index 63abc335..4cd7f41a 100644 --- a/src/reducers/bootloader.ts +++ b/src/reducers/bootloader.ts @@ -2,6 +2,7 @@ // Copyright (c) 2020 The Pybricks Authors import { Reducer, combineReducers } from 'redux'; +import { Action } from '../actions'; import { BootloaderConnectionActionType, BootloaderRequestActionType, @@ -30,7 +31,7 @@ export enum BootloaderConnectionState { Disconnecting = 'bootloader.connection.disconnecting', } -const connection: Reducer = ( +const connection: Reducer = ( state = BootloaderConnectionState.Disconnected, action, ) => { @@ -124,7 +125,7 @@ export enum FirmwareFlashState { Error = 'bootloader.flash.error', } -const flash: Reducer = ( +const flash: Reducer = ( state = FirmwareFlashState.EndDisconnect, action, ) => { diff --git a/src/reducers/editor.ts b/src/reducers/editor.ts index 12cb77d0..111869de 100644 --- a/src/reducers/editor.ts +++ b/src/reducers/editor.ts @@ -3,14 +3,12 @@ import { Ace } from 'ace-builds'; import { Reducer, combineReducers } from 'redux'; -import { CurrentEditorAction, EditorActionType } from '../actions/editor'; +import { Action } from '../actions'; +import { EditorActionType } from '../actions/editor'; type CurrentEditSession = Ace.EditSession | null; -const current: Reducer = ( - state = null, - action, -) => { +const current: Reducer = (state = null, action) => { switch (action.type) { case EditorActionType.Current: return action.editSession || null; diff --git a/src/sagas/bootloader.test.ts b/src/sagas/bootloader.test.ts index f8132b16..7e05a1d6 100644 --- a/src/sagas/bootloader.test.ts +++ b/src/sagas/bootloader.test.ts @@ -1,8 +1,8 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020 The Pybricks Authors -import { Action } from 'redux'; import { runSaga, stdChannel } from 'redux-saga'; +import { Action } from '../actions'; import { BootloaderRequestActionType, checksumRequest, diff --git a/src/sagas/bootloader.ts b/src/sagas/bootloader.ts index a6861e47..fc8ad8ba 100644 --- a/src/sagas/bootloader.ts +++ b/src/sagas/bootloader.ts @@ -4,7 +4,6 @@ import cPlusHubZip from '@pybricks/firmware/build/cplushub.zip'; import moveHubZip from '@pybricks/firmware/build/movehub.zip'; import JSZip from 'jszip'; -import { Action } from 'redux'; import { Channel, buffers } from 'redux-saga'; import { Effect, @@ -18,6 +17,7 @@ import { take, takeEvery, } from 'redux-saga/effects'; +import { Action } from '../actions'; import { BootloaderActionType, BootloaderChecksumResponseAction, @@ -38,6 +38,7 @@ import { BootloaderProgramResponseAction, BootloaderRequestAction, BootloaderRequestActionType, + BootloaderResponseAction, BootloaderResponseActionType, checksumRequest, checksumResponse, @@ -95,13 +96,15 @@ const firmwareZipMap = new Map([ /** * Converts a request action into bytecodes and creates a new action to send * the bytecodes to to the device. - * @param action The request action that was observed. */ function* encodeRequest(): Generator { // Using a while loop to serialize sending data to avoid "busy" errors. const chan = (yield actionChannel( - (a: Action) => Object.values(BootloaderRequestActionType).includes(a.type), + (a: Action) => + Object.values(BootloaderRequestActionType).includes( + a.type as BootloaderRequestActionType, + ), buffers.expanding(), )) as Channel; while (true) { @@ -200,7 +203,7 @@ function* decodeResponse(action: BootloaderConnectionDidReceiveAction): Generato /** * Helper type for return value of wait() function. */ -type WaitResponse> = [ +type WaitResponse = [ T, BootloaderErrorResponseAction, boolean, diff --git a/src/services/bootloader.ts b/src/services/bootloader.ts index 37899a9f..b8302fa6 100644 --- a/src/services/bootloader.ts +++ b/src/services/bootloader.ts @@ -1,10 +1,10 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020 The Pybricks Authors -import { Action, Dispatch } from 'redux'; +import { Action, Dispatch } from '../actions'; + import { BootloaderConnectionActionType, - BootloaderConnectionSendAction, didCancel, didConnect, didDisconnect, @@ -130,11 +130,10 @@ async function send(action: Action, dispatch: Dispatch): Promise { if (!char) { throw Error('Not connected'); } - const sendAction = action as BootloaderConnectionSendAction; - if (sendAction.withResponse) { - await char.xWriteValueWithResponse(sendAction.data); + if (action.withResponse) { + await char.xWriteValueWithResponse(action.data); } else { - await char.xWriteValueWithoutResponse(sendAction.data); + await char.xWriteValueWithoutResponse(action.data); } dispatch(didSend()); } catch (err) { diff --git a/src/services/editor.ts b/src/services/editor.ts index f55b9e04..722a49b4 100644 --- a/src/services/editor.ts +++ b/src/services/editor.ts @@ -2,7 +2,7 @@ // Copyright (c) 2020 The Pybricks Authors import * as FileSaver from 'file-saver'; -import { Action, Dispatch } from 'redux'; +import { Action, Dispatch } from '../actions'; import { EditorActionType, EditorOpenAction } from '../actions/editor'; import { RootState } from '../reducers'; import { combineServices } from '.'; diff --git a/src/services/index.ts b/src/services/index.ts index 14546cda..73c52385 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,7 +1,8 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020 The Pybricks Authors -import { Action, Dispatch, Middleware } from 'redux'; +import { Middleware } from 'redux'; +import { Action, Dispatch } from '../actions'; import { RootState } from '../reducers'; import bootloader from './bootloader'; import editor from './editor';