Hi dear community,
I've made a small utility for a problem I had to solve. This utility had to sequence a whole bunch of methods, some of those methods were async and some of the were synchronous.
The utility is working fine and I found myself using it across different applications as well! Here's some code snippets to illustrate use of the utility:
class Controller
{
public function authenticate():IRequestAction // IRequestAction basically has 4 methods - to add success/error call backs and to call those.
{
var authenticateRequest:IRequestAction= new Authenticate();
return authenticateRequest;
}
public function loadTweetData( tweetId:String ):IRequestAction
{
var loadTweetDataRequest:IRequestAction= new LoadTweetData(tweetId);
return loadTweetDataRequest;
}
public function downloadCSVFile():IRequestAction
{
var downloadCSVFileRequest:IRequestAction = new ExportRetweetingUserDataAsCSV();
return downloadCSVFileRequest;
}
// More of similar methods below
}
So any "action" method that you want to perform: loading data, sorting, searching etc., can be written in it's own class. And so long as it's implementing a simple IRequestAction interface, you can call them like so:
Controller.controllerInstance.loadRetweetData(tweetId).onSuccess( onRetweetDataLoadSuccess).onError(onRetweetDataLoadError );
I liked chaining the methods like this from long ago when I were using MaxTween tweener. Joshua's Actuate tweener feels very similar to that.
And yes, if you put your action implementation into the constructor (so it would execute before you can add success/error handlers) - it would still call the handler you need once it's added.
The next thing I've done is making a queue manager for actions, so you can write something like this:
function setupStartupActionQueue():Void
{
var actionQueue = new ActionQueue();
actionQueue.addAction( Controller.controllerInstance.loadTweetData("") ); // you can actually add handlers to the actions here too!
actionQueue.addAction( Controller.controllerInstance.loadRetweetData("") );
actionQueue.addAction( Controller.controllerInstance.loadListOfRetweetingUsers([]) );
actionQueue.addAction( Controller.controllerInstance.updateSessionFile() );
actionQueue.addEventListener( ActionQueueEvent.QUEUE_COMPLETE, onQueueComplete);
actionQueue.startQueue();
}
The benefit of this utility is to write cleaner code. Every action does one thing, it has it's own class for that. With this utility it's transparent and easy to manage.
But I'm not 100% sure that it's:
- Not a duplicate of some other well-known library
- Really the preferred way of writing code and follows all the Best Practices
I'd like to get some feedback from you:
- Would you be interested to see this utility published (it's used for OpenFL, but isn't really depending on any other library, the event system of OpenFL that I've used can be easily removed)
- What features are important for you to have to make it usable?
- If you have ideas about API and how to work with this tool more efficiently - let me know.