Web Application Development in PLT Scheme - send/suspend/dispatch
April 16th, 2008
send/suspend/dispatch
send/suspend/dispatch takes a function as its argument just like send/suspend. The difference is that this page-generating function takes another function as its argument, conventionally called embed/url. embed/url is used to embed continuations into a URL. To generate continuation URLs with embed/url, you pass it a function that takes the HTTP request object as its argument. When that continuation URL is requested, either by clicking a link or posting a form, the continuation is resumed with the HTTP request object being passed to it.
This article’s example will be a simple counter that lets users increment or decrement their counter by clicking the appropriate links. Each link will use embed/url to dispatch to separate functions in the servlet. The two increment functions are really simple. They take the current value of the counter and return its new result.
Most of the work is done in the show-counter function. It shows the current value of the counter and generates links for incrementing and decrementing the counter. I also added a little styling to the links that just separates them with a little whitespace.
This code should be pretty self-explanatory after my little introduction to send/suspend/dispatch at the top of the article. And if you’ve followed along with my previous posts you should know what all the supporting pieces of code do.
Everytime the increment and decrement functions are called, show-counter returns control back to the start function of the servlet. This only allows us to increment or decrement once, so we have to loop each time show-counter returns.
When show-counter returns, it returns the new result of the counter. The loop then calls show-counter again with the new counter value.
This is a pretty good introduction to send/suspend/dispatch I think and it is a building block for bigger and greater things. The code for this article can be found here.
I’m trying to decide what I want to write about next. I have a pretty cool dispatching server that lets programmers create “pretty” URLs similar to Ruby on Rails’ controller/action dispatching. I may write about how to do that and provide a little proof-of-concept web application. I’ve also been meaning to write about keyword tracking in scheme. Keyword tracking is a pretty hot topic in the world of internet marketing. I’ll come up with something to write about. Until next time, hack away at the code and learn some stuff.
Leave a Reply