{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
#ifdef USE_REFLEX_OPTIMIZER
{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
#endif
module Reflex.Host.Class
( ReflexHost (..)
, MonadSubscribeEvent (..)
, MonadReadEvent (..)
, MonadReflexCreateTrigger (..)
, MonadReflexHost (..)
, fireEvents
, newEventWithTriggerRef
, fireEventRef
, fireEventRefAndRead
) where
import Reflex.Class
import Control.Monad.Fix
import Control.Monad.Identity
import Control.Monad.Ref
import Control.Monad.Trans
import Control.Monad.Trans.Cont (ContT)
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Reader (ReaderT)
import Control.Monad.Trans.RWS (RWST)
import Control.Monad.Trans.State (StateT)
import qualified Control.Monad.Trans.State.Strict as Strict
import Control.Monad.Trans.Writer (WriterT)
import Data.Dependent.Sum (DSum (..))
import Data.GADT.Compare
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) Identity] -> ReadPhase m a -> m a
runHostFrame :: HostFrame t a -> m a
fireEvents :: MonadReflexHost t m => [DSum (EventTrigger t) Identity] -> m ()
fireEvents dm = fireEventsAndRead dm $ return ()
{-# INLINE fireEvents #-}
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)
{-# INLINE newEventWithTriggerRef #-}
fireEventRef :: (MonadReflexHost t m, MonadRef m, Ref m ~ Ref IO) => Ref m (Maybe (EventTrigger t a)) -> a -> m ()
fireEventRef mtRef input = do
mt <- readRef mtRef
case mt of
Nothing -> return ()
Just trigger -> fireEvents [trigger :=> Identity input]
fireEventRefAndRead :: (MonadReflexHost t m, MonadRef m, Ref m ~ Ref IO) => Ref m (Maybe (EventTrigger t a)) -> a -> EventHandle t b -> m (Maybe b)
fireEventRefAndRead mtRef input e = do
mt <- readRef mtRef
case mt of
Nothing -> return Nothing
Just trigger -> fireEventsAndRead [trigger :=> Identity input] $ do
mGetValue <- readEvent e
case mGetValue of
Nothing -> return Nothing
Just getValue -> fmap Just getValue
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 (Strict.StateT s m) where
newEventWithTrigger = lift . newEventWithTrigger
newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
instance MonadSubscribeEvent t m => MonadSubscribeEvent t (Strict.StateT r m) where
subscribeEvent = lift . subscribeEvent
instance MonadReflexHost t m => MonadReflexHost t (Strict.StateT s m) where
type ReadPhase (Strict.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