Response Pattern in ActionScript 3

Earlier I posted about the Response Pattern. Today I wanted to show an ActionScript implementation and how it would be used. The API I decided to go with for this pattern uses method chaining. This is one of those things where people love it or they despise it. I’m sorry if you are one of the latter. Method chaining is where an object returns a reference to itself as the result of its methods. This allows you to call many methods on the object in one line. jQuery uses this and it is used a lot in the Zend Framework. As example you might have a drawing API helper that does this:

 shape.fill(0x660000).rect(0, 0, 100, 100).fill(0x990000).rect(1, 1, 98, 98); 

This would draw a red box with a darker red border around it, all in one line. If the above code gives you tingles in your stomach, you’ll like response. If it upsets you, you can still use response with one method per line of code. You can see in the interface IResponse that it has method chaining and supports a bindable progress property as well. The Response implementation has a few more methods that are useful when first creating a response. It allows events to trigger the complete cycle (which runs through the result handlers and calls them) or the error cycle. It also will create a object that can be used in NetConnection calls or other situations that take a native responder object. There are also some more advanced uses of Response which we might talk about another time. But let’s see it in action! I’ve posted my library of code I reuse on Google Code under the MIT license. Here you’ll find a class called ImageLibrary. Here is how it works.

 // MySprite which extends Sprite public function addImage(url:String):void { var bitmap:Bitmap = new Bitmap(); addChild(bitmap); ImageLibrary.getInstance().getImage(url).handle(addDataToBitmap, bitmap); } private function addDataToBitmap(data:Object, bitmap:Bitmap):void { if (data is BitmapData) { bitmap.bitmapData = data as BitmapData; } } 

In this example, we have a result handler that sets the BitmapData object to the bitmap object. We could have created and added the bitmap to the Sprite in the handler, but I wanted to show how you can pass extra parameters to the handlers in the handle(resultHandler:Function, method. Another thing to note, is that if we were to add the same image to our MySprite class again, addDataToBitmap would be called instantly because ImageLibrary caches the results of previous calls so that the player won’t have to load them from the server again. This results in virtualized lists having the images instantaneously showing up after the first load. The RemoteProxy class is a Flash remoting service which is extremely small. It uses Response.

 public function loadContacts():IResponse { var service:RemoteProxy = new RemoteProxy(gateway); // call the list operation and then handle the results with setContacts // and handle errors with errorLoading return service.contacts.list().handle(setContacts).handleError(errorLoading); } public function setContacts(contacts:Array):void { _contacts = contacts; } protected function errorLoading(error:Error):void { Logging.log("Error loading contacts: " + error.toString()); } 

In this example we pass the Response object along so that whoever is calling loadContacts can know when the result is ready or if there was an error. Here is a list of some of the classes I’ve made which use Response:

This is just a little taste of using the Response Pattern. I have found it much nicer to use when you are waiting for the results of a particular call. Event listeners are still useful

online viagracanada pharmacyhow long does it cialis take to kick incanadian mail order pharmacycialis generic

for user input and for socket-based libraries such as XMPP or FMS, but I like Response for most other async operations.

6 Responses to “Response Pattern in ActionScript 3”

  1. Scott Says:

    Just a quick question about error handling and debugging… How does Flash output an error when using syntax like this?

    Does it compile that as if it was a nested function within a function sort of thing or does it just say that service.contacts.list() has an error?

  2. The Response Pattern - Jacob Wright – Flex, AIR, PHP, etc. Says:

    […] I posted about the ActionScript implementation of the Response pattern. You can see how it is used and why it is different. It looks similar to other type of […]

  3. Paul Robertson Says:

    Hi Jac,

    I really like this approach. It reminds me a bit of the old (Flash MX 2004 I think) Flash Remoting classes. With those you called the method to invoke a service, and it returned the Responder object where you then set result and fault methods (as properties).

    But your approach obviously adds a lot to that.

    Several months ago I wrote a SQL library that I’ve been using. I just got around to uploading the code and was going to post about it soon. Now you’ve got me thinking about re-doing the API to use the Response pattern instead. If only you’d thought of this earlier =)

    (I know, I could always just use yours instead. Maybe I’ll take yours and add a couple of features that I built into the one I made, including db connection pooling and caching compiled statements. Nothing that couldn’t be added in, I don’t think, after looking at your code.)

  4. Jacob Wright Says:

    Paul, I would love contributions to my database class. Or would love to see yours for ideas. As long as you have a simple query(sql, …params):Response on the database class, I’m happy. Creating a separate statement object for that is annoying. :)

  5. Druckertinte Says:

    Thanks for share the ” “. It works great! THANKS THANKS THANKS!

  6. Jacob Wright Says:

    You’re very welcome!