1-- | This module defines 'Requester', which indicates that an action can make
2-- requests and receive responses to them. Typically, this is used for things
3-- like a WebSocket, where it's desirable to collect many potential sources of
4-- events and send them over a single channel, then distribute the results back
5-- out efficiently to their original request sites.
7{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE FunctionalDependencies #-}
9{-# LANGUAGE RecursiveDo #-}
10{-# LANGUAGE TypeFamilies #-}
11{-# LANGUAGE UndecidableInstances #-}
12#ifdef USE_REFLEX_OPTIMIZER
13{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
15module Reflex.Requester.Class
21import Control.Monad.Identity
22import Control.Monad.Reader
23import qualified Control.Monad.State.Lazy as Lazy
24import Control.Monad.State.Strict
27-- | A 'Requester' action can trigger requests of type @Request m a@ based on
28-- 'Event's, and receive responses of type @Response m a@ in return. Note that
29-- the @a@ type can vary within the 'Requester' action, but will be linked for a
30-- given request. For example, if @Request m@ is 'IO' and @Response m@ is
31-- 'Identity', then 'requestingIdentity' has the same type as
32-- 'Reflex.PerformEvent.Class.performEvent'.
33class (Reflex t, Monad m) => Requester t m | m -> t where
34 -- | The type of requests that this 'Requester' can emit
35 type Request m :: * -> *
36 -- | The type of responses that this 'Requester' can receive
37 type Response m :: * -> *
38 -- | Emit a request whenever the given 'Event' fires, and return responses in
39 -- the resulting 'Event'.
40 requesting :: Event t (Request m a) -> m (Event t (Response m a))
41 -- | Emit a request whenever the given 'Event' fires, and ignore all responses.
42 requesting_ :: Event t (Request m a) -> m ()
45instance Requester t m => Requester t (ReaderT r m) where
46 type Request (ReaderT r m) = Request m
47 type Response (ReaderT r m) = Response m
48 requesting = lift . requesting
49 requesting_ = lift . requesting_
51instance Requester t m => Requester t (StateT s m) where
52 type Request (StateT s m) = Request m
53 type Response (StateT s m) = Response m
54 requesting = lift . requesting
55 requesting_ = lift . requesting_
57instance Requester t m => Requester t (Lazy.StateT s m) where
58 type Request (Lazy.StateT s m) = Request m
59 type Response (Lazy.StateT s m) = Response m
60 requesting = lift . requesting
61 requesting_ = lift . requesting_
63-- | Emit a request whenever the given 'Event' fires, and unwrap the responses
64-- before returning them. @Response m@ must be 'Identity'.
65requestingIdentity :: (Requester t m, Response m ~ Identity) => Event t (Request m a) -> m (Event t a)
66requestingIdentity = fmap coerceEvent . requesting
68withRequesting :: (Requester t m, MonadFix m) => (Event t (Response m a) -> m (Event t (Request m a), r)) -> m r
70 rec response <- requesting request
71 (request, result) <- f response
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list