The server-side half of a typed AJAX communication channel.
To use this library, you could start by defining a type in a file that can be shared between the Haskell Server and Fay client. For example:
data Command = SendGuess Guess (ResponseType (Maybe Row)) | FetchBoard (ResponseType (Maybe Board)) deriving (Read, Show, Data, Typeable) instance Foreign Command
ResponseType argument specifies what type each command should
return. Using GADTs would be cleaner, but Fay does not support GADTs
In the server, you would then have a route that handles ajax requests such as:
, dir ajax $ handleCommand (commandR acid)
commandR would then call functions to handle the various requests:
-- | handle an AJAX request commandR :: AcidState Games -> Command -> ServerPart Response commandR acid cmd = case cmd of (SendGuess guess rt) -> fayResponse rt $ sendGuessC acid guess (FetchBoard rt) -> fayResponse rt $ fetchBoardC acid
fayResponse to convert the value returned by each
command handler to a valid Fay value. Note that it takes
ResponseType argument and passes it to
fayResponse. This is how we
ensure that each commend handler is returning the right type.
AJAX from the
cmd and call the response handler.