{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
#ifdef USE_REFLEX_OPTIMIZER
{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
#endif
module Reflex.Requester.Class
( Requester (..)
, withRequesting
, requestingIdentity
) where
import Control.Monad.Identity
import Control.Monad.Reader
import qualified Control.Monad.State.Lazy as Lazy
import Control.Monad.State.Strict
import Data.Kind (Type)
import Reflex.Class
class (Reflex t, Monad m) => Requester t m | m -> t where
type Request m :: Type -> Type
type Response m :: Type -> Type
requesting :: Event t (Request m a) -> m (Event t (Response m a))
requesting_ :: Event t (Request m a) -> m ()
instance Requester t m => Requester t (ReaderT r m) where
type Request (ReaderT r m) = Request m
type Response (ReaderT r m) = Response m
requesting = lift . requesting
requesting_ = lift . requesting_
instance Requester t m => Requester t (StateT s m) where
type Request (StateT s m) = Request m
type Response (StateT s m) = Response m
requesting = lift . requesting
requesting_ = lift . requesting_
instance Requester t m => Requester t (Lazy.StateT s m) where
type Request (Lazy.StateT s m) = Request m
type Response (Lazy.StateT s m) = Response m
requesting = lift . requesting
requesting_ = lift . requesting_
requestingIdentity :: (Requester t m, Response m ~ Identity) => Event t (Request m a) -> m (Event t a)
requestingIdentity = fmap coerceEvent . requesting
withRequesting :: (Requester t m, MonadFix m) => (Event t (Response m a) -> m (Event t (Request m a), r)) -> m r
withRequesting f = do
rec response <- requesting request
(request, result) <- f response
return result