Showing only posts with topic "web" [.rss for this topic]. See all posts.

My little continuation framework

Since continuations are so fast in Ruby 1.9, I’ve been playing with a simple little contination-based web framework I wrote based on code I ripped out of Whisper.

Using continuations is really awesome for webapps because you can write application control flow just like you would write the regular program control flow. You can write it in a single method, store state in local variables, use if statements, whatever.

For example, here’s the entire code for a webapp that lets you increment and decrement a number:

def run
  counter = 0

  while true
    click = html <<EOS
<p>Current counter value: #{counter}.</p>
<p>To increment, #{link_to "click here", :increment}</p>
<p>To decrement, #{link_to "click here", :decrement}</p>

    case click
    when :increment
      counter += 1
    when :decrement
      counter -= 1

Pretty obvious huh? It’s a loop, and when someone clicks on the increment link, you increment the counter, and when they click on the decrement counter, you decrement it. The app displays the HTML you see, and provides /increment and /decrement links. The logic isn’t spread out across different methods and different classes like it would be in a regular framework.

I added back-button support too, so pressing the back button after clicking increment brings you to the previous number, and further increments and decrements do the right thing.

I realize frameworks like Seaside and Wee have been doing this forever, but it’s new to me and very fun.