Skip to content

Commit b4636a2

Browse files
committed
Rewrite macroExpansion reducer with RTK
1 parent f4a5b3d commit b4636a2

File tree

7 files changed

+64
-76
lines changed

7 files changed

+64
-76
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = {
8181
'reducers/output/gist.ts',
8282
'reducers/output/hir.ts',
8383
'reducers/output/llvmIr.ts',
84+
'reducers/output/macroExpansion.ts',
8485
'reducers/output/meta.ts',
8586
'reducers/output/mir.ts',
8687
'reducers/output/miri.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ node_modules
3232
!reducers/output/gist.ts
3333
!reducers/output/hir.ts
3434
!reducers/output/llvmIr.ts
35+
!reducers/output/macroExpansion.ts
3536
!reducers/output/meta.ts
3637
!reducers/output/mir.ts
3738
!reducers/output/miri.ts

ui/frontend/ToolsMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import MenuGroup from './MenuGroup';
66
import MenuAside from './MenuAside';
77

88
import * as selectors from './selectors';
9-
import * as actions from './actions';
109
import { useAppDispatch } from './configureStore';
1110
import { performFormat } from './reducers/output/format';
1211
import { performClippy } from './reducers/output/clippy';
1312
import { performMiri } from './reducers/output/miri';
13+
import { performMacroExpansion } from './reducers/output/macroExpansion';
1414

1515
interface ToolsMenuProps {
1616
close: () => void;
@@ -40,7 +40,7 @@ const ToolsMenu: React.FC<ToolsMenuProps> = props => {
4040
props.close();
4141
}, [dispatch, props]);
4242
const expandMacros = useCallback(() => {
43-
dispatch(actions.performMacroExpansion());
43+
dispatch(performMacroExpansion());
4444
props.close();
4545
}, [dispatch, props]);
4646

ui/frontend/actions.ts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fetch from 'isomorphic-fetch';
22
import { ThunkAction as ReduxThunkAction, AnyAction } from '@reduxjs/toolkit';
33

44
import {
5-
codeSelector,
65
getCrateType,
76
runAsTest,
87
wasmLikelyToWork,
@@ -82,9 +81,6 @@ export enum ActionType {
8281
EnableFeatureGate = 'ENABLE_FEATURE_GATE',
8382
GotoPosition = 'GOTO_POSITION',
8483
SelectText = 'SELECT_TEXT',
85-
RequestMacroExpansion = 'REQUEST_MACRO_EXPANSION',
86-
MacroExpansionSucceeded = 'MACRO_EXPANSION_SUCCEEDED',
87-
MacroExpansionFailed = 'MACRO_EXPANSION_FAILED',
8884
NotificationSeen = 'NOTIFICATION_SEEN',
8985
BrowserWidthChanged = 'BROWSER_WIDTH_CHANGED',
9086
}
@@ -252,10 +248,6 @@ const performTestOnly = (): ThunkAction => (dispatch, getState) => {
252248
return dispatch(performCommonExecute(crateType, true));
253249
};
254250

255-
interface GenericApiFailure {
256-
error: string;
257-
}
258-
259251
const performCompileToNightlyHirOnly = (): ThunkAction => dispatch => {
260252
dispatch(changeChannel(Channel.Nightly));
261253
dispatch(performCompileToHirOnly());
@@ -331,51 +323,6 @@ export const gotoPosition = (line: string | number, column: string | number) =>
331323
export const selectText = (start: Position, end: Position) =>
332324
createAction(ActionType.SelectText, { start, end });
333325

334-
interface GeneralSuccess {
335-
stdout: string;
336-
stderr: string;
337-
}
338-
339-
const requestMacroExpansion = () =>
340-
createAction(ActionType.RequestMacroExpansion);
341-
342-
interface MacroExpansionRequestBody {
343-
code: string;
344-
edition: string;
345-
}
346-
347-
interface MacroExpansionResponseBody {
348-
success: boolean;
349-
stdout: string;
350-
stderr: string;
351-
}
352-
353-
type MacroExpansionSuccess = GeneralSuccess;
354-
355-
const receiveMacroExpansionSuccess = ({ stdout, stderr }: MacroExpansionSuccess) =>
356-
createAction(ActionType.MacroExpansionSucceeded, { stdout, stderr });
357-
358-
const receiveMacroExpansionFailure = ({ error }: GenericApiFailure) =>
359-
createAction(ActionType.MacroExpansionFailed, { error });
360-
361-
export function performMacroExpansion(): ThunkAction {
362-
// TODO: Check a cache
363-
return function(dispatch, getState) {
364-
dispatch(requestMacroExpansion());
365-
366-
const state = getState();
367-
const code = codeSelector(state);
368-
const { configuration: {
369-
edition,
370-
} } = state;
371-
const body: MacroExpansionRequestBody = { code, edition };
372-
373-
return jsonPost<MacroExpansionResponseBody>(routes.macroExpansion, body)
374-
.then(json => dispatch(receiveMacroExpansionSuccess(json)))
375-
.catch(json => dispatch(receiveMacroExpansionFailure(json)));
376-
};
377-
}
378-
379326
const notificationSeen = (notification: Notification) =>
380327
createAction(ActionType.NotificationSeen, { notification });
381328

@@ -495,9 +442,6 @@ export type Action =
495442
| ReturnType<typeof enableFeatureGate>
496443
| ReturnType<typeof gotoPosition>
497444
| ReturnType<typeof selectText>
498-
| ReturnType<typeof requestMacroExpansion>
499-
| ReturnType<typeof receiveMacroExpansionSuccess>
500-
| ReturnType<typeof receiveMacroExpansionFailure>
501445
| ReturnType<typeof notificationSeen>
502446
| ReturnType<typeof browserWidthChanged>
503447
| ReturnType<typeof wsExecuteRequest>
Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { Action, ActionType } from '../../actions';
2-
import { finish, start } from './sharedStateManagement';
1+
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2+
import * as z from 'zod';
33

4-
const DEFAULT: State = {
4+
import { adaptFetchError, jsonPost, routes } from '../../actions';
5+
import { macroExpansionRequestSelector } from '../../selectors';
6+
import RootState from '../../state';
7+
8+
const sliceName = 'output/macroExpansion';
9+
10+
const initialState: State = {
511
requestsInProgress: 0,
612
};
713

@@ -12,17 +18,47 @@ interface State {
1218
error?: string;
1319
}
1420

15-
export default function macroExpansion(state = DEFAULT, action: Action) {
16-
switch (action.type) {
17-
case ActionType.RequestMacroExpansion:
18-
return start(DEFAULT, state);
19-
case ActionType.MacroExpansionSucceeded: {
20-
const { stdout = '', stderr = '' } = action;
21-
return finish(state, { stdout, stderr });
22-
}
23-
case ActionType.MacroExpansionFailed:
24-
return finish(state, { error: action.error });
25-
default:
26-
return state;
27-
}
21+
interface MacroExpansionRequestBody {
22+
code: string;
23+
edition: string;
2824
}
25+
26+
const MacroExpansionResponseBody = z.object({
27+
success: z.boolean(),
28+
stdout: z.string(),
29+
stderr: z.string(),
30+
});
31+
32+
type MacroExpansionResponseBody = z.infer<typeof MacroExpansionResponseBody>;
33+
34+
export const performMacroExpansion = createAsyncThunk<
35+
MacroExpansionResponseBody,
36+
void,
37+
{ state: RootState }
38+
>(sliceName, async (_arg: void, { getState }) => {
39+
const body: MacroExpansionRequestBody = macroExpansionRequestSelector(getState());
40+
41+
const d = await adaptFetchError(() => jsonPost(routes.macroExpansion, body));
42+
return MacroExpansionResponseBody.parseAsync(d);
43+
});
44+
45+
const slice = createSlice({
46+
name: sliceName,
47+
initialState,
48+
reducers: {},
49+
extraReducers: (builder) => {
50+
builder
51+
.addCase(performMacroExpansion.pending, (state) => {
52+
state.requestsInProgress += 1;
53+
})
54+
.addCase(performMacroExpansion.fulfilled, (state, action) => {
55+
state.requestsInProgress -= 1;
56+
Object.assign(state, action.payload);
57+
})
58+
.addCase(performMacroExpansion.rejected, (state) => {
59+
state.requestsInProgress -= 1;
60+
});
61+
},
62+
});
63+
64+
export default slice.reducer;

ui/frontend/reducers/output/meta.ts

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

4-
import { ActionType } from '../../actions';
54
import { Focus } from '../../types';
65
import { performCompileAssembly } from './assembly';
76
import { performClippy } from './clippy';
@@ -10,6 +9,7 @@ import { performFormat } from './format';
109
import { performGistLoad, performGistSave } from './gist';
1110
import { performCompileHir } from './hir';
1211
import { performCompileLlvmIr } from './llvmIr';
12+
import { performMacroExpansion } from './macroExpansion';
1313
import { performCompileMir } from './mir';
1414
import { performMiri } from './miri';
1515
import { performCompileWasm } from './wasm';
@@ -45,7 +45,7 @@ const slice = createSlice({
4545
state.focus = Focus.Miri;
4646
})
4747

48-
.addCase(ActionType.RequestMacroExpansion, (state) => {
48+
.addCase(performMacroExpansion.pending, (state) => {
4949
state.focus = Focus.MacroExpansion;
5050
})
5151

ui/frontend/selectors/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ export const miriRequestSelector = createSelector(
339339
(edition, code) => ({ edition, code }),
340340
);
341341

342+
export const macroExpansionRequestSelector = createSelector(
343+
editionSelector,
344+
codeSelector,
345+
(edition, code) => ({ edition, code })
346+
);
347+
342348
const focus = (state: State) => state.output.meta.focus;
343349
export const isOutputFocused = createSelector(
344350
focus,

0 commit comments

Comments
 (0)