From 094d3e0e1370f2f8b3619ba6cea8b33ac83dceed Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Fri, 12 Jan 2024 14:54:22 -0500 Subject: Update frontend --- fig-frontend/src/Fig/Frontend/State.hs | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 fig-frontend/src/Fig/Frontend/State.hs (limited to 'fig-frontend/src/Fig/Frontend/State.hs') diff --git a/fig-frontend/src/Fig/Frontend/State.hs b/fig-frontend/src/Fig/Frontend/State.hs new file mode 100644 index 0000000..6053105 --- /dev/null +++ b/fig-frontend/src/Fig/Frontend/State.hs @@ -0,0 +1,41 @@ +{-# Language TemplateHaskell #-} + +module Fig.Frontend.State where + +import Control.Lens.TH (makeLensesFor) +import Control.Lens ((<>=)) +import Control.Monad.State (runStateT) + +import Fig.Prelude + +import qualified Data.IORef as IORef + +newtype State = State + { buffer :: Text + } +makeLensesFor [("buffer", "buffer")] ''State + +defaultState :: State +defaultState = State + { buffer = "" + } + +type StateRef = IORef.IORef State + +stateRef :: IO StateRef +stateRef = IORef.newIORef defaultState + +withState :: + MonadIO m' => + StateRef -> + (forall m. (MonadIO m, MonadState State m) => m a) -> + m' a +withState ref f = do + s <- liftIO $ IORef.readIORef ref + (res, s') <- liftIO $ runStateT f s + liftIO $ IORef.writeIORef ref s' + pure res + +sayHi :: StateRef -> IO () +sayHi ref = withState ref do + buffer <>= "hi" -- cgit v1.2.3