mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-06-07 17:32:12 +00:00
002f36dde0
* Add 9.6 compat * compile with GHC9.6.2: dependencies, imports, code * refactor typeclasses * refactor record dot * update cabal version * update github actions * update direct-sqlcipher * 5.4.0.0 * update cabal.project --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
25 lines
732 B
Haskell
25 lines
732 B
Haskell
module Simplex.Messaging.Agent.TAsyncs where
|
|
|
|
import Control.Monad.IO.Unlift (MonadUnliftIO)
|
|
import Simplex.Messaging.TMap (TMap)
|
|
import qualified Simplex.Messaging.TMap as TM
|
|
import UnliftIO.Async (Async, async)
|
|
import UnliftIO.STM
|
|
|
|
data TAsyncs = TAsyncs
|
|
{ actionId :: TVar Int,
|
|
actions :: TMap Int (Async ())
|
|
}
|
|
|
|
newTAsyncs :: STM TAsyncs
|
|
newTAsyncs = TAsyncs <$> newTVar 0 <*> TM.empty
|
|
|
|
newAsyncAction :: MonadUnliftIO m => (Int -> m ()) -> TAsyncs -> m ()
|
|
newAsyncAction action as = do
|
|
aId <- atomically $ stateTVar (actionId as) $ \i -> (i + 1, i + 1)
|
|
a <- async $ action aId
|
|
atomically $ TM.insert aId a $ actions as
|
|
|
|
removeAsyncAction :: Int -> TAsyncs -> STM ()
|
|
removeAsyncAction aId = TM.delete aId . actions
|