From 6b5dca2f1f857387ee615a1bbf4ea5db94052dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Thu, 10 Jul 2025 19:40:54 +0200 Subject: Add GlobalState type --- src/Main.hs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index 28b0731..b969ba9 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -28,8 +28,6 @@ import Erebos.Storable import Erebos.Storage import Erebos.Sync -import System.IO.Unsafe - import Text.Blaze.Html5 ((!)) import Text.Blaze.Html5 qualified as H import Text.Blaze.Html5.Attributes qualified as A @@ -41,15 +39,18 @@ import WebSocket main :: IO () main = error "unused" -{-# NOINLINE globalStorage #-} -globalStorage :: Storage -globalStorage = unsafePerformIO $ memoryStorage +data GlobalState = GlobalState + { globalStorage :: Storage + , globalHead :: Head LocalState + } -{-# NOINLINE globalHead #-} -globalHead :: Head LocalState -globalHead = unsafePerformIO $ do +initGlobalState :: IO GlobalState +initGlobalState = do + globalStorage <- memoryStorage identity <- createIdentity globalStorage Nothing Nothing - storeHead globalStorage $ LocalState { lsPrev = Nothing, lsIdentity = idExtData identity, lsShared = [], lsOther = [] } + globalHead <- storeHead globalStorage $ LocalState + { lsPrev = Nothing, lsIdentity = idExtData identity, lsShared = [], lsOther = [] } + return GlobalState {..} foreign export javascript setup :: IO () setup :: IO () @@ -74,6 +75,8 @@ setup = do "Peers" H.ul ! A.id "peer_list" $ return () + GlobalState {..} <- initGlobalState + nameElem <- js_document_getElementById (toJSString "name_text") _ <- watchHead globalHead $ \ls -> do js_set_textContent nameElem $ toJSString $ maybe "(Anonymous)" T.unpack $ idName $ finalOwner $ headLocalIdentity ls -- cgit v1.2.3