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

Add instances to Awaitable (MonadThrow, MonadCatch and more)

parent c364e76c
No related branches found
No related tags found
No related merge requests found
Pipeline #2399 passed
...@@ -34,6 +34,7 @@ module Quasar.Awaitable ( ...@@ -34,6 +34,7 @@ module Quasar.Awaitable (
cacheAwaitableDefaultImplementation, cacheAwaitableDefaultImplementation,
) where ) where
import Control.Applicative (empty)
import Control.Concurrent.STM import Control.Concurrent.STM
import Control.Monad.Catch import Control.Monad.Catch
import Control.Monad.Reader import Control.Monad.Reader
...@@ -93,6 +94,25 @@ instance Semigroup r => Semigroup (Awaitable r) where ...@@ -93,6 +94,25 @@ instance Semigroup r => Semigroup (Awaitable r) where
instance Monoid r => Monoid (Awaitable r) where instance Monoid r => Monoid (Awaitable r) where
mempty = pure mempty mempty = pure mempty
instance MonadThrow Awaitable where
throwM = failedAwaitable . toException
instance MonadCatch Awaitable where
catch awaitable handler = toAwaitable $ FnAwaitable do
runAwaitable awaitable >>= \case
l@(Left ex) -> maybe (pure l) (runAwaitable . handler) $ fromException ex
Right value -> pure $ Right value
instance MonadFail Awaitable where
fail = throwM . userError
instance Alternative Awaitable where
x <|> y = x `catchAll` const y
empty = failedAwaitable $ toException $ userError "empty"
instance MonadPlus Awaitable
newtype FnAwaitable r = FnAwaitable (forall m. (MonadQuerySTM m) => m (Either SomeException r)) newtype FnAwaitable r = FnAwaitable (forall m. (MonadQuerySTM m) => m (Either SomeException r))
......
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