Skip to content

Commit cf75619

Browse files
Resi
1 parent 9c0c4c5 commit cf75619

5 files changed

Lines changed: 138 additions & 69 deletions

File tree

packages/dev-playground/src/CompilerApi.res

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ type failure = {
4848
time: float,
4949
}
5050

51-
type formatResult =
52-
| Formatted(string)
53-
| FormatFailed(failure)
51+
type compileResult = result<success, failure>
52+
type formatResult = result<string, failure>
5453

5554
type normalizedConfig = {
5655
moduleSystem: PlaygroundConfig.moduleSystem,
@@ -266,8 +265,6 @@ let failureFromCompileOutput = (compileOutput, elapsedMs): failure => {
266265
{errors, warnings, message, time: elapsedMs}
267266
}
268267

269-
type compileResult = result<success, failure>
270-
271268
let normalize = (compileOutput, elapsedMs): compileResult => {
272269
switch (
273270
compileOutput->CompileResult.parsetree,
@@ -454,16 +451,16 @@ let format = async (source, config: PlaygroundConfig.t) => {
454451

455452
if formatOutput->resultIsSuccess {
456453
switch formatOutput->CompileResult.code {
457-
| Some(code) => Formatted(code)
454+
| Some(code) => Ok(code)
458455
| None =>
459-
FormatFailed({
456+
Error({
460457
errors: [],
461458
warnings: [],
462459
message: "Formatting did not return code",
463460
time: elapsedMs,
464461
})
465462
}
466463
} else {
467-
FormatFailed(failureFromCompileOutput(formatOutput, elapsedMs))
464+
Error(failureFromCompileOutput(formatOutput, elapsedMs))
468465
}
469466
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Version: {
2+
type t = {
3+
id: string,
4+
label: string,
5+
}
6+
}
7+
8+
type info = {
9+
bundleId: string,
10+
version: string,
11+
apiVersion: string,
12+
moduleSystem: PlaygroundConfig.moduleSystem,
13+
warnFlags: string,
14+
jsxPreserveMode: bool,
15+
experimentalFeatures: array<PlaygroundConfig.experimentalFeature>,
16+
libraries: array<string>,
17+
}
18+
19+
type success = {
20+
jsCode: string,
21+
parsetree: string,
22+
typedtree: string,
23+
lambda: string,
24+
lam: string,
25+
warnings: array<string>,
26+
time: float,
27+
}
28+
29+
type failure = {
30+
errors: array<string>,
31+
warnings: array<string>,
32+
message: string,
33+
time: float,
34+
}
35+
36+
let defaultWarnFlags: string
37+
let defaultCompilerVersion: string
38+
let availableCompilerVersions: array<Version.t>
39+
40+
type compileResult = result<success, failure>
41+
type formatResult = result<string, failure>
42+
43+
let init: string => promise<info>
44+
45+
let compile: (string, PlaygroundConfig.t) => promise<compileResult>
46+
47+
let format: (string, PlaygroundConfig.t) => promise<formatResult>

packages/dev-playground/src/Main.res

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let jsErrorMessage = obj =>
6060
| None => "Unknown JavaScript error"
6161
}
6262

63-
let insertTabIndent = (event: Dom.event): option<string> => {
63+
let insertTabIndent = (event: Dom.event): option<string> =>
6464
if event->Event.key !== "Tab" {
6565
None
6666
} else {
@@ -78,9 +78,8 @@ let insertTabIndent = (event: Dom.event): option<string> => {
7878

7979
Some(nextValue)
8080
}
81-
}
8281

83-
let configureSourceEditor = (scrollHandler: Dom.event => unit): unit => {
82+
let configureSourceEditor = (scrollHandler: Dom.event => unit): unit =>
8483
Window.requestAnimationFrame(() =>
8584
switch Document.current->Document.getElementById("source-editor") {
8685
| None => ()
@@ -98,7 +97,6 @@ let configureSourceEditor = (scrollHandler: Dom.event => unit): unit => {
9897
}
9998
}
10099
)
101-
}
102100

103101
let lineNumbersText = source => {
104102
let lineCount = source->String.split("\n")->Array.length
@@ -596,12 +594,14 @@ module App = {
596594
@jsx.component
597595
let make = () => {
598596
let requestedCompilerVersion = UrlState.queryCompilerVersion(CompilerApi.defaultCompilerVersion)
597+
599598
let initialCompilerVersion =
600599
CompilerApi.availableCompilerVersions->Array.some(version =>
601600
version.id === requestedCompilerVersion
602601
)
603602
? requestedCompilerVersion
604603
: CompilerApi.defaultCompilerVersion
604+
605605
let initialModuleSystem = UrlState.queryModuleSystem(Esmodule)
606606
let initialWarnFlags = UrlState.queryWarnFlags(CompilerApi.defaultWarnFlags)
607607
let initialJsxPreserveMode = UrlState.queryJsxPreserveMode(false)
@@ -636,6 +636,7 @@ module App = {
636636
let syncEditorState = event => {
637637
let currentSource = Event.value(event)
638638
let cursorPosition = cursorPositionForOffset(currentSource, Event.selectionStart(event))
639+
639640
Signal.set(editorScrollTop, Event.scrollTop(event))
640641
Signal.set(editorScrollLeft, Event.scrollLeft(event))
641642
Signal.set(activeLine, cursorPosition.line)
@@ -694,16 +695,15 @@ module App = {
694695
timerId := Some(Window.setTimeout(compileNow, 280))
695696
}
696697

697-
let syncUrlNow = () => {
698+
let syncUrlNow = () =>
698699
UrlState.replaceUrlState(
699-
Signal.peek(source),
700-
Signal.peek(compilerVersion),
701-
Signal.peek(moduleSystem),
702-
Signal.peek(warnFlags),
703-
Signal.peek(jsxPreserveMode),
704-
Signal.peek(experimentalFeatures),
705-
)->ignore
706-
}
700+
~source=Signal.peek(source),
701+
~compilerVersion=Signal.peek(compilerVersion),
702+
~moduleSystem=Signal.peek(moduleSystem),
703+
~warnFlags=Signal.peek(warnFlags),
704+
~jsxPreserveMode=Signal.peek(jsxPreserveMode),
705+
~experimentalFeatures=Signal.peek(experimentalFeatures),
706+
)->Promise.ignore
707707

708708
let scheduleUrlSync = () => {
709709
switch urlTimerId.contents {
@@ -726,7 +726,7 @@ module App = {
726726
Signal.set(status, Compiling)
727727
try {
728728
switch await CompilerApi.format(sourceBeforeFormat, currentConfig()) {
729-
| Formatted(formattedSource) =>
729+
| Ok(formattedSource) =>
730730
if sequence === compileSequence.contents {
731731
if Signal.peek(source) === sourceBeforeFormat {
732732
Signal.set(source, formattedSource)
@@ -740,7 +740,7 @@ module App = {
740740
Signal.set(status, Ready)
741741
}
742742
}
743-
| FormatFailed(failure) =>
743+
| Error(failure) =>
744744
if sequence === compileSequence.contents {
745745
Signal.set(compileResult, Some(Error(failure)))
746746
Signal.set(status, Ready)
@@ -779,12 +779,12 @@ module App = {
779779

780780
let share = async () => {
781781
switch await UrlState.copyUrlState(
782-
Signal.peek(source),
783-
Signal.peek(compilerVersion),
784-
Signal.peek(moduleSystem),
785-
Signal.peek(warnFlags),
786-
Signal.peek(jsxPreserveMode),
787-
Signal.peek(experimentalFeatures),
782+
~source=Signal.peek(source),
783+
~compilerVersion=Signal.peek(compilerVersion),
784+
~moduleSystem=Signal.peek(moduleSystem),
785+
~warnFlags=Signal.peek(warnFlags),
786+
~jsxPreserveMode=Signal.peek(jsxPreserveMode),
787+
~experimentalFeatures=Signal.peek(experimentalFeatures),
788788
) {
789789
| Ok() => showToast("Link copied")
790790
| Error(message) => showToast(message)

packages/dev-playground/src/UrlState.res

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
open PlaygroundConfig
2-
31
let maxEncodedCodeLength = 300 * 1024
42
let maxDecodedSourceLength = 200 * 1024
53
let replaceSequence = ref(0)
@@ -8,16 +6,13 @@ let getParam = name => {
86
UrlSearchParams.make(Location.search)->UrlSearchParams.get(name)
97
}
108

11-
let encodeCode = SharedCode.encode
12-
let decodeCode = SharedCode.decode
13-
149
let applyUrlState = (
15-
encoded,
16-
compilerVersion,
17-
moduleSystem: moduleSystem,
18-
warnFlags,
19-
jsxPreserveMode,
20-
experimentalFeatures: array<experimentalFeature>,
10+
~encoded,
11+
~compilerVersion,
12+
~moduleSystem: PlaygroundConfig.moduleSystem,
13+
~warnFlags,
14+
~jsxPreserveMode,
15+
~experimentalFeatures: array<PlaygroundConfig.experimentalFeature>,
2116
) => {
2217
let moduleSystem = (moduleSystem :> string)
2318
let experimentalFeatures = experimentalFeatures->Array.map(feature => (feature :> string))
@@ -50,7 +45,7 @@ let initialSource = async defaultSource => {
5045
| Some(encoded)
5146
if encoded === "" || encoded->String.length > maxEncodedCodeLength => defaultSource
5247
| Some(encoded) =>
53-
switch await decodeCode(encoded) {
48+
switch await SharedCode.decode(encoded) {
5449
| decoded => decoded->String.length <= maxDecodedSourceLength ? decoded : defaultSource
5550
| exception error =>
5651
Console.warn2("Could not restore shared playground source", error)
@@ -68,7 +63,7 @@ let queryCompilerVersion = defaultVersion =>
6863
let queryModuleSystem = defaultModuleSystem =>
6964
switch getParam("module") {
7065
| Some(value) =>
71-
switch value->parseModuleSystem {
66+
switch value->PlaygroundConfig.parseModuleSystem {
7267
| Some(moduleSystem) => moduleSystem
7368
| None => defaultModuleSystem
7469
}
@@ -90,47 +85,48 @@ let queryJsxPreserveMode = defaultValue =>
9085
let queryExperimentalFeatures = () =>
9186
switch getParam("experimental") {
9287
| Some(value) if value !== "" =>
93-
value->String.split(",")->Array.filterMap(parseExperimentalFeature)
88+
value->String.split(",")->Array.filterMap(PlaygroundConfig.parseExperimentalFeature)
9489
| _ => []
9590
}
9691

9792
let replaceUrlState = async (
98-
source,
99-
compilerVersion,
100-
moduleSystem,
101-
warnFlags,
102-
jsxPreserveMode,
103-
experimentalFeatures,
93+
~source,
94+
~compilerVersion,
95+
~moduleSystem,
96+
~warnFlags,
97+
~jsxPreserveMode,
98+
~experimentalFeatures,
10499
) => {
105100
replaceSequence := replaceSequence.contents + 1
106101
let sequence = replaceSequence.contents
107-
let encoded = await encodeCode(source)
102+
let encoded = await SharedCode.encode(source)
103+
108104
if sequence === replaceSequence.contents {
109105
applyUrlState(
110-
encoded,
111-
compilerVersion,
112-
moduleSystem,
113-
warnFlags,
114-
jsxPreserveMode,
115-
experimentalFeatures,
106+
~encoded,
107+
~compilerVersion,
108+
~moduleSystem,
109+
~warnFlags,
110+
~jsxPreserveMode,
111+
~experimentalFeatures,
116112
)
117113
}
118114
}
119115

120116
let windowHref = () => Location.href
121117

122118
let copyUrlState = async (
123-
source,
124-
compilerVersion,
125-
moduleSystem,
126-
warnFlags,
127-
jsxPreserveMode,
128-
experimentalFeatures,
119+
~source,
120+
~compilerVersion,
121+
~moduleSystem,
122+
~warnFlags,
123+
~jsxPreserveMode,
124+
~experimentalFeatures,
129125
): result<unit, string> => {
130126
replaceSequence := replaceSequence.contents + 1
131127
let sequence = replaceSequence.contents
132128

133-
let? Ok(encoded) = switch await encodeCode(source) {
129+
let? Ok(encoded) = switch await SharedCode.encode(source) {
134130
| encoded => Ok(encoded)
135131
| exception _ => Error("Could not copy link")
136132
}
@@ -139,12 +135,12 @@ let copyUrlState = async (
139135
Error("Link changed before it could be copied")
140136
} else {
141137
applyUrlState(
142-
encoded,
143-
compilerVersion,
144-
moduleSystem,
145-
warnFlags,
146-
jsxPreserveMode,
147-
experimentalFeatures,
138+
~encoded,
139+
~compilerVersion,
140+
~moduleSystem,
141+
~warnFlags,
142+
~jsxPreserveMode,
143+
~experimentalFeatures,
148144
)
149145

150146
let href = windowHref()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
let initialSource: string => promise<string>
2+
3+
let queryCompilerVersion: string => string
4+
5+
let queryModuleSystem: PlaygroundConfig.moduleSystem => PlaygroundConfig.moduleSystem
6+
7+
let queryWarnFlags: string => string
8+
9+
let queryJsxPreserveMode: bool => bool
10+
11+
let queryExperimentalFeatures: unit => array<PlaygroundConfig.experimentalFeature>
12+
13+
let replaceUrlState: (
14+
~source: string,
15+
~compilerVersion: string,
16+
~moduleSystem: PlaygroundConfig.moduleSystem,
17+
~warnFlags: string,
18+
~jsxPreserveMode: bool,
19+
~experimentalFeatures: array<PlaygroundConfig.experimentalFeature>,
20+
) => promise<unit>
21+
22+
let copyUrlState: (
23+
~source: string,
24+
~compilerVersion: string,
25+
~moduleSystem: PlaygroundConfig.moduleSystem,
26+
~warnFlags: string,
27+
~jsxPreserveMode: bool,
28+
~experimentalFeatures: array<PlaygroundConfig.experimentalFeature>,
29+
) => promise<result<unit, string>>

0 commit comments

Comments
 (0)