MFlow  Create and maintain dynamic Web applications as easy and fast as console applications
Thou shall not write request handlers
 This release: 11/06/2014. (@agocorona) <>< . issues & bugs .  Mflow Source code,     source code of this site

A persistent and stateful flow: A counter

A persistent counter. It uses the same mechanism than the shopping example, but it is more simple. Since the state is smaller, not only the event (the option chosen), but the state is stored. It is not necessary since it can be reconstructed from the events like in the case of the shopping cart.

What is different in this example from the counter of the pageFlow example is that the former is persistent: You can navigate elsewhere in the application and, when you return here, the counter will have the same value as long as the user session last.

Note that the user session timeout is set to one hour.


Running example

(in the light red box):


++ --

Source code:

{-# OPTIONS  -XCPP #-}
module MCounter (
) where
import Data.Monoid
import Data.String
-- #define ALONE -- to execute it alone, uncomment this
#ifdef ALONE
import MFlow.Wai.Blaze.Html.All
main= runNavigation "" $ transientNav grid
import MFlow.Wai.Blaze.Html.All hiding(retry, page)
import Menu

mcounter  = do 
 (op,n) <- step . page $ do
              n <- getSessionData  `onNothing` return (0 :: Int) -- get Int data from the session
              op <- h2 << show n    
                     ++> wlink "i" << b << " ++ "
                     <|> wlink "d" << b << " -- "

              return(op,n)  -- unlike in the case of the shopping example where the shopcart is not logged
                            -- here the state is smaller (the Int counter) anc can be logged      
 case op  of
          "i" -> setSessionData  (n + 1)                     
          "d" -> setSessionData  (n - 1)


comments powered by Disqus