Skip to content

Commit 31e59ed

Browse files
committed
Rewrite code reducer in RTK
1 parent 09ac072 commit 31e59ed

File tree

8 files changed

+44
-68
lines changed

8 files changed

+44
-68
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module.exports = {
7272
'editor/AceEditor.tsx',
7373
'editor/SimpleEditor.tsx',
7474
'reducers/client.ts',
75+
'reducers/code.ts',
7576
'reducers/crates.ts',
7677
'reducers/featureFlags.ts',
7778
'reducers/globalConfiguration.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ node_modules
2323
!editor/AceEditor.tsx
2424
!editor/SimpleEditor.tsx
2525
!reducers/client.ts
26+
!reducers/code.ts
2627
!reducers/crates.ts
2728
!reducers/featureFlags.ts
2829
!reducers/globalConfiguration.ts

ui/frontend/Output/Execute.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { useCallback } from 'react';
22
import { useSelector, useDispatch } from 'react-redux';
33

4-
import * as actions from '../actions';
54
import * as selectors from '../selectors';
65
import { State } from '../reducers';
6+
import * as code from '../reducers/code';
77

88
import Section from './Section';
99
import SimplePane from './SimplePane';
@@ -15,7 +15,7 @@ const Execute: React.FC = () => {
1515
const isAutoBuild = useSelector(selectors.isAutoBuildSelector);
1616

1717
const dispatch = useDispatch();
18-
const addMainFunction = useCallback(() => dispatch(actions.addMainFunction()), [dispatch]);
18+
const addMainFunction = useCallback(() => dispatch(code.addMainFunction()), [dispatch]);
1919

2020
return (
2121
<SimplePane {...details} kind="execute">

ui/frontend/actions.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { performCompileToLlvmIrOnly } from './reducers/output/llvmIr';
3232
import { performCompileToMirOnly } from './reducers/output/mir';
3333
import { performCompileToWasmOnly } from './reducers/output/wasm';
3434
import { navigateToHelp, navigateToIndex } from './reducers/page';
35+
import { addCrateType, editCode } from './reducers/code';
3536

3637
export type ThunkAction<T = void> = ReduxThunkAction<T, State, {}, Action>;
3738
export type SimpleThunkAction<T = void> = ReduxThunkAction<T, State, {}, AnyAction>;
@@ -56,11 +57,6 @@ export enum ActionType {
5657
ChangeMode = 'CHANGE_MODE',
5758
ChangeEdition = 'CHANGE_EDITION',
5859
ChangeBacktrace = 'CHANGE_BACKTRACE',
59-
EditCode = 'EDIT_CODE',
60-
AddMainFunction = 'ADD_MAIN_FUNCTION',
61-
AddImport = 'ADD_IMPORT',
62-
AddCrateType = 'ADD_CRATE_TYPE',
63-
EnableFeatureGate = 'ENABLE_FEATURE_GATE',
6460
SelectText = 'SELECT_TEXT',
6561
NotificationSeen = 'NOTIFICATION_SEEN',
6662
BrowserWidthChanged = 'BROWSER_WIDTH_CHANGED',
@@ -196,21 +192,6 @@ export const performCompileToNightlyHir =
196192
export const performCompileToWasm =
197193
performAndSwitchPrimaryAction(performCompileToCdylibWasmOnly, PrimaryActionCore.Wasm);
198194

199-
export const editCode = (code: string) =>
200-
createAction(ActionType.EditCode, { code });
201-
202-
export const addMainFunction = () =>
203-
createAction(ActionType.AddMainFunction);
204-
205-
export const addImport = (code: string) =>
206-
createAction(ActionType.AddImport, { code });
207-
208-
export const addCrateType = (crateType: string) =>
209-
createAction(ActionType.AddCrateType, { crateType });
210-
211-
export const enableFeatureGate = (featureGate: string) =>
212-
createAction(ActionType.EnableFeatureGate, { featureGate });
213-
214195
export const selectText = (start: Position, end: Position) =>
215196
createAction(ActionType.SelectText, { start, end });
216197

@@ -323,14 +304,11 @@ export type Action =
323304
| ReturnType<typeof changeProcessAssembly>
324305
| ReturnType<typeof changeAceTheme>
325306
| ReturnType<typeof changeMonacoTheme>
326-
| ReturnType<typeof editCode>
327-
| ReturnType<typeof addMainFunction>
328-
| ReturnType<typeof addImport>
329-
| ReturnType<typeof addCrateType>
330-
| ReturnType<typeof enableFeatureGate>
331307
| ReturnType<typeof selectText>
332308
| ReturnType<typeof notificationSeen>
333309
| ReturnType<typeof browserWidthChanged>
310+
| ReturnType<typeof editCode>
311+
| ReturnType<typeof addCrateType>
334312
| ReturnType<typeof navigateToIndex>
335313
| ReturnType<typeof wsExecuteRequest>
336314
;

ui/frontend/editor/Editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import MonacoEditor from './MonacoEditor';
1010
import { Editor as EditorType } from '../types';
1111
import { codeSelector, positionSelector, selectionSelector } from '../selectors';
1212
import { State } from '../reducers';
13+
import { editCode } from '../reducers/code';
1314

1415
import styles from './Editor.module.css';
1516

@@ -28,7 +29,7 @@ const Editor: React.FC = () => {
2829

2930
const dispatch = useAppDispatch();
3031
const execute = useCallback(() => dispatch(actions.performPrimaryAction()), [dispatch]);
31-
const onEditCode = useCallback((c: string) => dispatch(actions.editCode(c)), [dispatch]);
32+
const onEditCode = useCallback((c: string) => dispatch(editCode(c)), [dispatch]);
3233

3334
const SelectedEditor = editorMap[editor];
3435

ui/frontend/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import { Provider } from 'react-redux';
99
import { v4 } from 'uuid';
1010

1111
import {
12-
editCode,
13-
enableFeatureGate,
1412
selectText,
15-
addImport,
1613
reExecuteWithBacktrace,
1714
browserWidthChanged,
1815
} from './actions';
@@ -27,6 +24,7 @@ import configureStore from './configureStore';
2724
import { performVersionsLoad } from './reducers/versions';
2825
import { performCratesLoad } from './reducers/crates';
2926
import { gotoPosition } from './reducers/position';
27+
import { addImport, editCode, enableFeatureGate } from './reducers/code';
3028

3129
const store = configureStore(window);
3230

ui/frontend/reducers/code.ts

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
1-
import { Action, ActionType } from '../actions';
2-
import { performGistLoad } from './output/gist'
3-
import { performFormat } from './output/format'
1+
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
42

5-
const DEFAULT: State = `fn main() {
3+
import { performFormat } from './output/format';
4+
import { performGistLoad } from './output/gist';
5+
6+
const initialState: string = `fn main() {
67
println!("Hello, world!");
78
}`;
89

9-
export type State = string;
10-
11-
export default function code(state = DEFAULT, action: Action): State {
12-
switch (action.type) {
13-
case ActionType.EditCode:
14-
return action.code;
15-
16-
case ActionType.AddMainFunction:
17-
return `${state}\n\n${DEFAULT}`;
18-
19-
case ActionType.AddImport:
20-
return action.code + state;
21-
22-
case ActionType.AddCrateType:
23-
return `#![crate_type = "${action.crateType}"]\n${state}`;
24-
25-
case ActionType.EnableFeatureGate:
26-
return `#![feature(${action.featureGate})]\n${state}`;
27-
28-
default: {
29-
if (performGistLoad.pending.match(action)) {
30-
return '';
31-
} else if (performGistLoad.fulfilled.match(action)) {
32-
return action.payload.code;
33-
} else if (performFormat.fulfilled.match(action)) {
34-
return action.payload.code;
35-
} else {
36-
return state;
37-
}
38-
}
39-
}
40-
}
10+
const slice = createSlice({
11+
name: 'code',
12+
initialState,
13+
reducers: {
14+
editCode: (_state, action: PayloadAction<string>) => action.payload,
15+
16+
addMainFunction: (state) => `${state}\n\n${initialState}`,
17+
18+
addImport: (state, action: PayloadAction<string>) => action.payload + state,
19+
20+
addCrateType: (state, action: PayloadAction<string>) =>
21+
`#![crate_type = "${action.payload}"]\n${state}`,
22+
23+
enableFeatureGate: (state, action: PayloadAction<string>) =>
24+
`#![feature(${action.payload})]\n${state}`,
25+
},
26+
extraReducers: (builder) => {
27+
builder
28+
.addCase(performGistLoad.pending, () => '')
29+
.addCase(performGistLoad.fulfilled, (_state, action) => action.payload.code)
30+
.addCase(performFormat.fulfilled, (_state, action) => action.payload.code);
31+
},
32+
});
33+
34+
export const { editCode, addMainFunction, addImport, addCrateType, enableFeatureGate } =
35+
slice.actions;
36+
37+
export default slice.reducer;

ui/frontend/selectors/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { editCode } from '../actions';
21
import reducer from '../reducers';
2+
import { editCode } from '../reducers/code';
33

44
import { hasMainFunctionSelector } from './index';
55

0 commit comments

Comments
 (0)