Skip to content

Commit

Permalink
Merge pull request #1006 from rust-lang/one-version-request
Browse files Browse the repository at this point in the history
Perform one request to get all versions
  • Loading branch information
shepmaster authored Dec 1, 2023
2 parents 7035da1 + 92d122b commit b4b8caf
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 96 deletions.
1 change: 1 addition & 0 deletions ui/frontend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module.exports = {
'reducers/output/meta.ts',
'reducers/output/mir.ts',
'reducers/output/wasm.ts',
'reducers/versions.ts',
'reducers/websocket.ts',
'websocketActions.ts',
'websocketMiddleware.ts',
Expand Down
1 change: 1 addition & 0 deletions ui/frontend/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ node_modules
!reducers/output/meta.ts
!reducers/output/mir.ts
!reducers/output/wasm.ts
!reducers/versions.ts
!reducers/websocket.ts
!websocketActions.ts
!websocketMiddleware.ts
50 changes: 1 addition & 49 deletions ui/frontend/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
ProcessAssembly,
Position,
makePosition,
Version,
Crate,
} from './types';

Expand All @@ -48,14 +47,7 @@ export const routes = {
macroExpansion: '/macro-expansion',
meta: {
crates: '/meta/crates',
version: {
stable: '/meta/version/stable',
beta: '/meta/version/beta',
nightly: '/meta/version/nightly',
rustfmt: '/meta/version/rustfmt',
clippy: '/meta/version/clippy',
miri: '/meta/version/miri',
},
versions: '/meta/versions',
gistSave: '/meta/gist',
gistLoad: '/meta/gist/id',
},
Expand Down Expand Up @@ -103,8 +95,6 @@ export enum ActionType {
MacroExpansionFailed = 'MACRO_EXPANSION_FAILED',
RequestCratesLoad = 'REQUEST_CRATES_LOAD',
CratesLoadSucceeded = 'CRATES_LOAD_SUCCEEDED',
RequestVersionsLoad = 'REQUEST_VERSIONS_LOAD',
VersionsLoadSucceeded = 'VERSIONS_LOAD_SUCCEEDED',
NotificationSeen = 'NOTIFICATION_SEEN',
BrowserWidthChanged = 'BROWSER_WIDTH_CHANGED',
}
Expand Down Expand Up @@ -488,42 +478,6 @@ export function performCratesLoad(): ThunkAction {
};
}

const requestVersionsLoad = () =>
createAction(ActionType.RequestVersionsLoad);

const receiveVersionsLoadSuccess = ({
stable, beta, nightly, rustfmt, clippy, miri,
}: {
stable: Version, beta: Version, nightly: Version, rustfmt: Version, clippy: Version, miri: Version,
}) =>
createAction(ActionType.VersionsLoadSucceeded, { stable, beta, nightly, rustfmt, clippy, miri });

export function performVersionsLoad(): ThunkAction {
return function(dispatch) {
dispatch(requestVersionsLoad());

const stable = jsonGet(routes.meta.version.stable);
const beta = jsonGet(routes.meta.version.beta);
const nightly = jsonGet(routes.meta.version.nightly);
const rustfmt = jsonGet(routes.meta.version.rustfmt);
const clippy = jsonGet(routes.meta.version.clippy);
const miri = jsonGet(routes.meta.version.miri);

const all = Promise.all([stable, beta, nightly, rustfmt, clippy, miri]);

return all
.then(([stable, beta, nightly, rustfmt, clippy, miri]) => dispatch(receiveVersionsLoadSuccess({
stable,
beta,
nightly,
rustfmt,
clippy,
miri,
})));
// TODO: Failure case
};
}

const notificationSeen = (notification: Notification) =>
createAction(ActionType.NotificationSeen, { notification });

Expand Down Expand Up @@ -654,8 +608,6 @@ export type Action =
| ReturnType<typeof receiveMacroExpansionFailure>
| ReturnType<typeof requestCratesLoad>
| ReturnType<typeof receiveCratesLoadSuccess>
| ReturnType<typeof requestVersionsLoad>
| ReturnType<typeof receiveVersionsLoadSuccess>
| ReturnType<typeof notificationSeen>
| ReturnType<typeof browserWidthChanged>
| ReturnType<typeof wsExecuteRequest>
Expand Down
2 changes: 1 addition & 1 deletion ui/frontend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
selectText,
addImport,
performCratesLoad,
performVersionsLoad,
reExecuteWithBacktrace,
browserWidthChanged,
} from './actions';
Expand All @@ -27,6 +26,7 @@ import { featureFlagsForceDisableAll, featureFlagsForceEnableAll } from './reduc
import { disableSyncChangesToStorage } from './reducers/globalConfiguration';
import Router from './Router';
import configureStore from './configureStore';
import { performVersionsLoad } from './reducers/versions';

