S
Scott Sauyet
I needed a technique to aggregate the results of several asynchronous
calls. Obviously there are many ways of doing this. I wrote an
Aggregator constructor function and was wondering if the API this
presents makes sense to people here.
Here is some demo code that uses this function:
var agg = new Aggregator();
var myHandler = function(data, statuses, errors) {
// statuses = {"blue": "completed", "red": "completed",
// "yellow": "completed"}
// errors = {} //
// data = {"red": { /* ... */}, "blue": { /* ... */ },
// "yellow": { /* ... */ }}
// do something with data.blue, data.red, data.yellow
}
agg.onComplete(myHandler);
// Or var agg = new Aggregator(myHandler);
agg.start("blue");
agg.start("red");
agg.start("yellow");
getJsonAjax({
url: "blue/",
success: function(data, status) {
agg.complete("blue", data);
},
failure: function(status, err) {
agg.error("blue", status);
}
});
getJsonAjax({
url: "red/",
success: function(data, status) {
agg.complete("red", data);
},
failure: function(status, err) {
agg.error("red", status);
}
});
getJsonAjax({
url: "yellow/",
success: function(data, status) {
agg.complete("yellow", data);
},
failure: function(status, err) {
agg.error("yellow", status);
}
});
(I don't actually have a getJsonAjax() function, but it should be
obvious enough what it's meant to do.)
The user associates each asynchronous process with a String (above
"red", "blue", and "yellow"). When each process completes, the
relevant returned data is supplied with the complete() call.
The completion handlers accept three parameters. The first represents
the actual results of the various asynchronous calls, the second, the
status flags of each call ("started", "completed", "error", possibly
"aborted"), and the third, any error messages generated by these
calls. Any actual combination of the results is left to the caller,
presumably in the completion handlers. You can pass such handlers to
the onComplete() method or to the constructor.
I do have an implementation of this, but at the moment, I'm mostly
concerned with whether the API makes sense. Is there a cleaner way to
do this? Is this general enough? Are the String identifiers for the
calls robust enough? Should the constructor also accept the
identifiers? What suggestions do you have?
calls. Obviously there are many ways of doing this. I wrote an
Aggregator constructor function and was wondering if the API this
presents makes sense to people here.
Here is some demo code that uses this function:
var agg = new Aggregator();
var myHandler = function(data, statuses, errors) {
// statuses = {"blue": "completed", "red": "completed",
// "yellow": "completed"}
// errors = {} //
// data = {"red": { /* ... */}, "blue": { /* ... */ },
// "yellow": { /* ... */ }}
// do something with data.blue, data.red, data.yellow
}
agg.onComplete(myHandler);
// Or var agg = new Aggregator(myHandler);
agg.start("blue");
agg.start("red");
agg.start("yellow");
getJsonAjax({
url: "blue/",
success: function(data, status) {
agg.complete("blue", data);
},
failure: function(status, err) {
agg.error("blue", status);
}
});
getJsonAjax({
url: "red/",
success: function(data, status) {
agg.complete("red", data);
},
failure: function(status, err) {
agg.error("red", status);
}
});
getJsonAjax({
url: "yellow/",
success: function(data, status) {
agg.complete("yellow", data);
},
failure: function(status, err) {
agg.error("yellow", status);
}
});
(I don't actually have a getJsonAjax() function, but it should be
obvious enough what it's meant to do.)
The user associates each asynchronous process with a String (above
"red", "blue", and "yellow"). When each process completes, the
relevant returned data is supplied with the complete() call.
The completion handlers accept three parameters. The first represents
the actual results of the various asynchronous calls, the second, the
status flags of each call ("started", "completed", "error", possibly
"aborted"), and the third, any error messages generated by these
calls. Any actual combination of the results is left to the caller,
presumably in the completion handlers. You can pass such handlers to
the onComplete() method or to the constructor.
I do have an implementation of this, but at the moment, I'm mostly
concerned with whether the API makes sense. Is there a cleaner way to
do this? Is this general enough? Are the String identifiers for the
calls robust enough? Should the constructor also accept the
identifiers? What suggestions do you have?