On Fri, May 17, 2013 at 6:15 PM, Anne van Kesteren <annevk@annevk.nl> wrote:
> The main problem is that Stream per Streams API is not what you expect
> from an IO stream, but it's more what Blob should've been (Blob
> without synchronous size). What we want I think is a real IO stream.
> If we also need Blob without synchronous size is less clear to me.
Forgetting File API completely, for example, ... how about simple socket
like interface?
// Downloading big data
var remaining;
var type = null;
var payload = '';
function processData(data) {
var offset = 0;
while (offset < data.length) {
if (!type) {
var type = data.substr(offset, 1);
remaining = payloadSize(type);
} else if (remaining > 0) {
var consume = Math.min(remaining, data.length - offset);
payload += data.substr(offset, consume);
offset += consume;
} else if (remaining == 0) {
if (type == FOO) {
foo(payload);
} else if (type == BAR) {
bar(payload);
}
type = null;
}
}
}
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
if (this.readyState == this.LOADING) {
var responseStream = this.response;
responseStream.setBufferSize(1024);
responseStream.ondata = function(evt) {
processData(evt.data);
// Consumed data will be invalidated and memory used for the data
will be released.
};
responseStream.onclose = function() {
// Reached end of response body
...
};
responseStream.start();
// Now responseStream starts forwarding events happen on XHR to its
callbacks.
}
};
client.open("GET", "/foobar");
client.responseType = "stream";
client.send();
// Uploading big data
var client = new XMLHttpRequest();
client.open("POST", "/foobar");
var requestStream = new WriteStream(1024);
var producer = new Producer();
producer.ondata = function(evt.data) {
requestStream.send(evt.data);
};
producer.onclose = function() {
requestStream.close();
};
client.send(requestStream);