-- | Module: Lifetimes.Async
-- Description: Lifteimes integration for the async package.
module Lifetimes.Async (acquireAsync) where

import Control.Concurrent.Async (Async, async, cancel, waitCatch)
import Lifetimes
import Zhp

-- | Spawn an async task. When it is time to reclaim the resource, 'cancel'
-- will be called, and the task will be waited on.
acquireAsync :: IO a -> Acquire (Async a)
acquireAsync :: forall a. IO a -> Acquire (Async a)
acquireAsync IO a
io = forall a. IO a -> (a -> IO ()) -> Acquire a
mkAcquire (forall a. IO a -> IO (Async a)
async IO a
io) (\Async a
a -> forall a. Async a -> IO ()
cancel Async a
a forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall a. Async a -> IO (Either SomeException a)
waitCatch Async a
a)