The concept of making modular requests is one I’ve used in recent projects, and I thought it was time to standardise and release this as a library. Each request revolves around a simple interface:
The aim is to make each request modular, consistent, simple and for it to clean up after itself. By making each request implement an interface, and therefore consistent, it facilitates the creation of useful utilities. With this in mind I thought I’d put together a library of common requests and utilities.
Benefits of using AS3 Signals
The main benefits of using AS3 signals over the traditional AS3 event system (for me) are that signals can be defined in an interface and a signal can free itself up for garbage collection by removing all of its listeners.
By defining the signals in an interface we can be certain that any implementation of the interface implements the defined signals; in this case each request much have an implementation of the started, progress, completed and failed signals. The advantage of this is that any utility class we create knows the signals that each request can dispatch, because they are enforced by the interface.
Another advantage of using AS3 signals in this library is the ability to remove all of the listeners of a particular signal at once, which is another feature not available to us when using the traditional event model. The benefit of using this method is that once a request has dispatch its failed or completed signal it can then confidently free itself up for garbage collection without worrying if all listeners have been removed (we all know that not removing event listeners is a common cause of memory leaks!).
As a side note, without the AS3 signals library I wouldn’t have thought of requests in terms of interfaces and thus created this library!
The initial release of StarRequests includes some common load requests, a batch utility and a queue utility. For now, I’ve only included the common requests that I’ve used in recent projects, which include loading an XML file, a Bitmap and BitmapData. I plan to add more varied requests as times go on as there’s no restriction on the types of requests that can be implemented. Heck, you could have a move request that moves a Sprite to a specified position. The point is that as long as a request implements the correct interface it doesn’t matter what the request does. Other ideas for requests include File I/O requests, database requests, etc.
The library includes a queue utility, which allows you to continuously add requests to the queue utility and it will execute them in the order they are added. This utility could prove particularly useful when creating applications that are continuously updated via external APIs, such as a Twitter client.
The other utility that’s included initially is a batch utility that allows you to set up a batch of requests to be sent together, one after the other. The clever thing about this utility is that it also implements the request interface, so you can create a batch of requests that can then be included in another batch or added to a queue. Without defined interfaces, and the use of AS3 signals, implementing this functionality using the traditional event model would have been a lot more complex!
Not only are the requests included in StarRequests modular and consistent they also help you save time when coding. The idea being that not only do StarRequests promote a good use of polymorphism, but they also require less lines of code to use. The following snippet shows loading an XML file using normal loading techniques, and cleaning that request once it’s complete.
And now look at the same functionality using StarRequests.
It’s much quicker to write the code to load XML files using StarRequests, and it takes out the complexities of cleaning up for you! Now imagine trying to create a queue for loading XML files in the normal way, or a batch process, it would be complicated but StarRequests makes that really easy for you.
Using StarRequests with Robotlegs
I’m almost exclusively using Robotlegs with AS3 signals for all Flash Platform projects and so I wanted to make sure that StarRequests fits in with my current workflow. I think it does. Each request is completely modular, fitting in with Robotlegs’ philosophy, and therefore can easily incorporated into a Robotlegs project. It’s even possible to set up a centralised request queue service for your application so every service sends their requests to the centralised queue to be sent.
Using StarRequests to build API libraries
Using StarRequests to build API libraries could prove to be very powerful. Imagine a Twittier API library built using StarRequests; you could have a login request, update status request, get friends request and a get timeline request all of which could return strongly typed responses in their completed signals, and could be added to queues. This would be much more efficient than expecting the user to manual parse each response. Furthermore, if you look at the code examples above you’ll see how easy the API for the library could be!
Another advantage to using StarRequests is that, because each request is modular, the compiler only needs to include the requests that are used as opposed to the entire library (usually an API library is encompassed in one class). Separating the requests into their own classes can help to reduce the final file size of the compiled application.
The StarRequests library is available to download from GitHub as both the source code and a SWC file. It can be used in any AS3 project whether it uses the Flex framework or not; so you have no excuse not to try it out!
Please feedback on any issues or requests you have using the issue tracking at GitHub and I will endeavour to solve the issue or implement your request (if I feel it fits in with the library).
Add ASDoc comments to the libraryUpdate: The library now has ASDocs!
- Implement unit testing
- Add more common requests/utilities
- (Possibly) create API libraries for common services (Twitter, Flickr, etc) using StarRequests
This is the first release of StarRequests and thus as improvements and additions are made the structure of the library may change slightly.