From 542b7531673603a92ee0a097e07f64c1fdd86a4a Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Wed, 21 Apr 2021 00:35:34 -0400 Subject: [PATCH] Fix LVar.set to work with empty TMVars --- src/Data/LVar.hs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Data/LVar.hs b/src/Data/LVar.hs index 7491e16..4316f3c 100644 --- a/src/Data/LVar.hs +++ b/src/Data/LVar.hs @@ -55,7 +55,13 @@ get v = -- | Set the @LVar@ value; listeners from @listen@ are automatically notifed. set :: MonadIO m => LVar a -> a -> m () -set v = modify v . const +set v val = do + atomically $ do + let var = lvarCurrent v + isEmptyTMVar var >>= \case + True -> putTMVar var val + False -> void $ swapTMVar var val + notifyListeners v -- | Modify the @LVar@ value; listeners from @listen@ are automatically -- notified. @@ -67,12 +73,12 @@ modify v f = do curr <- readTMVar (lvarCurrent v) void $ swapTMVar (lvarCurrent v) (f curr) notifyListeners v - where - notifyListeners :: LVar a -> STM () - notifyListeners v' = do - subs <- readTMVar $ lvarListeners v' - forM_ (Map.elems subs) $ \subVar -> do - tryPutTMVar subVar () + +notifyListeners :: LVar a -> STM () +notifyListeners v' = do + subs <- readTMVar $ lvarListeners v' + forM_ (Map.elems subs) $ \subVar -> do + tryPutTMVar subVar () -- | Listen to changes to the @LVar@, as they are set by @set@ or @modify@ --