Skip to content

Commit 2a10390

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 72b13a7 commit 2a10390

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
@@ -4,6 +4,7 @@ import url, { UrlObject } from 'url';
44
import { z } from 'zod';
55

66
import {
7+
codeSelector,
78
clippyRequestSelector,
89
formatRequestSelector,
910
getCrateType,
@@ -304,7 +305,8 @@ interface ExecuteRequestBody {
304305

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

310312
if (useWebsocketSelector(state)) {
@@ -365,7 +367,8 @@ function performCompileShow(
365367
dispatch(request());
366368

367369
const state = getState();
368-
const { code, configuration: {
370+
const code = codeSelector(state);
371+
const { configuration: {
369372
channel,
370373
mode,
371374
edition,
@@ -678,9 +681,11 @@ export function performMiri(): ThunkAction {
678681
return function(dispatch, getState) {
679682
dispatch(requestMiri());
680683

681-
const { code, configuration: {
684+
const state = getState();
685+
const code = codeSelector(state);
686+
const { configuration: {
682687
edition,
683-
} } = getState();
688+
} } = state;
684689
const body: MiriRequestBody = { code, edition };
685690

686691
return jsonPost<MiriResponseBody>(routes.miri, body)
@@ -716,9 +721,11 @@ export function performMacroExpansion(): ThunkAction {
716721
return function(dispatch, getState) {
717722
dispatch(requestMacroExpansion());
718723

719-
const { code, configuration: {
724+
const state = getState();
725+
const code = codeSelector(state);
726+
const { configuration: {
720727
edition,
721-
} } = getState();
728+
} } = state;
722729
const body: MacroExpansionRequestBody = { code, edition };
723730

724731
return jsonPost<MacroExpansionResponseBody>(routes.macroExpansion, body)
@@ -779,8 +786,9 @@ export function performGistSave(): ThunkAction {
779786
return function(dispatch, getState) {
780787
dispatch(requestGistSave());
781788

789+
const state = getState();
790+
const code = codeSelector(state);
782791
const {
783-
code,
784792
configuration: {
785793
channel, mode, edition,
786794
},
@@ -790,7 +798,7 @@ export function performGistSave(): ThunkAction {
790798
stderr = '',
791799
},
792800
},
793-
} = getState();
801+
} = state;
794802

795803
return jsonPost<GistResponseBody>(routes.meta.gist, { code })
796804
.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
@@ -14,7 +14,9 @@ import {
1414
Version,
1515
} from '../types';
1616

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

1921
const HAS_TESTS_RE = /^\s*#\s*\[\s*test\s*([^"]*)]/m;
2022
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)