1111 */
1212'use strict' ;
1313
14+ var RCTNetworking = require ( 'RCTNetworking' ) ;
15+ var RCTDeviceEventEmitter = require ( 'RCTDeviceEventEmitter' ) ;
16+
1417/**
1518 * Shared base for platform-specific XMLHttpRequest implementations.
1619 */
@@ -30,6 +33,13 @@ class XMLHttpRequestBase {
3033 responseText : ?string ;
3134 status : number ;
3235
36+ upload : ?{
37+ onprogress ?: ( event : Object ) => void ;
38+ } ;
39+
40+ _requestId: ?number ;
41+ _subscriptions: [ any ] ;
42+
3343 _method: ?string ;
3444 _url: ?string ;
3545 _headers: Object ;
@@ -60,9 +70,81 @@ class XMLHttpRequestBase {
6070 this . responseText = '' ;
6171 this . status = 0 ;
6272
73+ this . _requestId = null ;
74+
6375 this . _headers = { } ;
6476 this . _sent = false ;
6577 this . _lowerCaseResponseHeaders = { } ;
78+
79+ this . _clearSubscriptions ( ) ;
80+ }
81+
82+ didCreateRequest ( requestId : number ) : void {
83+ this . _requestId = requestId ;
84+ this . _subscriptions . push ( RCTDeviceEventEmitter . addListener (
85+ 'didSendNetworkData' ,
86+ ( args ) => this . _didUploadProgress . call ( this , ...args )
87+ ) ) ;
88+ this . _subscriptions . push ( RCTDeviceEventEmitter . addListener (
89+ 'didReceiveNetworkResponse' ,
90+ ( args ) => this . _didReceiveResponse . call ( this , ...args )
91+ ) ) ;
92+ this . _subscriptions . push ( RCTDeviceEventEmitter . addListener (
93+ 'didReceiveNetworkData' ,
94+ ( args ) => this . _didReceiveData . call ( this , ...args )
95+ ) ) ;
96+ this . _subscriptions . push ( RCTDeviceEventEmitter . addListener (
97+ 'didCompleteNetworkResponse' ,
98+ ( args ) => this . _didCompleteResponse . call ( this , ...args )
99+ ) ) ;
100+ }
101+
102+ _didUploadProgress ( requestId : number , progress : number , total : number ) : void {
103+ if ( requestId === this . _requestId && this . upload && this . upload . onprogress ) {
104+ var event = {
105+ lengthComputable : true ,
106+ loaded : progress ,
107+ total,
108+ } ;
109+ this . upload . onprogress ( event ) ;
110+ }
111+ }
112+
113+ _didReceiveResponse ( requestId : number , status : number , responseHeaders : ?Object ) : void {
114+ if ( requestId === this . _requestId ) {
115+ this . status = status ;
116+ this . setResponseHeaders ( responseHeaders ) ;
117+ this . setReadyState ( this . HEADERS_RECEIVED ) ;
118+ }
119+ }
120+
121+ _didReceiveData ( requestId : number , responseText : string ) : void {
122+ if ( requestId === this . _requestId ) {
123+ if ( ! this . responseText ) {
124+ this . responseText = responseText ;
125+ } else {
126+ this . responseText += responseText ;
127+ }
128+ this . setReadyState ( this . LOADING ) ;
129+ }
130+ }
131+
132+ _didCompleteResponse ( requestId : number , error : string ) : void {
133+ if ( requestId === this . _requestId ) {
134+ if ( error ) {
135+ this . responseText = error ;
136+ }
137+ this . _clearSubscriptions ( ) ;
138+ this . _requestId = null ;
139+ this . setReadyState ( this . DONE ) ;
140+ }
141+ }
142+
143+ _clearSubscriptions ( ) : void {
144+ ( this . _subscriptions || [ ] ) . forEach ( sub => {
145+ sub . remove ( ) ;
146+ } ) ;
147+ this . _subscriptions = [ ] ;
66148 }
67149
68150 getAllResponseHeaders ( ) : ?string {
@@ -108,10 +190,6 @@ class XMLHttpRequestBase {
108190 throw new Error ( 'Subclass must define sendImpl method' ) ;
109191 }
110192
111- abortImpl ( ) : void {
112- throw new Error ( 'Subclass must define abortImpl method' ) ;
113- }
114-
115193 send ( data : any ) : void {
116194 if ( this . readyState !== this . OPENED ) {
117195 throw new Error ( 'Request has not been opened' ) ;
@@ -125,7 +203,10 @@ class XMLHttpRequestBase {
125203
126204 abort ( ) : void {
127205 this. _aborted = true ;
128- this . abortImpl ( ) ;
206+ if ( this . _requestId ) {
207+ console . log ( 'calling abort' , this . _requestId ) ;
208+ RCTNetworking . abortRequest ( this . _requestId ) ;
209+ }
129210 // only call onreadystatechange if there is something to abort,
130211 // below logic is per spec
131212 if ( ! ( this . readyState === this . UNSENT ||
@@ -138,16 +219,6 @@ class XMLHttpRequestBase {
138219 this . _reset ( ) ;
139220 }
140221
141- callback ( status : number , responseHeaders : ?Object , responseText : string ) : void {
142- if ( this . _aborted ) {
143- return ;
144- }
145- this . status = status ;
146- this . setResponseHeaders ( responseHeaders || { } ) ;
147- this . responseText = responseText ;
148- this . setReadyState ( this . DONE ) ;
149- }
150-
151222 setResponseHeaders ( responseHeaders : ?Object ) : void {
152223 this. responseHeaders = responseHeaders || null ;
153224 var headers = responseHeaders || { } ;
0 commit comments