module Control.Tweak.Tweakable
(
Tweakable (..)
, getVar
, getAnyVar
, readCache
, readCacheSTM
) where
import Control.Concurrent.STM
import Control.Tweak.Var
import Control.Lens hiding (children, to, from)
data Tweakable a where
App :: Var b -> Tweakable (a -> b) -> Tweakable a -> Tweakable b
Pure :: Var a -> Tweakable a
getVar :: Tweakable a -> Var a
getVar = \case
Pure x -> x
App x _ _ -> x
getAnyVar :: Tweakable a -> AnyVar
getAnyVar = AnyVar . getVar
instance Cacheable (Tweakable a) where
children = lens to from where
to = \case
Pure c -> view children c
App c _ _ -> view children c
from x y = case x of
Pure c -> Pure (set children y c)
App c a b -> App (set children y c) a b
readCache :: Tweakable a -> IO a
readCache = atomically . readCacheSTM
readCacheSTM :: Tweakable a -> STM a
readCacheSTM = readTVar . view output . getVar