-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathcompileActions.ts
53 lines (45 loc) · 1.4 KB
/
compileActions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { AsyncThunk, createAsyncThunk } from '@reduxjs/toolkit';
import * as z from 'zod';
import { ThunkAction } from './actions';
import { jsonPost, routes } from './api';
import { compileRequestPayloadSelector } from './selectors';
interface CompileRequestBody {
channel: string;
mode: string;
crateType: string;
tests: boolean; // Used?
code: string;
edition: string;
backtrace: boolean; // Used?
target: string;
assemblyFlavor: string;
demangleAssembly: string;
processAssembly: string;
cargoScript: boolean;
}
const CompileResponseBody = z.object({
code: z.string(),
stdout: z.string(),
stderr: z.string(),
});
type CompileResponseBody = z.infer<typeof CompileResponseBody>;
interface Props {
sliceName: string;
target: string;
}
interface CompileActions {
action: AsyncThunk<CompileResponseBody, CompileRequestBody, {}>;
performCompile: () => ThunkAction;
}
export const makeCompileActions = ({ sliceName, target }: Props): CompileActions => {
const action = createAsyncThunk(sliceName, async (payload: CompileRequestBody) => {
const d = await jsonPost(routes.compile, payload);
return CompileResponseBody.parseAsync(d);
});
const performCompile = (): ThunkAction => (dispatch, getState) => {
const state = getState();
const body = compileRequestPayloadSelector(state, { target });
dispatch(action(body));
};
return { action, performCompile };
};