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

Add IsDisposable instances for ResourceManager and AsyncTask

parent 985158ab
No related branches found
No related tags found
No related merge requests found
...@@ -7,11 +7,13 @@ module Quasar.Core ( ...@@ -7,11 +7,13 @@ module Quasar.Core (
withDefaultResourceManager, withDefaultResourceManager,
withUnlimitedResourceManager, withUnlimitedResourceManager,
newResourceManager, newResourceManager,
disposeResourceManager, defaultResourceManagerConfiguration,
unlimitedResourceManagerConfiguration,
-- * AsyncTask -- * AsyncTask
AsyncTask, AsyncTask,
cancelTask, cancelTask,
cancelTaskIO,
toAsyncTask, toAsyncTask,
successfulTask, successfulTask,
...@@ -30,6 +32,8 @@ module Quasar.Core ( ...@@ -30,6 +32,8 @@ module Quasar.Core (
noDisposable, noDisposable,
disposeEventually, disposeEventually,
boundDisposable, boundDisposable,
attachDisposeAction,
attachDisposeAction_,
) where ) where
import Control.Concurrent (ThreadId, forkIOWithUnmask, myThreadId) import Control.Concurrent (ThreadId, forkIOWithUnmask, myThreadId)
...@@ -84,12 +88,14 @@ instance HasResourceManager AsyncIO where ...@@ -84,12 +88,14 @@ instance HasResourceManager AsyncIO where
awaitResult :: IsAwaitable r a => AsyncIO a -> AsyncIO r awaitResult :: IsAwaitable r a => AsyncIO a -> AsyncIO r
awaitResult = (await =<<) awaitResult = (await =<<)
-- TODO rename to ResourceManager
data ResourceManager = ResourceManager { data ResourceManager = ResourceManager {
configuration :: ResourceManagerConfiguraiton, configuration :: ResourceManagerConfiguraiton,
threads :: TVar (HashSet ThreadId) threads :: TVar (HashSet ThreadId)
} }
instance IsDisposable ResourceManager where
dispose x = pure $ pure ()
-- | A task that is running asynchronously. It has a result and can fail. -- | A task that is running asynchronously. It has a result and can fail.
-- The result (or exception) can be aquired by using the `Awaitable` class (e.g. by calling `await` or `awaitIO`). -- The result (or exception) can be aquired by using the `Awaitable` class (e.g. by calling `await` or `awaitIO`).
...@@ -100,6 +106,9 @@ newtype AsyncTask r = AsyncTask (Awaitable r) ...@@ -100,6 +106,9 @@ newtype AsyncTask r = AsyncTask (Awaitable r)
instance IsAwaitable r (AsyncTask r) where instance IsAwaitable r (AsyncTask r) where
toAwaitable (AsyncTask awaitable) = awaitable toAwaitable (AsyncTask awaitable) = awaitable
instance IsDisposable (AsyncTask r) where
dispose = undefined
instance Functor AsyncTask where instance Functor AsyncTask where
fmap fn (AsyncTask x) = AsyncTask (fn <$> x) fmap fn (AsyncTask x) = AsyncTask (fn <$> x)
...@@ -107,9 +116,11 @@ instance Applicative AsyncTask where ...@@ -107,9 +116,11 @@ instance Applicative AsyncTask where
pure = AsyncTask . pure pure = AsyncTask . pure
liftA2 fn (AsyncTask fx) (AsyncTask fy) = AsyncTask $ liftA2 fn fx fy liftA2 fn (AsyncTask fx) (AsyncTask fy) = AsyncTask $ liftA2 fn fx fy
cancelTask :: AsyncTask r -> IO () cancelTask :: AsyncTask r -> IO (Awaitable ())
-- TODO resource management cancelTask = dispose
cancelTask = const (pure ())
cancelTaskIO :: AsyncTask r -> IO ()
cancelTaskIO = awaitIO <=< dispose
-- | Creates an `AsyncTask` from an `Awaitable`. -- | Creates an `AsyncTask` from an `Awaitable`.
-- The resulting task only depends on an external resource, so disposing it has no effect. -- The resulting task only depends on an external resource, so disposing it has no effect.
...@@ -228,4 +239,12 @@ disposeEventually resourceManager disposable = liftIO $ do ...@@ -228,4 +239,12 @@ disposeEventually resourceManager disposable = liftIO $ do
-- | Creates an `Disposable` that is bound to a ResourceManager. It will automatically be disposed when the resource manager is disposed. -- | Creates an `Disposable` that is bound to a ResourceManager. It will automatically be disposed when the resource manager is disposed.
boundDisposable :: HasResourceManager m => IO (Awaitable ()) -> m Disposable boundDisposable :: HasResourceManager m => IO (Awaitable ()) -> m Disposable
boundDisposable = undefined boundDisposable action = do
resourceManager <- askResourceManager
attachDisposeAction resourceManager action
attachDisposeAction :: MonadIO m => ResourceManager -> IO (Awaitable ()) -> m Disposable
attachDisposeAction = undefined
attachDisposeAction_ :: MonadIO m => ResourceManager -> IO (Awaitable ()) -> m ()
attachDisposeAction_ resourceManager action = void $ attachDisposeAction resourceManager action
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