Skip to content
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

Add rudimentary support for Cache to DevTools #20458

Merged
merged 2 commits into from
Jan 4, 2021
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 @@ -685,4 +685,5 @@ exports[`Store should show the right display names for special component types 1
<Baz> [withFoo][withBar]
<Baz> [Memo][withFoo][withBar]
<Baz> [ForwardRef][withFoo][withBar]
<Cache>
`;
1 change: 1 addition & 0 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ describe('Store', () => {
<FakeHigherOrderComponent />
<MemoizedFakeHigherOrderComponent />
<ForwardRefFakeHigherOrderComponent />
<React.unstable_Cache />
</React.Fragment>
);

Expand Down
19 changes: 16 additions & 3 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export function getInternalReactConstants(
// Currently the version in Git is 17.0.2 (but that version has not been/may not end up being released).
if (gt(version, '17.0.1')) {
ReactTypeOfWork = {
CacheComponent: 24, // Experimental
ClassComponent: 1,
ContextConsumer: 9,
ContextProvider: 10,
Expand Down Expand Up @@ -205,6 +206,7 @@ export function getInternalReactConstants(
};
} else if (gte(version, '17.0.0-alpha')) {
ReactTypeOfWork = {
CacheComponent: -1, // Doesn't exist yet
ClassComponent: 1,
ContextConsumer: 9,
ContextProvider: 10,
Expand Down Expand Up @@ -234,6 +236,7 @@ export function getInternalReactConstants(
};
} else if (gte(version, '16.6.0-beta.0')) {
ReactTypeOfWork = {
CacheComponent: -1, // Doens't exist yet
ClassComponent: 1,
ContextConsumer: 9,
ContextProvider: 10,
Expand Down Expand Up @@ -263,6 +266,7 @@ export function getInternalReactConstants(
};
} else if (gte(version, '16.4.3-alpha')) {
ReactTypeOfWork = {
CacheComponent: -1, // Doens't exist yet
ClassComponent: 2,
ContextConsumer: 11,
ContextProvider: 12,
Expand Down Expand Up @@ -292,6 +296,7 @@ export function getInternalReactConstants(
};
} else {
ReactTypeOfWork = {
CacheComponent: -1, // Doens't exist yet
ClassComponent: 2,
ContextConsumer: 12,
ContextProvider: 13,
Expand Down Expand Up @@ -335,6 +340,7 @@ export function getInternalReactConstants(
}

const {
CacheComponent,
ClassComponent,
IncompleteClassComponent,
FunctionComponent,
Expand Down Expand Up @@ -382,6 +388,8 @@ export function getInternalReactConstants(
let resolvedContext: any = null;

switch (tag) {
case CacheComponent:
return 'Cache';
case ClassComponent:
case IncompleteClassComponent:
return getDisplayName(resolvedType);
Expand Down Expand Up @@ -486,12 +494,13 @@ export function attach(
} = getInternalReactConstants(renderer.version);
const {Incomplete, NoFlags, PerformedWork, Placement} = ReactTypeOfSideEffect;
const {
FunctionComponent,
CacheComponent,
ClassComponent,
ContextConsumer,
DehydratedSuspenseComponent,
Fragment,
ForwardRef,
Fragment,
FunctionComponent,
HostRoot,
HostPortal,
HostComponent,
Expand Down Expand Up @@ -2518,6 +2527,10 @@ export function attach(
tag === ForwardRef) &&
(!!memoizedState || !!dependencies);

// TODO Show custom UI for Cache like we do for Suspense
// For now, just hide state data entirely since it's not meant to be inspected.
const showState = !usesHooks && tag !== CacheComponent;

const typeSymbol = getTypeSymbol(type);

let canViewSource = false;
Expand Down Expand Up @@ -2687,7 +2700,7 @@ export function attach(
context,
hooks,
props: memoizedProps,
state: usesHooks ? null : memoizedState,
state: showState ? memoizedState : null,
errors: Array.from(errors.entries()),
warnings: Array.from(warnings.entries()),

Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-shared/src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type WorkFlags = number;
export type ExpirationTime = number;

export type WorkTagMap = {|
CacheComponent: WorkTag,
ClassComponent: WorkTag,
ContextConsumer: WorkTag,
ContextProvider: WorkTag,
Expand Down
21 changes: 12 additions & 9 deletions packages/react-devtools-shell/src/app/ElementTypes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Profiler,
StrictMode,
Suspense,
unstable_Cache as Cache,
} from 'react';

const Context = createContext('abc');
Expand Down Expand Up @@ -61,15 +62,17 @@ export default function ElementTypes() {
<Context.Consumer>{value => null}</Context.Consumer>
</Context.Provider>
<StrictMode>
<Suspense fallback={<div>Loading...</div>}>
<ClassComponent />
<FunctionComponent />
<MemoFunctionComponent />
<ForwardRefComponent />
<ForwardRefComponentWithAnonymousFunction />
<ForwardRefComponentWithCustomDisplayName />
<LazyComponent />
</Suspense>
<Cache>
<Suspense fallback={<div>Loading...</div>}>
<ClassComponent />
<FunctionComponent />
<MemoFunctionComponent />
<ForwardRefComponent />
<ForwardRefComponentWithAnonymousFunction />
<ForwardRefComponentWithCustomDisplayName />
<LazyComponent />
</Suspense>
</Cache>
</StrictMode>
</Fragment>
</Profiler>
Expand Down