Skip to content

Commit acf3bb3

Browse files
committed
newtype Page
1 parent 42a335d commit acf3bb3

File tree

6 files changed

+40
-21
lines changed

6 files changed

+40
-21
lines changed

.hlint.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ignore: {name: "Use <$>"}
2+
- ignore: {name: "Use newtype instead of data"}

example/Example/Contacts.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ data Filter
3939
deriving (Show, Read, Eq)
4040

4141

42-
page :: forall es. (Hyperbole :> es, Users :> es, Debug :> es) => Eff es ()
42+
page :: forall es. (Hyperbole :> es, Users :> es, Debug :> es) => Page es ()
4343
page = do
4444
hyper contacts
4545
hyper contact

example/Example/Layout.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Web.UI
77

88

99
-- need to be able to set bg color of page, sure
10-
page :: (Hyperbole :> es) => Eff es ()
10+
page :: (Hyperbole :> es) => Page es ()
1111
page = load $ do
1212
pure $ do
1313
layout (bg GrayLight . big flexRow) $ do

example/Example/Transitions.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ data Action
1717

1818

1919
-- need to be able to set bg color of page, sure
20-
page :: (Hyperbole :> es) => Eff es ()
20+
page :: (Hyperbole :> es) => Page es ()
2121
page = do
2222
hyper content
2323

example/Main.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ app :: UserStore -> Application
5757
app users = waiApplication document (runUsersIO users . runHyperbole . runDebugIO . router)
5858
where
5959
router :: (Hyperbole :> es, Users :> es, Debug :> es) => AppRoute -> Eff es ()
60-
router (Hello h) = hello h
61-
router Echo = do
60+
router (Hello h) = page $ hello h
61+
router Echo = page $ load $ do
6262
f <- formData
63-
load $ pure $ col id $ do
63+
pure $ col id $ do
6464
el id "ECHO:"
6565
text $ cs $ show f
66-
router Contacts = Contacts.page
67-
router Layout = Layout.page
68-
router Transitions = Transitions.page
69-
router Main = load $ pure $ do
66+
router Contacts = page Contacts.page
67+
router Layout = page Layout.page
68+
router Transitions = page Transitions.page
69+
router Main = page $ load $ pure $ do
7070
col (gap 10 . pad 10) $ do
7171
el (bold . fontSize 32) "Examples"
7272
link (routeUrl (Hello (Greet "World"))) id "Hello World"

src/Web/Hyperbole/Effect.hs

+28-11
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ import Web.Hyperbole.HyperView
2121
import Web.UI
2222

2323

24-
-- you can't automatically derive FromHttpApiData. I don't like it!
25-
2624
data Hyperbole :: Effect where
27-
RespondView :: View () () -> Hyperbole m ()
2825
GetEvent :: (HyperView id action) => Hyperbole m (Maybe (Event id action))
26+
RespondView :: View () () -> Hyperbole m ()
2927
GetForm :: Hyperbole m Form
3028
HyperError :: HyperError -> Hyperbole m a
3129

@@ -46,16 +44,16 @@ runHyperbole = interpret $ \_ -> \case
4644
send $ ResHeader "Content-Type" "text/html"
4745
send $ ResBody ContentHtml bd
4846
Wai.continue
47+
GetForm -> Wai.formData
48+
HyperError NotFound -> send $ Interrupt Wai.NotFound
49+
HyperError (ParseError e) -> send $ Interrupt $ Wai.ParseError e
4950
GetEvent -> do
5051
q <- fmap queryString <$> send $ Wai.Request
5152
pure $ do
5253
Event ti ta <- lookupEvent q
5354
vid <- parseParam ti
5455
act <- parseParam ta
5556
pure $ Event vid act
56-
GetForm -> Wai.formData
57-
HyperError NotFound -> send $ Interrupt Wai.NotFound
58-
HyperError (ParseError e) -> send $ Interrupt $ Wai.ParseError e
5957
where
6058
lookupParam :: ByteString -> Query -> Maybe Text
6159
lookupParam p q =
@@ -90,18 +88,37 @@ data HyperError
9088
| ParseError Text
9189

9290

93-
load :: (Hyperbole :> es) => Eff es (View () ()) -> Eff es ()
94-
load pg = do
95-
vw <- pg
91+
newtype Page es a = Page (Eff es a)
92+
deriving newtype (Applicative, Monad, Functor)
93+
94+
95+
-- | Load the entire page when no HyperViews match
96+
load
97+
:: (Hyperbole :> es)
98+
=> Eff es (View () ())
99+
-> Page es ()
100+
load run = Page $ do
101+
vw <- run
96102
send $ RespondView vw
97103

98104

99-
hyper :: forall id action es. (Hyperbole :> es, HyperView id action) => (id -> action -> Eff es (View id ())) -> Eff es ()
100-
hyper run = do
105+
-- | Handle a HyperView. If the event matches our handler, respond with the fragment
106+
hyper
107+
:: (Hyperbole :> es, HyperView id action)
108+
=> (id -> action -> Eff es (View id ()))
109+
-> Page es ()
110+
hyper run = Page $ do
101111
-- Get an event matching our type. If it doesn't match, skip to the next handler
102112
mev <- send GetEvent
103113
case mev of
104114
Just (Event vid act) -> do
105115
vw <- run vid act
106116
send $ RespondView $ viewId vid vw
107117
_ -> pure ()
118+
119+
120+
page
121+
:: (Hyperbole :> es)
122+
=> Page es ()
123+
-> Eff es ()
124+
page (Page eff) = eff

0 commit comments

Comments
 (0)