Copyright | (c) Anton Gushcha, 2016 |
---|---|
License | BSD3 |
Maintainer | ncrashed@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
The core module contains API for embedding concurrent IO actions (async and sync) into main game loop for Gore&Ash.
The module does not depend on any other core modules, so AsyncT
could be placed at any place in monad stack.
The module is NOT pure within first phase (see ModuleStack
docs), therefore currently only IO
end monad can handler the module.
Example of embedding:
-- Language extensions: DataKinds DeriveGeneric GeneralizedNewtypeDeriving MultiParamTypeClasses TypeFamilies import Control.DeepSeq import Control.Monad.Catch import Control.Monad.Fix import Control.Monad.IO.Class import Data.Typeable import GHC.Generics (Generic) import Game.GoreAndAsh.Core import Game.GoreAndAsh.Async -- | Application monad is monad stack build from given list of modules over base monad (IO or Identity) type AppStack = ModuleStack [AsyncT ... other modules ... ] IO newtype AppState = AppState (ModuleState AppStack) deriving (Generic) instance NFData AppState -- | Wrapper around type family newtype AppMonad a = AppMonad (AppStack a) deriving (Functor, Applicative, Monad, MonadFix, MonadIO, MonadThrow, MonadCatch, MonadAsync ... other modules monads ... ) instance GameModule AppMonad AppState where type ModuleState AppMonad = AppState runModule (AppMonad m) (AppState s) = do (a, s') <- runModule m s return (a, AppState s') newModuleState = AppState $ newModuleState withModule _ = withModule (Proxy :: Proxy AppStack) cleanupModule (AppState s) = cleanupModule s -- | Arrow that is build over the monad stack type AppWire a b = GameWire AppMonad a b
- data AsyncState s
- data AsyncT s m a
- data AsyncId
- class (MonadIO m, MonadThrow m) => MonadAsync m where
- asyncActionM :: Typeable a => IO a -> m AsyncId
- asyncActionBoundM :: Typeable a => IO a -> m AsyncId
- asyncPollM :: Typeable a => AsyncId -> m (Maybe (Either SomeException a))
- asyncCancelM :: AsyncId -> m ()
- asyncSyncActionM :: Typeable a => IO a -> m SyncId
- asyncSyncPollM :: Typeable a => SyncId -> m (Maybe (Either SomeException a))
- asyncSyncCanceM :: SyncId -> m ()
- asyncAction :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event a)
- asyncActionC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event a)
- asyncActionEx :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event (Either SomeException a))
- asyncActionExC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event (Either SomeException a))
- asyncActionFactory :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq a))
- asyncActionFactoryEx :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq (Either SomeException a)))
- asyncActionBound :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event a)
- asyncActionBoundC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event a)
- asyncActionBoundEx :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event (Either SomeException a))
- asyncActionBoundExC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event (Either SomeException a))
- asyncActionBoundFactory :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq a))
- asyncActionBoundFactoryEx :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq (Either SomeException a)))
- asyncSyncAction :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event a)
- asyncSyncActionEx :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event (Either SomeException a))
- asyncSyncActionC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event a)
- asyncSyncActionExC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event (Either SomeException a))
- asyncSyncActionFactory :: forall m a. (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq a))
- asyncSyncActionFactoryEx :: forall m a. (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq (Either SomeException a)))
Low-level
data AsyncState s Source
Internal state of asynchronious core module
s
- - state of next module, they are chained until bottom, that is usually an empty data type.
Generic (AsyncState s) Source | |
NFData s => NFData (AsyncState s) Source | |
Monad m => MonadState (AsyncState s) (AsyncT s m) | |
type Rep (AsyncState s) Source |
Monad transformer of async core module.
s
- - State of next core module in modules chain;
m
- - Next monad in modules monad stack;
a
- - Type of result value;
How to embed module:
type AppStack = ModuleStack [AsyncT, ... other modules ... ] IO newtype AppMonad a = AppMonad (AppStack a) deriving (Functor, Applicative, Monad, MonadFix, MonadIO, MonadThrow, MonadCatch, MonadAsync)
The module is NOT pure within first phase (see ModuleStack
docs), therefore currently only IO
end monad can handler the module.
MonadTrans (AsyncT s) Source | |
Monad m => MonadState (AsyncState s) (AsyncT s m) Source | |
Monad m => Monad (AsyncT s m) Source | |
Functor m => Functor (AsyncT s m) Source | |
MonadFix m => MonadFix (AsyncT s m) Source | |
Monad m => Applicative (AsyncT s m) Source | |
MonadThrow m => MonadThrow (AsyncT s m) Source | |
MonadCatch m => MonadCatch (AsyncT s m) Source | |
MonadMask m => MonadMask (AsyncT s m) Source | |
MonadIO m => MonadIO (AsyncT s m) Source | |
(MonadIO m, MonadThrow m) => MonadAsync (AsyncT s m) Source | |
type ModuleState (AsyncT s m) = AsyncState s Source |
Id of async value, it is used to poll info about the value
class (MonadIO m, MonadThrow m) => MonadAsync m where Source
Low level monadic API for module.
Note: does not require m
to be IO
monad.
asyncActionM :: Typeable a => IO a -> m AsyncId Source
Start execution of IO
action concurrently and return its id
asyncActionBoundM :: Typeable a => IO a -> m AsyncId Source
Start execution of IO
action concurrently and return its id
Note: forks thread within same OS thread.
asyncPollM :: Typeable a => AsyncId -> m (Maybe (Either SomeException a)) Source
Check state of concurrent value
Could throw MonadAsyncExcepion
, AsyncWrongType
and AsyncNotFound
constructors.
asyncCancelM :: AsyncId -> m () Source
Stops given async execution
asyncSyncActionM :: Typeable a => IO a -> m SyncId Source
Schedule action to be executed at the end of frame.
Use asyncSyncPollM
to get result at next frame.
Note: order of IO actions is preserved.
asyncSyncPollM :: Typeable a => SyncId -> m (Maybe (Either SomeException a)) Source
Fires when given synchronious action is completed (at next frame after scheduling)
Could throw MonadAsyncExcepion
, SyncWrongType
constructor.
asyncSyncCanceM :: SyncId -> m () Source
Unshedule given action from execution
Actually you can unshedule it until end of frame when corresponding asyncSyncActionM
was called.
(MonadIO (mt m), MonadThrow (mt m), MonadAsync m, MonadTrans mt) => MonadAsync (mt m) Source | |
(MonadIO m, MonadThrow m) => MonadAsync (AsyncT s m) Source |
Arrow API
Not bounded async
asyncAction :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event a) Source
Execute given IO
action concurrently. Event fires once when the action
is finished. Exceptions are rethrown into main thread.
asyncActionC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event a) Source
Execute given IO
action concurrently. Event fires once when the action
is finished. Exceptions are rethrown into main thread.
The concurrent action can be canceled by input event.
asyncActionEx :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event (Either SomeException a)) Source
Execute given IO
action concurrently.
Event fires once when the action is finished. Exceptions in the concurrent action are returned in event payload.
asyncActionExC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event (Either SomeException a)) Source
Execute given IO
action concurrently.
Event fires once when the action is finished. Exceptions in the concurrent action are returned in event payload.
The concurrent action can be canceled by input event.
asyncActionFactory :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq a)) Source
Wire that executes incoming IO
actions concurrently and then produces
events with results once for each action.
Exceptions are rethrown into main thread.
asyncActionFactoryEx :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq (Either SomeException a))) Source
Wire that executes incoming IO
actions concurrently and then produces
events with results once for each action.
Exceptions are returned in event payload.
Bounded async
asyncActionBound :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event a) Source
Execute given IO
action concurrently. Event fires once when the action
is finished. Exceptions are rethrown into main thread.
Note: forks thread within same OS thread.
asyncActionBoundC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event a) Source
Execute given IO
action concurrently. Event fires once when the action
is finished. Exceptions are rethrown into main thread.
The concurrent action can be canceled by input event.
Note: forks thread within same OS thread.
asyncActionBoundEx :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event (Either SomeException a)) Source
Execute given IO
action concurrently.
Event fires once when the action is finished. Exceptions in the concurrent action are returned in event payload.
Note: forks thread within same OS thread.
asyncActionBoundExC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event (Either SomeException a)) Source
Execute given IO
action concurrently.
Event fires once when the action is finished. Exceptions in the concurrent action are returned in event payload.
The concurrent action can be canceled by input event.
Note: forks thread within same OS thread.`
asyncActionBoundFactory :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq a)) Source
Wire that executes incoming IO
actions concurrently and then produces
events with results once for each action.
Exceptions are rethrown into main thread.
Note: forks thread within same OS thread.
asyncActionBoundFactoryEx :: (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq (Either SomeException a))) Source
Wire that executes incoming IO
actions concurrently and then produces
events with results once for each action.
Exceptions are returned in event payload.
Note: forks thread within same OS thread.
Sync actions
asyncSyncAction :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event a) Source
Execute given IO
action at end of current frame. Event fires once at next frame.
Exceptions are rethrown into main thread.
asyncSyncActionEx :: (MonadAsync m, Typeable a) => IO a -> GameWire m b (Event (Either SomeException a)) Source
Execute given IO
action at end of current frame. Event fires once at next frame.
Exceptions are returned in event payload.
asyncSyncActionC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event a) Source
Execute given IO
action at end of current frame. Event fires once at next frame.
Exceptions are rethrown into main thread.
Action can be canceled with input event, although you have only the frame to do this.
asyncSyncActionExC :: (MonadAsync m, Typeable a) => IO a -> GameWire m (Event b) (Event (Either SomeException a)) Source
Execute given IO
action at end of current frame. Event fires once at next frame.
Exceptions are rethrown into main thread.
Action can be canceled with input event, although you have only the frame to do this.
asyncSyncActionFactory :: forall m a. (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq a)) Source
Wire that executes incoming IO
actions at end of current frame and then produces
events with results once for each action.
Exceptions are rethrown into main thread.
asyncSyncActionFactoryEx :: forall m a. (MonadAsync m, Typeable a) => GameWire m (Event (Seq (IO a))) (Event (Seq (Either SomeException a))) Source
Wire that executes incoming IO
actions at end of current frame and then produces
events with results once for each action.
Exceptions are rethrown into main thread.