Pure WS monad

This commit is contained in:
qvalentin 2023-02-19 11:41:32 +01:00
parent 5d3dced6f7
commit 865b69e799
12 changed files with 346 additions and 221 deletions

View file

@ -4,10 +4,12 @@ module Types.RoomsState
HasRoomsState (..),
roomStateDiffers,
updateRoomState,
getRoomState,
)
where
import ClassyPrelude
import State.RoomDataState (MonadRoomDataStateRead (getRoomDataState))
import Types.RoomData (RoomsData)
type RoomsState = MVar RoomsData
@ -30,16 +32,23 @@ updateRoomState newData = do
_ <- swapMVar state newData
return ()
roomStateDiffers ::
getRoomState ::
( HasRoomsState env,
MonadIO m,
MonadReader env m
) =>
m RoomsData
getRoomState = do
state <- getRoomsState <$> ask
readMVar state
roomStateDiffers ::
( MonadRoomDataStateRead m
) =>
RoomsData ->
m Bool
roomStateDiffers newData = do
currentData <- ask >>= readMVar . getRoomsState
return $ not $ eqIgnoreOrdering newData currentData
not . eqIgnoreOrdering newData <$> getRoomDataState
eqIgnoreOrdering :: (Eq a) => [a] -> [a] -> Bool
eqIgnoreOrdering a b = length a == length b && all (`elem` b) a