module Reflex.Host.Class where
import Reflex.Class
import Control.Applicative
import Control.Monad.Fix
import Control.Monad.Trans
import Control.Monad.Trans.Reader (ReaderT())
import Control.Monad.Trans.Writer (WriterT())
import Control.Monad.Trans.Cont (ContT())
import Control.Monad.Trans.Except (ExceptT())
import Control.Monad.Trans.RWS (RWST())
import Control.Monad.Trans.State (StateT())
import Data.Dependent.Sum (DSum)
import Data.Monoid
import Data.GADT.Compare
import Control.Monad.Ref
import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl)
class (Reflex t, MonadReflexCreateTrigger t (HostFrame t), MonadSample t (HostFrame t), MonadHold t (HostFrame t), MonadFix (HostFrame t), MonadSubscribeEvent t (HostFrame t)) => ReflexHost t where
type EventTrigger t :: * -> *
type EventHandle t :: * -> *
type HostFrame t :: * -> *
class (Reflex t, Monad m) => MonadSubscribeEvent t m | m -> t where
subscribeEvent :: Event t a -> m (EventHandle t a)
class (ReflexHost t, Applicative m, Monad m) => MonadReadEvent t m | m -> t where
readEvent :: EventHandle t a -> m (Maybe (m a))
class (Applicative m, Monad m) => MonadReflexCreateTrigger t m | m -> t where
newEventWithTrigger :: (EventTrigger t a -> IO (IO ())) -> m (Event t a)
newFanEventWithTrigger :: GCompare k => (forall a. k a -> EventTrigger t a -> IO (IO ())) -> m (EventSelector t k)
class (ReflexHost t, MonadReflexCreateTrigger t m, MonadSubscribeEvent t m, MonadReadEvent t (ReadPhase m), MonadSample t (ReadPhase m), MonadHold t (ReadPhase m)) => MonadReflexHost t m | m -> t where
type ReadPhase m :: * -> *
fireEventsAndRead :: [DSum (EventTrigger t)] -> (ReadPhase m a) -> m a
runHostFrame :: HostFrame t a -> m a
fireEvents :: MonadReflexHost t m => [DSum (EventTrigger t)] -> m ()
fireEvents dm = fireEventsAndRead dm $ return ()
newEventWithTriggerRef :: (MonadReflexCreateTrigger t m, MonadRef m, Ref m ~ Ref IO) => m (Event t a, Ref m (Maybe (EventTrigger t a)))
newEventWithTriggerRef = do
rt <- newRef Nothing
e <- newEventWithTrigger $ \t -> do
writeRef rt $ Just t
return $ writeRef rt Nothing
return (e, rt)
instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (ReaderT r m) where
newEventWithTrigger = lift . newEventWithTrigger
newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
instance MonadSubscribeEvent t m => MonadSubscribeEvent t (ReaderT r m) where
subscribeEvent = lift . subscribeEvent
instance MonadReflexHost t m => MonadReflexHost t (ReaderT r m) where
type ReadPhase (ReaderT r m) = ReadPhase m
fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
runHostFrame = lift . runHostFrame
instance (MonadReflexCreateTrigger t m, Monoid w) => MonadReflexCreateTrigger t (WriterT w m) where
newEventWithTrigger = lift . newEventWithTrigger
newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
instance (MonadSubscribeEvent t m, Monoid w) => MonadSubscribeEvent t (WriterT w m) where
subscribeEvent = lift . subscribeEvent
instance (MonadReflexHost t m, Monoid w) => MonadReflexHost t (WriterT w m) where
type ReadPhase (WriterT w m) = ReadPhase m
fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
runHostFrame = lift . runHostFrame
instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (StateT s m) where
newEventWithTrigger = lift . newEventWithTrigger
newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
instance MonadSubscribeEvent t m => MonadSubscribeEvent t (StateT r m) where
subscribeEvent = lift . subscribeEvent
instance MonadReflexHost t m => MonadReflexHost t (StateT s m) where
type ReadPhase (StateT s m) = ReadPhase m
fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
runHostFrame = lift . runHostFrame
instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (ContT r m) where
newEventWithTrigger = lift . newEventWithTrigger
newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
instance MonadSubscribeEvent t m => MonadSubscribeEvent t (ContT r m) where
subscribeEvent = lift . subscribeEvent
instance MonadReflexHost t m => MonadReflexHost t (ContT r m) where
type ReadPhase (ContT r m) = ReadPhase m
fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
runHostFrame = lift . runHostFrame
instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (ExceptT e m) where
newEventWithTrigger = lift . newEventWithTrigger
newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
instance MonadSubscribeEvent t m => MonadSubscribeEvent t (ExceptT r m) where
subscribeEvent = lift . subscribeEvent
instance MonadReflexHost t m => MonadReflexHost t (ExceptT e m) where
type ReadPhase (ExceptT e m) = ReadPhase m
fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
runHostFrame = lift . runHostFrame
instance (MonadReflexCreateTrigger t m, Monoid w) => MonadReflexCreateTrigger t (RWST r w s m) where
newEventWithTrigger = lift . newEventWithTrigger
newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
instance (MonadSubscribeEvent t m, Monoid w) => MonadSubscribeEvent t (RWST r w s m) where
subscribeEvent = lift . subscribeEvent
instance (MonadReflexHost t m, Monoid w) => MonadReflexHost t (RWST r w s m) where
type ReadPhase (RWST r w s m) = ReadPhase m
fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
runHostFrame = lift . runHostFrame