happstack-fay-0.2.0: Support for using Fay with Happstack

Safe HaskellNone



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

The ResponseType argument specifies what type each command should return. Using GADTs would be cleaner, but Fay does not support GADTs yet.

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

commandR uses 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.

See also AJAX from the happstack-client-fay package.



handleCommand :: (Data cmd, Show cmd, Happstack m) => (cmd -> m Response) -> m ResponseSource

decode the cmd and call the response handler.

See also: fayResponse



:: (Happstack m, Show a) 
=> ResponseType a

used to help the type-checker enforce type safety

-> m a

handler that calculates a response

-> m Response 

convert the return value to a fay response.