Skip to content

fix[devtools]: allow element updates polling only if bridge is alive #27067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export function InspectedElementContextController({
parseHookNamesByDefault || alreadyLoadedHookNames,
);

const [bridgeIsAlive, setBridgeIsAliveStatus] = useState<boolean>(true);

const elementHasChanged = element !== null && element !== state.element;

// Reset the cached inspected paths when a new element is selected.
Expand Down Expand Up @@ -213,14 +215,25 @@ export function InspectedElementContextController({
}
}, [state]);

useEffect(() => {
// Assuming that new bridge is always alive at this moment
setBridgeIsAliveStatus(true);

const listener = () => setBridgeIsAliveStatus(false);
bridge.addListener('shutdown', listener);

return () => bridge.removeListener('shutdown', listener);
}, [bridge]);

// Periodically poll the selected element for updates.
useEffect(() => {
if (element !== null) {
if (element !== null && bridgeIsAlive) {
const checkForUpdateWrapper = () => {
checkForUpdate({bridge, element, refresh, store});
timeoutID = setTimeout(checkForUpdateWrapper, POLL_INTERVAL);
};
let timeoutID = setTimeout(checkForUpdateWrapper, POLL_INTERVAL);

return () => {
clearTimeout(timeoutID);
};
Expand All @@ -232,6 +245,7 @@ export function InspectedElementContextController({
// No sense to ping right away after e.g. inspecting/hydrating a path.
inspectedElement,
state,
bridgeIsAlive,
]);

const value = useMemo<Context>(
Expand Down
51 changes: 27 additions & 24 deletions packages/react-devtools-shared/src/inspectedElementCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,32 +180,35 @@ export function checkForUpdate({
}): void {
const {id} = element;
const rendererID = store.getRendererIDForElement(id);
if (rendererID != null) {
inspectElementMutableSource({
bridge,
element,
path: null,
rendererID: ((rendererID: any): number),
}).then(
([inspectedElement, responseType]: [
InspectedElementFrontend,
InspectedElementResponseType,
]) => {
if (responseType === 'full-data') {
startTransition(() => {
const [key, value] = createCacheSeed(element, inspectedElement);
refresh(key, value);
});
}
},

// There isn't much to do about errors in this case,
// but we should at least log them so they aren't silent.
error => {
console.error(error);
},
);
if (rendererID == null) {
return;
}

inspectElementMutableSource({
bridge,
element,
path: null,
rendererID,
}).then(
([inspectedElement, responseType]: [
InspectedElementFrontend,
InspectedElementResponseType,
]) => {
if (responseType === 'full-data') {
startTransition(() => {
const [key, value] = createCacheSeed(element, inspectedElement);
refresh(key, value);
});
}
},

// There isn't much to do about errors in this case,
// but we should at least log them so they aren't silent.
error => {
console.error(error);
},
);
}

export function clearCacheBecauseOfError(refresh: RefreshFunction): void {
Expand Down