{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module FRP.Rhine.Clock.Realtime.Event (
module FRP.Rhine.Clock.Realtime.Event,
module Control.Monad.IO.Class,
newChan,
)
where
import Control.Concurrent.Chan
import Data.Time.Clock
import Control.DeepSeq
import Control.Monad.IO.Class
import Control.Monad.Trans.Reader
import FRP.Rhine.ClSF
import FRP.Rhine.Clock
import FRP.Rhine.Clock.Proxy
type EventChanT event m = ReaderT (Chan event) m
withChan :: Chan event -> EventChanT event m a -> m a
withChan :: forall event (m :: Type -> Type) a.
Chan event -> EventChanT event m a -> m a
withChan = (EventChanT event m a -> Chan event -> m a)
-> Chan event -> EventChanT event m a -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip EventChanT event m a -> Chan event -> m a
forall r (m :: Type -> Type) a. ReaderT r m a -> r -> m a
runReaderT
runEventChanT :: (MonadIO m) => EventChanT event m a -> m a
runEventChanT :: forall (m :: Type -> Type) event a.
MonadIO m =>
EventChanT event m a -> m a
runEventChanT EventChanT event m a
a = do
Chan event
chan <- IO (Chan event) -> m (Chan event)
forall a. IO a -> m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO IO (Chan event)
forall a. IO (Chan a)
newChan
EventChanT event m a -> Chan event -> m a
forall r (m :: Type -> Type) a. ReaderT r m a -> r -> m a
runReaderT EventChanT event m a
a Chan event
chan
withChanS ::
(Monad m) =>
Chan event ->
ClSF (EventChanT event m) cl a b ->
ClSF m cl a b
withChanS :: forall (m :: Type -> Type) event cl a b.
Monad m =>
Chan event -> ClSF (EventChanT event m) cl a b -> ClSF m cl a b
withChanS = (ClSF (EventChanT event m) cl a b -> Chan event -> ClSF m cl a b)
-> Chan event -> ClSF (EventChanT event m) cl a b -> ClSF m cl a b
forall a b c. (a -> b -> c) -> b -> a -> c
flip ClSF (EventChanT event m) cl a b -> Chan event -> ClSF m cl a b
forall (m :: Type -> Type) r cl a b.
Monad m =>
ClSF (ReaderT r m) cl a b -> r -> ClSF m cl a b
runReaderS_
emit :: (MonadIO m) => event -> EventChanT event m ()
emit :: forall (m :: Type -> Type) event.
MonadIO m =>
event -> EventChanT event m ()
emit event
event = do
Chan event
chan <- ReaderT (Chan event) m (Chan event)
forall (m :: Type -> Type) r. Monad m => ReaderT r m r
ask
IO () -> EventChanT event m ()
forall a. IO a -> ReaderT (Chan event) m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (IO () -> EventChanT event m ()) -> IO () -> EventChanT event m ()
forall a b. (a -> b) -> a -> b
$ Chan event -> event -> IO ()
forall a. Chan a -> a -> IO ()
writeChan Chan event
chan event
event
emitS :: (MonadIO m) => ClSF (EventChanT event m) cl event ()
emitS :: forall (m :: Type -> Type) event cl.
MonadIO m =>
ClSF (EventChanT event m) cl event ()
emitS = (event -> EventChanT event m ())
-> ClSF (EventChanT event m) cl event ()
forall (m :: Type -> Type) a b cl.
Monad m =>
(a -> m b) -> ClSF m cl a b
arrMCl event -> EventChanT event m ()
forall (m :: Type -> Type) event.
MonadIO m =>
event -> EventChanT event m ()
emit
emitSMaybe :: (MonadIO m) => ClSF (EventChanT event m) cl (Maybe event) ()
emitSMaybe :: forall (m :: Type -> Type) event cl.
MonadIO m =>
ClSF (EventChanT event m) cl (Maybe event) ()
emitSMaybe = ClSF (EventChanT event m) cl event ()
-> ClSF (EventChanT event m) cl (Maybe event) (Maybe ())
forall (m :: Type -> Type) cl a b.
Monad m =>
ClSF m cl a b -> ClSF m cl (Maybe a) (Maybe b)
mapMaybe ClSF (EventChanT event m) cl event ()
forall (m :: Type -> Type) event cl.
MonadIO m =>
ClSF (EventChanT event m) cl event ()
emitS ClSF (EventChanT event m) cl (Maybe event) (Maybe ())
-> Automaton
(ReaderT (TimeInfo cl) (EventChanT event m)) (Maybe ()) ()
-> Automaton
(ReaderT (TimeInfo cl) (EventChanT event m)) (Maybe event) ()
forall {k} (cat :: k -> k -> Type) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Maybe () -> ())
-> Automaton
(ReaderT (TimeInfo cl) (EventChanT event m)) (Maybe ()) ()
forall b c.
(b -> c)
-> Automaton (ReaderT (TimeInfo cl) (EventChanT event m)) b c
forall (a :: Type -> Type -> Type) b c.
Arrow a =>
(b -> c) -> a b c
arr (() -> Maybe () -> ()
forall a b. a -> b -> a
const ())
emit' :: (NFData event, MonadIO m) => event -> EventChanT event m ()
emit' :: forall event (m :: Type -> Type).
(NFData event, MonadIO m) =>
event -> EventChanT event m ()
emit' event
event =
event
event event -> EventChanT event m () -> EventChanT event m ()
forall a b. NFData a => a -> b -> b
`deepseq` do
Chan event
chan <- ReaderT (Chan event) m (Chan event)
forall (m :: Type -> Type) r. Monad m => ReaderT r m r
ask
IO () -> EventChanT event m ()
forall a. IO a -> ReaderT (Chan event) m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (IO () -> EventChanT event m ()) -> IO () -> EventChanT event m ()
forall a b. (a -> b) -> a -> b
$ Chan event -> event -> IO ()
forall a. Chan a -> a -> IO ()
writeChan Chan event
chan event
event
emitS' :: (NFData event, MonadIO m) => ClSF (EventChanT event m) cl event ()
emitS' :: forall event (m :: Type -> Type) cl.
(NFData event, MonadIO m) =>
ClSF (EventChanT event m) cl event ()
emitS' = (event -> EventChanT event m ())
-> ClSF (EventChanT event m) cl event ()
forall (m :: Type -> Type) a b cl.
Monad m =>
(a -> m b) -> ClSF m cl a b
arrMCl event -> EventChanT event m ()
forall event (m :: Type -> Type).
(NFData event, MonadIO m) =>
event -> EventChanT event m ()
emit'
emitSMaybe' ::
(NFData event, MonadIO m) =>
ClSF (EventChanT event m) cl (Maybe event) ()
emitSMaybe' :: forall event (m :: Type -> Type) cl.
(NFData event, MonadIO m) =>
ClSF (EventChanT event m) cl (Maybe event) ()
emitSMaybe' = ClSF (EventChanT event m) cl event ()
-> ClSF (EventChanT event m) cl (Maybe event) (Maybe ())
forall (m :: Type -> Type) cl a b.
Monad m =>
ClSF m cl a b -> ClSF m cl (Maybe a) (Maybe b)
mapMaybe ClSF (EventChanT event m) cl event ()
forall event (m :: Type -> Type) cl.
(NFData event, MonadIO m) =>
ClSF (EventChanT event m) cl event ()
emitS' ClSF (EventChanT event m) cl (Maybe event) (Maybe ())
-> Automaton
(ReaderT (TimeInfo cl) (EventChanT event m)) (Maybe ()) ()
-> Automaton
(ReaderT (TimeInfo cl) (EventChanT event m)) (Maybe event) ()
forall {k} (cat :: k -> k -> Type) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Maybe () -> ())
-> Automaton
(ReaderT (TimeInfo cl) (EventChanT event m)) (Maybe ()) ()
forall b c.
(b -> c)
-> Automaton (ReaderT (TimeInfo cl) (EventChanT event m)) b c
forall (a :: Type -> Type -> Type) b c.
Arrow a =>
(b -> c) -> a b c
arr (() -> Maybe () -> ()
forall a b. a -> b -> a
const ())
data EventClock event = EventClock
instance Semigroup (EventClock event) where
<> :: EventClock event -> EventClock event -> EventClock event
(<>) EventClock event
_ EventClock event
_ = EventClock event
forall event. EventClock event
EventClock
instance (MonadIO m) => Clock (EventChanT event m) (EventClock event) where
type Time (EventClock event) = UTCTime
type Tag (EventClock event) = event
initClock :: EventClock event
-> RunningClockInit
(EventChanT event m)
(Time (EventClock event))
(Tag (EventClock event))
initClock EventClock event
_ = do
UTCTime
initialTime <- IO UTCTime -> ReaderT (Chan event) m UTCTime
forall a. IO a -> ReaderT (Chan event) m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
getCurrentTime
(Automaton (EventChanT event m) () (UTCTime, event), UTCTime)
-> ReaderT
(Chan event)
m
(Automaton (EventChanT event m) () (UTCTime, event), UTCTime)
forall a. a -> ReaderT (Chan event) m a
forall (m :: Type -> Type) a. Monad m => a -> m a
return
( ReaderT (Chan event) m (UTCTime, event)
-> Automaton (EventChanT event m) () (UTCTime, event)
forall (m :: Type -> Type) b a. Functor m => m b -> Automaton m a b
constM (ReaderT (Chan event) m (UTCTime, event)
-> Automaton (EventChanT event m) () (UTCTime, event))
-> ReaderT (Chan event) m (UTCTime, event)
-> Automaton (EventChanT event m) () (UTCTime, event)
forall a b. (a -> b) -> a -> b
$ do
Chan event
chan <- ReaderT (Chan event) m (Chan event)
forall (m :: Type -> Type) r. Monad m => ReaderT r m r
ask
event
event <- IO event -> ReaderT (Chan event) m event
forall a. IO a -> ReaderT (Chan event) m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (IO event -> ReaderT (Chan event) m event)
-> IO event -> ReaderT (Chan event) m event
forall a b. (a -> b) -> a -> b
$ Chan event -> IO event
forall a. Chan a -> IO a
readChan Chan event
chan
UTCTime
time <- IO UTCTime -> ReaderT (Chan event) m UTCTime
forall a. IO a -> ReaderT (Chan event) m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
getCurrentTime
(UTCTime, event) -> ReaderT (Chan event) m (UTCTime, event)
forall a. a -> ReaderT (Chan event) m a
forall (m :: Type -> Type) a. Monad m => a -> m a
return (UTCTime
time, event
event)
, UTCTime
initialTime
)
instance GetClockProxy (EventClock event)
eventClockOn ::
(MonadIO m) =>
Chan event ->
HoistClock (EventChanT event m) m (EventClock event)
eventClockOn :: forall (m :: Type -> Type) event.
MonadIO m =>
Chan event -> HoistClock (EventChanT event m) m (EventClock event)
eventClockOn Chan event
chan =
HoistClock
{ unhoistedClock :: EventClock event
unhoistedClock = EventClock event
forall event. EventClock event
EventClock
, monadMorphism :: forall a. EventChanT event m a -> m a
monadMorphism = Chan event -> ReaderT (Chan event) m a -> m a
forall event (m :: Type -> Type) a.
Chan event -> EventChanT event m a -> m a
withChan Chan event
chan
}