Skip to content

Commit bba8139

Browse files
committed
Make CacheContext type non-nullable
Now that the root is a cache provider, there will always be a cache. Don't need the null check.
1 parent 10ab997 commit bba8139

File tree

5 files changed

+92
-115
lines changed

5 files changed

+92
-115
lines changed

packages/react-reconciler/src/ReactFiberBeginWork.new.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ function updateCacheComponent(
667667
// Read directly from the context. We don't set up a context dependency
668668
// because the propagation function automatically includes CacheComponents in
669669
// its search.
670-
const parentCacheInstance: CacheInstance | null = isPrimaryRenderer
670+
const parentCacheInstance: CacheInstance = isPrimaryRenderer
671671
? CacheContext._currentValue
672672
: CacheContext._currentValue2;
673673

@@ -684,10 +684,7 @@ function updateCacheComponent(
684684
// This may be the same as the parent cache, like if the current render
685685
// spawned from a previous render that already committed. Otherwise, this
686686
// is the root of a cache consistency boundary.
687-
if (
688-
parentCacheInstance === null ||
689-
freshCache !== parentCacheInstance.cache
690-
) {
687+
if (freshCache !== parentCacheInstance.cache) {
691688
ownCacheInstance = {
692689
cache: freshCache,
693690
provider: workInProgress,

packages/react-reconciler/src/ReactFiberBeginWork.old.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ function updateCacheComponent(
667667
// Read directly from the context. We don't set up a context dependency
668668
// because the propagation function automatically includes CacheComponents in
669669
// its search.
670-
const parentCacheInstance: CacheInstance | null = isPrimaryRenderer
670+
const parentCacheInstance: CacheInstance = isPrimaryRenderer
671671
? CacheContext._currentValue
672672
: CacheContext._currentValue2;
673673

@@ -684,10 +684,7 @@ function updateCacheComponent(
684684
// This may be the same as the parent cache, like if the current render
685685
// spawned from a previous render that already committed. Otherwise, this
686686
// is the root of a cache consistency boundary.
687-
if (
688-
parentCacheInstance === null ||
689-
freshCache !== parentCacheInstance.cache
690-
) {
687+
if (freshCache !== parentCacheInstance.cache) {
691688
ownCacheInstance = {
692689
cache: freshCache,
693690
provider: workInProgress,

packages/react-reconciler/src/ReactFiberCacheComponent.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ export type CacheInstance = {|
1818
provider: Fiber,
1919
|};
2020

21-
export const CacheContext: ReactContext<CacheInstance | null> = {
21+
export const CacheContext: ReactContext<CacheInstance> = {
2222
$$typeof: REACT_CONTEXT_TYPE,
2323
// We don't use Consumer/Provider for Cache components. So we'll cheat.
2424
Consumer: (null: any),
2525
Provider: (null: any),
2626
_calculateChangedBits: null,
27-
_currentValue: null,
28-
_currentValue2: null,
27+
// We'll initialize these at the root.
28+
_currentValue: (null: any),
29+
_currentValue2: (null: any),
2930
_threadCount: 0,
3031
};
3132

packages/react-reconciler/src/ReactFiberHooks.new.js

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,67 +1712,62 @@ function rerenderOpaqueIdentifier(): OpaqueIDType | void {
17121712
}
17131713

17141714
function mountRefresh() {
1715-
// TODO: CacheInstance should never be null. Update type.
1716-
const cacheInstance: CacheInstance | null = readContext(CacheContext);
1715+
const cacheInstance: CacheInstance = readContext(CacheContext);
17171716
return mountCallback(refreshCache.bind(null, cacheInstance), [cacheInstance]);
17181717
}
17191718

17201719
function updateRefresh() {
1721-
const cacheInstance: CacheInstance | null = readContext(CacheContext);
1720+
const cacheInstance: CacheInstance = readContext(CacheContext);
17221721
return updateCallback(refreshCache.bind(null, cacheInstance), [
17231722
cacheInstance,
17241723
]);
17251724
}
17261725

17271726
function refreshCache<T>(
1728-
cacheInstance: CacheInstance | null,
1727+
cacheInstance: CacheInstance,
17291728
seedKey: ?() => T,
17301729
seedValue: T,
17311730
) {
1732-
if (cacheInstance !== null) {
1733-
const provider = cacheInstance.provider;
1734-
1735-
// Inlined startTransition
1736-
// TODO: Maybe we shouldn't automatically give this transition priority. Are
1737-
// there valid use cases for a high-pri refresh? Like if the content is
1738-
// super stale and you want to immediately hide it.
1739-
const prevTransition = ReactCurrentBatchConfig.transition;
1740-
ReactCurrentBatchConfig.transition = 1;
1741-
// TODO: Do we really need the try/finally? I don't think any of these
1742-
// functions would ever throw unless there's an internal error.
1743-
try {
1744-
const eventTime = requestEventTime();
1745-
const lane = requestUpdateLane(provider);
1746-
// TODO: Does Cache work in legacy mode? Should decide and write a test.
1747-
const root = scheduleUpdateOnFiber(provider, lane, eventTime);
1748-
1749-
let seededCache = null;
1750-
if (seedKey !== null && seedKey !== undefined && root !== null) {
1751-
// TODO: Warn if wrong type
1752-
seededCache = new Map([[seedKey, seedValue]]);
1753-
transferCacheToSpawnedLane(root, seededCache, lane);
1754-
}
1731+
const provider = cacheInstance.provider;
1732+
1733+
// Inlined startTransition
1734+
// TODO: Maybe we shouldn't automatically give this transition priority. Are
1735+
// there valid use cases for a high-pri refresh? Like if the content is
1736+
// super stale and you want to immediately hide it.
1737+
const prevTransition = ReactCurrentBatchConfig.transition;
1738+
ReactCurrentBatchConfig.transition = 1;
1739+
// TODO: Do we really need the try/finally? I don't think any of these
1740+
// functions would ever throw unless there's an internal error.
1741+
try {
1742+
const eventTime = requestEventTime();
1743+
const lane = requestUpdateLane(provider);
1744+
// TODO: Does Cache work in legacy mode? Should decide and write a test.
1745+
const root = scheduleUpdateOnFiber(provider, lane, eventTime);
1746+
1747+
let seededCache = null;
1748+
if (seedKey !== null && seedKey !== undefined && root !== null) {
1749+
// TODO: Warn if wrong type
1750+
seededCache = new Map([[seedKey, seedValue]]);
1751+
transferCacheToSpawnedLane(root, seededCache, lane);
1752+
}
17551753

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-
}
1771-
} finally {
1772-
ReactCurrentBatchConfig.transition = prevTransition;
1754+
if (provider.tag === HostRoot) {
1755+
const refreshUpdate = createUpdate(eventTime, lane);
1756+
refreshUpdate.payload = {
1757+
cacheInstance: {
1758+
provider: provider,
1759+
cache:
1760+
// For the root cache, we won't bother to lazily initialize the
1761+
// map. Seed an empty one. This saves use the trouble of having
1762+
// to use an updater function. Maybe we should use this approach
1763+
// for non-root refreshes, too.
1764+
seededCache !== null ? seededCache : new Map(),
1765+
},
1766+
};
1767+
enqueueUpdate(provider, refreshUpdate);
17731768
}
1774-
} else {
1775-
// TODO: CacheInstance should never be null. Update type.
1769+
} finally {
1770+
ReactCurrentBatchConfig.transition = prevTransition;
17761771
}
17771772
}
17781773

@@ -1887,11 +1882,7 @@ function dispatchAction<S, A>(
18871882
}
18881883

18891884
function getCacheForType<T>(resourceType: () => T): T {
1890-
const cacheInstance: CacheInstance | null = readContext(CacheContext);
1891-
invariant(
1892-
cacheInstance !== null,
1893-
'Internal React error: Should always have a cache.',
1894-
);
1885+
const cacheInstance: CacheInstance = readContext(CacheContext);
18951886
let cache = cacheInstance.cache;
18961887
if (cache === null) {
18971888
cache = cacheInstance.cache = new Map();

packages/react-reconciler/src/ReactFiberHooks.old.js

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,67 +1783,62 @@ function rerenderOpaqueIdentifier(): OpaqueIDType | void {
17831783
}
17841784

17851785
function mountRefresh() {
1786-
// TODO: CacheInstance should never be null. Update type.
1787-
const cacheInstance: CacheInstance | null = readContext(CacheContext);
1786+
const cacheInstance: CacheInstance = readContext(CacheContext);
17881787
return mountCallback(refreshCache.bind(null, cacheInstance), [cacheInstance]);
17891788
}
17901789

17911790
function updateRefresh() {
1792-
const cacheInstance: CacheInstance | null = readContext(CacheContext);
1791+
const cacheInstance: CacheInstance = readContext(CacheContext);
17931792
return updateCallback(refreshCache.bind(null, cacheInstance), [
17941793
cacheInstance,
17951794
]);
17961795
}
17971796

17981797
function refreshCache<T>(
1799-
cacheInstance: CacheInstance | null,
1798+
cacheInstance: CacheInstance,
18001799
seedKey: ?() => T,
18011800
seedValue: T,
18021801
) {
1803-
if (cacheInstance !== null) {
1804-
const provider = cacheInstance.provider;
1805-
1806-
// Inlined startTransition
1807-
// TODO: Maybe we shouldn't automatically give this transition priority. Are
1808-
// there valid use cases for a high-pri refresh? Like if the content is
1809-
// super stale and you want to immediately hide it.
1810-
const prevTransition = ReactCurrentBatchConfig.transition;
1811-
ReactCurrentBatchConfig.transition = 1;
1812-
// TODO: Do we really need the try/finally? I don't think any of these
1813-
// functions would ever throw unless there's an internal error.
1814-
try {
1815-
const eventTime = requestEventTime();
1816-
const lane = requestUpdateLane(provider);
1817-
// TODO: Does Cache work in legacy mode? Should decide and write a test.
1818-
const root = scheduleUpdateOnFiber(provider, lane, eventTime);
1819-
1820-
let seededCache = null;
1821-
if (seedKey !== null && seedKey !== undefined && root !== null) {
1822-
// TODO: Warn if wrong type
1823-
seededCache = new Map([[seedKey, seedValue]]);
1824-
transferCacheToSpawnedLane(root, seededCache, lane);
1825-
}
1802+
const provider = cacheInstance.provider;
1803+
1804+
// Inlined startTransition
1805+
// TODO: Maybe we shouldn't automatically give this transition priority. Are
1806+
// there valid use cases for a high-pri refresh? Like if the content is
1807+
// super stale and you want to immediately hide it.
1808+
const prevTransition = ReactCurrentBatchConfig.transition;
1809+
ReactCurrentBatchConfig.transition = 1;
1810+
// TODO: Do we really need the try/finally? I don't think any of these
1811+
// functions would ever throw unless there's an internal error.
1812+
try {
1813+
const eventTime = requestEventTime();
1814+
const lane = requestUpdateLane(provider);
1815+
// TODO: Does Cache work in legacy mode? Should decide and write a test.
1816+
const root = scheduleUpdateOnFiber(provider, lane, eventTime);
1817+
1818+
let seededCache = null;
1819+
if (seedKey !== null && seedKey !== undefined && root !== null) {
1820+
// TODO: Warn if wrong type
1821+
seededCache = new Map([[seedKey, seedValue]]);
1822+
transferCacheToSpawnedLane(root, seededCache, lane);
1823+
}
18261824

1827-
if (provider.tag === HostRoot) {
1828-
const refreshUpdate = createUpdate(eventTime, lane);
1829-
refreshUpdate.payload = {
1830-
cacheInstance: {
1831-
provider: provider,
1832-
cache:
1833-
// For the root cache, we won't bother to lazily initialize the
1834-
// map. Seed an empty one. This saves use the trouble of having
1835-
// to use an updater function. Maybe we should use this approach
1836-
// for non-root refreshes, too.
1837-
seededCache !== null ? seededCache : new Map(),
1838-
},
1839-
};
1840-
enqueueUpdate(provider, refreshUpdate);
1841-
}
1842-
} finally {
1843-
ReactCurrentBatchConfig.transition = prevTransition;
1825+
if (provider.tag === HostRoot) {
1826+
const refreshUpdate = createUpdate(eventTime, lane);
1827+
refreshUpdate.payload = {
1828+
cacheInstance: {
1829+
provider: provider,
1830+
cache:
1831+
// For the root cache, we won't bother to lazily initialize the
1832+
// map. Seed an empty one. This saves use the trouble of having
1833+
// to use an updater function. Maybe we should use this approach
1834+
// for non-root refreshes, too.
1835+
seededCache !== null ? seededCache : new Map(),
1836+
},
1837+
};
1838+
enqueueUpdate(provider, refreshUpdate);
18441839
}
1845-
} else {
1846-
// TODO: CacheInstance should never be null. Update type.
1840+
} finally {
1841+
ReactCurrentBatchConfig.transition = prevTransition;
18471842
}
18481843
}
18491844

@@ -1958,11 +1953,7 @@ function dispatchAction<S, A>(
19581953
}
19591954

19601955
function getCacheForType<T>(resourceType: () => T): T {
1961-
const cacheInstance: CacheInstance | null = readContext(CacheContext);
1962-
invariant(
1963-
cacheInstance !== null,
1964-
'Internal React error: Should always have a cache.',
1965-
);
1956+
const cacheInstance: CacheInstance = readContext(CacheContext);
19661957
let cache = cacheInstance.cache;
19671958
if (cache === null) {
19681959
cache = cacheInstance.cache = new Map();

0 commit comments

Comments
 (0)