Skip to content

Commit

Permalink
Expose unstable_useCacheRefresh but without seeded argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Oct 24, 2022
1 parent b9cbd3b commit 47d2f15
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
15 changes: 12 additions & 3 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
enableUseHook,
enableUseMemoCacheHook,
enableUseEventHook,
enableLegacyCache,
} from 'shared/ReactFeatureFlags';
import {
REACT_CONTEXT_TYPE,
Expand Down Expand Up @@ -2357,9 +2358,17 @@ function refreshCache<T>(fiber: Fiber, seedKey: ?() => T, seedValue: T) {
// unmount that boundary before the refresh completes.
const seededCache = createCache();
if (seedKey !== null && seedKey !== undefined && root !== null) {
// Seed the cache with the value passed by the caller. This could be
// from a server mutation, or it could be a streaming response.
seededCache.data.set(seedKey, seedValue);
if (enableLegacyCache) {
// Seed the cache with the value passed by the caller. This could be
// from a server mutation, or it could be a streaming response.
seededCache.data.set(seedKey, seedValue);
} else {
if (__DEV__) {
console.error(
'The seed argument is not enabled outside experimental channels.',
);
}
}
}

const payload = {
Expand Down
15 changes: 12 additions & 3 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
enableUseHook,
enableUseMemoCacheHook,
enableUseEventHook,
enableLegacyCache,
} from 'shared/ReactFeatureFlags';
import {
REACT_CONTEXT_TYPE,
Expand Down Expand Up @@ -2357,9 +2358,17 @@ function refreshCache<T>(fiber: Fiber, seedKey: ?() => T, seedValue: T) {
// unmount that boundary before the refresh completes.
const seededCache = createCache();
if (seedKey !== null && seedKey !== undefined && root !== null) {
// Seed the cache with the value passed by the caller. This could be
// from a server mutation, or it could be a streaming response.
seededCache.data.set(seedKey, seedValue);
if (enableLegacyCache) {
// Seed the cache with the value passed by the caller. This could be
// from a server mutation, or it could be a streaming response.
seededCache.data.set(seedKey, seedValue);
} else {
if (__DEV__) {
console.error(
'The seed argument is not enabled outside experimental channels.',
);
}
}
}

const payload = {
Expand Down
47 changes: 25 additions & 22 deletions packages/react-reconciler/src/__tests__/ReactCache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('ReactCache', () => {
});

function readText(text) {
const signal = getCacheSignal();
const signal = getCacheSignal ? getCacheSignal() : null;
const textCache = getTextCache();
const record = textCache.data.get(text);
if (record !== undefined) {
Expand All @@ -94,11 +94,13 @@ describe('ReactCache', () => {
// schedule a cleanup function for it.
// TODO: Add ability to cleanup entries seeded w useCacheRefresh()
record.cleanupScheduled = true;
signal.addEventListener('abort', () => {
Scheduler.unstable_yieldValue(
`Cache cleanup: ${text} [v${textCache.version}]`,
);
});
if (getCacheSignal) {
signal.addEventListener('abort', () => {
Scheduler.unstable_yieldValue(
`Cache cleanup: ${text} [v${textCache.version}]`,
);
});
}
}
switch (record.status) {
case 'pending':
Expand Down Expand Up @@ -140,11 +142,13 @@ describe('ReactCache', () => {
};
textCache.data.set(text, newRecord);

signal.addEventListener('abort', () => {
Scheduler.unstable_yieldValue(
`Cache cleanup: ${text} [v${textCache.version}]`,
);
});
if (getCacheSignal) {
signal.addEventListener('abort', () => {
Scheduler.unstable_yieldValue(
`Cache cleanup: ${text} [v${textCache.version}]`,
);
});
}
throw thenable;
}
}
Expand Down Expand Up @@ -216,7 +220,7 @@ describe('ReactCache', () => {
expect(root).toMatchRenderedOutput('Bye');
});

// @gate enableCacheElement && enableCache
// @gate enableCache
test('root acts as implicit cache boundary', async () => {
const root = ReactNoop.createRoot();
await act(async () => {
Expand Down Expand Up @@ -662,7 +666,7 @@ describe('ReactCache', () => {
expect(root).toMatchRenderedOutput('Bye');
});

// @gate enableCacheElement && enableCache
// @gate enableCache
test('refresh a cache boundary', async () => {
let refresh;
function App() {
Expand All @@ -674,11 +678,9 @@ describe('ReactCache', () => {
const root = ReactNoop.createRoot();
await act(async () => {
root.render(
<Cache>
<Suspense fallback={<Text text="Loading..." />}>
<App />
</Suspense>
</Cache>,
<Suspense fallback={<Text text="Loading..." />}>
<App />
</Suspense>,
);
});
expect(Scheduler).toHaveYielded(['Cache miss! [A]', 'Loading...']);
Expand All @@ -701,15 +703,16 @@ describe('ReactCache', () => {
resolveMostRecentTextCache('A');
});
// Note that the version has updated
expect(Scheduler).toHaveYielded(['A [v2]']);
if (getCacheSignal) {
expect(Scheduler).toHaveYielded(['A [v2]', 'Cache cleanup: A [v1]']);
} else {
expect(Scheduler).toHaveYielded(['A [v2]']);
}
expect(root).toMatchRenderedOutput('A [v2]');

await act(async () => {
root.render('Bye');
});
// the original cache instance does not cleanup since it is still referenced
// by the root, but the refreshed inner cache does cleanup
expect(Scheduler).toHaveYielded(['Cache cleanup: A [v2]']);
expect(root).toMatchRenderedOutput('Bye');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/react/index.stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export {
lazy,
memo,
cache,
unstable_useCacheRefresh,
startTransition,
useId,
useCallback,
useContext,
useDebugValue,
useDeferredValue,
useEffect,
experimental_useEvent,
useImperativeHandle,
useInsertionEffect,
useLayoutEffect,
Expand Down

0 comments on commit 47d2f15

Please sign in to comment.