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

Rename disposable helpers


Co-authored-by: default avatarJan Beinke <git@janbeinke.com>
parent 807bf686
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,8 @@ module Quasar.Disposable (
registerDisposable,
registerDisposeAction,
disposeEventually,
withOnResourceManager,
withResourceManagerM,
withSubResourceManagerM,
onResourceManager,
captureDisposable,
captureTask,
......@@ -255,6 +256,11 @@ registerDisposeAction :: MonadResourceManager m => IO (Awaitable ()) -> m ()
registerDisposeAction disposeAction = mask_ $ registerDisposable =<< newDisposable disposeAction
withSubResourceManagerM :: MonadResourceManager m => m a -> m a
withSubResourceManagerM action =
bracket newResourceManager (await <=< dispose) \scope -> localResourceManager scope action
instance (MonadAwait m, MonadMask m, MonadIO m) => MonadResourceManager (ReaderT ResourceManager m) where
localResourceManager resourceManager = local (const resourceManager)
......@@ -320,8 +326,8 @@ instance IsDisposable ResourceManager where
withResourceManager :: (MonadAwait m, MonadMask m, MonadIO m) => (ResourceManager -> m a) -> m a
withResourceManager = bracket unsafeNewResourceManager (await <=< liftIO . dispose)
withOnResourceManager :: (MonadAwait m, MonadMask m, MonadIO m) => (ReaderT ResourceManager m a) -> m a
withOnResourceManager action = withResourceManager \resourceManager -> onResourceManager resourceManager action
withResourceManagerM :: (MonadAwait m, MonadMask m, MonadIO m) => (ReaderT ResourceManager m a) -> m a
withResourceManagerM action = withResourceManager \resourceManager -> onResourceManager resourceManager action
newResourceManager :: MonadResourceManager m => m ResourceManager
newResourceManager = mask_ do
......
......@@ -78,7 +78,7 @@ class IsRetrievable v a | a -> v where
retrieve :: MonadResourceManager m => a -> m (Awaitable v)
retrieveIO :: IsRetrievable v a => a -> IO v
retrieveIO x = withOnResourceManager $ await =<< retrieve x
retrieveIO x = withResourceManagerM $ await =<< retrieve x
class IsRetrievable v o => IsObservable v o | o -> v where
observe
......
......@@ -14,10 +14,10 @@ spec :: Spec
spec = parallel $ do
describe "async" $ do
it "can pass a value through async and await" $ do
withOnResourceManager (runUnlimitedAsync (await =<< async (pure 42))) `shouldReturn` (42 :: Int)
withResourceManagerM (runUnlimitedAsync (await =<< async (pure 42))) `shouldReturn` (42 :: Int)
it "can pass a value through async and await" $ do
withOnResourceManager (runUnlimitedAsync (await =<< async (liftIO (threadDelay 100000) >> pure 42))) `shouldReturn` (42 :: Int)
withResourceManagerM (runUnlimitedAsync (await =<< async (liftIO (threadDelay 100000) >> pure 42))) `shouldReturn` (42 :: Int)
describe "await" $ do
it "can await the result of an async that is completed later" $ do
......@@ -33,6 +33,6 @@ spec = parallel $ do
it "can terminate when encountering an asynchronous exception" $ do
never <- newAsyncVar :: IO (AsyncVar ())
result <- timeout 100000 $ withOnResourceManager $
result <- timeout 100000 $ withResourceManagerM $
await never
result `shouldBe` Nothing
......@@ -18,7 +18,7 @@ observableSpec = parallel do
it "works" $ io do
shouldReturn
do
withOnResourceManager do
withResourceManagerM do
observeWhile (pure () :: Observable ()) toObservableUpdate
()
......
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