summaryrefslogtreecommitdiff
path: root/rtq.hs
diff options
context:
space:
mode:
Diffstat (limited to 'rtq.hs')
-rw-r--r--rtq.hs46
1 files changed, 46 insertions, 0 deletions
diff --git a/rtq.hs b/rtq.hs
new file mode 100644
index 0000000..a47491a
--- /dev/null
+++ b/rtq.hs
@@ -0,0 +1,46 @@
1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE ExplicitForAll #-}
3{-# LANGUAGE KindSignatures #-}
4
5import BasePrelude hiding ((.))
6import RealTimeQueue
7import Control.Monad.IO.Class
8import Control.Monad.State
9import System.Clock
10
11data LoopState = LoopState {
12 _queue :: Queue String
13}
14
15main :: IO ()
16main = void $ runStateT main' (LoopState createQueue)
17
18main' :: StateT LoopState IO ()
19main' = do
20 queueAction $ scheduleEventIO (TimeSpec 1 0, "hello world")
21 queueAction $ scheduleEventIO (TimeSpec 2 0, "hello world")
22 queueAction $ scheduleEventIO (TimeSpec 3 0, "hello world")
23 queueAction $ scheduleEventIO (TimeSpec 4 0, "hello world")
24 mainLoop
25
26queueAction :: forall (m :: * -> *). MonadState LoopState m => (Queue String -> m (Queue String)) -> m ()
27queueAction act = do
28 q <- gets _queue
29 act q >>= modify . const . LoopState
30
31tickTime :: TimeSpec
32tickTime = TimeSpec 0 15000
33
34threadDelay' :: TimeSpec -> IO ()
35threadDelay' = threadDelay . fromIntegral . timeSpecAsNanoSecs
36
37runner :: TimeSpec -> String -> StateT LoopState IO ()
38runner delay str = liftIO $ do
39 threadDelay' delay
40 putStrLn str
41
42mainLoop :: StateT LoopState IO ()
43mainLoop = do
44 queueAction $ runScheduledIO tickTime runner
45 liftIO $ threadDelay' tickTime
46 mainLoop