-notification :: forall m a. (Monad m, FromJSON a) => ConduitParser FromServerMessage m (NotificationMessage ServerMethod a)
-notification = do
- let parser = decode . encodeMsg :: FromServerMessage -> Maybe (NotificationMessage ServerMethod a)
- x <- satisfy (isJust . parser)
- return $ decodeMsg $ encodeMsg x
+-- | Consumes and returns the result of the specified predicate if it returns `Just`.
+--
+-- @since 0.6.1.0
+satisfyMaybe :: (FromServerMessage -> Maybe a) -> Session a
+satisfyMaybe pred = satisfyMaybeM (pure . pred)
+
+satisfyMaybeM :: (FromServerMessage -> Session (Maybe a)) -> Session a
+satisfyMaybeM pred = do
+
+ skipTimeout <- overridingTimeout <$> get
+ timeoutId <- getCurTimeoutId
+ unless skipTimeout $ do
+ chan <- asks messageChan
+ timeout <- asks (messageTimeout . config)
+ void $ liftIO $ forkIO $ do
+ threadDelay (timeout * 1000000)
+ writeChan chan (TimeoutMessage timeoutId)
+
+ x <- Session await
+
+ unless skipTimeout (bumpTimeoutId timeoutId)
+
+ modify $ \s -> s { lastReceivedMessage = Just x }
+
+ res <- pred x
+
+ case res of
+ Just a -> do
+ logMsg LogServer x
+ return a
+ Nothing -> empty
+
+named :: T.Text -> Session a -> Session a
+named s (Session x) = Session (Data.Conduit.Parser.named s x)
+
+mEq :: SServerMethod m1 -> SServerMethod m2 -> Maybe (m1 :~~: m2)
+mEq m1 m2 = case (splitServerMethod m1, splitServerMethod m2) of
+ (IsServerNot, IsServerNot) -> do
+ Refl <- geq m1 m2
+ pure HRefl
+ (IsServerReq, IsServerReq) -> do
+ Refl <- geq m1 m2
+ pure HRefl
+ _ -> Nothing
+
+mEqClient :: SClientMethod m1 -> SClientMethod m2 -> Maybe (m1 :~~: m2)
+mEqClient m1 m2 = case (splitClientMethod m1, splitClientMethod m2) of
+ (IsClientNot, IsClientNot) -> do
+ Refl <- geq m1 m2
+ pure HRefl
+ (IsClientReq, IsClientReq) -> do
+ Refl <- geq m1 m2
+ pure HRefl
+ _ -> Nothing
+
+message :: SServerMethod m -> Session (ServerMessage m)
+message m1 = named (T.pack $ show m1) $ satisfyMaybe $ \case
+ FromServerMess m2 msg -> do
+ HRefl <- mEq m1 m2
+ pure msg
+ _ -> Nothing