{-# 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 = PropagationStrategy -> Maybe ctx -> (Store ctx -> m a) -> m a
forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
PropagationStrategy -> Maybe ctx -> (Store ctx -> m a) -> m a
Internal.withStore PropagationStrategy
defaultPropagation (Maybe ctx -> (Store ctx -> m a) -> m a)
-> (ctx -> Maybe ctx) -> ctx -> (Store ctx -> m a) -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx -> Maybe ctx
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 = PropagationStrategy -> Maybe ctx -> (Store ctx -> m a) -> m a
forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
PropagationStrategy -> Maybe ctx -> (Store ctx -> m a) -> m a
Internal.withStore PropagationStrategy
defaultPropagation Maybe ctx
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 = Store ctx -> (ctx -> ctx) -> (ctx -> m a) -> m a
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) -> m a) -> (ctx -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ m a -> ctx -> m a
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 <- Store ctx -> (ctx -> ctx) -> m ctx
forall (m :: * -> *) ctx a.
(MonadIO m, MonadThrow m) =>
Store ctx -> (ctx -> a) -> m a
mines Store ctx
store ctx -> ctx
f
Store ctx -> ctx -> m a -> m a
forall (m :: * -> *) ctx a.
(MonadIO m, MonadMask m) =>
Store ctx -> ctx -> m a -> m a
use Store ctx
store ctx
adjustedContext (m a -> m a) -> m a -> m a
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 = m ctx -> (ctx -> m ctx) -> Maybe ctx -> m ctx
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m ctx
forall (m :: * -> *) a. (MonadIO m, MonadThrow m) => m a
Internal.throwContextNotFound ctx -> m ctx
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe ctx -> m ctx)
-> (Store ctx -> m (Maybe ctx)) -> Store ctx -> m ctx
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Store ctx -> m (Maybe ctx)
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 = m a -> (a -> m a) -> Maybe a -> m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m a
forall (m :: * -> *) a. (MonadIO m, MonadThrow m) => m a
Internal.throwContextNotFound a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> m a)
-> ((ctx -> a) -> m (Maybe a)) -> (ctx -> a) -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Store ctx -> (ctx -> a) -> m (Maybe a)
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 = (Maybe ctx -> Maybe a) -> m (Maybe ctx) -> m (Maybe a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ctx -> a) -> Maybe ctx -> Maybe a
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ctx -> a
selector) (m (Maybe ctx) -> m (Maybe a)) -> m (Maybe ctx) -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ Store ctx -> m (Maybe ctx)
forall (m :: * -> *) ctx. MonadIO m => Store ctx -> m (Maybe ctx)
mineMay Store ctx
store