Skip to content

Commit 8e7f106

Browse files
Common signal for config
1 parent a184b4b commit 8e7f106

5 files changed

Lines changed: 52 additions & 88 deletions

File tree

packages/dev-playground/src/CompilerApi.res

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ let defaultWarnFlags = "+a-4-9-20-40-41-42-50-61-102-109"
6262

6363
let defaultCompilerVersion = Env.viteDefaultCompilerVersion->Option.getOr("local")
6464

65+
let defaultConfig: PlaygroundConfig.t = {
66+
compilerVersion: defaultCompilerVersion,
67+
moduleSystem: Esmodule,
68+
warnFlags: defaultWarnFlags,
69+
jsxPreserveMode: false,
70+
experimentalFeatures: [],
71+
}
72+
6573
let pathFromBase = relativePath => {
6674
let baseUrl = switch Env.viteBaseUrl {
6775
| Some("") | None => "/"
@@ -92,7 +100,7 @@ let parseCompilerVersions = defaultVersion => {
92100
}
93101
}
94102

95-
let availableCompilerVersions = parseCompilerVersions(defaultCompilerVersion)
103+
let availableCompilerVersions = parseCompilerVersions(defaultConfig.compilerVersion)
96104
let compilerRoot = pathFromBase("playground-bundles")
97105
let loadedScripts: Map.t<string, promise<unit>> = Map.make()
98106
let compilerApis: Map.t<string, compilerApi> = Map.make()
@@ -106,7 +114,7 @@ let hasFunction = (value, name) =>
106114
| None => false
107115
}
108116

109-
let versionOrDefault = version => version === "" ? defaultCompilerVersion : version
117+
let versionOrDefault = version => version === "" ? defaultConfig.compilerVersion : version
110118

