Switch to TVar
This commit is contained in:
parent
03985cede2
commit
1e318817a4
|
@ -41,8 +41,8 @@ broadCastToClientsGeneric ::
|
||||||
Text ->
|
Text ->
|
||||||
m ()
|
m ()
|
||||||
broadCastToClientsGeneric message = do
|
broadCastToClientsGeneric message = do
|
||||||
state <- getConnctedClients
|
connectedClients <- getConnctedClients
|
||||||
liftIO $ broadcast message state
|
liftIO (broadcast message connectedClients)
|
||||||
|
|
||||||
broadcast :: Text -> ConnectedClients -> IO ()
|
broadcast :: Text -> ConnectedClients -> IO ()
|
||||||
broadcast message clients = do
|
broadcast message clients = do
|
||||||
|
|
|
@ -72,9 +72,7 @@ modifyState ::
|
||||||
m ()
|
m ()
|
||||||
modifyState modifyFunc = do
|
modifyState modifyFunc = do
|
||||||
state <- getConnectedClientState <$> ask
|
state <- getConnectedClientState <$> ask
|
||||||
modifyMVar_ state $ \s ->
|
atomically $ modifyTVar state modifyFunc
|
||||||
let s' = modifyFunc s
|
|
||||||
in return s'
|
|
||||||
|
|
||||||
class Monad m => MonadConnectedClientsRead m where
|
class Monad m => MonadConnectedClientsRead m where
|
||||||
getConnctedClients :: m ConnectedClients
|
getConnctedClients :: m ConnectedClients
|
||||||
|
@ -86,4 +84,4 @@ getConnctedClientsGeneric ::
|
||||||
) =>
|
) =>
|
||||||
m ConnectedClients
|
m ConnectedClients
|
||||||
getConnctedClientsGeneric = do
|
getConnctedClientsGeneric = do
|
||||||
ask >>= readMVar . getConnectedClientState
|
ask >>= readTVarIO . getConnectedClientState
|
||||||
|
|
|
@ -17,12 +17,12 @@ data Client = Client
|
||||||
joinedRoom :: Bool
|
joinedRoom :: Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectedClientsState = MVar ConnectedClients
|
type ConnectedClientsState = TVar ConnectedClients
|
||||||
|
|
||||||
type ConnectedClients = [Client]
|
type ConnectedClients = [Client]
|
||||||
|
|
||||||
initConnectionsState :: IO ConnectedClientsState
|
initConnectionsState :: IO ConnectedClientsState
|
||||||
initConnectionsState = newMVar newConnectedClients
|
initConnectionsState = newTVarIO newConnectedClients
|
||||||
|
|
||||||
newConnectedClients :: ConnectedClients
|
newConnectedClients :: ConnectedClients
|
||||||
newConnectedClients = []
|
newConnectedClients = []
|
||||||
|
|
|
@ -12,10 +12,10 @@ import ClassyPrelude
|
||||||
import State.RoomDataState (MonadRoomDataStateRead (getRoomDataState))
|
import State.RoomDataState (MonadRoomDataStateRead (getRoomDataState))
|
||||||
import Types.RoomData (RoomsData)
|
import Types.RoomData (RoomsData)
|
||||||
|
|
||||||
type RoomsState = MVar RoomsData
|
type RoomsState = TVar RoomsData
|
||||||
|
|
||||||
initRoomsState :: IO RoomsState
|
initRoomsState :: IO RoomsState
|
||||||
initRoomsState = newMVar []
|
initRoomsState = newTVarIO []
|
||||||
|
|
||||||
class HasRoomsState a where
|
class HasRoomsState a where
|
||||||
getRoomsState :: a -> RoomsState
|
getRoomsState :: a -> RoomsState
|
||||||
|
@ -29,7 +29,7 @@ updateRoomState ::
|
||||||
m ()
|
m ()
|
||||||
updateRoomState newData = do
|
updateRoomState newData = do
|
||||||
state <- getRoomsState <$> ask
|
state <- getRoomsState <$> ask
|
||||||
_ <- swapMVar state newData
|
_ <- atomically $ swapTVar state newData
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
getRoomState ::
|
getRoomState ::
|
||||||
|
@ -40,7 +40,7 @@ getRoomState ::
|
||||||
m RoomsData
|
m RoomsData
|
||||||
getRoomState = do
|
getRoomState = do
|
||||||
state <- getRoomsState <$> ask
|
state <- getRoomsState <$> ask
|
||||||
readMVar state
|
readTVarIO state
|
||||||
|
|
||||||
roomStateDiffers ::
|
roomStateDiffers ::
|
||||||
( MonadRoomDataStateRead m
|
( MonadRoomDataStateRead m
|
||||||
|
|
|
@ -121,6 +121,24 @@ notFound = do
|
||||||
"404 - Not Found"
|
"404 - Not Found"
|
||||||
throwError response
|
throwError response
|
||||||
|
|
||||||
|
-- notFound ::
|
||||||
|
-- ( MonadIO m,
|
||||||
|
-- HasWebEnv env,
|
||||||
|
-- MonadReader env m,
|
||||||
|
-- MonadError ResponseReceived m
|
||||||
|
-- ) =>
|
||||||
|
-- m ResponseReceived
|
||||||
|
-- notFound = do
|
||||||
|
-- respond' <- getRespond <$> ask
|
||||||
|
-- response <-
|
||||||
|
-- liftIO $
|
||||||
|
-- respond' $
|
||||||
|
-- responseLBS
|
||||||
|
-- status200
|
||||||
|
-- [("Content-Type", "text/plain")]
|
||||||
|
-- "200 - Success"
|
||||||
|
-- response
|
||||||
|
|
||||||
runWebApp ::
|
runWebApp ::
|
||||||
( MonadIO m,
|
( MonadIO m,
|
||||||
MonadReader Env m
|
MonadReader Env m
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
# this way we can use prebuild binaries for hls
|
# this way we can use prebuild binaries for hls
|
||||||
#resolver: nightly-2022-11-12
|
#resolver: nightly-2022-11-12
|
||||||
#resolver: ghc-9.2.4
|
#resolver: ghc-9.2.4
|
||||||
resolver: lts-20.16
|
resolver: lts-20.26
|
||||||
#
|
#
|
||||||
# The location of a snapshot can be provided as a file or url. Stack assumes
|
# The location of a snapshot can be provided as a file or url. Stack assumes
|
||||||
# a snapshot provided as a file might change, whereas a url resource does not.
|
# a snapshot provided as a file might change, whereas a url resource does not.
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
packages: []
|
packages: []
|
||||||
snapshots:
|
snapshots:
|
||||||
- completed:
|
- completed:
|
||||||
sha256: dad15e2ec0c09280a5c2e07190fb18710fc54472f029f34f861f686540824d81
|
sha256: 5a59b2a405b3aba3c00188453be172b85893cab8ebc352b1ef58b0eae5d248a2
|
||||||
size: 649592
|
size: 650475
|
||||||
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/16.yaml
|
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/26.yaml
|
||||||
original: lts-20.16
|
original: lts-20.26
|
||||||
|
|
Loading…
Reference in a new issue