Skip to content
Snippets Groups Projects
Commit f7aba7a2 authored by Jens Nolte's avatar Jens Nolte
Browse files

Add awaitDispose (replaces disposeIO)

parent 2f44408e
No related branches found
No related tags found
No related merge requests found
......@@ -57,7 +57,7 @@ instance MonadAsync (ReaderT ResourceManager IO) where
-- Thread has completed work, "disarm" the disposable and fire it
void $ atomically $ swapTMVar threadIdVar Nothing
disposeIO disposable
awaitDispose disposable
do atomically $ putTMVar threadIdVar Nothing
......
......@@ -2,7 +2,7 @@ module Quasar.Disposable (
-- * Disposable
IsDisposable(..),
Disposable,
disposeIO,
awaitDispose,
newDisposable,
synchronousDisposable,
noDisposable,
......@@ -54,9 +54,12 @@ class IsDisposable a where
{-# MINIMAL toDisposable | (dispose, isDisposed) #-}
-- | Dispose a resource in the IO monad.
disposeIO :: IsDisposable a => a -> IO ()
disposeIO = await <=< dispose
awaitDispose :: (MonadAwait m, MonadIO m) => IsDisposable a => a -> m ()
awaitDispose disposable = await =<< liftIO (dispose disposable)
instance IsDisposable a => IsDisposable (Maybe a) where
toDisposable = maybe noDisposable toDisposable
......
......@@ -185,7 +185,7 @@ newTimer scheduler time = do
sleepUntil :: TimerScheduler -> UTCTime -> IO ()
sleepUntil scheduler time = bracketOnError (newTimer scheduler time) disposeIO await
sleepUntil scheduler time = bracketOnError (newTimer scheduler time) awaitDispose await
......
......@@ -26,20 +26,20 @@ spec = parallel $ do
describe "newDisposable" $ do
it "signals it's disposed state" $ do
disposable <- newDisposable $ pure $ pure ()
void $ forkIO $ threadDelay 100000 >> disposeIO disposable
void $ forkIO $ threadDelay 100000 >> awaitDispose disposable
await (isDisposed disposable)
pure () :: IO ()
it "can be disposed multiple times" $ do
it "can be disposed multiple times" $ io do
disposable <- newDisposable $ pure $ pure ()
disposeIO disposable
disposeIO disposable
awaitDispose disposable
awaitDispose disposable
await (isDisposed disposable)
it "can be disposed in parallel" $ do
disposable <- newDisposable $ pure () <$ threadDelay 100000
void $ forkIO $ disposeIO disposable
disposeIO disposable
void $ forkIO $ awaitDispose disposable
awaitDispose disposable
await (isDisposed disposable)
......@@ -98,4 +98,4 @@ spec = parallel $ do
it "can attach an disposable that is disposed asynchronously" $ do
withResourceManager \resourceManager -> do
disposable <- attachDisposeAction resourceManager $ pure () <$ threadDelay 100000
void $ forkIO $ disposeIO disposable
void $ forkIO $ awaitDispose disposable
......@@ -39,7 +39,7 @@ spec = parallel $ do
OM.insert "key2" "value2" om
lastCallbackShouldBe (HM.fromList [("key", "value"), ("key2", "value2")])
disposeIO subscriptionHandle
awaitDispose subscriptionHandle
lastCallbackShouldBe (HM.fromList [("key", "value"), ("key2", "value2")])
OM.insert "key3" "value3" om
......@@ -61,7 +61,7 @@ spec = parallel $ do
OM.insert "key2" "value2" om
lastDeltaShouldBe $ Insert "key2" "value2"
disposeIO subscriptionHandle
awaitDispose subscriptionHandle
lastDeltaShouldBe $ Insert "key2" "value2"
OM.insert "key3" "value3" om
......@@ -120,7 +120,7 @@ spec = parallel $ do
v2ShouldBe $ Just "changed"
retrieveIO om `shouldReturn` HM.singleton "key2" "changed"
disposeIO handle2
awaitDispose handle2
OM.lookupDelete "key2" om `shouldReturn` Just "changed"
v2ShouldBe $ Just "changed"
......
......@@ -22,9 +22,9 @@ spec = do
retrieveIO op `shouldReturn` Just "p2"
p1 <- OP.insertValue op 1 "p1"
retrieveIO op `shouldReturn` Just "p2"
disposeIO p2
awaitDispose p2
retrieveIO op `shouldReturn` Just "p1"
disposeIO p1
awaitDispose p1
retrieveIO op `shouldReturn` Nothing
it "sends updates when its value changes" $ do
result <- newIORef []
......@@ -40,9 +40,9 @@ spec = do
mostRecentShouldBe (Just "p2")
p1 <- OP.insertValue op 1 "p1"
mostRecentShouldBe (Just "p2")
disposeIO p2
awaitDispose p2
mostRecentShouldBe (Just "p1")
disposeIO p1
awaitDispose p1
mostRecentShouldBe Nothing
length <$> readIORef result `shouldReturn` 4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment