Skip to content

Commit 227a09b

Browse files
committed
Fix stream communication with backend.
1 parent 925359d commit 227a09b

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

src/Main.gren

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ type Msg
161161
| CompiledForRun { path : String, exitCode : Int }
162162
| RunStarted { processId : Process.Id, streams : ChildProcess.StreamIO }
163163
| RunExited Int
164+
| RedirectTerminalIO (Result Stream.Error { source : Stream.Readable Bytes, target : Stream.Writable Bytes })
164165

165166

166167
port completeStaticBuild : String -> Cmd msg
@@ -235,36 +236,18 @@ update msg model =
235236
parseUserArgs model compilerPath
236237

237238
CompilerInitialized { backendStreams, encodedCommand, quiet } ->
238-
-- TODO: prompts from the backend no longer works, which will probably block the repl
239-
Stream.write encodedCommand backendStreams.input
240-
|> Task.andThen (\_ -> Stream.closeWritable backendStreams.input)
241-
|> Task.onError
242-
(\streamErr ->
243-
Stream.Log.line model.stderr ("Failed to send command to compiler backend due to error: " ++ Stream.errorToString streamErr)
244-
)
245-
|> Task.andThen
246-
(\_ ->
247-
if quiet then
248-
Task.succeed model.stdout
249-
else
250-
backendStreams.output
251-
|> Stream.awaitAndPipeThrough Stream.textDecoder
252-
|> Task.andThen Stream.Extra.consumeString
253-
|> Task.andThen (\str -> Stream.writeStringAsBytes str model.stdout)
254-
)
255-
|> Task.andThen
256-
(\_ ->
257-
backendStreams.error
258-
|> Stream.awaitAndPipeThrough Stream.textDecoder
259-
|> Task.andThen Stream.Extra.consumeString
260-
|> Task.andThen (\str -> Stream.writeStringAsBytes str model.stderr)
261-
)
262-
|> Task.map (\_ -> {})
263-
|> Task.onError
264-
(\streamErr ->
265-
Stream.Log.line model.stderr ("Something went wrong when reading from backend: " ++ Stream.errorToString streamErr)
266-
)
267-
|> Task.execute
239+
Cmd.batch
240+
[ Stream.write encodedCommand backendStreams.input
241+
|> Task.map (\_ -> { source = model.stdin, target = backendStreams.input })
242+
|> Task.attempt RedirectTerminalIO
243+
, if quiet then
244+
Cmd.none
245+
else
246+
Task.succeed { source = backendStreams.output, target = model.stdout }
247+
|> Task.attempt RedirectTerminalIO
248+
, Task.succeed { source = backendStreams.error, target = model.stderr }
249+
|> Task.attempt RedirectTerminalIO
250+
]
268251

269252
CompilerRan exitCode ->
270253
Node.exitWithCode exitCode
@@ -293,6 +276,16 @@ update msg model =
293276

294277
RunExited exitCode ->
295278
Node.exitWithCode exitCode
279+
280+
RedirectTerminalIO (Ok streams) ->
281+
Stream.read streams.source
282+
|> Task.andThen (\str -> Stream.write str streams.target)
283+
|> Task.map (\_ -> streams)
284+
|> Task.attempt RedirectTerminalIO
285+
286+
RedirectTerminalIO (Err err) ->
287+
Stream.Log.line model.stderr ("Something went wrong when communicating with compiler backend: " ++ Stream.errorToString err)
288+
|> Task.execute
296289
}
297290

298291

terminal/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Main
44
where
55

66
import Command qualified
7-
import Data.ByteString qualified
7+
import Data.ByteString.Char8 qualified
88
import Docs qualified
99
import GHC.IO.Encoding (setLocaleEncoding, utf8)
1010
import Json.Decode qualified as Json
@@ -24,7 +24,7 @@ main =
2424
argStrings <- Env.getArgs
2525
case argStrings of
2626
[] -> do
27-
json <- Data.ByteString.getContents
27+
json <- Data.ByteString.Char8.getLine
2828
case Json.fromByteString Command.commandDecoder json of
2929
Left err ->
3030
error (show err)

0 commit comments

Comments
 (0)