Skip to content

Commit d4b9e4d

Browse files
committed
simplify states
1 parent af092fe commit d4b9e4d

File tree

6 files changed

+55
-102
lines changed

6 files changed

+55
-102
lines changed

src/OutputPanel.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
let make = (~compilerState, ~appendLog) => {
33
let validReact = switch compilerState {
44
| CompilerManagerHook.Executing({state: {validReactCode: true}})
5-
| Compiling({validReactCode: true})
5+
| Compiling({state: {validReactCode: true}})
66
| Ready({validReactCode: true}) => true
77
| _ => false
88
}
99

1010
let logs = switch compilerState {
1111
| CompilerManagerHook.Executing({state: {logs}})
12-
| Compiling({logs})
12+
| Compiling({state: {logs}})
1313
| Ready({logs}) => logs
1414
| _ => []
1515
}

src/Playground.res

Lines changed: 17 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,20 +1038,6 @@ module Settings = {
10381038
}
10391039

10401040
module ControlPanel = {
1041-
let codeFromResult = (result: FinalResult.t): string => {
1042-
open Api
1043-
switch result {
1044-
| FinalResult.Comp(comp) =>
1045-
switch comp {
1046-
| CompilationResult.Success({js_code}) => js_code
1047-
| UnexpectedError(_)
1048-
| Unknown(_, _)
1049-
| Fail(_) => "/* No JS code generated */"
1050-
}
1051-
| Nothing
1052-
| Conv(_) => "/* No JS code generated */"
1053-
}
1054-
}
10551041
module Button = {
10561042
@react.component
10571043
let make = (~children, ~onClick=?) =>
@@ -1144,7 +1130,7 @@ module ControlPanel = {
11441130

11451131
let autoRun = switch state {
11461132
| CompilerManagerHook.Executing({state: {autoRun: true}})
1147-
| Compiling({autoRun: true})
1133+
| Compiling({state: {autoRun: true}})
11481134
| Ready({autoRun: true}) => true
11491135
| _ => false
11501136
}
@@ -1211,77 +1197,24 @@ module OutputPanel = {
12111197
~editorCode: React.ref<string>,
12121198
~currentTab: tab,
12131199
) => {
1214-
/*
1215-
We need the prevState to understand different
1216-
state transitions, and to be able to keep displaying
1217-
old results until those transitions are done.
1218-
1219-
Goal was to reduce the UI flickering during different
1220-
state transitions
1221-
*/
1222-
let prevState = React.useRef(None)
1223-
1224-
let cmCode = switch prevState.current {
1225-
| Some(prev) =>
1226-
switch (prev, compilerState) {
1227-
| (_, Ready({result: Nothing})) => None
1228-
| (Ready(prevReady), Ready(ready)) =>
1229-
switch (prevReady.result, ready.result) {
1230-
| (_, Comp(Success(_))) => ControlPanel.codeFromResult(ready.result)->Some
1231-
| _ => None
1232-
}
1233-
| (_, Ready({result: Comp(Success(_)) as result})) =>
1234-
ControlPanel.codeFromResult(result)->Some
1235-
| (Ready({result: Comp(Success(_)) as result}), Compiling(_)) =>
1236-
ControlPanel.codeFromResult(result)->Some
1237-
| (_, Executing({jsCode})) => Some(jsCode)
1238-
| _ => None
1239-
}
1240-
| None =>
1241-
switch compilerState {
1242-
| Ready(ready) => ControlPanel.codeFromResult(ready.result)->Some
1243-
| _ => None
1244-
}
1245-
}
1246-
1247-
prevState.current = Some(compilerState)
1248-
1249-
let resultPane = switch compilerState {
1250-
| Compiling(ready)
1251-
| Ready(ready)
1252-
| Executing({state: ready}) =>
1253-
switch ready.result {
1254-
| Comp(Success(_))
1255-
| Conv(Success(_)) => React.null
1256-
| _ =>
1257-
<ResultPane
1258-
targetLang=ready.targetLang
1259-
compilerVersion=ready.selected.compilerVersion
1260-
result=ready.result
1261-
/>
1262-
}
1263-
1264-
| _ => React.null
1265-
}
1266-
1267-
let (code, showCm) = switch cmCode {
1268-
| None => ("", false)
1269-
| Some(code) => (code, true)
1270-
}
1271-
1272-
let codeElement =
1273-
<pre className={"whitespace-pre-wrap p-4 " ++ (showCm ? "block" : "hidden")}>
1274-
{HighlightJs.renderHLJS(~code, ~darkmode=true, ~lang="js", ())}
1275-
</pre>
1276-
12771200
let output =
12781201
<div className="text-gray-20">
1279-
resultPane
1280-
codeElement
1202+
{switch compilerState {
1203+
| Compiling({previousJsCode: Some(jsCode)})
1204+
| Executing({jsCode})
1205+
| Ready({result: Comp(Success({jsCode}))}) =>
1206+
<pre className={"whitespace-pre-wrap p-4 "}>
1207+
{HighlightJs.renderHLJS(~code=jsCode, ~darkmode=true, ~lang="js", ())}
1208+
</pre>
1209+
| Ready({result: Conv(Success(_))}) => React.null
1210+
| Ready({result, targetLang, selected}) =>
1211+
<ResultPane targetLang compilerVersion=selected.compilerVersion result />
1212+
| _ => React.null
1213+
}}
12811214
</div>
12821215

12831216
let errorPane = switch compilerState {
1284-
| Compiling(ready)
1217+
| Compiling({state: ready})
12851218
| Ready(ready)
12861219
| Executing({state: ready})
12871220
| SwitchingCompiler(ready, _) =>
@@ -1296,7 +1229,7 @@ module OutputPanel = {
12961229

12971230
let settingsPane = switch compilerState {
12981231
| Ready(ready)
1299-
| Compiling(ready)
1232+
| Compiling({state: ready})
13001233
| Executing({state: ready})
13011234
| SwitchingCompiler(ready, _) =>
13021235
let config = ready.selected.config
@@ -1671,8 +1604,8 @@ let make = (~versions: array<string>) => {
16711604
}
16721605

16731606
let cmHoverHints = switch compilerState {
1674-
| Ready({result: FinalResult.Comp(Success({type_hints}))}) =>
1675-
Array.map(type_hints, hint => {
1607+
| Ready({result: FinalResult.Comp(Success({typeHints}))}) =>
1608+
Array.map(typeHints, hint => {
16761609
switch hint {
16771610
| TypeDeclaration({start, end, hint})
16781611
| Binding({start, end, hint})

src/bindings/RescriptCompilerApi.res

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,20 @@ module TypeHint = {
237237

238238
module CompileSuccess = {
239239
type t = {
240-
js_code: string,
240+
@as("js_code")
241+
jsCode: string,
241242
warnings: array<Warning.t>,
242-
type_hints: array<TypeHint.t>,
243+
@as("type_hints")
244+
typeHints: array<TypeHint.t>,
243245
time: float, // total compilation time
244246
}
245247

246248
let decode = (~time: float, json): t => {
247249
open Json.Decode
248250
{
249-
js_code: field("js_code", string, json),
251+
jsCode: field("js_code", string, json),
250252
warnings: field("warnings", array(Warning.decode, ...), json),
251-
type_hints: withDefault([], field("type_hints", array(TypeHint.decode, ...), ...), json),
253+
typeHints: withDefault([], field("type_hints", array(TypeHint.decode, ...), ...), json),
252254
time,
253255
}
254256
}

src/bindings/RescriptCompilerApi.resi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ module TypeHint: {
104104

105105
module CompileSuccess: {
106106
type t = {
107-
js_code: string,
107+
@as("js_code")
108+
jsCode: string,
108109
warnings: array<Warning.t>,
109-
type_hints: array<TypeHint.t>, // Not supported in older versions <= 9.0.1 (will always be [])
110+
@as("type_hints")
111+
typeHints: array<TypeHint.t>, // Not supported in older versions <= 9.0.1 (will always be [])
110112
time: float, // total compilation time
111113
}
112114

src/common/CompilerManagerHook.res

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ type state =
178178
| SetupFailed(string)
179179
| SwitchingCompiler(ready, Semver.t) // (ready, targetId, libraries)
180180
| Ready(ready)
181-
| Compiling(ready)
181+
| Compiling({state: ready, previousJsCode: option<string>})
182182
| Executing({state: ready, jsCode: string})
183183

184184
type action =
@@ -245,12 +245,19 @@ let useCompilerManager = (
245245
| Ready(ready) =>
246246
ready.selected.instance->Compiler.setConfig(config)
247247
let selected = {...ready.selected, config}
248-
Compiling({...ready, selected})
248+
Compiling({state: {...ready, selected}, previousJsCode: None})
249249
| _ => state
250250
}
251251
| CompileCode(lang, code) =>
252252
switch state {
253-
| Ready(ready) => Compiling({...ready, code, targetLang: lang})
253+
| Ready(ready) =>
254+
Compiling({
255+
state: {...ready, code, targetLang: lang},
256+
previousJsCode: switch ready.result {
257+
| Comp(Success({jsCode})) => Some(jsCode)
258+
| _ => None
259+
},
260+
})
254261
| _ => state
255262
}
256263
| SwitchLanguage({lang, code}) =>
@@ -348,13 +355,23 @@ let useCompilerManager = (
348355
| ToggleAutoRun =>
349356
switch state {
350357
| Ready({autoRun: true} as ready) => Ready({...ready, autoRun: false})
351-
| Ready({autoRun: false} as ready) => Compiling({...ready, autoRun: true})
358+
| Ready({autoRun: false} as ready) =>
359+
Compiling({
360+
state: {
361+
...ready,
362+
autoRun: true,
363+
},
364+
previousJsCode: switch ready.result {
365+
| Comp(Success({jsCode})) => Some(jsCode)
366+
| _ => None
367+
},
368+
})
352369
| _ => state
353370
}
354371
| RunCode =>
355372
switch state {
356-
| Ready({result: Comp(Success({js_code}))} as ready) =>
357-
Executing({state: {...ready, autoRun: false}, jsCode: js_code})
373+
| Ready({result: Comp(Success({jsCode}))} as ready) =>
374+
Executing({state: {...ready, autoRun: false}, jsCode})
358375
| _ => state
359376
}
360377
}
@@ -487,7 +504,7 @@ let useCompilerManager = (
487504

488505
dispatchError(CompilerLoadingError(msg))
489506
}
490-
| Compiling({targetLang: lang, code, autoRun} as ready) =>
507+
| Compiling({state: {targetLang: lang, code, autoRun} as ready}) =>
491508
let apiVersion = ready.selected.apiVersion
492509
let instance = ready.selected.instance
493510

@@ -520,8 +537,7 @@ let useCompilerManager = (
520537
let ready = {...ready, result: FinalResult.Comp(compResult)}
521538
setState(_ =>
522539
switch (ready.result, autoRun) {
523-
| (FinalResult.Comp(Success({js_code})), true) =>
524-
Executing({state: ready, jsCode: js_code})
540+
| (FinalResult.Comp(Success({jsCode})), true) => Executing({state: ready, jsCode})
525541
| _ => Ready(ready)
526542
}
527543
)

src/common/CompilerManagerHook.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type state =
3939
| SetupFailed(string)
4040
| SwitchingCompiler(ready, Semver.t) // (ready, targetId, libraries)
4141
| Ready(ready)
42-
| Compiling(ready)
42+
| Compiling({state: ready, previousJsCode: option<string>})
4343
| Executing({state: ready, jsCode: string})
4444

4545
type action =

0 commit comments

Comments
 (0)