Skip to content

Commit fee1bc6

Browse files
Merge pull request #14642 from Snuffleupagus/Driver-send-fetch
Replace XMLHttpRequest usage with the Fetch API in `Driver._send`
2 parents e85bb0b + 19c2cc8 commit fee1bc6

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

test/driver.js

+33-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const {
2020
AnnotationLayer,
2121
AnnotationMode,
22+
createPromiseCapability,
2223
getDocument,
2324
GlobalWorkerOptions,
2425
PixelsPerInch,
@@ -571,7 +572,7 @@ class Driver {
571572

572573
if (!task.pdfDoc) {
573574
const dataUrl = this.canvas.toDataURL("image/png");
574-
this._sendResult(dataUrl, task, failure, () => {
575+
this._sendResult(dataUrl, task, failure).then(() => {
575576
this._log(
576577
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
577578
);
@@ -831,7 +832,7 @@ class Driver {
831832
this._log("Snapshotting... ");
832833

833834
const dataUrl = this.canvas.toDataURL("image/png");
834-
this._sendResult(dataUrl, task, failure, () => {
835+
this._sendResult(dataUrl, task, failure).then(() => {
835836
this._log(
836837
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
837838
);
@@ -885,7 +886,7 @@ class Driver {
885886
}
886887
}
887888

888-
_sendResult(snapshot, task, failure, callback) {
889+
_sendResult(snapshot, task, failure) {
889890
const result = JSON.stringify({
890891
browser: this.browser,
891892
id: task.id,
@@ -901,29 +902,38 @@ class Driver {
901902
viewportHeight: task.viewportHeight,
902903
outputScale: task.outputScale,
903904
});
904-
this._send("/submit_task_results", result, callback);
905+
return this._send("/submit_task_results", result);
905906
}
906907

907-
_send(url, message, callback) {
908-
const r = new XMLHttpRequest();
909-
r.open("POST", url, true);
910-
r.setRequestHeader("Content-Type", "application/json");
911-
r.onreadystatechange = e => {
912-
if (r.readyState === 4) {
913-
this.inFlightRequests--;
908+
_send(url, message) {
909+
const capability = createPromiseCapability();
910+
this.inflight.textContent = this.inFlightRequests++;
914911

915-
// Retry until successful
916-
if (r.status !== 200) {
917-
setTimeout(() => {
918-
this._send(url, message);
919-
});
920-
}
921-
if (callback) {
922-
callback();
912+
fetch(url, {
913+
method: "POST",
914+
headers: {
915+
"Content-Type": "application/json",
916+
},
917+
body: message,
918+
})
919+
.then(response => {
920+
// Retry until successful.
921+
if (!response.ok || response.status !== 200) {
922+
throw new Error(response.statusText);
923923
}
924-
}
925-
};
926-
this.inflight.textContent = this.inFlightRequests++;
927-
r.send(message);
924+
925+
this.inFlightRequests--;
926+
capability.resolve();
927+
})
928+
.catch(reason => {
929+
console.warn(`Driver._send failed (${url}): ${reason}`);
930+
931+
this.inFlightRequests--;
932+
capability.resolve();
933+
934+
this._send(url, message);
935+
});
936+
937+
return capability.promise;
928938
}
929939
}

0 commit comments

Comments
 (0)