module Matterhorn.State.Async
( AsyncPriority(..)
, doAsync
, doAsyncIO
, doAsyncWith
, doAsyncChannelMM
, doAsyncWithIO
, doAsyncMM
, tryMM
, endAsyncNOP
, scheduleMH
)
where
import Prelude ()
import Matterhorn.Prelude
import qualified Control.Concurrent.STM as STM
import Control.Exception ( try )
import Network.Mattermost.Types
import Matterhorn.Types
tryMM :: IO a
-> (a -> IO (Maybe (MH ())))
-> IO (Maybe (MH ()))
tryMM :: forall a. IO a -> (a -> IO (Maybe (MH ()))) -> IO (Maybe (MH ()))
tryMM IO a
act a -> IO (Maybe (MH ()))
onSuccess = do
Either MattermostError a
result <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => IO a -> IO (Either e a)
try IO a
act
case Either MattermostError a
result of
Left MattermostError
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ MHError -> MH ()
mhError forall a b. (a -> b) -> a -> b
$ MattermostError -> MHError
ServerError MattermostError
e
Right a
value -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ a -> IO (Maybe (MH ()))
onSuccess a
value
data AsyncPriority = Preempt | Normal
doAsync :: AsyncPriority -> IO () -> MH ()
doAsync :: AsyncPriority -> IO () -> MH ()
doAsync AsyncPriority
prio IO ()
act = AsyncPriority -> IO (Maybe (MH ())) -> MH ()
doAsyncWith AsyncPriority
prio (IO ()
act forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing)
doAsyncWith :: AsyncPriority -> IO (Maybe (MH ())) -> MH ()
doAsyncWith :: AsyncPriority -> IO (Maybe (MH ())) -> MH ()
doAsyncWith AsyncPriority
prio IO (Maybe (MH ()))
act = do
let putChan :: TChan a -> a -> STM ()
putChan = case AsyncPriority
prio of
AsyncPriority
Preempt -> forall a. TChan a -> a -> STM ()
STM.unGetTChan
AsyncPriority
Normal -> forall a. TChan a -> a -> STM ()
STM.writeTChan
RequestChan
queue <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState ChatResources
csResourcesforall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' ChatResources RequestChan
crRequestQueue)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. STM a -> IO a
STM.atomically forall a b. (a -> b) -> a -> b
$ forall a. TChan a -> a -> STM ()
putChan RequestChan
queue IO (Maybe (MH ()))
act
doAsyncIO :: AsyncPriority -> ChatState -> IO () -> IO ()
doAsyncIO :: AsyncPriority -> ChatState -> IO () -> IO ()
doAsyncIO AsyncPriority
prio ChatState
st IO ()
act =
AsyncPriority -> ChatState -> IO (Maybe (MH ())) -> IO ()
doAsyncWithIO AsyncPriority
prio ChatState
st (IO ()
act forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing)
scheduleMH :: ChatResources -> MH () -> IO ()
scheduleMH :: ChatResources -> MH () -> IO ()
scheduleMH ChatResources
r MH ()
act = do
let queue :: RequestChan
queue = ChatResources
rforall s a. s -> Getting a s a -> a
^.Lens' ChatResources RequestChan
crRequestQueue
forall a. STM a -> IO a
STM.atomically forall a b. (a -> b) -> a -> b
$ forall a. TChan a -> a -> STM ()
STM.writeTChan RequestChan
queue forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just MH ()
act
doAsyncWithIO :: AsyncPriority -> ChatState -> IO (Maybe (MH ())) -> IO ()
doAsyncWithIO :: AsyncPriority -> ChatState -> IO (Maybe (MH ())) -> IO ()
doAsyncWithIO AsyncPriority
prio ChatState
st IO (Maybe (MH ()))
act = do
let putChan :: TChan a -> a -> STM ()
putChan = case AsyncPriority
prio of
AsyncPriority
Preempt -> forall a. TChan a -> a -> STM ()
STM.unGetTChan
AsyncPriority
Normal -> forall a. TChan a -> a -> STM ()
STM.writeTChan
let queue :: RequestChan
queue = ChatState
stforall s a. s -> Getting a s a -> a
^.Lens' ChatState ChatResources
csResourcesforall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' ChatResources RequestChan
crRequestQueue
forall a. STM a -> IO a
STM.atomically forall a b. (a -> b) -> a -> b
$ forall a. TChan a -> a -> STM ()
putChan RequestChan
queue IO (Maybe (MH ()))
act
doAsyncMM :: AsyncPriority
-> (Session -> IO a)
-> (a -> Maybe (MH ()))
-> MH ()
doAsyncMM :: forall a.
AsyncPriority -> (Session -> IO a) -> (a -> Maybe (MH ())) -> MH ()
doAsyncMM AsyncPriority
prio Session -> IO a
mmOp a -> Maybe (MH ())
eventHandler = do
Session
session <- MH Session
getSession
AsyncPriority -> IO (Maybe (MH ())) -> MH ()
doAsyncWith AsyncPriority
prio forall a b. (a -> b) -> a -> b
$ do
a
r <- Session -> IO a
mmOp Session
session
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ a -> Maybe (MH ())
eventHandler a
r
type DoAsyncChannelMM a =
AsyncPriority
-> ChannelId
-> (Session -> ChannelId -> IO a)
-> (ChannelId -> a -> Maybe (MH ()))
-> MH ()
doAsyncChannelMM :: DoAsyncChannelMM a
doAsyncChannelMM :: forall a. DoAsyncChannelMM a
doAsyncChannelMM AsyncPriority
prio ChannelId
cId Session -> ChannelId -> IO a
mmOp ChannelId -> a -> Maybe (MH ())
eventHandler =
forall a.
AsyncPriority -> (Session -> IO a) -> (a -> Maybe (MH ())) -> MH ()
doAsyncMM AsyncPriority
prio (\Session
s -> Session -> ChannelId -> IO a
mmOp Session
s ChannelId
cId) (ChannelId -> a -> Maybe (MH ())
eventHandler ChannelId
cId)
endAsyncNOP :: ChannelId -> a -> Maybe (MH ())
endAsyncNOP :: forall a. ChannelId -> a -> Maybe (MH ())
endAsyncNOP ChannelId
_ a
_ = forall a. Maybe a
Nothing