Skip to content

Commit

Permalink
Refactor NetworkOverlay to use a symbol instead of a public property …
Browse files Browse the repository at this point in the history
…in XHR (facebook#48928)

Summary:
Pull Request resolved: facebook#48928

Changelog: [internal]

Just a minor change to reduce the number of Flow errors we will get when we refactor XHR soon.

Differential Revision: D68625224
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 24, 2025
1 parent c496570 commit 636aa27
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/react-native/Libraries/Inspector/NetworkOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

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

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

const XHR_ID_KEY = Symbol('XHR_ID');

function getXHRId(xhr: XMLHttpRequest): number {
// $FlowExpectedError[prop-missing]
return xhr[XHR_ID_KEY];
}

function setXHRId(xhr: XMLHttpRequest, id: number) {
// $FlowExpectedError[prop-missing]
xhr[XHR_ID_KEY] = id;
}

/**
* Show all the intercepted network requests over the InspectorPanel.
*/
Expand All @@ -110,7 +123,7 @@ class NetworkOverlay extends React.Component<Props, State> {

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

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

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

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

XHRInterceptor.setSendCallback((data, xhr) => {
// $FlowFixMe[prop-missing]
const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
const xhrIndex = this._getRequestIndexByXHRID(getXHRId(xhr));
if (xhrIndex === -1) {
return;
}
Expand All @@ -179,7 +192,7 @@ class NetworkOverlay extends React.Component<Props, State> {
XHRInterceptor.setHeaderReceivedCallback(
(type, size, responseHeaders, xhr) => {
// $FlowFixMe[prop-missing]
const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
const xhrIndex = this._getRequestIndexByXHRID(getXHRId(xhr));
if (xhrIndex === -1) {
return;
}
Expand All @@ -197,7 +210,7 @@ class NetworkOverlay extends React.Component<Props, State> {
XHRInterceptor.setResponseCallback(
(status, timeout, response, responseURL, responseType, xhr) => {
// $FlowFixMe[prop-missing]
const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
const xhrIndex = this._getRequestIndexByXHRID(getXHRId(xhr));
if (xhrIndex === -1) {
return;
}
Expand Down

0 comments on commit 636aa27

Please sign in to comment.