This commit is contained in:
parent
8c82cd81bd
commit
2159e273fa
13 changed files with 56 additions and 42 deletions
|
@ -1,10 +1,10 @@
|
|||
{-# LANGUAGE DerivingVia #-}
|
||||
|
||||
module Types.AppTypes (Env (..), App (..), getConnectedClientState, HasConnectedClientState, AppProfile (Prod, Dev)) where
|
||||
module Types.AppTypes (Env (..), App (..), getConnectedClientState, AppProfile (Prod, Dev)) where
|
||||
|
||||
import ClassyPrelude
|
||||
import Types.ConnectionState (ConnectedClientsState)
|
||||
import Types.RoomsState
|
||||
import State.ConnectedClientsState
|
||||
import State.RoomsState
|
||||
( HasRoomsState (getRoomsState),
|
||||
RoomsState,
|
||||
)
|
||||
|
@ -17,9 +17,6 @@ data Env = Env
|
|||
profile :: AppProfile
|
||||
}
|
||||
|
||||
class HasConnectedClientState a where
|
||||
getConnectedClientState :: a -> ConnectedClientsState
|
||||
|
||||
instance HasConnectedClientState Env where
|
||||
getConnectedClientState = connectedClientsState
|
||||
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
module Types.ConnectionState
|
||||
( Client (..),
|
||||
ConnectedClientsState,
|
||||
ConnectedClients,
|
||||
initConnectionsState,
|
||||
)
|
||||
where
|
||||
|
||||
|
@ -17,12 +15,4 @@ data Client = Client
|
|||
joinedRoom :: Bool
|
||||
}
|
||||
|
||||
type ConnectedClientsState = TVar ConnectedClients
|
||||
|
||||
type ConnectedClients = [Client]
|
||||
|
||||
initConnectionsState :: IO ConnectedClientsState
|
||||
initConnectionsState = newTVarIO newConnectedClients
|
||||
|
||||
newConnectedClients :: ConnectedClients
|
||||
newConnectedClients = []
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
module Types.RoomsState
|
||||
( RoomsState,
|
||||
initRoomsState,
|
||||
HasRoomsState (..),
|
||||
roomStateDiffers,
|
||||
updateRoomState,
|
||||
getRoomState,
|
||||
)
|
||||
where
|
||||
|
||||
import ClassyPrelude
|
||||
import State.RoomDataState (MonadRoomDataStateRead (getRoomDataState))
|
||||
import Types.RoomData (RoomsData)
|
||||
|
||||
type RoomsState = TVar RoomsData
|
||||
|
||||
initRoomsState :: IO RoomsState
|
||||
initRoomsState = newTVarIO []
|
||||
|
||||
class HasRoomsState a where
|
||||
getRoomsState :: a -> RoomsState
|
||||
|
||||
updateRoomState ::
|
||||
( HasRoomsState env,
|
||||
MonadIO m,
|
||||
MonadReader env m
|
||||
) =>
|
||||
RoomsData ->
|
||||
m ()
|
||||
updateRoomState newData = do
|
||||
state <- getRoomsState <$> ask
|
||||
liftIO $ putStrLn "Upating room state"
|
||||
atomically $ writeTVar state newData
|
||||
liftIO $ putStrLn "Done Upating room state"
|
||||
|
||||
getRoomState ::
|
||||
( HasRoomsState env,
|
||||
MonadIO m,
|
||||
MonadReader env m
|
||||
) =>
|
||||
m RoomsData
|
||||
getRoomState = do
|
||||
state <- getRoomsState <$> ask
|
||||
readTVarIO state
|
||||
|
||||
roomStateDiffers ::
|
||||
( MonadRoomDataStateRead m
|
||||
) =>
|
||||
RoomsData ->
|
||||
m Bool
|
||||
roomStateDiffers newData = do
|
||||
not . eqIgnoreOrdering newData <$> getRoomDataState
|
||||
|
||||
eqIgnoreOrdering :: (Eq a) => [a] -> [a] -> Bool
|
||||
eqIgnoreOrdering a b = length a == length b && all (`elem` b) a
|
|
@ -6,11 +6,11 @@ where
|
|||
|
||||
import ClassyPrelude
|
||||
import Network.Wai (Request, Response, ResponseReceived)
|
||||
import State.ConnectedClientsState (HasConnectedClientState (getConnectedClientState))
|
||||
import State.RoomsState (HasRoomsState (getRoomsState))
|
||||
import Types.AppTypes
|
||||
( Env (..),
|
||||
HasConnectedClientState (getConnectedClientState),
|
||||
)
|
||||
import Types.RoomsState (HasRoomsState (getRoomsState))
|
||||
|
||||
class HasWebEnv a where
|
||||
getRequest :: a -> Request
|
||||
|
|
|
@ -29,23 +29,22 @@ data WebSocketMessage = ClientInfoMessage SetClientInfo | JoinRoomMessage JoinRo
|
|||
instance FromJSON WebSocketMessage where
|
||||
parseJSON = genericParseJSON defaultOptions {sumEncoding = UntaggedValue}
|
||||
|
||||
data SetClientInfo = SetClientInfo
|
||||
{ displayName :: Text
|
||||
}
|
||||
data SetClientInfo where
|
||||
SetClientInfo :: {displayName :: Text} -> SetClientInfo
|
||||
deriving (Generic, Show)
|
||||
|
||||
instance FromJSON SetClientInfo
|
||||
|
||||
data JoinRoom = JoinRoom
|
||||
{ roomName :: Text
|
||||
}
|
||||
data JoinRoom where
|
||||
JoinRoom :: {roomName :: Text} -> JoinRoom
|
||||
deriving (Generic, Show)
|
||||
|
||||
instance FromJSON JoinRoom
|
||||
|
||||
data AllChatMessageIncoming = AllChatMessageIncoming
|
||||
{ content :: Text
|
||||
}
|
||||
data AllChatMessageIncoming where
|
||||
AllChatMessageIncoming ::
|
||||
{content :: Text} ->
|
||||
AllChatMessageIncoming
|
||||
deriving (Generic, Show)
|
||||
|
||||
instance FromJSON AllChatMessageIncoming
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue