{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Context
(
Store
, withNonEmptyStore
, withEmptyStore
, use
, adjust
, withAdjusted
, mine
, mines
, mineMay
, minesMay
, module Context.View
, NotFoundException(NotFoundException, threadId)
, module Context.Concurrent
, module Context.Storage
) where
import Context.Concurrent
import Context.Internal (NotFoundException(NotFoundException, threadId), Store, mineMay, use)
import Context.Storage
import Context.View
import Control.Monad ((<=<))
import Control.Monad.Catch (MonadMask, MonadThrow)
import Control.Monad.IO.Class (MonadIO)
import Prelude
import qualified Context.Internal as Internal
withNonEmptyStore
:: forall m ctx a
. (MonadIO m, MonadMask m)
=> ctx
-> (Store ctx -> m a)
-> m a
withNonEmptyStore :: forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
ctx -> (Store ctx -> m a) -> m a
withNonEmptyStore = forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
PropagationStrategy -> Maybe ctx -> (Store ctx -> m a) -> m a
Internal.withStore PropagationStrategy
defaultPropagation forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just
withEmptyStore
:: forall m ctx a
. (MonadIO m, MonadMask m)
=> (Store ctx -> m a)
-> m a
withEmptyStore :: forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
(Store ctx -> m a) -> m a
withEmptyStore = forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
PropagationStrategy -> Maybe ctx -> (Store ctx -> m a) -> m a
Internal.withStore PropagationStrategy
defaultPropagation forall a. Maybe a
Nothing
adjust
:: forall m ctx a
. (MonadIO m, MonadMask m)
=> Store ctx
-> (ctx -> ctx)
-> m a
-> m a
adjust :: forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
Store ctx -> (ctx -> ctx) -> m a -> m a
adjust Store ctx
store ctx -> ctx
f m a
action = forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
Store ctx -> (ctx -> ctx) -> (ctx -> m a) -> m a
withAdjusted Store ctx
store ctx -> ctx
f forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const m a
action
withAdjusted
:: forall m ctx a
. (MonadIO m, MonadMask m)
=> Store ctx
-> (ctx -> ctx)
-> (ctx -> m a)
-> m a
withAdjusted :: forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
Store ctx -> (ctx -> ctx) -> (ctx -> m a) -> m a
withAdjusted Store ctx
store ctx -> ctx
f ctx -> m a
action = do
ctx
adjustedContext <- forall (m :: * -> *) ctx a.
(MonadIO m, MonadThrow m) =>
Store ctx -> (ctx -> a) -> m a
mines Store ctx
store ctx -> ctx
f
forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
Store ctx -> ctx -> m a -> m a
use Store ctx
store ctx
adjustedContext forall a b. (a -> b) -> a -> b
$ ctx -> m a
action ctx
adjustedContext
mine
:: forall m ctx
. (MonadIO m, MonadThrow m)
=> Store ctx
-> m ctx
mine :: forall (m :: * -> *) ctx.
(MonadIO m, MonadThrow m) =>
Store ctx -> m ctx
mine = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *) a. (MonadIO m, MonadThrow m) => m a
Internal.throwContextNotFound forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall (m :: * -> *) ctx. MonadIO m => Store ctx -> m (Maybe ctx)
mineMay
mines
:: forall m ctx a
. (MonadIO m, MonadThrow m)
=> Store ctx
-> (ctx -> a)
-> m a
mines :: forall (m :: * -> *) ctx a.
(MonadIO m, MonadThrow m) =>
Store ctx -> (ctx -> a) -> m a
mines Store ctx
store = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *) a. (MonadIO m, MonadThrow m) => m a
Internal.throwContextNotFound forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall (m :: * -> *) ctx a.
MonadIO m =>
Store ctx -> (ctx -> a) -> m (Maybe a)
minesMay Store ctx
store
minesMay
:: forall m ctx a
. (MonadIO m)
=> Store ctx
-> (ctx -> a)
-> m (Maybe a)
minesMay :: forall (m :: * -> *) ctx a.
MonadIO m =>
Store ctx -> (ctx -> a) -> m (Maybe a)
minesMay Store ctx
store ctx -> a
selector = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ctx -> a
selector) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) ctx. MonadIO m => Store ctx -> m (Maybe ctx)
mineMay Store ctx
store