@@ -90,7 +90,6 @@ const createFetchOptions = (url, options, type) => {
90
90
*/
91
91
const fetchXhr = ( target , fetchOptions ) => new Promise ( ( resolve , reject ) => {
92
92
const req = new XMLHttpRequest ( ) ;
93
- let onProgress = ( ) => { } ;
94
93
95
94
const onError = ( msg ) => ( ev ) => {
96
95
console . warn ( msg , ev ) ;
@@ -112,18 +111,18 @@ const fetchXhr = (target, fetchOptions) => new Promise((resolve, reject) => {
112
111
} ;
113
112
114
113
if ( typeof fetchOptions . onProgress === 'function' ) {
115
- onProgress = ( ev ) => {
114
+ const rel = fetchOptions . method . toUpperCase ( ) === 'GET' ? req : req . upload ;
115
+ rel . addEventListener ( 'progress' , ( ev ) => {
116
116
if ( ev . lengthComputable ) {
117
- const percentComplete = ev . loaded / ev . total * 100 ;
117
+ const percentComplete = Math . round ( ev . loaded / ev . total * 100 ) ;
118
118
fetchOptions . onProgress ( ev , percentComplete ) ;
119
119
}
120
- } ;
120
+ } ) ;
121
121
}
122
122
123
123
req . addEventListener ( 'load' , onLoad ) ;
124
124
req . addEventListener ( 'error' , onError ( 'An error occured while performing XHR request' ) ) ;
125
125
req . addEventListener ( 'abort' , onError ( 'XHR request was aborted' ) ) ;
126
- req . addEventListener ( 'progress' , onProgress ) ;
127
126
req . open ( fetchOptions . method , target ) ;
128
127
Object . entries ( fetchOptions . headers ) . forEach ( ( [ k , v ] ) => req . setRequestHeader ( k , v ) ) ;
129
128
req . responseType = fetchOptions . responseType || '' ;
0 commit comments