Skip to content

Commit

Permalink
refactor: Use redux thunk for rust mode
Browse files Browse the repository at this point in the history
  • Loading branch information
marcustyphoon committed Oct 25, 2024
1 parent ca8252e commit b26ac8d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/components/nav/NavSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default function NavSettings({
dispatch(stopCalc);
} else {
// workers.forEach(({ worker }) => worker.postMessage({ type: STOP }));
stopCalculationParallel(dispatch);
dispatch(stopCalculationParallel);
}

const newMulticore = e.target.checked;
Expand Down
8 changes: 3 additions & 5 deletions src/components/sections/controls/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ErrorIcon from '@mui/icons-material/Error';
import HourglassEmptyIcon from '@mui/icons-material/HourglassEmpty';
import { Box, Button, Chip, Typography } from '@mui/material';
import { Trans, useTranslation } from 'react-i18next';
import { useSelector, useStore } from 'react-redux';
import { useSelector } from 'react-redux';
import { makeStyles } from 'tss-react/mui';
import { resumeCalc, startCalc, stopCalc } from '../../../state/async/calculationThunks';
import {
Expand All @@ -31,7 +31,6 @@ import {
getStatus,
} from '../../../state/slices/controlsSlice';
import { getAffixes } from '../../../state/slices/priorities';
import type { RootState } from '../../../state/store';
import ProgressIcon from '../../baseComponents/ProgressIcon';
import ResultTableSettings from './ResultTableSettings';

Expand All @@ -55,7 +54,6 @@ const ControlsBox = () => {
const { classes, cx } = useStyles();
const dispatch = useAppDispatch();
const { t } = useTranslation();
const store = useStore();

const status = useSelector(getStatus);
const error = useSelector(getError);
Expand All @@ -76,7 +74,7 @@ const ControlsBox = () => {
dispatch(changeError(''));
dispatch(startCalc);
} else {
calculateParallel(store.getState() as RootState, dispatch);
dispatch(calculateParallel);
}
};

Expand All @@ -94,7 +92,7 @@ const ControlsBox = () => {
dispatch(stopCalc);
} else {
// workers.forEach(({ worker }) => worker.postMessage({ type: STOP }));
stopCalculationParallel(dispatch);
dispatch(stopCalculationParallel);
}
};

Expand Down
13 changes: 6 additions & 7 deletions src/state/optimizer-parallel/calculate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { STOPPED } from '../optimizer/status';
import type { AppThunk } from '../redux-hooks';
import {
changeFilteredLists,
changeList,
Expand All @@ -7,7 +8,6 @@ import {
getHeuristics,
getHwThreads,
} from '../slices/controlsSlice';
import type { AppDispatch, RootState } from '../store';
import runCalcHeuristics from './modes/heuristics';
import runCalcNormal from './modes/normal';
import { createCalculationSettings, setupNormal } from './optimizerSetup';
Expand Down Expand Up @@ -40,7 +40,8 @@ const terminateActiveWorkers = () => {
});
};

export function calculateParallel(reduxState: RootState, dispatch: AppDispatch): WorkerWrapper[] {
export const calculateParallel: AppThunk = (dispatch, getState) => {
const reduxState = getState();
const selectedMaxThreads = getHwThreads(reduxState);

dispatch(changeList([]));
Expand Down Expand Up @@ -91,11 +92,9 @@ export function calculateParallel(reduxState: RootState, dispatch: AppDispatch):
false,
);
}
};

return workers;
}

export function stopCalculationParallel(dispatch: AppDispatch) {
export const stopCalculationParallel: AppThunk = (dispatch) => {
terminateActiveWorkers();
dispatch(changeStatus(STOPPED));
}
};

0 comments on commit b26ac8d

Please sign in to comment.