Safe Haskell | None |
---|---|
Language | Haskell2010 |
A generic Haxl datasource for performing arbitrary IO concurrently.
Every IO operation will be performed in a separate thread.
You can use this with any kind of IO, but each different operation
requires an instance of the ConcurrentIO
class.
For example, to make a concurrent sleep operation:
sleep :: Int -> GenHaxl u Int sleep n = dataFetch (Sleep n) data Sleep instance ConcurrentIO Sleep where data ConcurrentIOReq Sleep a where Sleep :: Int -> ConcurrentIOReq Sleep Int performIO (Sleep n) = threadDelay (n*1000) >> return n deriving instance Eq (ConcurrentIOReq Sleep a) deriving instance Show (ConcurrentIOReq Sleep a) instance ShowP (ConcurrentIOReq Sleep) where showp = show instance Hashable (ConcurrentIOReq Sleep a) where hashWithSalt s (Sleep n) = hashWithSalt s n
Note that you can have any number of constructors in your ConcurrentIOReq GADT, so most of the boilerplate only needs to be written once.
Documentation
mkConcurrentIOState :: IO (State (ConcurrentIOReq ())) Source #
class ConcurrentIO tag where Source #
data ConcurrentIOReq tag a Source #
performIO :: ConcurrentIOReq tag a -> IO a Source #