@@ -159,10 +159,7 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
159159 constructor ( destination : Subscriber < T > , public request : AjaxRequest ) {
160160 super ( destination ) ;
161161 this . resultSelector = request . resultSelector ;
162- this . xhr = this . createXHR ( ) ;
163- if ( this . xhr ) {
164- this . send ( ) ;
165- }
162+ this . send ( ) ;
166163 }
167164
168165 next ( e : Event ) : void {
@@ -182,38 +179,41 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
182179 }
183180 }
184181
185- private send ( ) {
182+ private send ( ) : XMLHttpRequest {
186183 const {
187- request : { user , method , url , async , password } ,
188- xhr
184+ request,
185+ request : { user , method , url , async , password }
189186 } = 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 ;
207187 const createXHR = request . createXHR ;
208188 const xhr = tryCatch ( createXHR ) . call ( request ) ;
209189
210190 if ( xhr === errorObject ) {
211191 this . error ( errorObject . e ) ;
212192 } 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
213209 xhr . timeout = request . timeout ;
214210 xhr . responseType = request . responseType ;
211+
212+ // now set up the events
215213 this . setupEvents ( xhr , request ) ;
216- return xhr ;
214+
215+ // finally send the request
216+ xhr . send ( ) ;
217217 }
218218 }
219219
0 commit comments