Skip to content

Commit 76d7cc5

Browse files
committed
Consistently use selectors for code/position/selection
This will reduce the amount of change in future commits where we move these values around.
1 parent 463dcfe commit 76d7cc5

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

ui/frontend/actions.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ThunkAction as ReduxThunkAction } from 'redux-thunk';
33
import { z } from 'zod';
44

55
import {
6+
codeSelector,
67
clippyRequestSelector,
78
formatRequestSelector,
89
getCrateType,
@@ -303,7 +304,8 @@ interface ExecuteRequestBody {
303304

304305
const performCommonExecute = (crateType: string, tests: boolean): ThunkAction => (dispatch, getState) => {
305306
const state = getState();
306-
const { code, configuration: { channel, mode, edition } } = state;
307+
const code = codeSelector(state);
308+
const { configuration: { channel, mode, edition } } = state;
307309
const backtrace = state.configuration.backtrace === Backtrace.Enabled;
308310

309311
if (useWebsocketSelector(state)) {
@@ -364,7 +366,8 @@ function performCompileShow(
364366
dispatch(request());
365367

366368
const state = getState();
367-
const { code, configuration: {
369+
const code = codeSelector(state);
370+
const { configuration: {
368371
channel,
369372
mode,
370373
edition,
@@ -677,9 +680,11 @@ export function performMiri(): ThunkAction {
677680
return function(dispatch, getState) {
678681
dispatch(requestMiri());
679682

680-
const { code, configuration: {
683+
const state = getState();
684+
const code = codeSelector(state);
685+
const { configuration: {
681686
edition,
682-
} } = getState();
687+
} } = state;
683688
const body: MiriRequestBody = { code, edition };
684689

685690
return jsonPost<MiriResponseBody>(routes.miri, body)
@@ -715,9 +720,11 @@ export function performMacroExpansion(): ThunkAction {
715720
return function(dispatch, getState) {
716721
dispatch(requestMacroExpansion());
717722

718-
const { code, configuration: {
723+
const state = getState();
724+
const code = codeSelector(state);
725+
const { configuration: {
719726
edition,
720-
} } = getState();
727+
} } = state;
721728
const body: MacroExpansionRequestBody = { code, edition };
722729

723730
return jsonPost<MacroExpansionResponseBody>(routes.macroExpansion, body)
@@ -783,8 +790,9 @@ export function performGistSave(): ThunkAction {
783790
return function(dispatch, getState) {
784791
dispatch(requestGistSave());
785792

793+
const state = getState();
794+
const code = codeSelector(state);
786795
const {
787-
code,
788796
configuration: {
789797
channel, mode, edition,
790798
},
@@ -794,7 +802,7 @@ export function performGistSave(): ThunkAction {
794802
stderr = '',
795803
},
796804
},
797-
} = getState();
805+
} = state;
798806

799807
return jsonPost<GistResponseBody>(routes.meta.gistSave, { code })
800808
.then(json => dispatch(receiveGistSaveSuccess({ ...json, code, stdout, stderr, channel, mode, edition })));

ui/frontend/editor/Editor.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import AceEditor from './AceEditor';
88
import SimpleEditor from './SimpleEditor';
99
import MonacoEditor from './MonacoEditor';
1010
import { Editor as EditorType } from '../types';
11+
import { codeSelector, positionSelector, selectionSelector } from '../selectors';
1112
import { State } from '../reducers';
1213

1314
import styles from './Editor.module.css';
@@ -19,10 +20,10 @@ const editorMap = {
1920
};
2021

2122
const Editor: React.FC = () => {
22-
const code = useSelector((state: State) => state.code);
23+
const code = useSelector(codeSelector);
2324
const editor = useSelector((state: State) => state.configuration.editor);
24-
const position = useSelector((state: State) => state.position);
25-
const selection = useSelector((state: State) => state.selection);
25+
const position = useSelector(positionSelector);
26+
const selection = useSelector(selectionSelector);
2627
const crates = useSelector((state: State) => state.crates);
2728

2829
const dispatch = useAppDispatch();

ui/frontend/local_storage.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import State from './state';
66
import {removeVersion, initializeStorage, PartialState} from './storage';
77
import { AssemblyFlavor, DemangleAssembly, Editor, Orientation, PairCharacters, ProcessAssembly } from './types';
8+
import { codeSelector } from './selectors';
89

910
const CURRENT_VERSION = 2;
1011

@@ -48,6 +49,7 @@ interface V1Configuration {
4849
type CurrentConfiguration = V2Configuration;
4950

5051
export function serialize(state: State): string {
52+
const code = codeSelector(state);
5153
const conf: CurrentConfiguration = {
5254
version: CURRENT_VERSION,
5355
configuration: {
@@ -65,7 +67,7 @@ export function serialize(state: State): string {
6567
demangleAssembly: state.configuration.demangleAssembly,
6668
processAssembly: state.configuration.processAssembly,
6769
},
68-
code: state.code,
70+
code,
6971
notifications: state.notifications,
7072
};
7173
return JSON.stringify(conf);

ui/frontend/selectors/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
Version,
1414
} from '../types';
1515

16-
const codeSelector = (state: State) => state.code;
16+
export const codeSelector = (state: State) => state.code;
17+
export const positionSelector = (state: State) => state.position;
18+
export const selectionSelector = (state: State) => state.selection;
1719

1820
const HAS_TESTS_RE = /^\s*#\s*\[\s*test\s*([^"]*)]/m;
1921
export const hasTestsSelector = createSelector(codeSelector, code => !!code.match(HAS_TESTS_RE));

ui/frontend/session_storage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
import { State } from './reducers';
66
import {removeVersion, initializeStorage, PartialState} from './storage';
7+
import { codeSelector } from './selectors';
78

89
const CURRENT_VERSION = 1;
910

1011
export function serialize(state: State): string {
12+
const code = codeSelector(state);
13+
1114
return JSON.stringify({
1215
version: CURRENT_VERSION,
1316
configuration: {
1417
primaryAction: state.configuration.primaryAction,
1518
},
16-
code: state.code,
19+
code,
1720
});
1821
}
1922

0 commit comments

Comments
 (0)