From 92409820fb67e7f3ba81f629e418393256adfa55 Mon Sep 17 00:00:00 2001 From: Efim Poberezkin <8711996+efim-poberezkin@users.noreply.github.com> Date: Fri, 11 Feb 2022 12:03:34 +0400 Subject: [PATCH] enable async commands (#290) * enable async * fix async command error response Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- src/Simplex/Chat.hs | 20 ++++++++++---------- src/Simplex/Chat/View.hs | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index bb700c3425..ff7932c709 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -384,17 +384,17 @@ processChatCommand = \case withAgentLock a . withLock l $ action -- below code would make command responses asynchronous where they can be slow -- in View.hs `r'` should be defined as `id` in this case - -- procCmd :: m ChatResponse -> m ChatResponse - -- procCmd action = do - -- ChatController {chatLock = l, smpAgent = a, outputQ = q, idsDrg = gVar} <- ask - -- corrId <- liftIO $ SMP.CorrId <$> randomBytes gVar 8 - -- void . forkIO $ - -- withAgentLock a . withLock l $ - -- (atomically . writeTBQueue q) . (Just corrId,) =<< (action `catchError` (pure . CRChatCmdError)) - -- pure $ CRCmdAccepted corrId - -- use function below to make commands "synchronous" procCmd :: m ChatResponse -> m ChatResponse - procCmd = id + procCmd action = do + ChatController {chatLock = l, smpAgent = a, outputQ = q, idsDrg = gVar} <- ask + corrId <- liftIO $ SMP.CorrId <$> randomBytes gVar 8 + void . forkIO $ + withAgentLock a . withLock l $ + (atomically . writeTBQueue q) . (Just corrId,) =<< (action `catchError` (pure . CRChatError)) + pure $ CRCmdAccepted corrId + -- use function below to make commands "synchronous" + -- procCmd :: m ChatResponse -> m ChatResponse + -- procCmd = id connect :: UserId -> ConnectionRequestUri c -> ChatMsgEvent -> m () connect userId cReq msg = do connId <- withAgent $ \a -> joinConnection a cReq $ directMessage msg diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index 4b95f0757c..ffbf8f1ce2 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -122,8 +122,8 @@ responseToView cmd testView = \case where r = (plain cmd :) -- this function should be `r` for "synchronous", `id` for "asynchronous" command responses - -- r' = id - r' = r + -- r' = r + r' = id testViewChats :: [AChat] -> [StyledString] testViewChats chats = [sShow $ map toChatView chats] where