1+ -----------------------------------------------------------------------------
12{-# LANGUAGE CPP #-}
2- {-# LANGUAGE DeriveGeneric #-}
3- {-# LANGUAGE ExtendedDefaultRules #-}
4- {-# LANGUAGE FlexibleInstances #-}
5- {-# LANGUAGE MultiParamTypeClasses #-}
63{-# LANGUAGE OverloadedStrings #-}
74{-# LANGUAGE RecordWildCards #-}
8- {-# LANGUAGE ScopedTypeVariables #-}
9- {-# LANGUAGE TypeFamilies #-}
10-
5+ -----------------------------------------------------------------------------
116module Main where
12-
7+ -----------------------------------------------------------------------------
138import Control.Monad.State
149import Data.Aeson
1510import Data.Bool
1611import GHC.Generics
17-
12+ -----------------------------------------------------------------------------
1813import Miso
1914import Miso.String (MisoString )
2015import qualified Miso.String as S
21-
2216import qualified Miso.Style as CSS
23-
17+ -----------------------------------------------------------------------------
2418#if WASM
2519foreign export javascript " hs_start" main :: IO ()
2620#endif
27-
21+ -----------------------------------------------------------------------------
2822main :: IO ()
29- main = run (startComponent app)
30-
31- app :: Component Model Action
23+ main = run (startApp app)
24+ -----------------------------------------------------------------------------
25+ app :: App Model Action
3226app = (component emptyModel updateModel appView)
3327 { events = defaultEvents <> keyboardEvents
3428 , subs =
@@ -37,38 +31,37 @@ app = (component emptyModel updateModel appView)
3731 } where
3832 url = URL " wss://echo.websocket.org"
3933 protocols = Protocols []
40-
41-
34+ -----------------------------------------------------------------------------
4235emptyModel :: Model
4336emptyModel = Model (Message " " ) mempty
44-
45- updateModel :: Action -> Effect Model Action
37+ -----------------------------------------------------------------------------
38+ updateModel :: Action -> Transition Model Action
4639updateModel (HandleWebSocket (WebSocketMessage (Message m))) =
4740 modify $ \ model -> model { received = m }
4841updateModel (SendMessage msg) =
4942 io_ (send msg)
5043updateModel (UpdateMessage m) = do
5144 modify $ \ model -> model { msg = Message m }
5245updateModel _ = pure ()
53-
46+ -----------------------------------------------------------------------------
5447instance ToJSON Message
5548instance FromJSON Message
56-
49+ -----------------------------------------------------------------------------
5750newtype Message = Message MisoString
5851 deriving (Eq , Show , Generic )
59-
52+ -----------------------------------------------------------------------------
6053data Action
6154 = HandleWebSocket (WebSocket Message )
6255 | SendMessage Message
6356 | UpdateMessage MisoString
6457 | Id
65-
58+ -----------------------------------------------------------------------------
6659data Model = Model
6760 { msg :: Message
6861 , received :: MisoString
6962 } deriving (Show , Eq )
70-
71- appView :: Model -> View Action
63+ -----------------------------------------------------------------------------
64+ appView :: Model -> View Model Action
7265appView Model {.. } =
7366 div_
7467 [ CSS. style_ [ CSS. textAlign " center" ] ]
@@ -91,6 +84,7 @@ appView Model{..} =
9184 [text (S. pack " Send to echo server" )]
9285 , div_ [] [p_ [] [text received | not . S. null $ received]]
9386 ]
94-
87+ -----------------------------------------------------------------------------
9588onEnter :: Action -> Attribute Action
9689onEnter action = onKeyDown $ bool Id action . (== KeyCode 13 )
90+ -----------------------------------------------------------------------------
0 commit comments