1
1
mirror of https://github.com/srid/ema.git synced 2024-11-29 09:25:14 +03:00

Get rid of the Ema type

This commit is contained in:
Sridhar Ratnakumar 2021-04-20 17:12:20 -04:00
parent 8bd0b0838d
commit 1adedf6f65
3 changed files with 20 additions and 22 deletions

View File

@ -1,36 +1,31 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeApplications #-}
module Ema.App where
import Data.LVar
( LVar,
module Ema.App
( runEma,
runEmaPure,
)
where
import Data.LVar (LVar)
import qualified Data.LVar as LVar
import Ema.Route (IsRoute (..))
import qualified Ema.Server as Server
import GHC.IO.Handle (BufferMode (LineBuffering), hSetBuffering)
data Ema s r = Ema
{ -- | The (ever-changing) state of the app
emaModel :: LVar s,
-- | HTML view function over app state and app route
emaRender :: s -> r -> LByteString
}
-- | Pure version of @runEma@ (i.e with no model).
--
-- Due to purity, there is no impure state, and thus no time-varying model. The
-- render function consequently takes only the route as argument.
runEmaPure ::
forall r.
(IsRoute r, Show r) =>
forall route.
(IsRoute route, Show route) =>
-- | How to render a route
(r -> LByteString) ->
(route -> LByteString) ->
IO ()
runEmaPure render = do
emptyModel <- LVar.empty
runEma $ Ema emptyModel (const render)
runEma emptyModel (const render)
-- | Run Ema live server
--
@ -39,11 +34,14 @@ runEmaPure render = do
runEma ::
forall model route.
(Show route, IsRoute route) =>
Ema model route ->
-- | Your site model
LVar model ->
-- | Your site render function
(model -> route -> LByteString) ->
IO ()
runEma ema = do
runEma model render = do
-- TODO: Use a logging library, in place of managing buffering and using putStrLn
hSetBuffering stdout LineBuffering
hSetBuffering stderr LineBuffering
putStrLn "Launching Ema at http://localhost:8000"
Server.runServerWithWebSocketHotReload (emaModel ema) (emaRender ema)
Server.runServerWithWebSocketHotReload model render

View File

@ -18,7 +18,7 @@ import Data.Time
formatTime,
getCurrentTime,
)
import Ema.App (Ema (Ema), runEma)
import Ema.App (runEma)
import qualified Ema.Layout as Layout
import Ema.Route (IsRoute (..))
import Text.Blaze.Html5 ((!))
@ -50,7 +50,7 @@ main = do
model <- LVar.new =<< getCurrentTime
race_
(changeTime model)
(runEma $ Ema model render)
(runEma model render)
where
render (now :: UTCTime) r =
Layout.tailwindSite (H.title "Clock") $

View File

@ -16,7 +16,7 @@ import qualified Data.Org as Org
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Time (Day, defaultTimeLocale, parseTimeM)
import Ema.App (Ema (..), runEma)
import Ema.App (runEma)
import qualified Ema.Layout as Layout
import Ema.Route
import qualified Shower
@ -105,7 +105,7 @@ mainWith args = do
_ -> canonicalizePath "src/Ema/Example/Diary"
model <- LVar.new =<< diaryFrom folder
race_
(runEma $ Ema model render)
(runEma model render)
(watchAndUpdateDiary folder model)
where
render (diary :: Diary) (r :: Route) =