the all-thing | 2010-07-29 19:51:28 -0400
==========================================
My little continuation framework
--------------------------------
Date: June 18, 2009 9:26pm
Author: William Morgan
Labels: continuations, web
URL: http://all-thing.net/my-little-continuation-framework.txt
Since continuations are so fast in Ruby 1.9 [1], I've been playing with a
simple little contination-based web framework I wrote based on code I ripped
out of Whisper [2].
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 <
To increment, #{link_to "click here", :increment}
To decrement, #{link_to "click here", :decrement}
Thanks!
EOS case click when :increment counter += 1 when :decrement counter -= 1 end end end 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. [1] http://all-thing.net/fibers-via-continuations [2] http://masanjin.net/whisper/ This delicious text version served up by Whisper