summaryrefslogtreecommitdiff
path: root/fig-web/src/Fig/Web/State.hs
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2024-11-07 22:37:32 -0500
committerLLLL Colonq <llll@colonq>2024-11-07 22:37:32 -0500
commit624f7ba8b2fcda6675951dd8d41dcc99017484cf (patch)
treeff1bcc3ee77c73e73c3e246bc8e18ce8f3aca004 /fig-web/src/Fig/Web/State.hs
parentbb3f54c297f480db32303e9ee78fb72c5418b77a (diff)
Rename fig-frontend to fig-web
(It was the backend anyway :3)
Diffstat (limited to 'fig-web/src/Fig/Web/State.hs')
-rw-r--r--fig-web/src/Fig/Web/State.hs41
1 files changed, 41 insertions, 0 deletions
diff --git a/fig-web/src/Fig/Web/State.hs b/fig-web/src/Fig/Web/State.hs
new file mode 100644
index 0000000..11e0ece
--- /dev/null
+++ b/fig-web/src/Fig/Web/State.hs
@@ -0,0 +1,41 @@
+{-# Language TemplateHaskell #-}
+
+module Fig.Web.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"