-request :: forall m a b. (Monad m, FromJSON a, FromJSON b) => ConduitParser FromServerMessage m (RequestMessage ServerMethod a b)
-request = do
- let parser = decode . encodeMsg :: FromServerMessage -> Maybe (RequestMessage ServerMethod a b)
- x <- satisfy (isJust . parser)
- return $ fromJust $ decode $ encodeMsg x
+ unless skipTimeout $
+ modify $ \s -> s { curTimeoutId = timeoutId + 1 }
+
+ modify $ \s -> s { lastReceivedMessage = Just x }
+
+ if pred x
+ then do
+ liftIO $ do
+ setSGR [SetColor Foreground Vivid Magenta]
+ putStrLn $ "<-- " ++ B.unpack (encodeMsg x)
+ setSGR [Reset]
+ return x
+ else empty
+
+-- | Matches a message of type 'a'.
+message :: forall a. (Typeable a, FromJSON a) => Session a
+message =
+ let parser = decode . encodeMsg :: FromServerMessage -> Maybe a
+ in named (T.pack $ show $ head $ snd $ splitTyConApp $ last $ typeRepArgs $ typeOf parser) $
+ castMsg <$> satisfy (isJust . parser)
+
+-- | Matches if the message is a notification.
+anyNotification :: Session FromServerMessage
+anyNotification = named "Any notification" $ satisfy isServerNotification
+
+-- | Matches if the message is a request.
+anyRequest :: Session FromServerMessage
+anyRequest = named "Any request" $ satisfy isServerRequest