module LaunchDarkly.Server.Client.Internal
( Client (..)
, Status (..)
, clientVersion
, setStatus
, getStatusI
) where
import Control.Concurrent (ThreadId)
import Control.Concurrent.MVar (MVar)
import Data.Generics.Product (getField)
import Data.IORef (IORef, atomicModifyIORef', readIORef)
import Data.Text (Text)
import GHC.Generics (Generic)
import LaunchDarkly.Server.Client.Status (Status (..), transitionStatus)
import LaunchDarkly.Server.Config.Internal (Config)
import LaunchDarkly.Server.DataSource.Internal (DataSource)
import LaunchDarkly.Server.Events (EventState)
import LaunchDarkly.Server.Store.Internal (StoreHandle, getInitializedC)
clientVersion :: Text
clientVersion :: Text
clientVersion = "4.3.0"
data Client = Client
{ Client -> Config
config :: !(Config)
, Client -> StoreHandle IO
store :: !(StoreHandle IO)
, Client -> IORef Status
status :: !(IORef Status)
, Client -> EventState
events :: !EventState
, Client -> Maybe (ThreadId, MVar ())
eventThreadPair :: !(Maybe (ThreadId, MVar ()))
, Client -> DataSource
dataSource :: !DataSource
}
deriving ((forall x. Client -> Rep Client x)
-> (forall x. Rep Client x -> Client) -> Generic Client
forall x. Rep Client x -> Client
forall x. Client -> Rep Client x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Client x -> Client
$cfrom :: forall x. Client -> Rep Client x
Generic)
setStatus :: Client -> Status -> IO ()
setStatus :: Client -> Status -> IO ()
setStatus client :: Client
client status' :: Status
status' =
IORef Status -> (Status -> (Status, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' (Client -> IORef Status
forall (f :: Symbol) a s. HasField' f s a => s -> a
getField @"status" Client
client) ((Status -> (Status, ()))
-> (Status -> Status) -> Status -> (Status, ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (,()) (Status -> Status -> Status
transitionStatus Status
status'))
getStatusI :: Client -> IO Status
getStatusI :: Client -> IO Status
getStatusI client :: Client
client =
IORef Status -> IO Status
forall a. IORef a -> IO a
readIORef (Client -> IORef Status
forall (f :: Symbol) a s. HasField' f s a => s -> a
getField @"status" Client
client) IO Status -> (Status -> IO Status) -> IO Status
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Unauthorized -> Status -> IO Status
forall (f :: * -> *) a. Applicative f => a -> f a
pure Status
Unauthorized
ShuttingDown -> Status -> IO Status
forall (f :: * -> *) a. Applicative f => a -> f a
pure Status
ShuttingDown
_ ->
StoreHandle IO -> StoreResultM IO Bool
forall store (m :: * -> *).
LaunchDarklyStoreRead store m =>
store -> StoreResultM m Bool
getInitializedC (Client -> StoreHandle IO
forall (f :: Symbol) a s. HasField' f s a => s -> a
getField @"store" Client
client) StoreResultM IO Bool
-> (Either Text Bool -> IO Status) -> IO Status
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Right True -> Status -> IO Status
forall (f :: * -> *) a. Applicative f => a -> f a
pure Status
Initialized
_ -> Status -> IO Status
forall (f :: * -> *) a. Applicative f => a -> f a
pure Status
Uninitialized