Skip to content

Commit 09ac072

Browse files
committed
Rewrite position reducer in RTK
1 parent 81f38f2 commit 09ac072

File tree

6 files changed

+22
-24
lines changed

6 files changed

+22
-24
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ module.exports = {
8888
'reducers/output/miri.ts',
8989
'reducers/output/wasm.ts',
9090
'reducers/page.ts',
91+
'reducers/position.ts',
9192
'reducers/versions.ts',
9293
'reducers/websocket.ts',
9394
'websocketActions.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ node_modules
3939
!reducers/output/miri.ts
4040
!reducers/output/wasm.ts
4141
!reducers/page.ts
42+
!reducers/position.ts
4243
!reducers/versions.ts
4344
!reducers/websocket.ts
4445
!websocketActions.ts

ui/frontend/actions.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
PrimaryActionCore,
2323
ProcessAssembly,
2424
Position,
25-
makePosition,
2625
} from './types';
2726

2827
import { performCommonExecute, wsExecuteRequest } from './reducers/output/execute';
@@ -62,7 +61,6 @@ export enum ActionType {
6261
AddImport = 'ADD_IMPORT',
6362
AddCrateType = 'ADD_CRATE_TYPE',
6463
EnableFeatureGate = 'ENABLE_FEATURE_GATE',
65-
GotoPosition = 'GOTO_POSITION',
6664
SelectText = 'SELECT_TEXT',
6765
NotificationSeen = 'NOTIFICATION_SEEN',
6866
BrowserWidthChanged = 'BROWSER_WIDTH_CHANGED',
@@ -213,9 +211,6 @@ export const addCrateType = (crateType: string) =>
213211
export const enableFeatureGate = (featureGate: string) =>
214212
createAction(ActionType.EnableFeatureGate, { featureGate });
215213

216-
export const gotoPosition = (line: string | number, column: string | number) =>
217-
createAction(ActionType.GotoPosition, makePosition(line, column));
218-
219214
export const selectText = (start: Position, end: Position) =>
220215
createAction(ActionType.SelectText, { start, end });
221216

@@ -333,7 +328,6 @@ export type Action =
333328
| ReturnType<typeof addImport>
334329
| ReturnType<typeof addCrateType>
335330
| ReturnType<typeof enableFeatureGate>
336-
| ReturnType<typeof gotoPosition>
337331
| ReturnType<typeof selectText>
338332
| ReturnType<typeof notificationSeen>
339333
| ReturnType<typeof browserWidthChanged>

ui/frontend/highlighting.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Channel, makePosition, Position } from './types';
44
interface ConfigureRustErrorsArgs {
55
enableFeatureGate: (feature: string) => void;
66
getChannel: () => Channel;
7-
gotoPosition: (line: string | number, column: string | number) => void;
7+
gotoPosition: (p: Position) => void;
88
selectText: (start: Position, end: Position) => void;
99
addImport: (code: string) => void;
1010
reExecuteWithBacktrace: () => void;
@@ -154,7 +154,7 @@ export function configureRustErrors({
154154
link.onclick = e => {
155155
e.preventDefault();
156156
if (line && col) {
157-
gotoPosition(line, col);
157+
gotoPosition(makePosition(line, col));
158158
}
159159
};
160160
}
@@ -178,7 +178,7 @@ export function configureRustErrors({
178178
e.preventDefault();
179179
if (link.dataset.featureGate) {
180180
enableFeatureGate(link.dataset.featureGate);
181-
gotoPosition(1, 1);
181+
gotoPosition(makePosition(1, 1));
182182
}
183183
};
184184
}

ui/frontend/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { v4 } from 'uuid';
1111
import {
1212
editCode,
1313
enableFeatureGate,
14-
gotoPosition,
1514
selectText,
1615
addImport,
1716
reExecuteWithBacktrace,
@@ -27,6 +26,7 @@ import Router from './Router';
2726
import configureStore from './configureStore';
2827
import { performVersionsLoad } from './reducers/versions';
2928
import { performCratesLoad } from './reducers/crates';
29+
import { gotoPosition } from './reducers/position';
3030

3131
const store = configureStore(window);
3232

@@ -61,7 +61,7 @@ maxWidthMediaQuery.addEventListener('change', whenBrowserWidthChanged);
6161

6262
configureRustErrors({
6363
enableFeatureGate: featureGate => store.dispatch(enableFeatureGate(featureGate)),
64-
gotoPosition: (line, col) => store.dispatch(gotoPosition(line, col)),
64+
gotoPosition: (p) => store.dispatch(gotoPosition(p)),
6565
selectText: (start, end) => store.dispatch(selectText(start, end)),
6666
addImport: (code) => store.dispatch(addImport(code)),
6767
reExecuteWithBacktrace: () => store.dispatch(reExecuteWithBacktrace()),

ui/frontend/reducers/position.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import { Action, ActionType } from '../actions';
1+
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
2+
23
import { Position } from '../types';
34

4-
const DEFAULT: Position = {
5+
const initialState: Position = {
56
line: 0,
67
column: 0,
78
};
89

9-
export type State = Position;
10+
const slice = createSlice({
11+
name: 'position',
12+
initialState,
13+
reducers: {
14+
gotoPosition: (_state, action: PayloadAction<Position>) => {
15+
return action.payload;
16+
},
17+
},
18+
});
19+
20+
export const { gotoPosition } = slice.actions;
1021

11-
export default function position(state = DEFAULT, action: Action) {
12-
switch (action.type) {
13-
case ActionType.GotoPosition: {
14-
const { line, column } = action;
15-
return { ...state, line, column };
16-
}
17-
default:
18-
return state;
19-
}
20-
}
22+
export default slice.reducer;

0 commit comments

Comments
 (0)