@@ -159,10 +159,7 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
159
159
constructor ( destination : Subscriber < T > , public request : AjaxRequest ) {
160
160
super ( destination ) ;
161
161
this . resultSelector = request . resultSelector ;
162
- this . xhr = this . createXHR ( ) ;
163
- if ( this . xhr ) {
164
- this . send ( ) ;
165
- }
162
+ this . send ( ) ;
166
163
}
167
164
168
165
next ( e : Event ) : void {
@@ -182,38 +179,41 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
182
179
}
183
180
}
184
181
185
- private send ( ) {
182
+ private send ( ) : XMLHttpRequest {
186
183
const {
187
- request : { user , method , url , async , password } ,
188
- xhr
184
+ request,
185
+ request : { user , method , url , async , password }
189
186
} = this ;
190
-
191
- let result ;
192
- if ( user ) {
193
- result = tryCatch ( xhr . open ) . call ( xhr , method , url , async , user , password ) ;
194
- } else {
195
- result = tryCatch ( xhr . open ) . call ( xhr , method , url , async ) ;
196
- }
197
-
198
- if ( result === errorObject ) {
199
- return this . error ( errorObject . e ) ;
200
- }
201
-
202
- xhr . send ( ) ;
203
- }
204
-
205
- private createXHR ( ) : XMLHttpRequest {
206
- const request = this . request ;
207
187
const createXHR = request . createXHR ;
208
188
const xhr = tryCatch ( createXHR ) . call ( request ) ;
209
189
210
190
if ( xhr === errorObject ) {
211
191
this . error ( errorObject . e ) ;
212
192
} else {
193
+ this . xhr = xhr ;
194
+
195
+ // open XHR first
196
+ let result ;
197
+ if ( user ) {
198
+ result = tryCatch ( xhr . open ) . call ( xhr , method , url , async , user , password ) ;
199
+ } else {
200
+ result = tryCatch ( xhr . open ) . call ( xhr , method , url , async ) ;
201
+ }
202
+
203
+ if ( result === errorObject ) {
204
+ this . error ( errorObject . e ) ;
205
+ return ;
206
+ }
207
+
208
+ // timeout and responseType can be set once the XHR is open
213
209
xhr . timeout = request . timeout ;
214
210
xhr . responseType = request . responseType ;
211
+
212
+ // now set up the events
215
213
this . setupEvents ( xhr , request ) ;
216
- return xhr ;
214
+
215
+ // finally send the request
216
+ xhr . send ( ) ;
217
217
}
218
218
}
219
219
0 commit comments