Skip to content

Commit

Permalink
Poll for update only refreshes cache when there's an update
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Feb 17, 2021
1 parent 07ca66a commit 399fefc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export type OwnersList = {|
owners: Array<Owner> | null,
|};

export type InspectedElementResponseType =
| 'full-data'
| 'hydrated-path'
| 'no-change'
| 'not-found';

export type InspectedElement = {|
id: number,

Expand Down
23 changes: 15 additions & 8 deletions packages/react-devtools-shared/src/inspectedElementCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {Wakeable} from 'shared/ReactTypes';
import type {
Element,
InspectedElement as InspectedElementFrontend,
InspectedElementResponseType,
} from 'react-devtools-shared/src/devtools/views/Components/types';

const Pending = 0;
Expand Down Expand Up @@ -122,7 +123,7 @@ export function inspectElement(
path,
rendererID: ((rendererID: any): number),
}).then(
(inspectedElement: InspectedElementFrontend) => {
([inspectedElement: InspectedElementFrontend]) => {
const resolvedRecord = ((newRecord: any): ResolvedRecord<InspectedElementFrontend>);
resolvedRecord.status = Resolved;
resolvedRecord.value = inspectedElement;
Expand Down Expand Up @@ -174,12 +175,18 @@ export function checkForUpdate({
element,
path: null,
rendererID: ((rendererID: any): number),
}).then((inspectedElement: InspectedElementFrontend) => {
// TODO only start transition if we got an update; right now we over-update even after "no-change"
startTransition(() => {
const [key, value] = createCacheSeed(element, inspectedElement);
refresh(key, value);
});
});
}).then(
([
inspectedElement: InspectedElementFrontend,
responseType: InspectedElementResponseType,
]) => {
if (responseType === 'full-data') {
startTransition(() => {
const [key, value] = createCacheSeed(element, inspectedElement);
refresh(key, value);
});
}
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
import type {
Element,
InspectedElement as InspectedElementFrontend,
InspectedElementResponseType,
} from 'react-devtools-shared/src/devtools/views/Components/types';

// Map an Element in the Store to the most recent copy of its inspected data.
Expand All @@ -36,6 +37,11 @@ const inspectedElementMap: WeakMap<

type Path = Array<string | number>;

type InspectElementReturnType = [
InspectedElementFrontend,
InspectedElementResponseType,
];

export function inspectElement({
bridge,
element,
Expand All @@ -46,7 +52,7 @@ export function inspectElement({
element: Element,
path: Path | null,
rendererID: number,
|}): Promise<InspectedElementFrontend> {
|}): Promise<InspectElementReturnType> {
const {id} = element;
return inspectElementAPI({
bridge,
Expand All @@ -62,7 +68,7 @@ export function inspectElement({
// This is a no-op for the purposes of our cache.
inspectedElement = inspectedElementMap.get(element);
if (inspectedElement != null) {
return inspectedElement;
return [inspectedElement, type];
}
break;

Expand All @@ -84,7 +90,7 @@ export function inspectElement({

inspectedElementMap.set(element, inspectedElement);

return inspectedElement;
return [inspectedElement, type];

case 'hydrated-path':
const hydratedPathData = ((data: any): InspectElementHydratedPath);
Expand All @@ -107,7 +113,7 @@ export function inspectElement({

inspectedElementMap.set(element, inspectedElement);

return inspectedElement;
return [inspectedElement, type];
}
break;

Expand Down

0 comments on commit 399fefc

Please sign in to comment.