Skip to content

Commit efc57e5

Browse files
authoredDec 18, 2020
Add built-in Suspense cache with support for invalidation (refreshing) (#20456)
1 parent 00a5b08 commit efc57e5

39 files changed

+2387
-59
lines changed
 

‎packages/react-dom/src/server/ReactPartialRendererHooks.js

+5
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ function useOpaqueIdentifier(): OpaqueIDType {
489489
);
490490
}
491491

492+
function useCacheRefresh(): <T>(?() => T, ?T) => void {
493+
invariant(false, 'Not implemented.');
494+
}
495+
492496
function noop(): void {}
493497

494498
export let currentPartialRenderer: PartialRenderer = (null: any);
@@ -520,4 +524,5 @@ export const Dispatcher: DispatcherType = {
520524

521525
if (enableCache) {
522526
Dispatcher.getCacheForType = getCacheForType;
527+
Dispatcher.useCacheRefresh = useCacheRefresh;
523528
}

‎packages/react-reconciler/src/ReactFiber.new.js

+26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
enableProfilerTimer,
2828
enableFundamentalAPI,
2929
enableScopeAPI,
30+
enableCache,
3031
} from 'shared/ReactFeatureFlags';
3132
import {NoFlags, Placement, StaticMask} from './ReactFiberFlags';
3233
import {ConcurrentRoot, BlockingRoot} from './ReactRootTags';
@@ -54,6 +55,7 @@ import {
5455
ScopeComponent,
5556
OffscreenComponent,
5657
LegacyHiddenComponent,
58+
CacheComponent,
5759
} from './ReactWorkTags';
5860
import getComponentName from 'shared/getComponentName';
5961

@@ -88,6 +90,7 @@ import {
8890
REACT_SCOPE_TYPE,
8991
REACT_OFFSCREEN_TYPE,
9092
REACT_LEGACY_HIDDEN_TYPE,
93+
REACT_CACHE_TYPE,
9194
} from 'shared/ReactSymbols';
9295

9396
export type {Fiber};
@@ -501,6 +504,11 @@ export function createFiberFromTypeAndProps(
501504
return createFiberFromScope(type, pendingProps, mode, lanes, key);
502505
}
503506
// eslint-disable-next-line no-fallthrough
507+
case REACT_CACHE_TYPE:
508+
if (enableCache) {
509+
return createFiberFromCache(pendingProps, mode, lanes, key);
510+
}
511+
// eslint-disable-next-line no-fallthrough
504512
default: {
505513
if (typeof type === 'object' && type !== null) {
506514
switch (type.$$typeof) {
@@ -745,6 +753,24 @@ export function createFiberFromLegacyHidden(
745753
return fiber;
746754
}
747755

756+
export function createFiberFromCache(
757+
pendingProps: any,
758+
mode: TypeOfMode,
759+
lanes: Lanes,
760+
key: null | string,
761+
) {
762+
const fiber = createFiber(CacheComponent, pendingProps, key, mode);
763+
// TODO: The Cache fiber shouldn't have a type. It has a tag.
764+
// This needs to be fixed in getComponentName so that it relies on the tag
765+
// instead.
766+
if (__DEV__) {
767+
fiber.type = REACT_CACHE_TYPE;
768+
}
769+
fiber.elementType = REACT_CACHE_TYPE;
770+
fiber.lanes = lanes;
771+
return fiber;
772+
}
773+
748774
export function createFiberFromText(
749775
content: string,
750776
mode: TypeOfMode,

‎packages/react-reconciler/src/ReactFiber.old.js

+26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
enableProfilerTimer,
2828
enableFundamentalAPI,
2929
enableScopeAPI,
30+
enableCache,
3031
} from 'shared/ReactFeatureFlags';
3132
import {NoFlags, Placement, StaticMask} from './ReactFiberFlags';
3233
import {ConcurrentRoot, BlockingRoot} from './ReactRootTags';
@@ -54,6 +55,7 @@ import {
5455
ScopeComponent,
5556
OffscreenComponent,
5657
LegacyHiddenComponent,
58+
CacheComponent,
5759
} from './ReactWorkTags';
5860
import getComponentName from 'shared/getComponentName';
5961

@@ -88,6 +90,7 @@ import {
8890
REACT_SCOPE_TYPE,
8991
REACT_OFFSCREEN_TYPE,
9092
REACT_LEGACY_HIDDEN_TYPE,
93+
REACT_CACHE_TYPE,
9194
} from 'shared/ReactSymbols';
9295

9396
export type {Fiber};
@@ -501,6 +504,11 @@ export function createFiberFromTypeAndProps(
501504
return createFiberFromScope(type, pendingProps, mode, lanes, key);
502505
}
503506
// eslint-disable-next-line no-fallthrough
507+
case REACT_CACHE_TYPE:
508+
if (enableCache) {
509+
return createFiberFromCache(pendingProps, mode, lanes, key);
510+
}
511+
// eslint-disable-next-line no-fallthrough
504512
default: {
505513
if (typeof type === 'object' && type !== null) {
506514
switch (type.$$typeof) {
@@ -745,6 +753,24 @@ export function createFiberFromLegacyHidden(
745753
return fiber;
746754
}
747755

756+
export function createFiberFromCache(
757+
pendingProps: any,
758+
mode: TypeOfMode,
759+
lanes: Lanes,
760+
key: null | string,
761+
) {
762+
const fiber = createFiber(CacheComponent, pendingProps, key, mode);
763+
// TODO: The Cache fiber shouldn't have a type. It has a tag.
764+
// This needs to be fixed in getComponentName so that it relies on the tag
765+
// instead.
766+
if (__DEV__) {
767+
fiber.type = REACT_CACHE_TYPE;
768+
}
769+
fiber.elementType = REACT_CACHE_TYPE;
770+
fiber.lanes = lanes;
771+
return fiber;
772+
}
773+
748774
export function createFiberFromText(
749775
content: string,
750776
mode: TypeOfMode,

0 commit comments

Comments
 (0)