module Eve.Internal.AppState
( AppState(..)
, App
, Action
, ActionM
, AppM
, exit
, isExiting
, asyncQueue
) where
import Eve.Internal.Actions
import Eve.Internal.States
import Control.Lens
import Data.Default
import Data.Typeable
import Control.Concurrent.Chan
data AppState = AppState
{ _baseStates :: States
}
makeLenses ''AppState
instance Default AppState where
def = AppState mempty
instance HasStates AppState where
states = baseStates
instance HasEvents AppState where
newtype AsyncQueue base m = AsyncQueue
{ _asyncQueue' :: Maybe (Chan (AppT base m ()))
} deriving Typeable
makeLenses ''AsyncQueue
instance Default (AsyncQueue base m) where
def = AsyncQueue Nothing
asyncQueue :: (HasStates s, Typeable m, Typeable base) => Lens' s (Maybe (Chan (AppT base m ())))
asyncQueue = stateLens.asyncQueue'
newtype Exiting =
Exiting Bool
instance Default Exiting where
def = Exiting False
exit :: (Monad m, HasStates s) => ActionT s zoomed m ()
exit = runApp $ stateLens .= Exiting True
isExiting :: (Monad m, HasStates s) => ActionT s zoomed m Bool
isExiting = runApp $ do
Exiting b <- use stateLens
return b
type App a = AppT AppState IO a
type Action state a = ActionT AppState state IO a
type AppM m a = AppT AppState m a
type ActionM s m a = ActionT AppState s m a