Skip to content

Commit 167a389

Browse files
authored
Merge pull request #1013 from rust-lang/simplify-actions
Simplify the frontend actions
2 parents 5c97d92 + 819ec60 commit 167a389

File tree

13 files changed

+114
-136
lines changed

13 files changed

+114
-136
lines changed

ui/frontend/.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ module.exports = {
6767
'Header.tsx',
6868
'PopButton.tsx',
6969
'Stdin.tsx',
70+
'actions.ts',
7071
'api.ts',
7172
'compileActions.ts',
7273
'editor/AceEditor.tsx',
@@ -96,6 +97,7 @@ module.exports = {
9697
'reducers/selection.ts',
9798
'reducers/versions.ts',
9899
'reducers/websocket.ts',
100+
'types.ts',
99101
'websocketActions.ts',
100102
'websocketMiddleware.ts',
101103
],

ui/frontend/.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ node_modules
1818
!Header.tsx
1919
!PopButton.tsx
2020
!Stdin.tsx
21+
!actions.ts
2122
!api.ts
2223
!compileActions.ts
2324
!editor/AceEditor.tsx
@@ -47,5 +48,6 @@ node_modules
4748
!reducers/selection.ts
4849
!reducers/versions.ts
4950
!reducers/websocket.ts
51+
!types.ts
5052
!websocketActions.ts
5153
!websocketMiddleware.ts

ui/frontend/Router.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { AnyAction } from '@reduxjs/toolkit';
23

34
import { createBrowserHistory as createHistory, Path, Location } from 'history';
45
import { createRouter, PlainOrThunk } from './uss-router';
@@ -64,7 +65,7 @@ const stateToLocation = ({ page, configuration, output }: Substate): Partial<Pat
6465
}
6566
};
6667

