module Data.HMemDb.MapTVar (MS, liftMaybe, readTVarMap) where import Control.Concurrent.STM (STM, TVar, readTVar) import Control.Monad.Trans.Class (lift) import Control.Monad.Trans.Maybe (MaybeT(MaybeT)) import Data.Map (Map) import qualified Data.Map as M (lookup) type MS = MaybeT STM liftMaybe :: Monad m => Maybe a -> MaybeT m a liftMaybe = MaybeT . return readTVarMap :: Ord k => TVar (Map k a) -> k -> MS a readTVarMap tv k = do mp <- lift $ readTVar tv liftMaybe $ M.lookup k mp