Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Redux to Mobx #118

Draft
wants to merge 37 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
12bd63a
Add mobx dependency
ewc340 Jun 17, 2021
0d82ab9
Add mobx store skeleton
ewc340 Jun 17, 2021
b78ece7
Remove mobx-react-lite and add mobx-react
ewc340 Jun 17, 2021
bb59063
Add RootStore context and hook
ewc340 Jun 17, 2021
01f880e
Add Dummy component
ewc340 Jun 17, 2021
fbe7282
Include bootstrap updates with mobx migration (#119)
lancejackson Jun 24, 2021
4464f95
Mostly done with converting the reducers
HaileyJang Jun 24, 2021
de8acc1
add fieldStore observable
lancejackson Jun 24, 2021
c498f9b
fix errors
HaileyJang Jul 1, 2021
0242a7e
Merge dev and fix conflicts
ewc340 Jul 1, 2021
aa6425a
Merge branch 'migrate-redux-to-mobx' of https://github.com/pioneers/d…
HaileyJang Jul 1, 2021
2e8e5be
remove all statesprop and dispatch to mobx action functions
HaileyJang Jul 8, 2021
7911573
Commiting to update with dev
HaileyJang Jul 22, 2021
f0f2fe6
Merge branch 'dev' into migrate-redux-to-mobx
HaileyJang Jul 22, 2021
050d65b
Merge branch 'dev' into migrate-redux-to-mobx
HaileyJang Jul 22, 2021
06e216b
Up to what we have. Almost done?
HaileyJang Jul 22, 2021
c228829
Fixing dispatch and getting rid of unnecessary observers
HaileyJang Aug 4, 2021
de6d0bf
Changed everything to boxes. Started working on Peripherals
HaileyJang Aug 6, 2021
c8db2eb
Work on peripheral
HaileyJang Aug 12, 2021
69ecd52
Start on sagas
HaileyJang Aug 19, 2021
20fb3c8
Almost done with editor. Continue working on sftp
HaileyJang Aug 19, 2021
c585644
Keep editing peripheral
HaileyJang Aug 26, 2021
cf7efce
Done with peripherals?
HaileyJang Aug 28, 2021
31943bc
Add sleep function and lint/refactor editor
ewc340 Aug 28, 2021
ebfc803
Lint info store
ewc340 Aug 28, 2021
3c7cb90
Upload 1st draft of transferStudentCode
ewc340 Aug 28, 2021
777494b
Fix merge conflicts
ewc340 Nov 6, 2021
1492bce
Fix observable value in ConsoleStore
ewc340 Nov 6, 2021
6f32b2f
Update store utils
ewc340 Nov 6, 2021
151e111
Refactor and lint stores
ewc340 Nov 6, 2021
c9e5cee
Refactor editor and gamepads, add transferFile to utils
ewc340 Dec 23, 2021
04aa03f
Make ipc channels consts for better search use and robustness
ewc340 Jan 6, 2022
d1b8221
Move utils to separate folders and add Task concept
ewc340 Feb 3, 2022
75739a7
Some store updates
ewc340 Feb 3, 2022
52ead4e
Component updates
ewc340 Feb 3, 2022
4377929
Move editor components to new editor folder
ewc340 Feb 24, 2022
eb0acd1
Small tweaks
ewc340 Feb 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion main/MenuTemplate/HelpMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import RendererBridge from '../RendererBridge';
import showAPI from '../main-process';
import { MenuItemConstructorOptions } from 'electron';
import { ipcChannels } from '../../shared';

const HelpMenu: MenuItemConstructorOptions = {
label: 'Help',
submenu: [
{
label: 'Interactive Tutorial',
click() {
RendererBridge.dispatch('main', 'start-interactive-tour');
RendererBridge.dispatch('main', ipcChannels.START_INTERACTIVE_TOUR);
},
accelerator: 'CommandOrControl+T',
},
Expand Down
5 changes: 3 additions & 2 deletions main/RendererBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import _ from 'lodash';
import { BrowserWindow } from "electron";
import { ipcChannels } from '../shared';

type SingleOrArray<T> = T | T[];

Expand Down Expand Up @@ -58,7 +59,7 @@ class RendererBridge {
const registeredWindow = this.registeredWindows[key];

// Special case for dispatching Redux since we can only dispatch one action at a time
if (channel === 'reduxDispatch') {
if (channel === ipcChannels.REDUX_DISPATCH) {
data = data[0];
}

Expand All @@ -75,7 +76,7 @@ class RendererBridge {
* Redux actions to the main window to avoid large refactors from the current usage of this method.
*/
reduxDispatch = (action: any, windowKeys: SingleOrArray<string> | 'all' | 'main' = 'main') => {
this.dispatch(windowKeys, 'reduxDispatch', action);
this.dispatch(windowKeys, ipcChannels.REDUX_DISPATCH, action);
};
};

Expand Down
23 changes: 14 additions & 9 deletions main/networking/Runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Socket as TCPSocket } from 'net';
import { ipcMain, IpcMainEvent } from 'electron';
import * as protos from '../../protos-main';
import { ipcChannels } from '../../shared';

import RendererBridge from '../RendererBridge';
import { updateConsole } from '../../renderer/actions/ConsoleActions';
Expand Down Expand Up @@ -227,7 +228,7 @@ class RuntimeConnection {
break;

default:
this.logger.log(`Unsupported received message type: ${packet.type}`)
this.logger.log(`Unsupported received message type: ${packet.type}`);
}
}

Expand All @@ -237,13 +238,17 @@ class RuntimeConnection {
/**
* TCP Socket IPC Connections
*/
ipcMain.on('runModeUpdate', (event: IpcMainEvent, ...args: any[]) => this.whenConnectionEstablished(this.sendRunMode, event, ...args));
ipcMain.on('initiateLatencyCheck', (event: IpcMainEvent, ...args: any[]) =>
ipcMain.on(ipcChannels.RUN_MODE_UPDATE, (event: IpcMainEvent, ...args: any[]) =>
this.whenConnectionEstablished(this.sendRunMode, event, ...args)
);
ipcMain.on(ipcChannels.INITIATE_LATENCY_CHECK, (event: IpcMainEvent, ...args: any[]) =>
this.whenConnectionEstablished(this.initiateLatencyCheck, event, ...args)
);
ipcMain.on('stateUpdate', (event: IpcMainEvent, ...args: any[]) => this.whenConnectionEstablished(this.sendInputs, event, ...args));
ipcMain.on(ipcChannels.STATE_UPDATE, (event: IpcMainEvent, ...args: any[]) =>
this.whenConnectionEstablished(this.sendInputs, event, ...args)
);

ipcMain.on('ipAddress', this.ipAddressListener);
ipcMain.on(ipcChannels.IP_ADDRESS, this.ipAddressListener);
}

whenConnectionEstablished = (cb: (event: IpcMainEvent, ...args: any[]) => void, event: IpcMainEvent, ...args: any[]) => {
Expand Down Expand Up @@ -326,10 +331,10 @@ class RuntimeConnection {

close = () => {
this.socket.end();
ipcMain.removeListener('runModeUpdate', this.sendRunMode);
ipcMain.removeListener('ipAddress', this.ipAddressListener);
ipcMain.removeListener('initiateLatencyCheck', this.initiateLatencyCheck);
ipcMain.removeListener('stateUpdate', this.sendInputs);
ipcMain.removeListener(ipcChannels.RUN_MODE_UPDATE, this.sendRunMode);
ipcMain.removeListener(ipcChannels.IP_ADDRESS, this.ipAddressListener);
ipcMain.removeListener(ipcChannels.INITIATE_LATENCY_CHECK, this.initiateLatencyCheck);
ipcMain.removeListener(ipcChannels.STATE_UPDATE, this.sendInputs);
};
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
"json-loader": "0.5.7",
"keymirror": "0.1.1",
"lodash": ">=4.17.21",
"mobx": "^6.3.2",
"mobx-react": "^7.2.0",
"mousetrap": "1.6.1",
"numeral": "2.0.6",
"object-assign": "4.1.1",
Expand Down
31 changes: 9 additions & 22 deletions renderer/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { logging, startLog } from '../utils/utils';
import { FieldControlConfig } from '../types';
import { library } from '@fortawesome/fontawesome-svg-core';
import { fas } from '@fortawesome/free-solid-svg-icons';
import { Observer } from 'mobx-react';
import { useStores } from '../hooks';
import { ipcChannels } from '../../shared';

type ElectronJSONStorage = typeof electronJSONStorage;

Expand All @@ -30,7 +33,6 @@ interface StateProps {
}

interface DispatchProps {
onAlertDone: (id: number) => void;
onFCUpdate: (param: FieldControlConfig) => void;
}

Expand All @@ -41,9 +43,11 @@ export const AppComponent = (props: Props) => {
const [tourRunning, changeTourRunning] = useState(false);
startLog();

const { info, settings, fieldStore } = useStores();

useEffect(() => {
addSteps(joyrideSteps);
ipcRenderer.on('start-interactive-tour', () => {
ipcRenderer.on(ipcChannels.START_INTERACTIVE_TOUR, () => {
startTour();
});
storage.has('firstTime', (hasErr: any, hasKey: any) => {
Expand All @@ -67,7 +71,7 @@ export const AppComponent = (props: Props) => {
return;
}
const fieldControlConfig = data as FieldControlConfig;
props.onFCUpdate(fieldControlConfig);
fieldStore.updateFCConfig(fieldControlConfig);
ipcRenderer.send('FC_CONFIG_CHANGE', fieldControlConfig);
});
}, []);
Expand All @@ -93,19 +97,11 @@ export const AppComponent = (props: Props) => {
}
};

const { runtimeStatus, masterStatus, connectionStatus, isRunningCode } = props;

const bsPrefix = props.globalTheme === 'dark' ? 'text-light bg-dark' : '';

return (
<div className={`${bsPrefix} mt-4`.trim()}>
<DNav
startTour={startTour}
runtimeStatus={runtimeStatus}
masterStatus={masterStatus}
connectionStatus={connectionStatus}
isRunningCode={isRunningCode}
/>
<DNav startTour={startTour} />
<Joyride
steps={steps}
continuous={true}
Expand All @@ -121,13 +117,7 @@ export const AppComponent = (props: Props) => {
}}
/>
<div style={{ height: '35px', marginBottom: '21px' }} />
<Dashboard
{...props}
addSteps={addSteps}
connectionStatus={connectionStatus}
runtimeStatus={runtimeStatus}
isRunningCode={isRunningCode}
/>
<Observer>{() => <Dashboard {...props} addSteps={addSteps} isRunningCode={info.isRunningCode} />}</Observer>
</div>
);
};
Expand All @@ -143,9 +133,6 @@ const mapStateToProps = (state: ApplicationState) => ({
});

const mapDispatchToProps = (dispatch: Dispatch) => ({
onAlertDone: (id: number) => {
dispatch(removeAsyncAlert(id));
},
onFCUpdate: (param: FieldControlConfig) => {
dispatch(updateFieldControl(param));
}
Expand Down
36 changes: 20 additions & 16 deletions renderer/components/ConfigBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { updateFieldControl } from '../actions/FieldActions';
import { ipChange, sshIpChange } from '../actions/InfoActions';
import storage from 'electron-json-storage';
import { Formik } from 'formik';
import { useStores } from '../hooks';
import { Observer } from 'mobx-react';

interface Config {
stationNumber: number;
Expand All @@ -33,30 +35,32 @@ interface OwnProps {
hide: () => void;
}

type Props = StateProps & DispatchProps & OwnProps;
type Props = DispatchProps & OwnProps;
type FormControlElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;


// TODO: lots of changes to be done here
export const ConfigBoxComponent = (props: Props) => {
const [ipAddress, setIPAddress] = useState(props.ipAddress);
const [sshAddress, setSSHAddress] = useState(props.sshAddress);
const [fcAddress, setFCAddress] = useState(props.fcAddress);
const [stationNumber, setStationNumber] = useState(props.stationNumber);
const [originalIPAddress, setOriginalIPAddress] = useState(props.ipAddress);
const [originalSSHAddress, setOriginalSSHAddress] = useState(props.sshAddress);
const [originalStationNumber, setOriginalStationNumber] = useState(props.stationNumber);
const [originalFCAddress, setOriginalFCAddress] = useState(props.fcAddress);

const {info, fieldStore} = useStores();

const [ipAddress, setIPAddress] = useState(info.ipAddress);
const [sshAddress, setSSHAddress] = useState(info.sshAddress);
const [fcAddress, setFCAddress] = useState(props.fcAddress); //TODO
const [stationNumber, setStationNumber] = useState(fieldStore.stationNumber);
const [originalIPAddress, setOriginalIPAddress] = useState(info.ipAddress);
const [originalSSHAddress, setOriginalSSHAddress] = useState(info.sshAddress);
const [originalStationNumber, setOriginalStationNumber] = useState(fieldStore.stationNumber);
const [originalFCAddress, setOriginalFCAddress] = useState(props.fcAddress); //TODO

const saveChanges = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

props.onIPChange(ipAddress);
setOriginalIPAddress(ipAddress);
info.ipChange(ipAddress);
storage.set('ipAddress', { ipAddress }, (err: any) => {
if (err) logging.log(err);
});

props.onSSHAddressChange(sshAddress);
info.sshIpChange(sshAddress);
setOriginalSSHAddress(sshAddress);
storage.set('sshAddress', { sshAddress }, (err: any) => {
if (err) {
Expand All @@ -68,7 +72,7 @@ export const ConfigBoxComponent = (props: Props) => {
stationNumber: stationNumber,
bridgeAddress: fcAddress,
};
props.onFCUpdate(newConfig);
fieldStore.updateFCConfig(newConfig);
setOriginalStationNumber(stationNumber);
setOriginalFCAddress(fcAddress);

Expand Down Expand Up @@ -118,7 +122,7 @@ export const ConfigBoxComponent = (props: Props) => {
} else if (!_.isEmpty(data)) {
const ipAddress = (data as { ipAddress: string | undefined }).ipAddress ?? defaults.IPADDRESS;

props.onIPChange(ipAddress);
info.ipChange(ipAddress);
setIPAddress(ipAddress);
setOriginalIPAddress(ipAddress);
}
Expand All @@ -130,7 +134,7 @@ export const ConfigBoxComponent = (props: Props) => {
} else if (!_.isEmpty(data)) {
const sshAddress = (data as { sshAddress: string | undefined }).sshAddress ?? defaults.IPADDRESS;

props.onSSHAddressChange(sshAddress);
info.sshIpChange(sshAddress);
setSSHAddress(sshAddress);
setOriginalSSHAddress(sshAddress);
}
Expand Down
13 changes: 6 additions & 7 deletions renderer/components/ConsoleOutput.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import React, { useEffect, useRef } from 'react';
import { Card } from 'react-bootstrap';

interface StateProps {
toggleConsole: () => void;
disableScroll: boolean;
}
import { useStores } from '../hooks';

interface OwnProps {
toggleConsole: () => void;
height: number;
output: string[];
show: boolean;
}

type Props = StateProps & OwnProps;
type Props = OwnProps;

export function ConsoleOutput(props: Props) {
let outerDiv: HTMLDivElement | HTMLPreElement | null;
const { show, output } = props;

const { console } = useStores();

const prevOutputRef = useRef([] as string[]);
const prevOutput = prevOutputRef.current;
useEffect(() => {
Expand All @@ -29,7 +28,7 @@ export function ConsoleOutput(props: Props) {
});

const scrollToBottom = () => {
if (!props.disableScroll) {
if (!console.disableScroll.get()) {
if (outerDiv !== null) {
outerDiv.scrollTop = outerDiv.scrollHeight;
}
Expand Down
Loading