2023-02-18 18:25:38 +01:00
|
|
|
module State.ConnectedClientsState
|
|
|
|
( MonadConnectedClientsModify (..),
|
|
|
|
MonadConnectedClientsRead (..),
|
2023-08-13 12:06:23 +02:00
|
|
|
ConnectedClients,
|
|
|
|
ConnectedClientsState,
|
|
|
|
HasConnectedClientState (..),
|
|
|
|
initConnectionsState,
|
2023-02-18 18:25:38 +01:00
|
|
|
addWSClientGeneric,
|
|
|
|
updateWSClientGeneric,
|
|
|
|
removeWSClientGeneric,
|
|
|
|
getConnctedClientsGeneric,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import ClassyPrelude
|
|
|
|
import Data.UUID
|
|
|
|
import Types.ConnectionState
|
|
|
|
|
2023-08-13 12:06:23 +02:00
|
|
|
type ConnectedClientsState = TVar ConnectedClients
|
|
|
|
|
|
|
|
initConnectionsState :: IO ConnectedClientsState
|
|
|
|
initConnectionsState = newTVarIO newConnectedClients
|
|
|
|
|
|
|
|
newConnectedClients :: ConnectedClients
|
|
|
|
newConnectedClients = []
|
|
|
|
|
2023-02-18 18:25:38 +01:00
|
|
|
class Monad m => MonadConnectedClientsModify m where
|
|
|
|
addWSClient :: Client -> m ()
|
|
|
|
removeWSClient :: UUID -> m ()
|
|
|
|
updateWSClient :: UUID -> (Client -> Client) -> m ()
|
|
|
|
|
2023-08-13 12:06:23 +02:00
|
|
|
class HasConnectedClientState a where
|
|
|
|
getConnectedClientState :: a -> ConnectedClientsState
|
|
|
|
|
2023-02-18 18:25:38 +01:00
|
|
|
addWSClientGeneric ::
|
|
|
|
( HasConnectedClientState env,
|
|
|
|
MonadReader env m,
|
|
|
|
MonadUnliftIO m
|
|
|
|
) =>
|
|
|
|
Client ->
|
|
|
|
m ()
|
|
|
|
addWSClientGeneric client = do
|
|
|
|
modifyState $ addClient client
|
|
|
|
|
|
|
|
addClient :: Client -> ConnectedClients -> ConnectedClients
|
|
|
|
addClient client clients = client : clients
|
|
|
|
|
|
|
|
updateWSClientGeneric ::
|
|
|
|
( HasConnectedClientState env,
|
|
|
|
MonadReader env m,
|
|
|
|
MonadUnliftIO m
|
|
|
|
) =>
|
|
|
|
UUID ->
|
|
|
|
(Client -> Client) ->
|
|
|
|
m
|
|
|
|
()
|
|
|
|
updateWSClientGeneric clientId patchFunction = do
|
|
|
|
modifyState $ updateClient clientId patchFunction
|
|
|
|
|
|
|
|
updateClient :: UUID -> (Client -> Client) -> ConnectedClients -> ConnectedClients
|
|
|
|
updateClient toUpdate f allClients = do
|
|
|
|
client <- allClients
|
|
|
|
if toUpdate == uuid client
|
|
|
|
then return $ f client
|
|
|
|
else return client
|
|
|
|
|
|
|
|
removeWSClientGeneric ::
|
|
|
|
( HasConnectedClientState env,
|
|
|
|
MonadReader env m,
|
|
|
|
MonadUnliftIO m
|
|
|
|
) =>
|
|
|
|
UUID ->
|
|
|
|
m ()
|
|
|
|
removeWSClientGeneric clientId = do
|
|
|
|
modifyState $ removeClient clientId
|
|
|
|
|
|
|
|
removeClient :: UUID -> ConnectedClients -> ConnectedClients
|
|
|
|
removeClient toRemove = filter ((/= toRemove) . uuid)
|
|
|
|
|
|
|
|
modifyState ::
|
|
|
|
( HasConnectedClientState env,
|
|
|
|
MonadReader env m,
|
|
|
|
MonadUnliftIO m
|
|
|
|
) =>
|
|
|
|
(ConnectedClients -> ConnectedClients) ->
|
|
|
|
m ()
|
|
|
|
modifyState modifyFunc = do
|
|
|
|
state <- getConnectedClientState <$> ask
|
2023-06-19 17:53:40 +02:00
|
|
|
atomically $ modifyTVar state modifyFunc
|
2023-02-18 18:25:38 +01:00
|
|
|
|
|
|
|
class Monad m => MonadConnectedClientsRead m where
|
|
|
|
getConnctedClients :: m ConnectedClients
|
|
|
|
|
|
|
|
getConnctedClientsGeneric ::
|
|
|
|
( HasConnectedClientState env,
|
|
|
|
MonadReader env m,
|
2023-02-19 11:41:32 +01:00
|
|
|
MonadIO m
|
2023-02-18 18:25:38 +01:00
|
|
|
) =>
|
|
|
|
m ConnectedClients
|
|
|
|
getConnctedClientsGeneric = do
|
2023-06-19 17:53:40 +02:00
|
|
|
ask >>= readTVarIO . getConnectedClientState
|