|
1 |
| -import { Action, ActionType } from '../../actions'; |
| 1 | +import { PayloadAction, createSlice } from '@reduxjs/toolkit'; |
| 2 | +import { Draft } from 'immer'; |
| 3 | + |
| 4 | +import { ActionType } from '../../actions'; |
2 | 5 | import { Focus } from '../../types';
|
3 |
| -import { performGistLoad, performGistSave } from './gist'; |
4 |
| -import { performFormat } from './format'; |
5 |
| -import { performExecute, wsExecuteRequest } from './execute'; |
6 | 6 | import { performCompileAssembly } from './assembly';
|
| 7 | +import { performExecute, wsExecuteRequest } from './execute'; |
| 8 | +import { performFormat } from './format'; |
| 9 | +import { performGistLoad, performGistSave } from './gist'; |
7 | 10 | import { performCompileHir } from './hir';
|
8 | 11 | import { performCompileLlvmIr } from './llvmIr';
|
9 | 12 | import { performCompileMir } from './mir';
|
10 | 13 | import { performCompileWasm } from './wasm';
|
11 | 14 |
|
12 |
| -const DEFAULT: State = { |
13 |
| -}; |
| 15 | +const initialState: State = {}; |
14 | 16 |
|
15 | 17 | interface State {
|
16 | 18 | focus?: Focus;
|
17 | 19 | }
|
18 | 20 |
|
19 |
| -export default function meta(state = DEFAULT, action: Action) { |
20 |
| - switch (action.type) { |
21 |
| - case ActionType.ChangeFocus: |
22 |
| - return { ...state, focus: action.focus }; |
| 21 | +function setExecute(state: Draft<State>) { |
| 22 | + state.focus = Focus.Execute; |
| 23 | +} |
| 24 | +function setGist(state: Draft<State>) { |
| 25 | + state.focus = Focus.Gist; |
| 26 | +} |
23 | 27 |
|
24 |
| - case ActionType.RequestClippy: |
25 |
| - return { ...state, focus: Focus.Clippy }; |
| 28 | +const slice = createSlice({ |
| 29 | + name: 'output/meta', |
| 30 | + initialState, |
| 31 | + reducers: { |
| 32 | + changeFocus: (state, action: PayloadAction<Focus | undefined>) => { |
| 33 | + state.focus = action.payload; |
| 34 | + }, |
| 35 | + }, |
| 36 | + extraReducers: (builder) => { |
| 37 | + builder |
| 38 | + .addCase(ActionType.RequestClippy, (state) => { |
| 39 | + state.focus = Focus.Clippy; |
| 40 | + }) |
26 | 41 |
|
27 |
| - case ActionType.RequestMiri: |
28 |
| - return { ...state, focus: Focus.Miri }; |
| 42 | + .addCase(ActionType.RequestMiri, (state) => { |
| 43 | + state.focus = Focus.Miri; |
| 44 | + }) |
29 | 45 |
|
30 |
| - case ActionType.RequestMacroExpansion: |
31 |
| - return { ...state, focus: Focus.MacroExpansion }; |
| 46 | + .addCase(ActionType.RequestMacroExpansion, (state) => { |
| 47 | + state.focus = Focus.MacroExpansion; |
| 48 | + }) |
32 | 49 |
|
33 |
| - case performExecute.pending.type: |
34 |
| - case wsExecuteRequest.type: |
35 |
| - return { ...state, focus: Focus.Execute }; |
| 50 | + .addCase(performExecute.pending, setExecute) |
| 51 | + .addCase(wsExecuteRequest, setExecute) |
36 | 52 |
|
37 |
| - case performCompileAssembly.pending.type: |
38 |
| - return { ...state, focus: Focus.Asm }; |
| 53 | + .addCase(performCompileAssembly.pending, (state) => { |
| 54 | + state.focus = Focus.Asm; |
| 55 | + }) |
39 | 56 |
|
40 |
| - case performCompileHir.pending.type: |
41 |
| - return { ...state, focus: Focus.Hir }; |
| 57 | + .addCase(performCompileHir.pending, (state) => { |
| 58 | + state.focus = Focus.Hir; |
| 59 | + }) |
42 | 60 |
|
43 |
| - case performCompileLlvmIr.pending.type: |
44 |
| - return { ...state, focus: Focus.LlvmIr }; |
| 61 | + .addCase(performCompileLlvmIr.pending, (state) => { |
| 62 | + state.focus = Focus.LlvmIr; |
| 63 | + }) |
45 | 64 |
|
46 |
| - case performCompileMir.pending.type: |
47 |
| - return { ...state, focus: Focus.Mir }; |
| 65 | + .addCase(performCompileMir.pending, (state) => { |
| 66 | + state.focus = Focus.Mir; |
| 67 | + }) |
48 | 68 |
|
49 |
| - case performCompileWasm.pending.type: |
50 |
| - return { ...state, focus: Focus.Wasm }; |
| 69 | + .addCase(performCompileWasm.pending, (state) => { |
| 70 | + state.focus = Focus.Wasm; |
| 71 | + }) |
51 | 72 |
|
52 |
| - default: { |
53 |
| - if (performGistLoad.pending.match(action) || performGistSave.pending.match(action)) { |
54 |
| - return { ...state, focus: Focus.Gist }; |
55 |
| - } else if (performFormat.pending.match(action)) { |
56 |
| - return { ...state, focus: Focus.Format }; |
57 |
| - } else if (performFormat.fulfilled.match(action)) { |
58 |
| - return { ...state, focus: undefined }; |
59 |
| - } else { |
60 |
| - return state; |
61 |
| - } |
62 |
| - } |
63 |
| - } |
64 |
| -} |
| 73 | + .addCase(performFormat.pending, (state) => { |
| 74 | + state.focus = Focus.Format; |
| 75 | + }) |
| 76 | + |
| 77 | + .addCase(performFormat.fulfilled, (state, action) => { |
| 78 | + if (action.payload.success) { |
| 79 | + state.focus = undefined; |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + .addCase(performGistLoad.pending, setGist) |
| 84 | + .addCase(performGistSave.pending, setGist); |
| 85 | + }, |
| 86 | +}); |
| 87 | + |
| 88 | +export const { changeFocus } = slice.actions; |
| 89 | + |
| 90 | +export default slice.reducer; |
0 commit comments