@@ -31,6 +31,7 @@ import {
31
31
enableUseRefAccessWarning ,
32
32
} from 'shared/ReactFeatureFlags' ;
33
33
34
+ import { HostRoot } from './ReactWorkTags' ;
34
35
import { NoMode , BlockingMode , DebugTracingMode } from './ReactTypeOfMode' ;
35
36
import {
36
37
NoLane ,
@@ -94,6 +95,7 @@ import {getIsRendering} from './ReactCurrentFiber';
94
95
import { logStateUpdateScheduled } from './DebugTracing' ;
95
96
import { markStateUpdateScheduled } from './SchedulingProfiler' ;
96
97
import { CacheContext } from './ReactFiberCacheComponent' ;
98
+ import { createUpdate , enqueueUpdate } from './ReactUpdateQueue.new' ;
97
99
98
100
const { ReactCurrentDispatcher, ReactCurrentBatchConfig} = ReactSharedInternals ;
99
101
@@ -1741,12 +1743,31 @@ function refreshCache<T>(
1741
1743
try {
1742
1744
const eventTime = requestEventTime ( ) ;
1743
1745
const lane = requestUpdateLane ( provider ) ;
1746
+ // TODO: Does Cache work in legacy mode? Should decide and write a test.
1744
1747
const root = scheduleUpdateOnFiber ( provider , lane , eventTime ) ;
1748
+
1749
+ let seededCache = null ;
1745
1750
if ( seedKey !== null && seedKey !== undefined && root !== null ) {
1746
1751
// TODO: Warn if wrong type
1747
- const seededCache = new Map ( [ [ seedKey , seedValue ] ] ) ;
1752
+ seededCache = new Map ( [ [ seedKey , seedValue ] ] ) ;
1748
1753
transferCacheToSpawnedLane ( root , seededCache , lane ) ;
1749
1754
}
1755
+
1756
+ if ( provider . tag === HostRoot ) {
1757
+ const refreshUpdate = createUpdate ( eventTime , lane ) ;
1758
+ refreshUpdate . payload = {
1759
+ cacheInstance : {
1760
+ provider : provider ,
1761
+ cache :
1762
+ // For the root cache, we won't bother to lazily initialize the
1763
+ // map. Seed an empty one. This saves use the trouble of having
1764
+ // to use an updater function. Maybe we should use this approach
1765
+ // for non-root refreshes, too.
1766
+ seededCache !== null ? seededCache : new Map ( ) ,
1767
+ } ,
1768
+ } ;
1769
+ enqueueUpdate ( provider , refreshUpdate ) ;
1770
+ }
1750
1771
} finally {
1751
1772
ReactCurrentBatchConfig . transition = prevTransition ;
1752
1773
}
@@ -1869,9 +1890,7 @@ function getCacheForType<T>(resourceType: () => T): T {
1869
1890
const cacheInstance : CacheInstance | null = readContext ( CacheContext ) ;
1870
1891
invariant (
1871
1892
cacheInstance !== null ,
1872
- 'Tried to fetch data, but no cache was found. To fix, wrap your ' +
1873
- "component in a <Cache /> boundary. It doesn't need to be a direct " +
1874
- 'parent; it can be anywhere in the ancestor path' ,
1893
+ 'Internal React error: Should always have a cache.' ,
1875
1894
) ;
1876
1895
let cache = cacheInstance . cache ;
1877
1896
if ( cache === null ) {
0 commit comments