111119
let createScriptLoadPromise = src =>
112120
Promise.make((resolve, reject) => {
@@ -145,7 +153,7 @@ let applyConfig = (
145153
instance->Instance.setModuleSystem((moduleSystem :> string))
146154
}
147155
if hasFunction(instance, "setWarnFlags") {
148-
instance->Instance.setWarnFlags(warnFlags === "" ? defaultWarnFlags : warnFlags)
156+
instance->Instance.setWarnFlags(warnFlags === "" ? defaultConfig.warnFlags : warnFlags)
149157
}
150158
if hasFunction(instance, "setFilename") {
151159
instance->Instance.setFilename("Playground.res")
@@ -181,15 +189,15 @@ let normalizeConfig = (configValue: option<compilerConfig>): normalizedConfig =>
181189
switch configValue {
182190
| None => {
183191
moduleSystem: Esmodule,
184-
warnFlags: defaultWarnFlags,
192+
warnFlags: defaultConfig.warnFlags,
185193
jsxPreserveMode: false,
186194
experimentalFeatures: [],
187195
}
188196
| Some(configValue) => {
189197
moduleSystem: configValue->moduleSystemFromConfig,
190198
warnFlags: switch configValue->Config.warnFlags {
191199
| Some(warnFlags) => warnFlags
192-
| None => defaultWarnFlags
200+
| None => defaultConfig.warnFlags
193201
},
194202
jsxPreserveMode: switch configValue->Config.jsxPreserveMode {
195203
| Some(jsxPreserveMode) => jsxPreserveMode
@@ -346,7 +354,7 @@ let ensureCompiler = async version => {
346354
applyConfig(
347355
instance,
348356
~moduleSystem=Esmodule,
349-
~warnFlags=defaultWarnFlags,
357+
~warnFlags=defaultConfig.warnFlags,
350358
~jsxPreserveMode=false,
351359
~experimentalFeatures=[],
352360
)

packages/dev-playground/src/CompilerApi.resi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ type failure = {
3333
time: float,
3434
}
3535

36-
let defaultWarnFlags: string
37-
let defaultCompilerVersion: string
38-
let availableCompilerVersions: array<Version.t>
39-
4036
type compileResult = result<success, failure>
4137
type formatResult = result<string, failure>
4238

39+
let defaultConfig: PlaygroundConfig.t
40+
41+
let availableCompilerVersions: array<Version.t>
42+
4343
let init: string => promise<info>
4444

4545
let compile: (string, PlaygroundConfig.t) => promise<compileResult>

packages/dev-playground/src/Main.res

Lines changed: 28 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,14 @@ module SettingsPanel = {
437437
let make = (
438438
~activeTab: Signal.t<tab>,
439439
~compilerInfo: Signal.t<option<CompilerApi.info>>,
440-
~compilerVersion: Signal.t<string>,
441-
~moduleSystem: Signal.t<moduleSystem>,
442-
~warnFlags: Signal.t<string>,
443-
~jsxPreserveMode: Signal.t<bool>,
444-
~experimentalFeatures: Signal.t<array<experimentalFeature>>,
440+
~config: Signal.t<PlaygroundConfig.t>,
445441
~switchCompiler: string => unit,
446442
~compileNow: unit => unit,
447443
~scheduleCompile: unit => unit,
448444
~scheduleUrlSync: unit => unit,
449445
) => {
446+
let updateConfig = f => Signal.update(config, f)
447+
450448
<div
451449
class={() =>
452450
Signal.get(activeTab) === Settings ? "settings-panel" : "settings-panel hidden-panel"}
@@ -457,10 +455,10 @@ module SettingsPanel = {
457455
</label>
458456
<select
459457
id="compiler-version"
460-
value={ReactiveProp.reactive(compilerVersion)}
458+
value={() => Signal.get(config).compilerVersion}
461459
onChange={event => {
462460
let nextVersion = Event.value(event)
463-
Signal.set(compilerVersion, nextVersion)
461+
updateConfig(config => {...config, compilerVersion: nextVersion})
464462
switchCompiler(nextVersion)
465463
}}
466464
>
@@ -486,11 +484,11 @@ module SettingsPanel = {
486484
<label class="setting-label" for_="module-system"> {Node.text("Module System")} </label>
487485
<select
488486
id="module-system"
489-
value={() => (Signal.get(moduleSystem) :> string)}
487+
value={() => (Signal.get(config).moduleSystem :> string)}
490488
onChange={event => {
491489
switch event->Event.value->parseModuleSystem {
492490
| Some(nextModuleSystem) =>
493-
Signal.set(moduleSystem, nextModuleSystem)
491+
updateConfig(config => {...config, moduleSystem: nextModuleSystem})
494492
scheduleUrlSync()
495493
compileNow()
496494
| None => ()
@@ -509,18 +507,18 @@ module SettingsPanel = {
509507
<label class="setting-label" for_="warning-flags"> {Node.text("Warning Flags")} </label>
510508
<input
511509
id="warning-flags"
512-
value={ReactiveProp.reactive(warnFlags)}
510+
value={() => Signal.get(config).warnFlags}
513511
spellcheck=false
514512
onInput={event => {
515-
Signal.set(warnFlags, Event.value(event))
513+
updateConfig(config => {...config, warnFlags: Event.value(event)})
516514
scheduleUrlSync()
517515
scheduleCompile()
518516
}}
519517
/>
520518
<button
521519
class="secondary-action"
522520
onClick={_ => {
523-
Signal.set(warnFlags, CompilerApi.defaultWarnFlags)
521+
updateConfig(config => {...config, warnFlags: CompilerApi.defaultConfig.warnFlags})
524522
scheduleUrlSync()
525523
compileNow()
526524
}}
@@ -532,9 +530,9 @@ module SettingsPanel = {
532530
<input
533531
id="jsx-preserve"
534532
type_="checkbox"
535-
checked={ReactiveProp.reactive(jsxPreserveMode)}
533+
checked={() => Signal.get(config).jsxPreserveMode}
536534
onChange={event => {
537-
Signal.set(jsxPreserveMode, Event.checked(event))
535+
updateConfig(config => {...config, jsxPreserveMode: Event.checked(event)})
538536
scheduleUrlSync()
539537
compileNow()
540538
}}
@@ -545,9 +543,12 @@ module SettingsPanel = {
545543
<input
546544
id="feature-let-unwrap"
547545
type_="checkbox"
548-
checked={() => Signal.get(experimentalFeatures)->hasFeature(LetUnwrap)}
546+
checked={() => Signal.get(config).experimentalFeatures->hasFeature(LetUnwrap)}
549547
onChange={_ => {
550-
Signal.update(experimentalFeatures, features => toggleFeature(features, LetUnwrap))
548+
updateConfig(config => {
549+
...config,
550+
experimentalFeatures: toggleFeature(config.experimentalFeatures, LetUnwrap),
551+
})
551552
scheduleUrlSync()
552553
compileNow()
553554
}}
@@ -593,26 +594,12 @@ module StatusBadge = {
593594
module App = {
594595
@jsx.component
595596
let make = () => {
596-
let defaultConfig = {
597-
PlaygroundConfig.compilerVersion: CompilerApi.defaultCompilerVersion,
598-
moduleSystem: Esmodule,
599-
warnFlags: CompilerApi.defaultWarnFlags,
600-
jsxPreserveMode: false,
601-
experimentalFeatures: [],
602-
}
603-
604597
let source = Signal.make(defaultSource)
605598
let activeTab = Signal.make(JavaScript)
606599
let status = Signal.make(Loading)
607600
let compilerInfo: Signal.t<option<CompilerApi.info>> = Signal.make(None)
608601
let compileResult: Signal.t<option<CompilerApi.compileResult>> = Signal.make(None)
609-
let compilerVersion = Signal.make(defaultConfig.compilerVersion)
610-
let moduleSystem = Signal.make(defaultConfig.moduleSystem)
611-
let warnFlags = Signal.make(defaultConfig.warnFlags)
612-
let jsxPreserveMode = Signal.make(defaultConfig.jsxPreserveMode)
613-
let experimentalFeatures: Signal.t<array<experimentalFeature>> = Signal.make(
614-
defaultConfig.experimentalFeatures,
615-
)
602+
let config = Signal.make(CompilerApi.defaultConfig)
616603
let activeLine = Signal.make(1)
617604
let editorScrollTop = Signal.make(0)
618605
let editorScrollLeft = Signal.make(0)
@@ -641,14 +628,6 @@ module App = {
641628
Signal.set(editorScrollLeft, Event.scrollLeft(event))
642629
}
643630

644-
let currentConfig = () => {
645-
PlaygroundConfig.compilerVersion: Signal.peek(compilerVersion),
646-
moduleSystem: Signal.peek(moduleSystem),
647-
warnFlags: Signal.peek(warnFlags),
648-
jsxPreserveMode: Signal.peek(jsxPreserveMode),
649-
experimentalFeatures: Signal.peek(experimentalFeatures),
650-
}
651-
652631
let compileNow = () => {
653632
compileSequence := compileSequence.contents + 1
654633
let sequence = compileSequence.contents
@@ -660,7 +639,7 @@ module App = {
660639
| Ready | Compiling =>
661640
Signal.set(status, Compiling)
662641
try {
663-
let result = await CompilerApi.compile(Signal.peek(source), currentConfig())
642+
let result = await CompilerApi.compile(Signal.peek(source), Signal.peek(config))
664643
if sequence === compileSequence.contents {
665644
Signal.set(compileResult, Some(result))
666645
Signal.set(status, Ready)
@@ -690,7 +669,7 @@ module App = {
690669
}
691670

692671
let syncUrlNow = () =>
693-
UrlState.replace(~source=Signal.peek(source), ~config=currentConfig())->Promise.ignore
672+
UrlState.replace(~source=Signal.peek(source), ~config=Signal.peek(config))->Promise.ignore
694673

695674
let scheduleUrlSync = () => {
696675
switch urlTimerId.contents {
@@ -712,7 +691,7 @@ module App = {
712691
| Ready | Compiling =>
713692
Signal.set(status, Compiling)
714693
try {
715-
switch await CompilerApi.format(sourceBeforeFormat, currentConfig()) {
694+
switch await CompilerApi.format(sourceBeforeFormat, Signal.peek(config)) {
716695
| Ok(formattedSource) =>
717696
if sequence === compileSequence.contents {
718697
if Signal.peek(source) === sourceBeforeFormat {
@@ -765,7 +744,7 @@ module App = {
765744
}
766745

767746
let share = async () => {
768-
switch await UrlState.copy(~source=Signal.peek(source), ~config=currentConfig()) {
747+
switch await UrlState.copy(~source=Signal.peek(source), ~config=Signal.peek(config)) {
769748
| Ok() => showToast("Link copied")
770749
| Error(message) => showToast(message)
771750
}
@@ -787,8 +766,8 @@ module App = {
787766
if sequence === compilerLoadSequence.contents {
788767
let firstLoadConfigValue = firstLoadConfig.contents
789768
firstLoadConfig := None
790-
let config = switch firstLoadConfigValue {
791-
| Some(config) => config
769+
let nextConfig = switch firstLoadConfigValue {
770+
| Some(config) => {...config, compilerVersion: info.bundleId}
792771
| None => {
793772
PlaygroundConfig.compilerVersion: info.bundleId,
794773
moduleSystem: info.moduleSystem,
@@ -798,11 +777,7 @@ module App = {
798777
}
799778
}
800779
Signal.set(compilerInfo, Some(info))
801-
Signal.set(compilerVersion, info.bundleId)
802-
Signal.set(moduleSystem, config.moduleSystem)
803-
Signal.set(warnFlags, config.warnFlags)
804-
Signal.set(jsxPreserveMode, config.jsxPreserveMode)
805-
Signal.set(experimentalFeatures, config.experimentalFeatures)
780+
Signal.set(config, nextConfig)
806781
Signal.set(status, Ready)
807782
switch firstLoadConfigValue {
808783
| Some(_) => ()
@@ -831,17 +806,9 @@ module App = {
831806

832807
Effect.run(() => {
833808
let start = async () => {
834-
let urlState = await UrlState.init(
835-
~defaultSource,
836-
~defaultConfig,
837-
~availableCompilerVersions=CompilerApi.availableCompilerVersions,
838-
)
809+
let urlState = await UrlState.init(~defaultSource)
839810
Signal.set(source, urlState.source)
840-
Signal.set(compilerVersion, urlState.config.compilerVersion)
841-
Signal.set(moduleSystem, urlState.config.moduleSystem)
842-
Signal.set(warnFlags, urlState.config.warnFlags)
843-
Signal.set(jsxPreserveMode, urlState.config.jsxPreserveMode)
844-
Signal.set(experimentalFeatures, urlState.config.experimentalFeatures)
811+
Signal.set(config, urlState.config)
845812
Signal.set(activeLine, 1)
846813
Signal.set(editorScrollTop, 0)
847814
Signal.set(editorScrollLeft, 0)
@@ -961,17 +928,7 @@ module App = {
961928
<Problems compileResult />
962929
</div>
963930
<SettingsPanel
964-
activeTab
965-
compilerInfo
966-
compilerVersion
967-
moduleSystem
968-
warnFlags
969-
jsxPreserveMode
970-
experimentalFeatures
971-
switchCompiler
972-
compileNow
973-
scheduleCompile
974-
scheduleUrlSync
931+
activeTab compilerInfo config switchCompiler compileNow scheduleCompile scheduleUrlSync
975932
/>
976933
</div>
977934
</section>

packages/dev-playground/src/UrlState.res

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ let queryConfig = (
104104
}
105105
}
106106

107-
let init = async (~defaultSource, ~defaultConfig, ~availableCompilerVersions): state => {
107+
let init = async (~defaultSource): state => {
108108
let source = await initialSource(defaultSource)
109-
let config = queryConfig(~defaultConfig, ~availableCompilerVersions)
109+
let config = queryConfig(
110+
~defaultConfig=CompilerApi.defaultConfig,
111+
~availableCompilerVersions=CompilerApi.availableCompilerVersions,
112+
)
110113
{source, config}
111114
}
112115

packages/dev-playground/src/UrlState.resi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ type state = {
33
config: PlaygroundConfig.t,
44
}
55

6-
let init: (
7-
~defaultSource: string,
8-
~defaultConfig: PlaygroundConfig.t,
9-
~availableCompilerVersions: array<CompilerApi.Version.t>,
10-
) => promise<state>
6+
let init: (~defaultSource: string) => promise<state>
117

128
let replace: (~source: string, ~config: PlaygroundConfig.t) => promise<unit>
139

0 commit comments

Comments
 (0)