Skip to content

Commit

Permalink
Added rudimentary support for Cache to DevTools
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Dec 14, 2020
1 parent 0581bdf commit 22ea9b3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
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 @@ -886,6 +886,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 @@ -171,6 +171,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 @@ -200,6 +201,7 @@ export function getInternalReactConstants(
};
} else if (gte(version, '17.0.0-alpha')) {
ReactTypeOfWork = {
CacheComponent: -1, // Doens't exist yet
ClassComponent: 1,
ContextConsumer: 9,
ContextProvider: 10,
Expand Down Expand Up @@ -229,6 +231,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 @@ -258,6 +261,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 @@ -287,6 +291,7 @@ export function getInternalReactConstants(
};
} else {
ReactTypeOfWork = {
CacheComponent: -1, // Doens't exist yet
ClassComponent: 2,
ContextConsumer: 12,
ContextProvider: 13,
Expand Down Expand Up @@ -330,6 +335,7 @@ export function getInternalReactConstants(
}

const {
CacheComponent,
ClassComponent,
IncompleteClassComponent,
FunctionComponent,
Expand Down Expand Up @@ -377,6 +383,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 @@ -481,12 +489,13 @@ export function attach(
} = getInternalReactConstants(renderer.version);
const {NoFlags, PerformedWork, Placement} = ReactTypeOfSideEffect;
const {
FunctionComponent,
CacheComponent,
ClassComponent,
ContextConsumer,
DehydratedSuspenseComponent,
Fragment,
ForwardRef,
Fragment,
FunctionComponent,
HostRoot,
HostPortal,
HostComponent,
Expand Down Expand Up @@ -2330,6 +2339,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 @@ -2496,7 +2509,7 @@ export function attach(
context,
hooks,
props: memoizedProps,
state: usesHooks ? null : memoizedState,
state: showState ? memoizedState : null,

// List of owners
owners,
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

0 comments on commit 22ea9b3

Please sign in to comment.