Advertisement
NLinker

reactive-banana example

Feb 27th, 2020
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cmdLine :: IO ()
  2. cmdLine = do
  3.   displayHelpMessage
  4.   -- newAddHandler :: IO (AddHandler a, Handler a)
  5.   -- newAddHandler :: Control.Event.Handler
  6.   -- setupNetwork :: (EventSource (), EventSource EventNetwork) -> IO EventNetwork
  7.   -- sources :: ((AddHandler (), Handler ()), (AddHandler EventNetwork, Handler EventNetwork))
  8.   -- actuate :: EventNetwork -> IO ()
  9.   -- eventLoop :: (EventSource (), EventSource EventNetwork) -> EventNetwork -> IO ()
  10.   sources <- (,) <$> newAddHandler <*> newAddHandler
  11.   network <- setupNetwork sources
  12.   actuate network
  13.   eventLoop sources network
  14.  
  15. displayHelpMessage :: IO ()
  16. displayHelpMessage =
  17.   mapM_ putStrLn
  18.   [ "Commands are:"
  19.   , "   count   - send counter event"
  20.   , "   pause   - pause event network"
  21.   , "   actuate - actuate event network"
  22.   , "   quit    - quit the program"
  23.   , ""
  24.   ]
  25.  
  26. -- Read commands and fire corresponding events.
  27. eventLoop :: (EventSource (), EventSource EventNetwork) -> EventNetwork -> IO ()
  28. eventLoop (counterEs, pauseEs) network = loop
  29.   where
  30.     loop = do
  31.       putStr "> "
  32.       hFlush stdout
  33.       s <- getLine
  34.       case s of
  35.         "c" -> fire counterEs ()
  36.         "p" -> fire pauseEs network
  37.         "a" -> actuate network
  38.         "q" -> return ()
  39.         _   -> putStrLn $ s ++ " - unknown command"
  40.       when (s /= "q") loop
  41.  
  42.  
  43. -- Event Sources - allows you to register event handlers
  44. -- Your GUI framework should provide something like this for you
  45. type EventSource a = (AddHandler a, a -> IO ())
  46.  
  47. -- Set up the program logic in terms of events and behaviors.
  48. setupNetwork :: (EventSource (), EventSource EventNetwork) -> IO EventNetwork
  49. setupNetwork (counterEs, pauseEs) =
  50.   compile $ do
  51.     ecounter <- fromAddHandler (addHandler counterEs)
  52.     epause <- fromAddHandler (addHandler pauseEs)
  53.     ecount <- accumE 0 $ (+ 1) <$ ecounter
  54.     reactimate $ fmap print ecount
  55.     reactimate $ fmap pause epause
  56.  
  57. addHandler :: EventSource a -> AddHandler a
  58. addHandler = fst
  59.  
  60. fire :: EventSource a -> a -> IO ()
  61. fire = snd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement