This post originated from an RSS feed registered with Java Buzz
by Brian McCallister.
Original Post: Dojo Deferred and DFS
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
A while back I talked about the "DataFlowSequence" which Tim and I came up with for dealing with concurrent XHRs. To recap, if you needed a result not yet available you would register interest in it and when it became available it would be yielded to your callback:
var d = new dojo.Deferred();
// register callback
d.addCallback(function(name) {
log.debug("name: " + name)
});
// supply data
d.callback("Sam Jones");
Deferred has an additional registerable callback, an errback, which is called if Deferred#errback(value) is invoked instead of Deferred#callback(value).
var d = new dojo.Deferred();
d.addCallback(function(v) { log.debug("success! : " + v) })
d.addErrback(function(e) { log.debug("error! : " + v) })
if (Math.random() > 0.5) {
d.callback("yea!")
}
else {
d.errback("boo!")
}
Is fun stuff to hack around with. The Mochikit and Dojo Deferred implementations are almost certainly more performant than the DFS -- they can work with arrays instead of hashes, which should be much quicker. They optimize the common case, wanting one result, and can handle the less common case of wanting several by having deferreds waiting on deferreds. I think I prefer the DFS syntax for needing multiple results, though. I need to play more with deferreds waiting on the results of other deferreds. The single argument nature might be nice to work with, rather than using ~named arguments.
Wheee! Reminds me of a silly hack I have been wanting to do to optimize the syntax of all this. The string value of a JS function includes the declaration, which includes the parameter names. These could be parsed out fairly painlessly, allowing us to do the DFS like:
var dfs = new skife.ajax.DataFlowSequence();
dfs.when(function(first, last) {
log.debug(["Hello,", first, last].join(" "))
});
dfs.supply("first", "John");
dfs.supply("last", "Flynn")
This would be much slower, but is kind of nice :-)