@@ -30,22 +30,23 @@ Let's start with some imports and language pragmas.
3030> {-# LANGUAGE TemplateHaskell #-}
3131> {-# LANGUAGE TypeFamilies #-}
3232> {-# LANGUAGE UndecidableInstances #-}
33- >
33+
3434> module Readme where
35- >
35+
3636> import Control. Monad (void)
3737> import Control.Monad.IO. Class (MonadIO )
3838> import Control.Monad. Fix (MonadFix )
3939> import Data. Aeson (ToJSON (.. ), FromJSON (.. ))
4040> import Data.Aeson.GADT. TH (deriveJSONGADT)
4141> import Data.Constraint. Extras (Has )
4242> import Data.Constraint.Extras. TH (deriveArgDict)
43+ > import Data. Kind (Type )
4344> import Data. Text as T (Text , null , pack)
4445> import Data. Time (UTCTime , Day )
4546> import GHC. Generics (Generic )
4647> import Reflex.Dom. Core
4748> import Reflex.Dom. GadtApi
48- >
49+
4950
5051```
5152
@@ -60,30 +61,30 @@ The code that follows would typically go in a common module, since it would be u
6061> , _dog_imageUri :: Maybe Text
6162> }
6263> deriving (Generic )
63- >
64+
6465> instance ToJSON Dog
6566> instance FromJSON Dog
66- >
67+
6768
6869```
6970
7071Here we have an API for retrieving and interacting with the ` Dog ` data:
7172
7273``` haskell
7374
74- > data DogApi :: * -> * where
75+ > data DogApi :: Type -> Type where
7576> DogApi_GetByDay :: Day -> DogApi [Dog ]
7677> DogApi_GetByName :: Text -> DogApi [Dog ]
7778> DogApi_GetLastSeen :: DogApi (Maybe Dog )
7879> DogApi_ReportSighting :: Text -> Bool -> Maybe Text -> DogApi (Either Text () )
7980> DogApi_GetSuspiciousSightings :: DogApi [Dog ]
80- >
81+
8182> newtype Token = Token { unToken :: Text }
8283> deriving (Generic )
83- >
84+
8485> instance ToJSON Token
8586> instance FromJSON Token
86- >
87+
8788
8889```
8990
@@ -94,12 +95,12 @@ We can take the `DogApi` and embed it in another GADT API. This outer API will h
9495> data CatApi a where
9596> CatApi_Identify :: Text -> CatApi (Either Text Token )
9697> CatApi_DogApi :: Token -> DogApi a -> CatApi a
97- >
98+
9899> deriveJSONGADT ''DogApi
99100> deriveJSONGADT ''CatApi
100101> deriveArgDict ''DogApi
101102> deriveArgDict ''CatApi
102- >
103+
103104
104105```
105106
@@ -108,7 +109,7 @@ On the frontend, we'll run a `RequesterT` widget that allows us to emit an event
108109``` haskell
109110
110111> type Catnet t m = (RequesterT t CatApi (Either Text ) m)
111- >
112+
112113
113114```
114115
@@ -178,7 +179,7 @@ We're using reflex `Workflow`s to switch between pages, but we could accomplish
178179> rsp <- dogSighting token
179180> leave <- delay 3 rsp
180181> pure (() , catnetW token <$ leave) -- Go back to catnet
181- >
182+
182183
183184```
184185
@@ -191,7 +192,7 @@ If you're building your frontend in a context where the user interface needs to
191192> => Event t (Request (Client (Catnet t m)) a)
192193> -> Catnet t m (Event t (Response (Client (Catnet t m)) a))
193194> requestingJs r = fmap (switch . current) $ prerender (pure never) $ requesting r
194- >
195+
195196
196197```
197198
@@ -214,7 +215,7 @@ The response from the server is an `Event` that can be used to update the user i
214215> Right (Right catToken) -> Just catToken
215216> _ -> Nothing
216217> pure token
217- >
218+
218219
219220```
220221
@@ -246,7 +247,7 @@ This function builds a UI with a few buttons. Depending on which button is click
246247> Right dogs -> el " ul" $ mapM_ (el " li" . showDog) dogs
247248> -- Return the "Report a sighting" click event
248249> pure addDog
249- >
250+
250251
251252```
252253
@@ -268,7 +269,7 @@ The `showDog` widget below does not have `Catnet` or `RequesterT` in its type si
268269> el " dd" $ case _dog_imageUri dog of
269270> Just img -> elAttr " img" (" src" =: img <> " alt" =: " dog mugshot" ) blank
270271> Nothing -> text " None"
271- >
272+
272273
273274```
274275
@@ -318,7 +319,7 @@ Once we've got those three values, we can apply them to the `DogApi_ReportSighti
318319> Right (Left err) -> Left err
319320> Left err -> Left err
320321> Right (Right result) -> Right result
321- >
322+
322323> main :: IO ()
323324> main = return ()
324325
0 commit comments