git.iko.soy/typeable/servant-reflex
on master help sign in
Merge pull request #76 from marcotoniut/master

`fanReqResult` (and tagged alternative)

commitd64362d0ccdf2c5939f83bbe87f3de2f4226eaa3 authorGreg Hale <ImAlsoGreg@gmail.com> dateWed 9 Jan 2019, 20:21 (8 years ago) parent9a3b58cf6d2b36
1 file changed+21
src/Servant/Common/Req.hs+21
src/Servant/Common/Req.hs+21
@@ -12,6 +12,7 @@ module Servant.Common.Req where
1212
1313-------------------------------------------------------------------------------
1414import Control.Applicative (liftA2, liftA3)
15+import Control.Arrow ((&&&))
1516import Control.Concurrent
1617import Control.Monad (join)
1718import Control.Monad.IO.Class (MonadIO, liftIO)
@@ -91,6 +92,13 @@ reqFailure (ResponseFailure _ s _) = Just s
9192reqFailure (RequestFailure _ s) = Just s
9293reqFailure _ = Nothing
9394
95+------------------------------------------------------------------------------
96+-- | Simple filter/accessor like 'reqFailure', but keeping the request tag
97+reqFailure' :: ReqResult tag a -> Maybe (tag,Text)
98+reqFailure' (ResponseFailure tag s _) = Just (tag,s)
99+reqFailure' (RequestFailure tag s) = Just (tag,s)
100+reqFailure' _ = Nothing
101+
94102
95103------------------------------------------------------------------------------
96104-- | Simple filter/accessor for the raw XHR response
@@ -394,6 +402,18 @@ evalResponse decode (tag, xhr) =
394402 else ResponseFailure tag errMsg xhr
395403 in respPayld
396404
405+------------------------------------------------------------------------------
406+-- | Utility for simultaneously accessing/filtering Success and Failure
407+-- response 'Event's,
408+fanReqResult :: Reflex t => Event t (ReqResult tag a) -> (Event t Text, Event t a)
409+fanReqResult = fmapMaybe reqFailure &&& fmapMaybe reqSuccess
410+
411+------------------------------------------------------------------------------
412+-- | Utility for simultaneously accessing/filtering Success and Failure
413+-- response 'Event's, but keeping the request tag
414+fanReqResult' :: Reflex t => Event t (ReqResult tag a) -> (Event t (tag, Text), Event t (tag, a))
415+fanReqResult' = fmapMaybe reqFailure' &&& fmapMaybe reqSuccess'
416+
397417
398418note :: e -> Maybe a -> Either e a
399419note e = maybe (Left e) Right
@@ -407,3 +427,4 @@ builderToText = TE.decodeUtf8 . BL.toStrict . Builder.toLazyByteString
407427
408428escape :: T.Text -> T.Text
409429escape = T.pack . N.escapeURIString (not . N.isReserved) . T.unpack . TE.decodeUtf8 . BL.toStrict . Builder.toLazyByteString . toEncodedUrlPiece
430+