Skip to content

Commit 5d10e41

Browse files
author
Brian Vaughn
committed
Auto-refresh inspected element right after cleaning warings/errors
This is a temporary solution until we are using a more modern Cache in DevTools.
1 parent aa5f89d commit 5d10e41

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

packages/react-devtools-shared/src/devtools/store.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,6 @@ export default class Store extends EventEmitter<{|
396396
rendererID,
397397
id,
398398
});
399-
400-
// Immediately refresh the inspected element.
401-
// Waiting for the polling timer makes the action feel less responsive.
402-
this._bridge.send('inspectElement', {id, rendererID});
403399
}
404400
}
405401

@@ -414,10 +410,6 @@ export default class Store extends EventEmitter<{|
414410
rendererID,
415411
id,
416412
});
417-
418-
// Immediately refresh the inspected element.
419-
// Waiting for the polling timer makes the action feel less responsive.
420-
this._bridge.send('inspectElement', {id, rendererID});
421413
}
422414
}
423415

packages/react-devtools-shared/src/devtools/views/Components/InspectedElementContext.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ export type GetInspectedElement = (
5151
id: number,
5252
) => InspectedElementFrontend | null;
5353

54+
type RefreshInspectedElement = () => void;
55+
5456
export type InspectedElementContextType = {|
5557
copyInspectedElementPath: CopyInspectedElementPath,
5658
getInspectedElementPath: GetInspectedElementPath,
5759
getInspectedElement: GetInspectedElement,
60+
refreshInspectedElement: RefreshInspectedElement,
5861
storeAsGlobal: StoreAsGlobal,
5962
|};
6063

@@ -159,6 +162,15 @@ function InspectedElementContextController({children}: Props) {
159162
// would itself be blocked by the same render that suspends (waiting for the data).
160163
const {selectedElementID} = useContext(TreeStateContext);
161164

165+
const refreshInspectedElement = useCallback<RefreshInspectedElement>(() => {
166+
if (selectedElementID !== null) {
167+
const rendererID = store.getRendererIDForElement(selectedElementID);
168+
if (rendererID !== null) {
169+
bridge.send('inspectElement', {id: selectedElementID, rendererID});
170+
}
171+
}
172+
}, [bridge, selectedElementID]);
173+
162174
const [
163175
currentlyInspectedElement,
164176
setCurrentlyInspectedElement,
@@ -347,6 +359,7 @@ function InspectedElementContextController({children}: Props) {
347359
copyInspectedElementPath,
348360
getInspectedElement,
349361
getInspectedElementPath,
362+
refreshInspectedElement,
350363
storeAsGlobal,
351364
}),
352365
// InspectedElement is used to invalidate the cache and schedule an update with React.
@@ -355,6 +368,7 @@ function InspectedElementContextController({children}: Props) {
355368
currentlyInspectedElement,
356369
getInspectedElement,
357370
getInspectedElementPath,
371+
refreshInspectedElement,
358372
storeAsGlobal,
359373
],
360374
);

packages/react-devtools-shared/src/devtools/views/Components/InspectedElementErrorsAndWarningsTree.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
*/
99

1010
import * as React from 'react';
11+
import {useContext} from 'react';
1112
import Button from '../Button';
1213
import ButtonIcon from '../ButtonIcon';
1314
import Store from '../../store';
1415
import sharedStyles from './InspectedElementSharedStyles.css';
1516
import styles from './InspectedElementErrorsAndWarningsTree.css';
1617
import {SettingsContext} from '../Settings/SettingsContext';
18+
import {InspectedElementContext} from './InspectedElementContext';
1719

1820
import type {InspectedElement} from './types';
1921
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
@@ -29,19 +31,33 @@ export default function InspectedElementErrorsAndWarningsTree({
2931
inspectedElement,
3032
store,
3133
}: Props) {
32-
const {showInlineWarningsAndErrors} = React.useContext(SettingsContext);
34+
const {refreshInspectedElement} = useContext(InspectedElementContext);
35+
36+
const {showInlineWarningsAndErrors} = useContext(SettingsContext);
3337
if (!showInlineWarningsAndErrors) {
3438
return null;
3539
}
3640

3741
const {errors, warnings} = inspectedElement;
3842

3943
const clearErrors = () => {
40-
store.clearErrorsForElement(inspectedElement.id);
44+
const {id} = inspectedElement;
45+
store.clearErrorsForElement(id);
46+
47+
// Immediately poll for updated data.
48+
// This avoids a delay between clicking the clear button and refreshing errors.
49+
// Ideally this would be done with useTranstion but that requires updating to a newer Cache strategy.
50+
refreshInspectedElement();
4151
};
4252

4353
const clearWarnings = () => {
44-
store.clearWarningsForElement(inspectedElement.id);
54+
const {id} = inspectedElement;
55+
store.clearWarningsForElement(id);
56+
57+
// Immediately poll for updated data.
58+
// This avoids a delay between clicking the clear button and refreshing warnings.
59+
// Ideally this would be done with useTranstion but that requires updating to a newer Cache strategy.
60+
refreshInspectedElement();
4561
};
4662

4763
return (

0 commit comments

Comments
 (0)