module Haxl.Core.Env
( Env(..)
, emptyEnv
, initEnv
, initEnvWithData
, caches
) where
import Haxl.Core.DataCache as DataCache
import Haxl.Core.StateStore
import Haxl.Core.Types
import Data.IORef
data Env u = Env
{ cacheRef :: IORef DataCache
, memoRef :: IORef DataCache
, flags :: Flags
, userEnv :: u
, statsRef :: IORef Stats
, states :: StateStore
}
type Caches = (IORef DataCache, IORef DataCache)
caches :: Env u -> Caches
caches env = (cacheRef env, memoRef env)
initEnvWithData :: StateStore -> u -> Caches -> IO (Env u)
initEnvWithData states e (cref, mref) = do
sref <- newIORef emptyStats
return Env
{ cacheRef = cref
, memoRef = mref
, flags = defaultFlags
, userEnv = e
, states = states
, statsRef = sref
}
initEnv :: StateStore -> u -> IO (Env u)
initEnv states e = do
cref <- newIORef DataCache.empty
mref <- newIORef DataCache.empty
initEnvWithData states e (cref,mref)
emptyEnv :: u -> IO (Env u)
emptyEnv = initEnv stateEmpty