Changes

Jump to: navigation, search

WebAPI/XHRBatch

1,180 bytes added, 09:51, 19 November 2013
Proposed API
readonly attribute long numAllBatches;
Promise<IXHRBatch[]> getBatches(); // return submitted batches of the same origin. Promise<IXHRBatch[]> getAllBatches(); // return all submitted batches, not strict by the same origin if with the permission
}
readonly attribute unsigned short state;
Promise<XMLHttpRequest> createXMLHttpRequest();
/*
* The batch is ready for a transmission and known by the user agent.
* from NOTSUBMITTED to WAITING.
*/
Promise<boolean> submit(); Promise<boolean> start(); // Start the transmission. The state is transited from WAITING to SENDING.
/*
* Stop the transmission.
* until completed or failed for requests in sending.
*/
Promise<boolean> stop(boolean force); Promise<boolean> drop(); // remove the batch from the user agent. Promise getRequests();
}
readonly attribute XHRBatch batch; // The batch this request belonging to.
Promise<boolean> removeFromBatch(); // Remove this request from the batch belonged to.
}
 
== Examples ==
let batches_defer = navigator.XHRBatch.getBatches();
batches_defer.then(batches => {
for (let i in batches) {
let batch = batches[i];
console.log("Batch Name: " + batch.name);
console.log(" Description: " + batch.description);
}
});
 
function onNetworkAvailable() {
let allBatches_defer = navigator.XHRBatch.getAllBatches();
allBatches_defer.then(batches => {
for (let i in batches) {
let batch = batches[i];
if (batch.state == navigator.XHRBatch.WAITING) {
batch.start();
}
}
}
}
 
function onBatchCompleted() {
let batches_defer = navigator.XHRBatch.getBatches();
batches_defer.then(e => {
for (let i in e.batches) {
let batch = e.batches[i];
if (batch.state != navigator.XHRBatch.COMPLETED)
continue;
batch.drop();
batch.getRequests().then(e => {
let error_count = 0;
for (let i in e.requests) {
let xhr = e.requests[i];
if (xhr.status != 200) {
error_count++;
}
}
console.log("Batch " + e.batch.name + " is completed!");
console.log(" " + error_count + " errors!");
}
}
}
}
Confirm
398
edits

Navigation menu