mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
import { combineEpics, Epic, ofType } from 'redux-observable';
|
|
import { Subject } from 'rxjs';
|
|
import { map, tap, ignoreElements } from 'rxjs/operators';
|
|
import { write, BLEThunkAction } 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(TerminalDataActionType.ReceivedData),
|
|
map((a: TerminalDataAction): BLEThunkAction => 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(TerminalDataActionType.SendData),
|
|
tap((a: TerminalDataAction): void => terminalOutputSubject.next(a.value)),
|
|
ignoreElements(),
|
|
);
|
|
|
|
export default combineEpics(receiveTerminalData, sendTerminalData);
|