67-
const locationToAction = (location: Location): PlainOrThunk<State, actions.Action> | null => {
68+
const locationToAction = (location: Location): PlainOrThunk<State, AnyAction> | null => {
6869
const matchedHelp = helpRoute.match(location.pathname);
6970

7071
if (matchedHelp) {

ui/frontend/actions.ts

Lines changed: 67 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
import { ThunkAction as ReduxThunkAction, AnyAction } from '@reduxjs/toolkit';
1+
import { AnyAction, ThunkAction as ReduxThunkAction } from '@reduxjs/toolkit';
22

3+
import { addCrateType, editCode } from './reducers/code';
34
import {
4-
getCrateType,
5-
runAsTest,
6-
wasmLikelyToWork,
7-
} from './selectors';
5+
changeBacktrace,
6+
changeChannel,
7+
changeEditionRaw,
8+
changeMode,
9+
changePrimaryAction,
10+
} from './reducers/configuration';
11+
import { performCompileToAssemblyOnly } from './reducers/output/assembly';
12+
import { performCommonExecute } from './reducers/output/execute';
13+
import { performGistLoad } from './reducers/output/gist';
14+
import { performCompileToHirOnly } from './reducers/output/hir';
15+
import { performCompileToLlvmIrOnly } from './reducers/output/llvmIr';
16+
import { performCompileToMirOnly } from './reducers/output/mir';
17+
import { performCompileToWasmOnly } from './reducers/output/wasm';
18+
import { navigateToHelp, navigateToIndex } from './reducers/page';
19+
import { getCrateType, runAsTest, wasmLikelyToWork } from './selectors';
820
import State from './state';
921
import {
1022
Backtrace,
@@ -14,47 +26,20 @@ import {
1426
PrimaryAction,
1527
PrimaryActionAuto,
1628
PrimaryActionCore,
29+
parseChannel,
30+
parseEdition,
31+
parseMode,
1732
} from './types';
1833

19-
import { performCommonExecute, wsExecuteRequest } from './reducers/output/execute';
20-
import { performGistLoad } from './reducers/output/gist';
21-
import { performCompileToAssemblyOnly } from './reducers/output/assembly';
22-
import { performCompileToHirOnly } from './reducers/output/hir';
23-
import { performCompileToLlvmIrOnly } from './reducers/output/llvmIr';
24-
import { performCompileToMirOnly } from './reducers/output/mir';
25-
import { performCompileToWasmOnly } from './reducers/output/wasm';
26-
import { navigateToHelp, navigateToIndex } from './reducers/page';
27-
import { addCrateType, editCode } from './reducers/code';
28-
import {
29-
changeBacktrace,
30-
changeChannel,
31-
changeEditionRaw,
32-
changeMode,
33-
changePrimaryAction,
34-
} from './reducers/configuration';
35-
36-
export type ThunkAction<T = void> = ReduxThunkAction<T, State, {}, Action>;
37-
export type SimpleThunkAction<T = void> = ReduxThunkAction<T, State, {}, AnyAction>;
38-
39-
const createAction = <T extends string, P extends {}>(type: T, props?: P) => (
40-
Object.assign({ type }, props)
41-
);
42-
43-
export enum ActionType {
44-
InitializeApplication = 'INITIALIZE_APPLICATION',
45-
}
46-
47-
export const initializeApplication = () => createAction(ActionType.InitializeApplication);
34+
export type ThunkAction<T = void> = ReduxThunkAction<T, State, {}, AnyAction>;
4835

49-
50-
export const reExecuteWithBacktrace = (): ThunkAction => dispatch => {
36+
export const reExecuteWithBacktrace = (): ThunkAction => (dispatch) => {
5137
dispatch(changeBacktrace(Backtrace.Enabled));
5238
dispatch(performExecuteOnly());
5339
};
5440

55-
5641
function performAutoOnly(): ThunkAction {
57-
return function(dispatch, getState) {
42+
return function (dispatch, getState) {
5843
const state = getState();
5944
const crateType = getCrateType(state);
6045
const tests = runAsTest(state);
@@ -71,7 +56,7 @@ const performTestOnly = (): ThunkAction => (dispatch, getState) => {
7156
return dispatch(performCommonExecute(crateType, true));
7257
};
7358

74-
const performCompileToNightlyHirOnly = (): ThunkAction => dispatch => {
59+
const performCompileToNightlyHirOnly = (): ThunkAction => (dispatch) => {
7560
dispatch(changeChannel(Channel.Nightly));
7661
dispatch(performCompileToHirOnly());
7762
};
@@ -103,75 +88,56 @@ export const performPrimaryAction = (): ThunkAction => (dispatch, getState) => {
10388
dispatch(primaryAction());
10489
};
10590

106-
const performAndSwitchPrimaryAction = (inner: () => ThunkAction, id: PrimaryAction) => (): ThunkAction => dispatch => {
107-
dispatch(changePrimaryAction(id));
108-
dispatch(inner());
109-
};
110-
111-
export const performExecute =
112-
performAndSwitchPrimaryAction(performExecuteOnly, PrimaryActionCore.Execute);
113-
export const performCompile =
114-
performAndSwitchPrimaryAction(performCompileOnly, PrimaryActionCore.Compile);
115-
export const performTest =
116-
performAndSwitchPrimaryAction(performTestOnly, PrimaryActionCore.Test);
117-
export const performCompileToAssembly =
118-
performAndSwitchPrimaryAction(performCompileToAssemblyOnly, PrimaryActionCore.Asm);
119-
export const performCompileToLLVM =
120-
performAndSwitchPrimaryAction(performCompileToLlvmIrOnly, PrimaryActionCore.LlvmIr);
121-
export const performCompileToMir =
122-
performAndSwitchPrimaryAction(performCompileToMirOnly, PrimaryActionCore.Mir);
123-
export const performCompileToNightlyHir =
124-
performAndSwitchPrimaryAction(performCompileToNightlyHirOnly, PrimaryActionCore.Hir);
125-
export const performCompileToWasm =
126-
performAndSwitchPrimaryAction(performCompileToCdylibWasmOnly, PrimaryActionCore.Wasm);
127-
128-
function parseChannel(s?: string): Channel | null {
129-
switch (s) {
130-
case 'stable':
131-
return Channel.Stable;
132-
case 'beta':
133-
return Channel.Beta;
134-
case 'nightly':
135-
return Channel.Nightly;
136-
default:
137-
return null;
138-
}
139-
}
140-
141-
function parseMode(s?: string): Mode | null {
142-
switch (s) {
143-
case 'debug':
144-
return Mode.Debug;
145-
case 'release':
146-
return Mode.Release;
147-
default:
148-
return null;
149-
}
150-
}
91+
const performAndSwitchPrimaryAction =
92+
(inner: () => ThunkAction, id: PrimaryAction) => (): ThunkAction => (dispatch) => {
93+
dispatch(changePrimaryAction(id));
94+
dispatch(inner());
95+
};
15196

152-
function parseEdition(s?: string): Edition | null {
153-
switch (s) {
154-
case '2015':
155-
return Edition.Rust2015;
156-
case '2018':
157-
return Edition.Rust2018;
158-
case '2021':
159-
return Edition.Rust2021;
160-
case '2024':
161-
return Edition.Rust2024;
162-
default:
163-
return null;
164-
}
165-
}
97+
export const performExecute = performAndSwitchPrimaryAction(
98+
performExecuteOnly,
99+
PrimaryActionCore.Execute,
100+
);
101+
export const performCompile = performAndSwitchPrimaryAction(
102+
performCompileOnly,
103+
PrimaryActionCore.Compile,
104+
);
105+
export const performTest = performAndSwitchPrimaryAction(performTestOnly, PrimaryActionCore.Test);
106+
export const performCompileToAssembly = performAndSwitchPrimaryAction(
107+
performCompileToAssemblyOnly,
108+
PrimaryActionCore.Asm,
109+
);
110+
export const performCompileToLLVM = performAndSwitchPrimaryAction(
111+
performCompileToLlvmIrOnly,
112+
PrimaryActionCore.LlvmIr,
113+
);
114+
export const performCompileToMir = performAndSwitchPrimaryAction(
115+
performCompileToMirOnly,
116+
PrimaryActionCore.Mir,
117+
);
118+
export const performCompileToNightlyHir = performAndSwitchPrimaryAction(
119+
performCompileToNightlyHirOnly,
120+
PrimaryActionCore.Hir,
121+
);
122+
export const performCompileToWasm = performAndSwitchPrimaryAction(
123+
performCompileToCdylibWasmOnly,
124+
PrimaryActionCore.Wasm,
125+
);
166126

167127
export function indexPageLoad({
168128
code,
169129
gist,
170130
version,
171131
mode: modeString,
172132
edition: editionString,
173-
}: { code?: string, gist?: string, version?: string, mode?: string, edition?: string }): ThunkAction {
174-
return function(dispatch) {
133+
}: {
134+
code?: string;
135+
gist?: string;
136+
version?: string;
137+
mode?: string;
138+
edition?: string;
139+
}): ThunkAction {
140+
return function (dispatch) {
175141
const channel = parseChannel(version) || Channel.Stable;
176142
const mode = parseMode(modeString) || Mode.Debug;
177143
let maybeEdition = parseEdition(editionString);
@@ -204,21 +170,8 @@ export function indexPageLoad({
204170
export const helpPageLoad = navigateToHelp;
205171

206172
export function showExample(code: string): ThunkAction {
207-
return function(dispatch) {
173+
return function (dispatch) {
208174
dispatch(navigateToIndex());
209175
dispatch(editCode(code));
210176
};
211177
}
212-
213-
export type Action =
214-
| ReturnType<typeof initializeApplication>
215-
| ReturnType<typeof changeBacktrace>
216-
| ReturnType<typeof changeChannel>
217-
| ReturnType<typeof changeEditionRaw>
218-
| ReturnType<typeof changeMode>
219-
| ReturnType<typeof changePrimaryAction>
220-
| ReturnType<typeof editCode>
221-
| ReturnType<typeof addCrateType>
222-
| ReturnType<typeof navigateToIndex>
223-
| ReturnType<typeof wsExecuteRequest>
224-
;

ui/frontend/compileActions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AsyncThunk, createAsyncThunk } from '@reduxjs/toolkit';
22
import * as z from 'zod';
33

4-
import { SimpleThunkAction } from './actions';
4+
import { ThunkAction } from './actions';
55
import { jsonPost, routes } from './api';
66
import { compileRequestPayloadSelector } from './selectors';
77

@@ -33,7 +33,7 @@ interface Props {
3333

3434
interface CompileActions {
3535
action: AsyncThunk<CompileResponseBody, CompileRequestBody, {}>;
36-
performCompile: () => SimpleThunkAction;
36+
performCompile: () => ThunkAction;
3737
}
3838

3939
export const makeCompileActions = ({ sliceName, target }: Props): CompileActions => {
@@ -42,7 +42,7 @@ export const makeCompileActions = ({ sliceName, target }: Props): CompileActions
4242
return CompileResponseBody.parseAsync(d);
4343
});
4444

45-
const performCompile = (): SimpleThunkAction => (dispatch, getState) => {
45+
const performCompile = (): ThunkAction => (dispatch, getState) => {
4646
const state = getState();
4747
const body = compileRequestPayloadSelector(state, { target });
4848
dispatch(action(body));

ui/frontend/configureStore.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { configureStore as reduxConfigureStore } from '@reduxjs/toolkit';
44
import { produce } from 'immer';
55
import type {} from 'redux-thunk/extend-redux';
66

7-
import { initializeApplication } from './actions';
87
import initializeLocalStorage from './local_storage';
98
import initializeSessionStorage from './session_storage';
109
import { websocketMiddleware } from './websocketMiddleware';
@@ -19,7 +18,7 @@ export default function configureStore(window: Window) {
1918
baseUrl,
2019
},
2120
};
22-
const initialAppState = reducer(undefined, initializeApplication());
21+
const initialAppState = reducer(undefined, { type: 'initializeForStore' });
2322

2423
const localStorage = initializeLocalStorage();
2524
const sessionStorage = initializeSessionStorage();

ui/frontend/reducers/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const initialState: State = {
44
isSmall: true,
55
};
66

7-
export type State = {
7+
type State = {
88
isSmall: boolean;
99
};
1010

ui/frontend/reducers/configuration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
22

3-
import { SimpleThunkAction } from '../actions';
3+
import { ThunkAction } from '../actions';
44
import {
55
AssemblyFlavor,
66
Backtrace,
@@ -16,7 +16,7 @@ import {
1616
ProcessAssembly,
1717
} from '../types';
1818

19-
export interface State {
19+
interface State {
2020
editor: Editor;
2121
ace: {
2222
keybinding: string;
@@ -138,7 +138,7 @@ export const {
138138
} = slice.actions;
139139

140140
export const changeEdition =
141-
(edition: Edition): SimpleThunkAction =>
141+
(edition: Edition): ThunkAction =>
142142
(dispatch) => {
143143
if (edition === Edition.Rust2024) {
144144
dispatch(changeChannel(Channel.Nightly));

ui/frontend/reducers/crates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const sliceName = 'crates';
99

1010
const initialState: State = [];
1111

12-
export type State = Crate[];
12+
type State = Crate[];
1313

1414
const CratesResponse = z.object({
1515
crates: Crate.array(),

0 commit comments

Comments
 (0)