[server] Add return value to tree modules

Now, tree modules can't just stop, they can Just stop. Sorry... But they can
return values now, with a tiny bit of type class trickery.
This commit is contained in:
Joscha 2020-02-13 23:38:26 +00:00
parent 220b5a3234
commit 2a2b148046
6 changed files with 55 additions and 39 deletions

View file

@ -4,6 +4,7 @@ module Forest.Util
( findPrev
, findNext
, whileM
, runUntilJustM
, withThread
, sendPacket
, closeWithErrorMessage
@ -34,6 +35,14 @@ whileM f = do
then whileM f
else pure ()
-- | Run a monadic action until it returns @Just a@ for the first time.
runUntilJustM :: Monad m => m (Maybe a) -> m a
runUntilJustM f = do
result <- f
case result of
Nothing -> runUntilJustM f
Just a -> pure a
sendPacket :: ToJSON a => WS.Connection -> a -> IO ()
sendPacket conn packet = WS.sendTextData conn $ encode packet