diff options
Diffstat (limited to 'rtq.hs')
-rw-r--r-- | rtq.hs | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -0,0 +1,46 @@ | |||
1 | {-# LANGUAGE FlexibleContexts #-} | ||
2 | {-# LANGUAGE ExplicitForAll #-} | ||
3 | {-# LANGUAGE KindSignatures #-} | ||
4 | |||
5 | import BasePrelude hiding ((.)) | ||
6 | import RealTimeQueue | ||
7 | import Control.Monad.IO.Class | ||
8 | import Control.Monad.State | ||
9 | import System.Clock | ||
10 | |||
11 | data LoopState = LoopState { | ||
12 | _queue :: Queue String | ||
13 | } | ||
14 | |||
15 | main :: IO () | ||
16 | main = void $ runStateT main' (LoopState createQueue) | ||
17 | |||
18 | main' :: StateT LoopState IO () | ||
19 | main' = 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 | |||
26 | queueAction :: forall (m :: * -> *). MonadState LoopState m => (Queue String -> m (Queue String)) -> m () | ||
27 | queueAction act = do | ||
28 | q <- gets _queue | ||
29 | act q >>= modify . const . LoopState | ||
30 | |||
31 | tickTime :: TimeSpec | ||
32 | tickTime = TimeSpec 0 15000 | ||
33 | |||
34 | threadDelay' :: TimeSpec -> IO () | ||
35 | threadDelay' = threadDelay . fromIntegral . timeSpecAsNanoSecs | ||
36 | |||
37 | runner :: TimeSpec -> String -> StateT LoopState IO () | ||
38 | runner delay str = liftIO $ do | ||
39 | threadDelay' delay | ||
40 | putStrLn str | ||
41 | |||
42 | mainLoop :: StateT LoopState IO () | ||
43 | mainLoop = do | ||
44 | queueAction $ runScheduledIO tickTime runner | ||
45 | liftIO $ threadDelay' tickTime | ||
46 | mainLoop | ||