module IHP.Controller.Context where
import IHP.Prelude
import qualified Data.TMap as TypeMap
import qualified Data.Typeable as Typeable
import IHP.Controller.RequestContext
import IHP.FrameworkConfig
import IHP.Log.Types
import System.IO.Unsafe (unsafePerformIO)
data ControllerContext = ControllerContext { ControllerContext -> RequestContext
requestContext :: RequestContext, ControllerContext -> IORef TMap
customFieldsRef :: IORef TypeMap.TMap }
| FrozenControllerContext { requestContext :: RequestContext, ControllerContext -> TMap
customFields :: TypeMap.TMap }
newControllerContext :: (?requestContext :: RequestContext) => IO ControllerContext
newControllerContext :: (?requestContext::RequestContext) => IO ControllerContext
newControllerContext = do
IORef TMap
customFieldsRef <- TMap -> IO (IORef TMap)
forall a. a -> IO (IORef a)
newIORef TMap
TypeMap.empty
ControllerContext -> IO ControllerContext
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ControllerContext { requestContext :: RequestContext
requestContext = ?requestContext::RequestContext
RequestContext
?requestContext, IORef TMap
customFieldsRef :: IORef TMap
customFieldsRef :: IORef TMap
customFieldsRef }
{-# INLINABLE newControllerContext #-}
freeze :: ControllerContext -> IO ControllerContext
freeze :: ControllerContext -> IO ControllerContext
freeze ControllerContext { RequestContext
requestContext :: ControllerContext -> RequestContext
requestContext :: RequestContext
requestContext, IORef TMap
customFieldsRef :: ControllerContext -> IORef TMap
customFieldsRef :: IORef TMap
customFieldsRef } = RequestContext -> TMap -> ControllerContext
FrozenControllerContext RequestContext
requestContext (TMap -> ControllerContext) -> IO TMap -> IO ControllerContext
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef TMap -> IO TMap
forall a. IORef a -> IO a
readIORef IORef TMap
customFieldsRef
freeze ControllerContext
frozen = ControllerContext -> IO ControllerContext
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ControllerContext
frozen
{-# INLINABLE freeze #-}
unfreeze :: ControllerContext -> IO ControllerContext
unfreeze :: ControllerContext -> IO ControllerContext
unfreeze FrozenControllerContext { RequestContext
requestContext :: ControllerContext -> RequestContext
requestContext :: RequestContext
requestContext, TMap
customFields :: ControllerContext -> TMap
customFields :: TMap
customFields } = do
IORef TMap
customFieldsRef <- TMap -> IO (IORef TMap)
forall a. a -> IO (IORef a)
newIORef TMap
customFields
ControllerContext -> IO ControllerContext
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ControllerContext { IORef TMap
RequestContext
requestContext :: RequestContext
customFieldsRef :: IORef TMap
requestContext :: RequestContext
customFieldsRef :: IORef TMap
.. }
unfreeze ControllerContext {} = Text -> IO ControllerContext
forall a. Text -> a
error Text
"Cannot call unfreeze on a controller context that is not frozen"
{-# INLINABLE unfreeze #-}
fromContext :: forall value. (?context :: ControllerContext, Typeable value) => IO value
fromContext :: forall value.
(?context::ControllerContext, Typeable value) =>
IO value
fromContext = forall value.
(?context::ControllerContext, Typeable value) =>
IO (Maybe value)
maybeFromContext @value IO (Maybe value) -> (Maybe value -> IO value) -> IO value
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just value
value -> value -> IO value
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure value
value
Maybe value
Nothing -> do
let ControllerContext { IORef TMap
customFieldsRef :: ControllerContext -> IORef TMap
customFieldsRef :: IORef TMap
customFieldsRef } = ?context::ControllerContext
ControllerContext
?context
TMap
customFields <- IORef TMap -> IO TMap
forall a. IORef a -> IO a
readIORef IORef TMap
customFieldsRef
let notFoundMessage :: Text
notFoundMessage = (Text
"Unable to find " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (TypeRep -> Text
forall a. Show a => a -> Text
show (Proxy value -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
Typeable.typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Typeable.Proxy @value))) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" in controller context: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TMap -> Text
forall a. Show a => a -> Text
show TMap
customFields)
Text -> IO value
forall a. Text -> a
error Text
notFoundMessage
{-# INLINABLE fromContext #-}
fromFrozenContext :: forall value. (?context :: ControllerContext, Typeable value) => value
fromFrozenContext :: forall value.
(?context::ControllerContext, Typeable value) =>
value
fromFrozenContext = case forall value.
(?context::ControllerContext, Typeable value) =>
Maybe value
maybeFromFrozenContext @value of
Just value
value -> value
value
Maybe value
Nothing -> do
let FrozenControllerContext { TMap
customFields :: ControllerContext -> TMap
customFields :: TMap
customFields } = ?context::ControllerContext
ControllerContext
?context
let notFoundMessage :: Text
notFoundMessage = (Text
"Unable to find " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (TypeRep -> Text
forall a. Show a => a -> Text
show (Proxy value -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
Typeable.typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Typeable.Proxy @value))) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" in controller context: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TMap -> Text
forall a. Show a => a -> Text
show TMap
customFields)
Text -> value
forall a. Text -> a
error Text
notFoundMessage
{-# INLINABLE fromFrozenContext #-}
maybeFromContext :: forall value. (?context :: ControllerContext, Typeable value) => IO (Maybe value)
maybeFromContext :: forall value.
(?context::ControllerContext, Typeable value) =>
IO (Maybe value)
maybeFromContext = do
ControllerContext
frozen <- ControllerContext -> IO ControllerContext
freeze ?context::ControllerContext
ControllerContext
?context
let ?context = ?context::ControllerContext
ControllerContext
frozen
Maybe value -> IO (Maybe value)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall value.
(?context::ControllerContext, Typeable value) =>
Maybe value
maybeFromFrozenContext @value)
{-# INLINABLE maybeFromContext #-}
maybeFromFrozenContext :: forall value. (?context :: ControllerContext, Typeable value) => Maybe value
maybeFromFrozenContext :: forall value.
(?context::ControllerContext, Typeable value) =>
Maybe value
maybeFromFrozenContext = case ?context::ControllerContext
ControllerContext
?context of
FrozenControllerContext { TMap
customFields :: ControllerContext -> TMap
customFields :: TMap
customFields } -> forall a. Typeable a => TMap -> Maybe a
TypeMap.lookup @value TMap
customFields
ControllerContext {} -> Text -> Maybe value
forall a. Text -> a
error (Text
"maybeFromFrozenContext called on a non frozen context while trying to access " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (TypeRep -> Text
forall a. Show a => a -> Text
show (Proxy value -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
Typeable.typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Typeable.Proxy @value))))
{-# INLINABLE maybeFromFrozenContext #-}
putContext :: forall value. (?context :: ControllerContext, Typeable value) => value -> IO ()
putContext :: forall value.
(?context::ControllerContext, Typeable value) =>
value -> IO ()
putContext value
value = do
let ControllerContext { IORef TMap
customFieldsRef :: ControllerContext -> IORef TMap
customFieldsRef :: IORef TMap
customFieldsRef } = ?context::ControllerContext
ControllerContext
?context
IORef TMap -> (TMap -> TMap) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef IORef TMap
customFieldsRef (value -> TMap -> TMap
forall a. Typeable a => a -> TMap -> TMap
TypeMap.insert value
value)
() -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINABLE putContext #-}
newtype ActionType = ActionType Typeable.TypeRep
instance HasField "frameworkConfig" ControllerContext FrameworkConfig where
getField :: ControllerContext -> FrameworkConfig
getField ControllerContext
controllerContext = ControllerContext
controllerContext.requestContext.frameworkConfig
{-# INLINABLE getField #-}
instance HasField "logger" ControllerContext Logger where
getField :: ControllerContext -> Logger
getField context :: ControllerContext
context@(FrozenControllerContext { TMap
customFields :: ControllerContext -> TMap
customFields :: TMap
customFields }) = Logger -> Maybe Logger -> Logger
forall a. a -> Maybe a -> a
fromMaybe ControllerContext
context.frameworkConfig.logger (forall a. Typeable a => TMap -> Maybe a
TypeMap.lookup @Logger TMap
customFields)
getField ControllerContext
context = (IO ControllerContext -> ControllerContext
forall a. IO a -> a
unsafePerformIO (ControllerContext -> IO ControllerContext
freeze ControllerContext
context)).logger