Skip to content

Commit ba55cff

Browse files
rubennortefacebook-github-bot
authored andcommitted
Refactor NetworkOverlay to use a symbol instead of a public property in XHR (#48928)
Summary: Changelog: [internal] Just a minor change to reduce the number of Flow errors we will get when we refactor XHR soon. Differential Revision: D68625224
1 parent d1c5f69 commit ba55cff

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

packages/react-native/Libraries/Inspector/NetworkOverlay.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
'use strict';
1212

13+
import type XMLHttpRequest from '../Network/XMLHttpRequest';
1314
import type {RenderItemProps} from '@react-native/virtualized-lists';
1415

1516
import ScrollView from '../Components/ScrollView/ScrollView';
@@ -87,6 +88,18 @@ function keyExtractor(request: NetworkRequestInfo): string {
8788
return String(request.id);
8889
}
8990

91+
const XHR_ID_KEY = Symbol('XHR_ID');
92+
93+
function getXHRId(xhr: XMLHttpRequest): number {
94+
// $FlowExpectedError[prop-missing]
95+
return xhr[XHR_ID_KEY];
96+
}
97+
98+
function setXHRId(xhr: XMLHttpRequest, id: number) {
99+
// $FlowExpectedError[prop-missing]
100+
xhr[XHR_ID_KEY] = id;
101+
}
102+
90103
/**
91104
* Show all the intercepted network requests over the InspectorPanel.
92105
*/
@@ -110,7 +123,7 @@ class NetworkOverlay extends React.Component<Props, State> {
110123

111124
// Map of `socketId` -> `index in `this.state.requests`.
112125
_socketIdMap: {[number]: number} = {};
113-
// Map of `xhr._index` -> `index in `this.state.requests`.
126+
// Map of `xhr[XHR_ID_KEY]` -> `index in `this.state.requests`.
114127
_xhrIdMap: {[key: number]: number, ...} = {};
115128

116129
state: State = {
@@ -127,9 +140,9 @@ class NetworkOverlay extends React.Component<Props, State> {
127140
// Generate a global id for each intercepted xhr object, add this id
128141
// to the xhr object as a private `_index` property to identify it,
129142
// so that we can distinguish different xhr objects in callbacks.
130-
xhr._index = nextXHRId++;
143+
setXHRId(xhr, nextXHRId++);
131144
const xhrIndex = this.state.requests.length;
132-
this._xhrIdMap[xhr._index] = xhrIndex;
145+
this._xhrIdMap[getXHRId(xhr)] = xhrIndex;
133146

134147
const _xhr: NetworkRequestInfo = {
135148
id: xhrIndex,
@@ -147,7 +160,7 @@ class NetworkOverlay extends React.Component<Props, State> {
147160

148161
XHRInterceptor.setRequestHeaderCallback((header, value, xhr) => {
149162
// $FlowFixMe[prop-missing]
150-
const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
163+
const xhrIndex = this._getRequestIndexByXHRID(getXHRId(xhr));
151164
if (xhrIndex === -1) {
152165
return;
153166
}
@@ -164,7 +177,7 @@ class NetworkOverlay extends React.Component<Props, State> {
164177

165178
XHRInterceptor.setSendCallback((data, xhr) => {
166179
// $FlowFixMe[prop-missing]
167-
const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
180+
const xhrIndex = this._getRequestIndexByXHRID(getXHRId(xhr));
168181
if (xhrIndex === -1) {
169182
return;
170183
}
@@ -179,7 +192,7 @@ class NetworkOverlay extends React.Component<Props, State> {
179192
XHRInterceptor.setHeaderReceivedCallback(
180193
(type, size, responseHeaders, xhr) => {
181194
// $FlowFixMe[prop-missing]
182-
const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
195+
const xhrIndex = this._getRequestIndexByXHRID(getXHRId(xhr));
183196
if (xhrIndex === -1) {
184197
return;
185198
}
@@ -197,7 +210,7 @@ class NetworkOverlay extends React.Component<Props, State> {
197210
XHRInterceptor.setResponseCallback(
198211
(status, timeout, response, responseURL, responseType, xhr) => {
199212
// $FlowFixMe[prop-missing]
200-
const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
213+
const xhrIndex = this._getRequestIndexByXHRID(getXHRId(xhr));
201214
if (xhrIndex === -1) {
202215
return;
203216
}

0 commit comments

Comments
 (0)