mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 17:30:09 +02:00
Replace XMLHttpRequest usage with the Fetch API in Driver._send
This is another part in a series of patches that try to re-implement PR 14287 in smaller steps. Besides converting `Driver._send` to use the Fetch API, this also changes the method to return a `Promise` to get rid of the callback function. Please note that I *purposely* try to maintain the existing behaviour of re-sending the data on failure/unexpected response, including how/where the old callback function was invoked.
This commit is contained in:
parent
3e593cfc1d
commit
19c2cc8689
1 changed files with 34 additions and 24 deletions
|
@ -19,6 +19,7 @@
|
||||||
const {
|
const {
|
||||||
AnnotationLayer,
|
AnnotationLayer,
|
||||||
AnnotationMode,
|
AnnotationMode,
|
||||||
|
createPromiseCapability,
|
||||||
getDocument,
|
getDocument,
|
||||||
GlobalWorkerOptions,
|
GlobalWorkerOptions,
|
||||||
PixelsPerInch,
|
PixelsPerInch,
|
||||||
|
@ -571,7 +572,7 @@ class Driver {
|
||||||
|
|
||||||
if (!task.pdfDoc) {
|
if (!task.pdfDoc) {
|
||||||
const dataUrl = this.canvas.toDataURL("image/png");
|
const dataUrl = this.canvas.toDataURL("image/png");
|
||||||
this._sendResult(dataUrl, task, failure, () => {
|
this._sendResult(dataUrl, task, failure).then(() => {
|
||||||
this._log(
|
this._log(
|
||||||
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
|
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
|
||||||
);
|
);
|
||||||
|
@ -831,7 +832,7 @@ class Driver {
|
||||||
this._log("Snapshotting... ");
|
this._log("Snapshotting... ");
|
||||||
|
|
||||||
const dataUrl = this.canvas.toDataURL("image/png");
|
const dataUrl = this.canvas.toDataURL("image/png");
|
||||||
this._sendResult(dataUrl, task, failure, () => {
|
this._sendResult(dataUrl, task, failure).then(() => {
|
||||||
this._log(
|
this._log(
|
||||||
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
|
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
|
||||||
);
|
);
|
||||||
|
@ -885,7 +886,7 @@ class Driver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_sendResult(snapshot, task, failure, callback) {
|
_sendResult(snapshot, task, failure) {
|
||||||
const result = JSON.stringify({
|
const result = JSON.stringify({
|
||||||
browser: this.browser,
|
browser: this.browser,
|
||||||
id: task.id,
|
id: task.id,
|
||||||
|
@ -901,29 +902,38 @@ class Driver {
|
||||||
viewportHeight: task.viewportHeight,
|
viewportHeight: task.viewportHeight,
|
||||||
outputScale: task.outputScale,
|
outputScale: task.outputScale,
|
||||||
});
|
});
|
||||||
this._send("/submit_task_results", result, callback);
|
return this._send("/submit_task_results", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
_send(url, message, callback) {
|
_send(url, message) {
|
||||||
const r = new XMLHttpRequest();
|
const capability = createPromiseCapability();
|
||||||
r.open("POST", url, true);
|
this.inflight.textContent = this.inFlightRequests++;
|
||||||
r.setRequestHeader("Content-Type", "application/json");
|
|
||||||
r.onreadystatechange = e => {
|
fetch(url, {
|
||||||
if (r.readyState === 4) {
|
method: "POST",
|
||||||
this.inFlightRequests--;
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: message,
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
// Retry until successful.
|
||||||
|
if (!response.ok || response.status !== 200) {
|
||||||
|
throw new Error(response.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.inFlightRequests--;
|
||||||
|
capability.resolve();
|
||||||
|
})
|
||||||
|
.catch(reason => {
|
||||||
|
console.warn(`Driver._send failed (${url}): ${reason}`);
|
||||||
|
|
||||||
|
this.inFlightRequests--;
|
||||||
|
capability.resolve();
|
||||||
|
|
||||||
// Retry until successful
|
|
||||||
if (r.status !== 200) {
|
|
||||||
setTimeout(() => {
|
|
||||||
this._send(url, message);
|
this._send(url, message);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
if (callback) {
|
return capability.promise;
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
this.inflight.textContent = this.inFlightRequests++;
|
|
||||||
r.send(message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue