Add example demonstrating fork and wait

This commit is contained in:
Joscha 2020-01-06 23:28:42 +00:00
parent 5709d1aeec
commit b56ff35ec1

View file

@ -5,6 +5,8 @@ module Haboli.Euphoria.Example where
import Control.Monad import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Data.Foldable
import Haboli.Euphoria.Api
import Haboli.Euphoria.Client import Haboli.Euphoria.Client
printAllEventsBot :: Client () () printAllEventsBot :: Client () ()
@ -35,3 +37,26 @@ sendMessagesUntilThrottledBot = forever $ do
void $ fork $ handle (\_ -> reply msg "got throttled") $ void $ fork $ handle (\_ -> reply msg "got throttled") $
forever $ reply msg "continue thread" forever $ reply msg "continue thread"
_ -> pure () _ -> pure ()
sendMessagesThreadedBot :: Client () ()
sendMessagesThreadedBot = forever $ do
event <- respondingToPing nextEvent
case event of
EventSnapshot _ -> void $ nick "TreeBot"
EventSend e ->
let msg = sendMessage e
in when (msgContent msg == "!tree") $
void $ fork $ buildTree msg
_ -> pure ()
where
buildTree msg = do
t1 <- fork $ reply msg "subtree 1"
t2 <- fork $ reply msg "subtree 2"
subtree1 <- wait t1
subtree2 <- wait t2
t3 <- fork $ reply subtree1 "subtree 1.1"
t4 <- fork $ reply subtree1 "subtree 1.2"
t5 <- fork $ reply subtree2 "subtree 2.1"
t6 <- fork $ reply subtree2 "subtree 2.2"
for_ [t3, t4, t5, t6] wait
reply msg "tree done"