const store = configureStore(window);

Expand Down
62 changes: 37 additions & 25 deletions ui/frontend/reducers/versions.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import { Action, ActionType } from '../actions';
import { Version } from '../types';

const DEFAULT: State = {
};

export interface State {
stable?: Version;
beta?: Version;
nightly?: Version;
rustfmt?: Version;
clippy?: Version;
miri?: Version;
}

export default function crates(state = DEFAULT, action: Action) {
switch (action.type) {
case ActionType.VersionsLoadSucceeded: {
const { stable, beta, nightly, rustfmt, clippy, miri } = action;
return { stable, beta, nightly, rustfmt, clippy, miri };
}
default:
return state;
}
}
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import * as z from 'zod';

import { adaptFetchError, jsonGet, routes } from '../actions';
import { ChannelVersion } from '../types';

const sliceName = 'versions';

const initialState: State = {};

type State = Partial<Response>;

const Response = z.object({
stable: ChannelVersion,
beta: ChannelVersion,
nightly: ChannelVersion,
});

type Response = z.infer<typeof Response>;

export const performVersionsLoad = createAsyncThunk(sliceName, async () => {
const d = await adaptFetchError(() => jsonGet(routes.meta.versions));
return Response.parseAsync(d);
});

const slice = createSlice({
name: sliceName,
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(performVersionsLoad.fulfilled, (state, versions) => {
Object.assign(state, versions.payload);
});
},
});

export default slice.reducer;
12 changes: 6 additions & 6 deletions ui/frontend/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ const LABELS: { [index in PrimaryActionCore]: string } = {

export const getExecutionLabel = createSelector(primaryActionSelector, primaryAction => LABELS[primaryAction]);

const getStable = (state: State) => state.versions?.stable;
const getBeta = (state: State) => state.versions?.beta;
const getNightly = (state: State) => state.versions?.nightly;
const getRustfmt = (state: State) => state.versions?.rustfmt;
const getClippy = (state: State) => state.versions?.clippy;
const getMiri = (state: State) => state.versions?.miri;
const getStable = (state: State) => state.versions.stable?.rustc;
const getBeta = (state: State) => state.versions.beta?.rustc;
const getNightly = (state: State) => state.versions.nightly?.rustc;
const getRustfmt = (state: State) => state.versions.nightly?.rustfmt;
const getClippy = (state: State) => state.versions.nightly?.clippy;
const getMiri = (state: State) => state.versions?.nightly?.miri;

const versionNumber = (v: Version | undefined) => v ? v.version : '';
export const stableVersionText = createSelector(getStable, versionNumber);
Expand Down
23 changes: 18 additions & 5 deletions ui/frontend/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as z from 'zod';

export type Page = 'index' | 'help';

export interface Position {
Expand All @@ -19,11 +21,22 @@ export interface Crate {
version: string;
}

export interface Version {
version: string;
hash: string;
date: string;
}
export const Version = z.object({
version: z.string(),
hash: z.string(),
date: z.string(),
});

export type Version = z.infer<typeof Version>;

export const ChannelVersion = z.object({
rustc: Version,
rustfmt: Version,
clippy: Version,
miri: Version.optional(),
});

export type ChannelVersion = z.infer<typeof ChannelVersion>;

export interface CommonEditorProps {
code: string;
Expand Down
16 changes: 16 additions & 0 deletions ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,22 @@ struct MetaCratesResponse {
crates: Arc<[CrateInformation]>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
struct MetaVersionsResponse {
stable: MetaChannelVersionResponse,
beta: MetaChannelVersionResponse,
nightly: MetaChannelVersionResponse,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
struct MetaChannelVersionResponse {
rustc: MetaVersionResponse,
rustfmt: MetaVersionResponse,
clippy: MetaVersionResponse,
#[serde(skip_serializing_if = "Option::is_none")]
miri: Option<MetaVersionResponse>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
struct MetaVersionResponse {
version: Arc<str>,
Expand Down
1 change: 1 addition & 0 deletions ui/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub(crate) enum Endpoint {
Clippy,
MacroExpansion,
MetaCrates,
MetaVersions,
MetaVersionStable,
MetaVersionBeta,
MetaVersionNightly,
Expand Down
Loading

0 comments on commit b4b8caf

Please sign in to comment.