11import fetch from 'isomorphic-fetch' ;
2- import { ThunkAction as ReduxThunkAction } from '@reduxjs/toolkit' ;
2+ import { ThunkAction as ReduxThunkAction , AnyAction } from '@reduxjs/toolkit' ;
33import { z } from 'zod' ;
44
55import {
66 codeSelector ,
77 clippyRequestSelector ,
88 getCrateType ,
99 runAsTest ,
10- useWebsocketSelector ,
1110} from './selectors' ;
1211import State from './state' ;
1312import {
@@ -33,6 +32,7 @@ import {
3332 Crate ,
3433} from './types' ;
3534
35+ import { ExecuteRequestBody , performCommonExecute , wsExecuteRequest } from './reducers/output/execute' ;
3636import { performGistLoad } from './reducers/output/gist' ;
3737
3838export const routes = {
@@ -58,6 +58,7 @@ export const routes = {
5858} ;
5959
6060export type ThunkAction < T = void > = ReduxThunkAction < T , State , { } , Action > ;
61+ export type SimpleThunkAction < T = void > = ReduxThunkAction < T , State , { } , AnyAction > ;
6162
6263const createAction = < T extends string , P extends { } > ( type : T , props ?: P ) => (
6364 Object . assign ( { type } , props )
@@ -82,9 +83,6 @@ export enum ActionType {
8283 ChangeEdition = 'CHANGE_EDITION' ,
8384 ChangeBacktrace = 'CHANGE_BACKTRACE' ,
8485 ChangeFocus = 'CHANGE_FOCUS' ,
85- ExecuteRequest = 'EXECUTE_REQUEST' ,
86- ExecuteSucceeded = 'EXECUTE_SUCCEEDED' ,
87- ExecuteFailed = 'EXECUTE_FAILED' ,
8886 CompileAssemblyRequest = 'COMPILE_ASSEMBLY_REQUEST' ,
8987 CompileAssemblySucceeded = 'COMPILE_ASSEMBLY_SUCCEEDED' ,
9088 CompileAssemblyFailed = 'COMPILE_ASSEMBLY_FAILED' ,
@@ -126,8 +124,6 @@ export enum ActionType {
126124 WebSocketConnected = 'WEBSOCKET_CONNECTED' ,
127125 WebSocketDisconnected = 'WEBSOCKET_DISCONNECTED' ,
128126 WebSocketFeatureFlagEnabled = 'WEBSOCKET_FEATURE_FLAG_ENABLED' ,
129- WSExecuteRequest = 'WS_EXECUTE_REQUEST' ,
130- WSExecuteResponse = 'WS_EXECUTE_RESPONSE' ,
131127}
132128
133129export const WebSocketError = z . object ( {
@@ -136,20 +132,6 @@ export const WebSocketError = z.object({
136132} ) ;
137133export type WebSocketError = z . infer < typeof WebSocketError > ;
138134
139- const ExecuteExtra = z . object ( {
140- sequenceNumber : z . number ( ) ,
141- } ) ;
142- type ExecuteExtra = z . infer < typeof ExecuteExtra > ;
143-
144- export const WSExecuteResponse = z . object ( {
145- type : z . literal ( ActionType . WSExecuteResponse ) ,
146- success : z . boolean ( ) ,
147- stdout : z . string ( ) ,
148- stderr : z . string ( ) ,
149- extra : ExecuteExtra ,
150- } ) ;
151- export type WSExecuteResponse = z . infer < typeof WSExecuteResponse > ;
152-
153135export const initializeApplication = ( ) => createAction ( ActionType . InitializeApplication ) ;
154136
155137export const disableSyncChangesToStorage = ( ) => createAction ( ActionType . DisableSyncChangesToStorage ) ;
@@ -210,20 +192,6 @@ export const reExecuteWithBacktrace = (): ThunkAction => dispatch => {
210192export const changeFocus = ( focus ?: Focus ) =>
211193 createAction ( ActionType . ChangeFocus , { focus } ) ;
212194
213- interface ExecuteResponseBody {
214- stdout : string ;
215- stderr : string ;
216- }
217-
218- const requestExecute = ( ) =>
219- createAction ( ActionType . ExecuteRequest ) ;
220-
221- const receiveExecuteSuccess = ( { stdout, stderr } : ExecuteResponseBody ) =>
222- createAction ( ActionType . ExecuteSucceeded , { stdout, stderr } ) ;
223-
224- const receiveExecuteFailure = ( { error } : { error ?: string } ) =>
225- createAction ( ActionType . ExecuteFailed , { error } ) ;
226-
227195type FetchArg = Parameters < typeof fetch > [ 0 ] ;
228196
229197export function jsonGet ( url : FetchArg ) {
@@ -298,35 +266,6 @@ export const adaptFetchError = async <R>(cb: () => Promise<R>): Promise<R> => {
298266 }
299267}
300268
301- interface ExecuteRequestBody {
302- channel : string ;
303- mode : string ;
304- crateType : string ;
305- tests : boolean ;
306- code : string ;
307- edition : string ;
308- backtrace : boolean ;
309- }
310-
311- const performCommonExecute = ( crateType : string , tests : boolean ) : ThunkAction => ( dispatch , getState ) => {
312- const state = getState ( ) ;
313- const code = codeSelector ( state ) ;
314- const { configuration : { channel, mode, edition } } = state ;
315- const backtrace = state . configuration . backtrace === Backtrace . Enabled ;
316-
317- if ( useWebsocketSelector ( state ) ) {
318- return dispatch ( wsExecuteRequest ( channel , mode , edition , crateType , tests , code , backtrace ) ) ;
319- } else {
320- dispatch ( requestExecute ( ) ) ;
321-
322- const body : ExecuteRequestBody = { channel, mode, edition, crateType, tests, code, backtrace } ;
323-
324- return jsonPost < ExecuteResponseBody > ( routes . execute , body )
325- . then ( json => dispatch ( receiveExecuteSuccess ( json ) ) )
326- . catch ( json => dispatch ( receiveExecuteFailure ( json ) ) ) ;
327- }
328- } ;
329-
330269function performAutoOnly ( ) : ThunkAction {
331270 return function ( dispatch , getState ) {
332271 const state = getState ( ) ;
@@ -506,32 +445,6 @@ const PRIMARY_ACTIONS: { [index in PrimaryAction]: () => ThunkAction } = {
506445 [ PrimaryActionCore . Wasm ] : performCompileToNightlyWasmOnly ,
507446} ;
508447
509- let sequenceNumber = 0 ;
510- const nextSequenceNumber = ( ) => sequenceNumber ++ ;
511- const makeExtra = ( ) : ExecuteExtra => ( {
512- sequenceNumber : nextSequenceNumber ( ) ,
513- } ) ;
514-
515- const wsExecuteRequest = (
516- channel : Channel ,
517- mode : Mode ,
518- edition : Edition ,
519- crateType : string ,
520- tests : boolean ,
521- code : string ,
522- backtrace : boolean
523- ) =>
524- createAction ( ActionType . WSExecuteRequest , {
525- channel,
526- mode,
527- edition,
528- crateType,
529- tests,
530- code,
531- backtrace,
532- extra : makeExtra ( ) ,
533- } ) ;
534-
535448export const performPrimaryAction = ( ) : ThunkAction => ( dispatch , getState ) => {
536449 const state = getState ( ) ;
537450 const primaryAction = PRIMARY_ACTIONS [ state . configuration . primaryAction ] ;
@@ -871,9 +784,6 @@ export type Action =
871784 | ReturnType < typeof changeProcessAssembly >
872785 | ReturnType < typeof changeAceTheme >
873786 | ReturnType < typeof changeMonacoTheme >
874- | ReturnType < typeof requestExecute >
875- | ReturnType < typeof receiveExecuteSuccess >
876- | ReturnType < typeof receiveExecuteFailure >
877787 | ReturnType < typeof requestCompileAssembly >
878788 | ReturnType < typeof receiveCompileAssemblySuccess >
879789 | ReturnType < typeof receiveCompileAssemblyFailure >
@@ -916,5 +826,4 @@ export type Action =
916826 | ReturnType < typeof websocketDisconnected >
917827 | ReturnType < typeof websocketFeatureFlagEnabled >
918828 | ReturnType < typeof wsExecuteRequest >
919- | WSExecuteResponse
920829 ;
0 commit comments