19
19
const {
20
20
AnnotationLayer,
21
21
AnnotationMode,
22
+ createPromiseCapability,
22
23
getDocument,
23
24
GlobalWorkerOptions,
24
25
PixelsPerInch,
@@ -571,7 +572,7 @@ class Driver {
571
572
572
573
if ( ! task . pdfDoc ) {
573
574
const dataUrl = this . canvas . toDataURL ( "image/png" ) ;
574
- this . _sendResult ( dataUrl , task , failure , ( ) => {
575
+ this . _sendResult ( dataUrl , task , failure ) . then ( ( ) => {
575
576
this . _log (
576
577
"done" + ( failure ? " (failed !: " + failure + ")" : "" ) + "\n"
577
578
) ;
@@ -831,7 +832,7 @@ class Driver {
831
832
this . _log ( "Snapshotting... " ) ;
832
833
833
834
const dataUrl = this . canvas . toDataURL ( "image/png" ) ;
834
- this . _sendResult ( dataUrl , task , failure , ( ) => {
835
+ this . _sendResult ( dataUrl , task , failure ) . then ( ( ) => {
835
836
this . _log (
836
837
"done" + ( failure ? " (failed !: " + failure + ")" : "" ) + "\n"
837
838
) ;
@@ -885,7 +886,7 @@ class Driver {
885
886
}
886
887
}
887
888
888
- _sendResult ( snapshot , task , failure , callback ) {
889
+ _sendResult ( snapshot , task , failure ) {
889
890
const result = JSON . stringify ( {
890
891
browser : this . browser ,
891
892
id : task . id ,
@@ -901,29 +902,38 @@ class Driver {
901
902
viewportHeight : task . viewportHeight ,
902
903
outputScale : task . outputScale ,
903
904
} ) ;
904
- this . _send ( "/submit_task_results" , result , callback ) ;
905
+ return this . _send ( "/submit_task_results" , result ) ;
905
906
}
906
907
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 ++ ;
914
911
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 ) ;
923
923
}
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 ;
928
938
}
929
939
}
0 commit comments