Recently I've been thinking about how to improve the handling of state in the web applications I write. I had the idea to store the states in a HoH:
state | prev | next | run | pre_cond cart | undef | shipping | show_cart | undef shipping | cart | billing | get_ship | is_valid_cart billing | shipping | summary | get_bill | is_valid_ship summary | billing | undef | show_summary | is_valid_bill
"run" and "pre_cond" are method names to call, where "run" methods show the next form and "pre_cond" are predicates to determine if we can call this method. I've included the "next" state in this table so it can be passed to the "run" method so as not to hard code it.
This is a change from how I've been previously doing it, normally I'd just have a hash to look up from state name to a sub ref to call. The idea is now I can make sure that all the previous states are valid before showing the next. This solves the problem where we worry about a user fiddling with the flow by changing the state in the HTML. It also means that we can take into account things external to the current flow, for example this means that a user can bookmark a specific page and return to it, but if any of the previous states are now invalid (e.g. a product is now out of stock) we can return to that state without blindly continuing (or having to repeat the code to do the test twice).
It's something I've been thinking about for the last week. Is this something that makes sense? How do you handle state in your web applications?