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 built-in Suspense cache with support for invalidation (refreshing) #20456

Merged
merged 30 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2d2a65b
Initial scaffolding for <Cache />
acdlite Dec 14, 2020
1874591
Implement getCacheForType
acdlite Dec 14, 2020
f9b829d
Use same cache for all new data in a single update
acdlite Dec 14, 2020
57d6c1d
Retain in-progress caches on the root, per lane
acdlite Dec 14, 2020
893a831
Cache refreshing
acdlite Dec 14, 2020
eabf255
Refresh with seeded data
acdlite Dec 14, 2020
c40683a
Refreshes should not affect "sibling" boundaries
acdlite Dec 14, 2020
220a9d7
Add implicit root-level cache
acdlite Dec 15, 2020
da9dc4c
Make CacheContext type non-nullable
acdlite Dec 15, 2020
bc43607
Use an update queue for refreshes
acdlite Dec 15, 2020
900af34
Add fast path for nested mounting Caches
acdlite Dec 15, 2020
3cb4936
Every request in initial render shares same cache
acdlite Dec 15, 2020
7485268
pushCacheProvider/popCacheProvider
acdlite Dec 15, 2020
23d46bf
Add warnings if cache context is in invalid state
acdlite Dec 15, 2020
0d605b1
Explicitly check if the parent provider is fresh
acdlite Dec 15, 2020
65458e4
Restore retry cache from Suspense/Offscreen fiber
acdlite Dec 16, 2020
c599f98
Code size optimizations
acdlite Dec 16, 2020
27c30e1
Remove useRefresh from unstable-shared-subset
acdlite Dec 16, 2020
ddaa1f1
Previous retry cache takes precedence over pool
acdlite Dec 17, 2020
79c65dc
To resume pooled cache, override root.pooledCache
acdlite Dec 17, 2020
52fd1eb
Use only a single pooled cache at a time
acdlite Dec 17, 2020
f4e2124
useRefresh -> useCacheRefresh
acdlite Dec 17, 2020
e451117
More tests
acdlite Dec 17, 2020
8e0b756
Wrap more things in flag
acdlite Dec 17, 2020
eed9285
Remove default transition priority for refreshes
acdlite Dec 17, 2020
9894eb2
Fork ReactFiberCacheComponent
acdlite Dec 17, 2020
ea6700c
Only mutate `root.pooledCache` in complete/unwind
acdlite Dec 18, 2020
3cfe44e
Remove provider fiber from cache context
acdlite Dec 18, 2020
4bb19da
Detect refreshes by comparing to previous parent
acdlite Dec 18, 2020
7c7d2ba
Fix cross-fork discrepancy
acdlite Dec 18, 2020
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
5 changes: 5 additions & 0 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ function useOpaqueIdentifier(): OpaqueIDType {
);
}

function useCacheRefresh(): <T>(?() => T, ?T) => void {
invariant(false, 'Not implemented.');
}

function noop(): void {}

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

if (enableCache) {
Dispatcher.getCacheForType = getCacheForType;
Dispatcher.useCacheRefresh = useCacheRefresh;
}
26 changes: 26 additions & 0 deletions packages/react-reconciler/src/ReactFiber.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
enableProfilerTimer,
enableFundamentalAPI,
enableScopeAPI,
enableCache,
} from 'shared/ReactFeatureFlags';
import {NoFlags, Placement, StaticMask} from './ReactFiberFlags';
import {ConcurrentRoot, BlockingRoot} from './ReactRootTags';
Expand Down Expand Up @@ -54,6 +55,7 @@ import {
ScopeComponent,
OffscreenComponent,
LegacyHiddenComponent,
CacheComponent,
} from './ReactWorkTags';
import getComponentName from 'shared/getComponentName';

Expand Down Expand Up @@ -88,6 +90,7 @@ import {
REACT_SCOPE_TYPE,
REACT_OFFSCREEN_TYPE,
REACT_LEGACY_HIDDEN_TYPE,
REACT_CACHE_TYPE,
} from 'shared/ReactSymbols';

export type {Fiber};
Expand Down Expand Up @@ -501,6 +504,11 @@ export function createFiberFromTypeAndProps(
return createFiberFromScope(type, pendingProps, mode, lanes, key);
}
// eslint-disable-next-line no-fallthrough
case REACT_CACHE_TYPE:
if (enableCache) {
return createFiberFromCache(pendingProps, mode, lanes, key);
}
// eslint-disable-next-line no-fallthrough
default: {
if (typeof type === 'object' && type !== null) {
switch (type.$$typeof) {
Expand Down Expand Up @@ -745,6 +753,24 @@ export function createFiberFromLegacyHidden(
return fiber;
}

export function createFiberFromCache(
pendingProps: any,
mode: TypeOfMode,
lanes: Lanes,
key: null | string,
) {
const fiber = createFiber(CacheComponent, pendingProps, key, mode);
// TODO: The Cache fiber shouldn't have a type. It has a tag.
// This needs to be fixed in getComponentName so that it relies on the tag
// instead.
if (__DEV__) {
fiber.type = REACT_CACHE_TYPE;
}
fiber.elementType = REACT_CACHE_TYPE;
fiber.lanes = lanes;
return fiber;
}

export function createFiberFromText(
content: string,
mode: TypeOfMode,
Expand Down
26 changes: 26 additions & 0 deletions packages/react-reconciler/src/ReactFiber.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
enableProfilerTimer,
enableFundamentalAPI,
enableScopeAPI,
enableCache,
} from 'shared/ReactFeatureFlags';
import {NoFlags, Placement, StaticMask} from './ReactFiberFlags';
import {ConcurrentRoot, BlockingRoot} from './ReactRootTags';
Expand Down Expand Up @@ -54,6 +55,7 @@ import {
ScopeComponent,
OffscreenComponent,
LegacyHiddenComponent,
CacheComponent,
} from './ReactWorkTags';
import getComponentName from 'shared/getComponentName';

Expand Down Expand Up @@ -88,6 +90,7 @@ import {
REACT_SCOPE_TYPE,
REACT_OFFSCREEN_TYPE,
REACT_LEGACY_HIDDEN_TYPE,
REACT_CACHE_TYPE,
} from 'shared/ReactSymbols';

export type {Fiber};
Expand Down Expand Up @@ -501,6 +504,11 @@ export function createFiberFromTypeAndProps(
return createFiberFromScope(type, pendingProps, mode, lanes, key);
}
// eslint-disable-next-line no-fallthrough
case REACT_CACHE_TYPE:
if (enableCache) {
return createFiberFromCache(pendingProps, mode, lanes, key);
}
// eslint-disable-next-line no-fallthrough
default: {
if (typeof type === 'object' && type !== null) {
switch (type.$$typeof) {
Expand Down Expand Up @@ -745,6 +753,24 @@ export function createFiberFromLegacyHidden(
return fiber;
}

export function createFiberFromCache(
pendingProps: any,
mode: TypeOfMode,
lanes: Lanes,
key: null | string,
) {
const fiber = createFiber(CacheComponent, pendingProps, key, mode);
// TODO: The Cache fiber shouldn't have a type. It has a tag.
// This needs to be fixed in getComponentName so that it relies on the tag
// instead.
if (__DEV__) {
fiber.type = REACT_CACHE_TYPE;
}
fiber.elementType = REACT_CACHE_TYPE;
fiber.lanes = lanes;
return fiber;
}

export function createFiberFromText(
content: string,
mode: TypeOfMode,
Expand Down
Loading