2018-05-11 12:31:53 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
module App (
|
|
|
|
T
|
|
|
|
, Context(..)
|
|
|
|
, debug
|
2019-11-12 23:25:00 +01:00
|
|
|
, exec
|
2019-11-18 17:06:02 +01:00
|
|
|
, get
|
2019-11-12 23:25:00 +01:00
|
|
|
, session
|
2018-05-11 12:31:53 +02:00
|
|
|
, update
|
|
|
|
) where
|
|
|
|
|
2019-11-12 23:25:00 +01:00
|
|
|
import Control.Concurrent (MVar, modifyMVar, readMVar)
|
2018-05-11 12:31:53 +02:00
|
|
|
import Control.Monad.Reader (ReaderT(..), ask, asks, lift)
|
2019-10-13 22:00:35 +02:00
|
|
|
import Data.Map ((!))
|
2018-05-11 12:31:53 +02:00
|
|
|
import qualified Server (T(..))
|
2019-11-05 18:14:24 +01:00
|
|
|
import qualified Session (ID, T(..))
|
2018-05-11 12:31:53 +02:00
|
|
|
|
|
|
|
data Context = Context {
|
|
|
|
mServer :: MVar Server.T
|
2019-11-05 18:14:24 +01:00
|
|
|
, sessionID :: Session.ID
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type T a = ReaderT Context IO a
|
|
|
|
|
2019-11-12 23:25:00 +01:00
|
|
|
get :: (Server.T -> a) -> T a
|
|
|
|
get projector =
|
2019-11-18 17:06:02 +01:00
|
|
|
lift . fmap projector . readMVar =<< asks mServer
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2019-11-12 23:25:00 +01:00
|
|
|
session :: T Session.T
|
|
|
|
session = do
|
|
|
|
Context {sessionID} <- ask
|
|
|
|
get $ (! sessionID) . Server.sessions
|
2018-05-11 12:31:53 +02:00
|
|
|
|
|
|
|
debug :: String -> T ()
|
|
|
|
debug message =
|
2019-11-05 18:14:24 +01:00
|
|
|
show <$> asks sessionID
|
2018-05-11 12:31:53 +02:00
|
|
|
>>= lift . putStrLn . (++ ' ':message)
|
|
|
|
|
2019-11-12 23:25:00 +01:00
|
|
|
exec :: (Server.T -> IO (Server.T, a)) -> T a
|
|
|
|
exec f = do
|
2018-05-11 12:31:53 +02:00
|
|
|
Context {mServer} <- ask
|
2019-11-12 23:25:00 +01:00
|
|
|
lift $ modifyMVar mServer f
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2019-11-12 23:25:00 +01:00
|
|
|
update :: (Server.T -> Server.T) -> T ()
|
|
|
|
update f = exec $ (\x -> return (x, ())) . f
|