Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to cancel running Async actions #321

Merged
merged 2 commits into from
Jun 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Polysemy/Async.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Polysemy.Async
-- * Actions
, async
, await
, cancel

-- * Helpers
, sequenceConcurrently
Expand All @@ -33,6 +34,7 @@ import Polysemy.Final
data Async m a where
Async :: m a -> Async m (A.Async (Maybe a))
Await :: A.Async a -> Async m a
Cancel :: A.Async a -> Async m ()

makeSem ''Async

Expand All @@ -46,7 +48,6 @@ sequenceConcurrently :: forall t r a. (Traversable t, Member Async r) =>
sequenceConcurrently t = traverse async t >>= traverse await
{-# INLINABLE sequenceConcurrently #-}


------------------------------------------------------------------------------
-- | A more flexible --- though less performant ---
-- version of 'asyncToIOFinal'.
Expand Down Expand Up @@ -80,6 +81,7 @@ asyncToIO m = withLowerToIO $ \lower _ -> lower $
pureT $ fmap (inspect ins) fa

Await a -> pureT =<< embed (A.wait a)
Cancel a -> pureT =<< embed (A.cancel a)
) m
{-# INLINE asyncToIO #-}

Expand Down Expand Up @@ -110,6 +112,7 @@ asyncToIOFinal = interpretFinal $ \case
m' <- runS m
liftS $ A.async (inspect ins <$> m')
Await a -> liftS (A.wait a)
Cancel a -> liftS (A.cancel a)
{-# INLINE asyncToIOFinal #-}

------------------------------------------------------------------------------
Expand All @@ -132,6 +135,7 @@ lowerAsync lower m = interpretH
pureT $ fmap (inspect ins) fa

Await a -> pureT =<< embed (A.wait a)
Cancel a -> pureT =<< embed (A.cancel a)
) m
{-# INLINE lowerAsync #-}
{-# DEPRECATED lowerAsync "Use 'asyncToIOFinal' instead" #-}