| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Haxl.DataSource.ConcurrentIO
Description
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 nNote 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 #
Minimal complete definition
Associated Types
data ConcurrentIOReq tag a Source #
Methods
performIO :: ConcurrentIOReq tag a -> IO a Source #