on master help sign in
Make reactor more reliable when compiling Elm code

Rather than writing a file, I just give out the builder for HTML. Technique could be useful for REPL as well, but not sure if it is super easy to give node stdin as input

commit8a0fb69d628c38dd0fcf89bb1b1be957cc5a6123 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 9 Apr 2018, 14:32 (9 years ago) parent495a5f8
2 files changed+28−32
builder/src/Generate/Output.hs+14−12 ui/terminal/src/Develop.hs+14−20
builder/src/Generate/Output.hs+14−12
@@ -11,6 +11,7 @@ module Generate.Output
1111 where
1212
1313
14+import Control.Concurrent.MVar (MVar, putMVar)
1415import Control.Monad.Trans (liftIO)
1516import qualified Data.ByteString.Builder as B
1617import qualified Data.Map as Map
@@ -81,7 +82,7 @@ generateMonolith (Options mode target maybeOutput) (Summary.Summary _ project _
8182 Obj.None ->
8283 return ()
8384
84− Obj.Some name names builder ->
85+ Obj.Some name _names builder ->
8586 let
8687 monolith =
8788 "(function(scope){\n'use strict';"
@@ -92,22 +93,22 @@ generateMonolith (Options mode target maybeOutput) (Summary.Summary _ project _
9293 Nothing ->
9394 IO.writeBuilder "elm.js" monolith
9495
95− Just None ->
96− return ()
96+ Just output_ ->
97+ case output_ of
98+ None ->
99+ return ()
100+
101+ JavaScript maybeDir fileName ->
102+ do path <- toWritablePath maybeDir fileName
103+ IO.writeBuilder path monolith
97104
98− Just (JavaScript maybeDir fileName) ->
99− do path <- toWritablePath maybeDir fileName
100− IO.writeBuilder path monolith
105+ HtmlBuilder mvar ->
106+ putMVar mvar (Html.sandwich name monolith)
101107
102− Just (Html maybeDir fileName) ->
103− case names of
104− [] ->
108+ Html maybeDir fileName ->
105109 do path <- toWritablePath maybeDir fileName
106110 IO.writeBuilder path (Html.sandwich name monolith)
107111
108− _:_ ->
109− return ()
110−
111112
112113
113114-- GENERATE REPL MONOLITH
@@ -181,6 +182,7 @@ data Output
181182 = None
182183 | Html (Maybe FilePath) FilePath
183184 | JavaScript (Maybe FilePath) FilePath
185+ | HtmlBuilder (MVar B.Builder)
184186
185187
186188toWritablePath :: Maybe FilePath -> FilePath -> IO FilePath
ui/terminal/src/Develop.hs+14−20
@@ -156,35 +156,29 @@ serveCode file =
156156serveElm :: FilePath -> Snap ()
157157serveElm file =
158158 do guard (takeExtension file == ".elm")
159+ modifyResponse (setContentType "text/html")
160+ writeBuilder =<< liftIO (compileToHtmlBuilder file)
159161
160− result <- liftIO $
161− do mvar <- newEmptyMVar
162162
163− let reporter = Progress.Reporter (\_ -> return ()) (putMVar mvar)
163+compileToHtmlBuilder :: FilePath -> IO B.Builder
164+compileToHtmlBuilder file =
165+ do mvar1 <- newEmptyMVar
166+ mvar2 <- newEmptyMVar
164167
165− void $ Task.run reporter $
166− do summary <- Project.getRoot
167− Project.compile options Nothing summary [file]
168+ let reporter = Progress.Reporter (\_ -> return ()) (putMVar mvar1)
169+ let options = Output.Options Obj.Dev Obj.Client (Just (Output.HtmlBuilder mvar2))
168170
169− takeMVar mvar
171+ void $ Task.run reporter $
172+ do summary <- Project.getRoot
173+ Project.compile options Nothing summary [file]
170174
175+ result <- takeMVar mvar1
171176 case result of
172177 Just exit ->
173− do modifyResponse (setContentType "text/html")
174− writeBuilder $
175− Generate.makePageHtml "Errors" (Encode.encode (Exit.toJson exit))
178+ return $ Generate.makePageHtml "Errors" (Encode.encode (Exit.toJson exit))
176179
177180 Nothing ->
178− serveFileAs "text/html" (Path.temp "html")
179−
180−
181−options :: Output.Options
182−options =
183− let
184− (directory, filePath) = FP.splitFileName (Path.temp "html")
185− output = Output.Html (Just directory) filePath
186− in
187− Output.Options Obj.Dev Obj.Client (Just output)
181+ takeMVar mvar2
188182
189183
190184