module LaunchDarkly.Server.Client.Internal
( Client(..)
, ClientI(..)
, Status(..)
, clientVersion
, setStatus
) where
import Data.Text (Text)
import Data.IORef (IORef, atomicModifyIORef')
import GHC.Generics (Generic)
import Control.Concurrent (ThreadId)
import Control.Concurrent.MVar (MVar)
import Data.Generics.Product (getField)
import LaunchDarkly.Server.Config.Internal (ConfigI)
import LaunchDarkly.Server.Store.Internal (StoreHandle)
import LaunchDarkly.Server.Events (EventState)
newtype Client = Client ClientI
clientVersion :: Text
clientVersion = "1.0.0"
data Status
= Uninitialized
| Unauthorized
| Initialized
| ShuttingDown
deriving (Eq)
setStatus :: ClientI -> Status -> IO ()
setStatus client status' = atomicModifyIORef' (getField @"status" client) $ \status ->
case status' of
Initialized -> (if status == Uninitialized then Initialized else status, ())
_ -> (if status == ShuttingDown then ShuttingDown else status', ())
data ClientI = ClientI
{ config :: !(ConfigI)
, store :: !(StoreHandle IO)
, status :: !(IORef Status)
, events :: !EventState
, downloadThreadPair :: !(Maybe (ThreadId, MVar ()))
, eventThreadPair :: !(Maybe (ThreadId, MVar ()))
} deriving (Generic)