@@ -24,6 +24,9 @@ const XHRInterceptor = require('XHRInterceptor');
2424const LISTVIEW_CELL_HEIGHT = 15 ;
2525const SEPARATOR_THICKNESS = 2 ;
2626
27+ // Global id for the intercepted XMLHttpRequest objects.
28+ let nextXHRId = 0 ;
29+
2730type NetworkRequestInfo = {
2831 url ?: string ,
2932 method ?: string ,
@@ -61,6 +64,8 @@ class NetworkOverlay extends React.Component {
6164 ) => ReactElement < any > ;
6265 _renderScrollComponent : ( props : Object ) = > ReactElement < any > ;
6366 _closeButtonClicked : ( ) = > void ;
67+ // Map of `xhr._index` -> `index in `_requests``.
68+ _xhrIdMap : { [ key : number ] : number } ;
6469
6570 state : {
6671 dataSource : ListView . DataSource ,
@@ -87,6 +92,7 @@ class NetworkOverlay extends React.Component {
8792 this . _renderRow = this . _renderRow . bind ( this ) ;
8893 this . _renderScrollComponent = this . _renderScrollComponent . bind ( this ) ;
8994 this . _closeButtonClicked = this . _closeButtonClicked . bind ( this ) ;
95+ this . _xhrIdMap = { } ;
9096 }
9197
9298 _enableInterception ( ) : void {
@@ -95,49 +101,59 @@ class NetworkOverlay extends React.Component {
95101 }
96102 // Show the network request item in listView as soon as it was opened.
97103 XHRInterceptor . setOpenCallback ( function ( method , url , xhr ) {
98- // Add one private `_index` property to identify the xhr object,
104+ // Generate a global id for each intercepted xhr object, add this id
105+ // to the xhr object as a private `_index` property to identify it,
99106 // so that we can distinguish different xhr objects in callbacks.
100- xhr . _index = this . _requests . length ;
101- const _xhr : NetworkRequestInfo = { 'method' : method , 'url' : url } ;
107+ xhr . _index = nextXHRId ++ ;
108+ const xhrIndex = this . _requests . length ;
109+ this . _xhrIdMap [ xhr . _index ] = xhrIndex ;
110+
111+ const _xhr : NetworkRequestInfo = {
112+ 'method' : method ,
113+ 'url' : url
114+ } ;
102115 this . _requests . push ( _xhr ) ;
103116 this . _detailViewItems . push ( [ ] ) ;
104- this . _genDetailViewItem ( xhr . _index ) ;
117+ this . _genDetailViewItem ( xhrIndex ) ;
105118 this . setState (
106119 { dataSource : this . _listViewDataSource . cloneWithRows ( this . _requests ) } ,
107120 this . _scrollToBottom ( ) ,
108121 ) ;
109122 } . bind ( this ) ) ;
110123
111124 XHRInterceptor . setRequestHeaderCallback ( function ( header , value , xhr ) {
112- if ( xhr . _index === undefined ) {
125+ const xhrIndex = this . _getRequestIndexByXHRID ( xhr . _index ) ;
126+ if ( xhrIndex === - 1 ) {
113127 return ;
114128 }
115- const networkInfo = this . _requests [ xhr . _index ] ;
129+ const networkInfo = this . _requests [ xhrIndex ] ;
116130 if ( ! networkInfo . requestHeaders ) {
117131 networkInfo . requestHeaders = { } ;
118132 }
119133 networkInfo . requestHeaders [ header ] = value ;
120- this . _genDetailViewItem ( xhr . _index ) ;
134+ this . _genDetailViewItem ( xhrIndex ) ;
121135 } . bind ( this ) ) ;
122136
123137 XHRInterceptor . setSendCallback ( function ( data , xhr ) {
124- if ( xhr . _index === undefined ) {
138+ const xhrIndex = this . _getRequestIndexByXHRID ( xhr . _index ) ;
139+ if ( xhrIndex === - 1 ) {
125140 return ;
126141 }
127- this . _requests [ xhr . _index ] . dataSent = data ;
128- this . _genDetailViewItem ( xhr . _index ) ;
142+ this . _requests [ xhrIndex ] . dataSent = data ;
143+ this . _genDetailViewItem ( xhrIndex ) ;
129144 } . bind ( this ) ) ;
130145
131146 XHRInterceptor . setHeaderReceivedCallback (
132147 function ( type , size , responseHeaders , xhr ) {
133- if ( xhr . _index === undefined ) {
148+ const xhrIndex = this . _getRequestIndexByXHRID ( xhr . _index ) ;
149+ if ( xhrIndex === - 1 ) {
134150 return ;
135151 }
136- const networkInfo = this . _requests [ xhr . _index ] ;
152+ const networkInfo = this . _requests [ xhrIndex ] ;
137153 networkInfo . responseContentType = type ;
138154 networkInfo . responseSize = size ;
139155 networkInfo . responseHeaders = responseHeaders ;
140- this . _genDetailViewItem ( xhr . _index ) ;
156+ this . _genDetailViewItem ( xhrIndex ) ;
141157 } . bind ( this )
142158 ) ;
143159
@@ -150,16 +166,17 @@ class NetworkOverlay extends React.Component {
150166 responseType ,
151167 xhr ,
152168 ) {
153- if ( xhr . _index === undefined ) {
169+ const xhrIndex = this . _getRequestIndexByXHRID ( xhr . _index ) ;
170+ if ( xhrIndex === - 1 ) {
154171 return ;
155172 }
156- const networkInfo = this . _requests [ xhr . _index ] ;
173+ const networkInfo = this . _requests [ xhrIndex ] ;
157174 networkInfo . status = status ;
158175 networkInfo . timeout = timeout ;
159176 networkInfo . response = response ;
160177 networkInfo . responseURL = responseURL ;
161178 networkInfo . responseType = responseType ;
162- this . _genDetailViewItem ( xhr . _index ) ;
179+ this . _genDetailViewItem ( xhrIndex ) ;
163180 } . bind ( this )
164181 ) ;
165182
@@ -296,11 +313,24 @@ class NetworkOverlay extends React.Component {
296313 return JSON . stringify ( value ) ;
297314 }
298315 if ( typeof value === 'string' && value . length > 500 ) {
299- return String ( value ) . substr ( 0 , 500 ) . concat ( '\n***TRUNCATED TO 500 CHARACTERS***' ) ;
316+ return String ( value ) . substr ( 0 , 500 ) . concat (
317+ '\n***TRUNCATED TO 500 CHARACTERS***' ) ;
300318 }
301319 return value ;
302320 }
303321
322+ _getRequestIndexByXHRID ( index : number ) : number {
323+ if ( index === undefined ) {
324+ return - 1 ;
325+ }
326+ const xhrIndex = this . _xhrIdMap [ index ] ;
327+ if ( xhrIndex === undefined ) {
328+ return - 1 ;
329+ } else {
330+ return xhrIndex ;
331+ }
332+ }
333+
304334 /**
305335 * Generate a list of views containing network request information for
306336 * a XHR object, to be shown in the detail scrollview. This function
@@ -324,7 +354,7 @@ class NetworkOverlay extends React.Component {
324354 ) ;
325355 }
326356 // Re-render if this network request is showing in the detail view.
327- if ( this . state . detailRowID != null && this . state . detailRowID == index ) {
357+ if ( this . state . detailRowID != null && Number ( this . state . detailRowID ) = == index ) {
328358 this . setState ( { newDetailInfo : true } ) ;
329359 }
330360 }
0 commit comments