on develop help sign in
Add withRequesterT
commit81097f8bc692078ccedbd36a6d578fc8e8955291 authorAli Abrar <aliabrar@gmail.com> dateSat 16 Mar 2019, 19:48 (8 years ago) parent28b7fde
1 file changed+15−1
src/Reflex/Requester/Base.hs+15−1
src/Reflex/Requester/Base.hs+15−1
@@ -21,6 +21,7 @@
2121module Reflex.Requester.Base
2222 ( RequesterT (..)
2323 , runRequesterT
24+ , withRequesterT
2425 , runWithReplaceRequesterTWith
2526 , traverseIntMapWithKeyWithAdjustRequesterTWith
2627 , traverseDMapWithKeyWithAdjustRequesterTWith
@@ -265,7 +266,6 @@ instance (S.Semigroup a, Monad m) => S.Semigroup (RequesterT t request response
265266-- requests are made, and responses should be provided in the input 'Event'.
266267-- The 'Tag' keys will be used to return the responses to the same place the
267268-- requests were issued.
268−
269269runRequesterT :: (Reflex t, Monad m)
270270 => RequesterT t request response m a
271271 -> Event t (RequesterData response) --TODO: This DMap will be in reverse order, so we need to make sure the caller traverses it in reverse
@@ -275,6 +275,20 @@ runRequesterT (RequesterT a) responses = do
275275 coerceEvent responses
276276 return (result, fmapCheap (RequesterData . TagMap) $ mergeInt $ IntMap.fromDistinctAscList $ _requesterState_requests s)
277277
278+-- | Map a function over the request and response of a 'RequesterT'
279+withRequesterT
280+ :: (Reflex t, MonadFix m)
281+ => (forall x. req x -> req' x) -- ^ The function to map over the request
282+ -> (forall x. rsp' x -> rsp x) -- ^ The function to map over the response
283+ -> RequesterT t req rsp m a -- ^ The internal 'RequesterT' whose input and output will be transformed
284+ -> RequesterT t req' rsp' m a -- ^ The resulting 'RequesterT'
285+withRequesterT freq frsp child = do
286+ rec let rsp = fmap (runIdentity . traverseRequesterData (Identity . frsp)) rsp'
287+ (a, req) <- lift $ runRequesterT child rsp
288+ rsp' <- fmap (flip selectInt 0 . fanInt . fmapCheap unMultiEntry) $ requesting' $
289+ fmapCheap (multiEntry . IntMap.singleton 0) $ fmap (runIdentity . traverseRequesterData (Identity . freq)) req
290+ return a
291+
278292instance (Reflex t, Monad m) => Requester t (RequesterT t request response m) where
279293 type Request (RequesterT t request response m) = request
280294 type Response (RequesterT t request response m) = response