convert terminal from epic to saga

This commit is contained in:
David Lechner
2020-06-10 21:59:34 -05:00
committed by David Lechner
parent 314904605c
commit e09fd8713f
11 changed files with 264 additions and 52 deletions
+1 -2
View File
@@ -5,10 +5,9 @@ import { Epic, combineEpics } from 'redux-observable';
import { catchError } from 'rxjs/operators';
import ble from './ble';
import hub from './hub';
import terminal from './terminal';
const rootEpic: Epic = (action$, store$, dependencies) =>
combineEpics(ble, hub, terminal)(action$, store$, dependencies).pipe(
combineEpics(ble, hub)(action$, store$, dependencies).pipe(
catchError((error, source) => {
console.error(error);
return source;
-32
View File
@@ -1,32 +0,0 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
import { AnyAction } from 'redux';
import { Epic, combineEpics, ofType } from 'redux-observable';
import { Subject } from 'rxjs';
import { ignoreElements, map, tap } from 'rxjs/operators';
import { write } from '../actions/ble';
import { TerminalDataAction, TerminalDataActionType } from '../actions/terminal';
const encoder = new TextEncoder();
const terminalOutputSubject = new Subject<string>();
export const terminalOutput = terminalOutputSubject.asObservable();
// When terminal has focus this receives the input. All input is sent over BLE connection.
const receiveTerminalData: Epic = (action$) =>
action$.pipe(
ofType<AnyAction, TerminalDataAction>(TerminalDataActionType.ReceivedData),
map((a) => write(encoder.encode(a.value))),
);
// Request to write to the terminal are handled by an observable rather than
// using redux state.
const sendTerminalData: Epic = (action$) =>
action$.pipe(
ofType<AnyAction, TerminalDataAction>(TerminalDataActionType.SendData),
tap((a) => terminalOutputSubject.next(a.value)),
ignoreElements(),
);
export default combineEpics(receiveTerminalData, sendTerminalData);