module Salak.Internal.Writable(
Writable
, toWritable
, getWritable
, setWritable
) where
import Control.Concurrent.MVar
import Control.Monad
data Writable a = Writable
{ valRef :: IO a
, setRef :: MVar (Maybe a)
}
{-# INLINE toWritable #-}
toWritable :: IO a -> IO (Writable a)
toWritable valRef = do
setRef <- newMVar Nothing
return Writable{..}
{-# INLINE getWritable #-}
getWritable :: Writable a -> IO a
getWritable Writable{..} = do
v <- readMVar setRef
case v of
Just a -> return a
_ -> valRef
{-# INLINE setWritable #-}
setWritable :: Maybe a -> Writable a -> IO ()
setWritable s Writable{..} = void $ swapMVar setRef s