mbostock/d3 — pull request #819 by ZJONSSON (2012-09-23 22:26:54+00:00)
Title: dsv.parse asynchronous when no arguments

DSV data is special in the sense that it can be parsed incrementally (unlike json, xml etc). This pull request allows an asynchronous version of dsv.parse when no arguments are passed to the constructor.

Here is a quick hacked-up of an asynchronous csv function where .on("data"...) could for example add records to a Crossfilter, reducing the total time of loading and preparing the filter.

```
csvAsync = function(url) {
var xhr = d3.xhr(url,"text/csv"),
buffer = "",
lastLocation = 0,
parser = d3.csv.parse(),
dataDispatch = d3.dispatch(["data"])
on = xhr.on;

xhr.on = function(source,args) {
return (source=="data") ? dataDispatch.data.on.apply(xhr,arguments) : on.apply(xhr,arguments);
}

xhr.on("progress",function(d) {
buffer += d3.event.target.responseText.slice(lastLocation);
lastLocation = d3.event.loaded;
var lines = buffer.split("\n");
if (lines.length > 1) {
var results = parser(lines.slice(0,lines.length-1).join("\n"));
if (results.length) dataDispatch.data(results);
buffer = results[results.length-1]
}
})
return xhr.send("GET")
}
```

It might make sense to add a data dispatch to the d3.xhr object, for any partial data parsed so far (where applicable).