@@ -5,78 +5,78 @@ import {Response} from '../static_response';
55import { ResponseOptions , BaseResponseOptions } from '../base_response_options' ;
66import { Injectable } from 'angular2/src/core/di' ;
77import { BrowserXhr } from './browser_xhr' ;
8- import { EventEmitter , ObservableWrapper } from 'angular2/src/core/facade/async' ;
98import { isPresent } from 'angular2/src/core/facade/lang' ;
10-
9+ var Observable = require ( '@reactivex/rxjs/dist/cjs/Observable' ) ;
1110/**
12- * Creates connections using `XMLHttpRequest`. Given a fully-qualified
13- * request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
14- * request.
15- *
16- * This class would typically not be created or interacted with directly inside applications, though
17- * the {@link MockConnection} may be interacted with in tests.
18- */
11+ * Creates connections using `XMLHttpRequest`. Given a fully-qualified
12+ * request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
13+ * request.
14+ *
15+ * This class would typically not be created or interacted with directly inside applications, though
16+ * the {@link MockConnection} may be interacted with in tests.
17+ */
1918export class XHRConnection implements Connection {
2019 request : Request ;
2120 /**
2221 * Response {@link EventEmitter} which emits a single {@link Response} value on load event of
2322 * `XMLHttpRequest`.
2423 */
25- response : EventEmitter ; // TODO: Make generic of <Response>;
24+ response : any ; // TODO: Make generic of <Response>;
2625 readyState : ReadyStates ;
27- private _xhr ; // TODO: make type XMLHttpRequest, pending resolution of
28- // https://github.com/angular/ts2dart/issues/230
2926 constructor ( req : Request , browserXHR : BrowserXhr , baseResponseOptions ?: ResponseOptions ) {
3027 this . request = req ;
31- this . response = new EventEmitter ( ) ;
32- this . _xhr = browserXHR . build ( ) ;
33- // TODO(jeffbcross): implement error listening/propagation
34- this . _xhr . open ( RequestMethods [ req . method ] . toUpperCase ( ) , req . url ) ;
35- this . _xhr . addEventListener ( 'load' , ( _ ) => {
36- // responseText is the old-school way of retrieving response (supported by IE8 & 9)
37- // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
38- let response = isPresent ( this . _xhr . response ) ? this . _xhr . response : this . _xhr . responseText ;
28+ this . response = new Observable ( responseObserver => {
29+ let _xhr : XMLHttpRequest = browserXHR . build ( ) ;
30+ _xhr . open ( RequestMethods [ req . method ] . toUpperCase ( ) , req . url ) ;
31+ // load event handler
32+ let onLoad = ( ) => {
33+ // responseText is the old-school way of retrieving response (supported by IE8 & 9)
34+ // response/responseType properties were introduced in XHR Level2 spec (supported by
35+ // IE10)
36+ let response = isPresent ( _xhr . response ) ? _xhr . response : _xhr . responseText ;
3937
40- // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
41- let status = this . _xhr . status === 1223 ? 204 : this . _xhr . status ;
38+ // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
39+ let status = _xhr . status === 1223 ? 204 : _xhr . status ;
4240
43- // fix status code when it is 0 (0 status is undocumented).
44- // Occurs when accessing file resources or on Android 4.1 stock browser
45- // while retrieving files from application cache.
46- if ( status === 0 ) {
47- status = response ? 200 : 0 ;
48- }
41+ // fix status code when it is 0 (0 status is undocumented).
42+ // Occurs when accessing file resources or on Android 4.1 stock browser
43+ // while retrieving files from application cache.
44+ if ( status === 0 ) {
45+ status = response ? 200 : 0 ;
46+ }
47+ var responseOptions = new ResponseOptions ( { body : response , status : status } ) ;
48+ if ( isPresent ( baseResponseOptions ) ) {
49+ responseOptions = baseResponseOptions . merge ( responseOptions ) ;
50+ }
51+ responseObserver . next ( new Response ( responseOptions ) ) ;
52+ // TODO(gdi2290): defer complete if array buffer until done
53+ responseObserver . complete ( ) ;
54+ } ;
55+ // error event handler
56+ let onError = ( err ) => {
57+ var responseOptions = new ResponseOptions ( { body : err , type : ResponseTypes . Error } ) ;
58+ if ( isPresent ( baseResponseOptions ) ) {
59+ responseOptions = baseResponseOptions . merge ( responseOptions ) ;
60+ }
61+ responseObserver . error ( new Response ( responseOptions ) ) ;
62+ } ;
4963
50- var responseOptions = new ResponseOptions ( { body : response , status : status } ) ;
51- if ( isPresent ( baseResponseOptions ) ) {
52- responseOptions = baseResponseOptions . merge ( responseOptions ) ;
64+ if ( isPresent ( req . headers ) ) {
65+ req . headers . forEach ( ( value , name ) => { _xhr . setRequestHeader ( name , value ) ; } ) ;
5366 }
5467
55- ObservableWrapper . callNext ( this . response , new Response ( responseOptions ) ) ;
56- // TODO(gdi2290): defer complete if array buffer until done
57- ObservableWrapper . callReturn ( this . response ) ;
58- } ) ;
68+ _xhr . addEventListener ( 'load' , onLoad ) ;
69+ _xhr . addEventListener ( 'error' , onError ) ;
5970
60- this . _xhr . addEventListener ( 'error' , ( err ) => {
61- var responseOptions = new ResponseOptions ( { body : err , type : ResponseTypes . Error } ) ;
62- if ( isPresent ( baseResponseOptions ) ) {
63- responseOptions = baseResponseOptions . merge ( responseOptions ) ;
64- }
65- ObservableWrapper . callThrow ( this . response , new Response ( responseOptions ) ) ;
66- } ) ;
67- // TODO(jeffbcross): make this more dynamic based on body type
71+ _xhr . send ( this . request . text ( ) ) ;
6872
69- if ( isPresent ( req . headers ) ) {
70- req . headers . forEach ( ( value , name ) => { this . _xhr . setRequestHeader ( name , value ) ; } ) ;
71- }
72-
73- this . _xhr . send ( this . request . text ( ) ) ;
73+ return ( ) => {
74+ _xhr . removeEventListener ( 'load' , onLoad ) ;
75+ _xhr . removeEventListener ( 'error' , onError ) ;
76+ _xhr . abort ( ) ;
77+ } ;
78+ } ) ;
7479 }
75-
76- /**
77- * Calls abort on the underlying XMLHttpRequest.
78- */
79- dispose ( ) : void { this . _xhr . abort ( ) ; }
8080}
8181
8282/**
0 commit comments