Skip to content

Commit 9e67e7a

Browse files
Scaffolding for useMemoCache hook (#25123)
* Scaffolding for useMemoCache hook * cleanup leftovers from copy/paste of use() diff Co-authored-by: Andrew Clark <git@andrewclark.io>
1 parent e25648b commit 9e67e7a

21 files changed

+143
-1
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
enableLazyContextPropagation,
3333
enableUseMutableSource,
3434
enableTransitionTracing,
35+
enableUseMemoCacheHook,
3536
} from 'shared/ReactFeatureFlags';
3637

3738
import {
@@ -721,6 +722,10 @@ function createFunctionComponentUpdateQueue(): FunctionComponentUpdateQueue {
721722
};
722723
}
723724

725+
function useMemoCache(size: number): Array<any> {
726+
throw new Error('Not implemented.');
727+
}
728+
724729
function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
725730
// $FlowFixMe: Flow doesn't like mixed types
726731
return typeof action === 'function' ? action(state) : action;
@@ -2416,6 +2421,9 @@ if (enableCache) {
24162421
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
24172422
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
24182423
}
2424+
if (enableUseMemoCacheHook) {
2425+
(ContextOnlyDispatcher: Dispatcher).useMemoCache = throwInvalidHookError;
2426+
}
24192427

24202428
const HooksDispatcherOnMount: Dispatcher = {
24212429
readContext,
@@ -2444,6 +2452,9 @@ if (enableCache) {
24442452
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
24452453
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
24462454
}
2455+
if (enableUseMemoCacheHook) {
2456+
(HooksDispatcherOnMount: Dispatcher).useMemoCache = useMemoCache;
2457+
}
24472458
const HooksDispatcherOnUpdate: Dispatcher = {
24482459
readContext,
24492460

@@ -2471,6 +2482,9 @@ if (enableCache) {
24712482
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
24722483
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
24732484
}
2485+
if (enableUseMemoCacheHook) {
2486+
(HooksDispatcherOnUpdate: Dispatcher).useMemoCache = useMemoCache;
2487+
}
24742488

24752489
const HooksDispatcherOnRerender: Dispatcher = {
24762490
readContext,
@@ -2499,6 +2513,9 @@ if (enableCache) {
24992513
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
25002514
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
25012515
}
2516+
if (enableUseMemoCacheHook) {
2517+
(HooksDispatcherOnRerender: Dispatcher).useMemoCache = useMemoCache;
2518+
}
25022519

25032520
let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
25042521
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
@@ -2674,6 +2691,9 @@ if (__DEV__) {
26742691
return mountRefresh();
26752692
};
26762693
}
2694+
if (enableUseMemoCacheHook) {
2695+
(HooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = useMemoCache;
2696+
}
26772697

26782698
HooksDispatcherOnMountWithHookTypesInDEV = {
26792699
readContext<T>(context: ReactContext<T>): T {
@@ -2816,6 +2836,9 @@ if (__DEV__) {
28162836
return mountRefresh();
28172837
};
28182838
}
2839+
if (enableUseMemoCacheHook) {
2840+
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useMemoCache = useMemoCache;
2841+
}
28192842

28202843
HooksDispatcherOnUpdateInDEV = {
28212844
readContext<T>(context: ReactContext<T>): T {
@@ -2958,6 +2981,9 @@ if (__DEV__) {
29582981
return updateRefresh();
29592982
};
29602983
}
2984+
if (enableUseMemoCacheHook) {
2985+
(HooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = useMemoCache;
2986+
}
29612987

29622988
HooksDispatcherOnRerenderInDEV = {
29632989
readContext<T>(context: ReactContext<T>): T {
@@ -3101,6 +3127,9 @@ if (__DEV__) {
31013127
return updateRefresh();
31023128
};
31033129
}
3130+
if (enableUseMemoCacheHook) {
3131+
(HooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = useMemoCache;
3132+
}
31043133

31053134
InvalidNestedHooksDispatcherOnMountInDEV = {
31063135
readContext<T>(context: ReactContext<T>): T {
@@ -3260,6 +3289,14 @@ if (__DEV__) {
32603289
return mountRefresh();
32613290
};
32623291
}
3292+
if (enableUseMemoCacheHook) {
3293+
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
3294+
size: number,
3295+
): Array<any> {
3296+
warnInvalidHookAccess();
3297+
return useMemoCache(size);
3298+
};
3299+
}
32633300

32643301
InvalidNestedHooksDispatcherOnUpdateInDEV = {
32653302
readContext<T>(context: ReactContext<T>): T {
@@ -3419,6 +3456,14 @@ if (__DEV__) {
34193456
return updateRefresh();
34203457
};
34213458
}
3459+
if (enableUseMemoCacheHook) {
3460+
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
3461+
size: number,
3462+
): Array<any> {
3463+
warnInvalidHookAccess();
3464+
return useMemoCache(size);
3465+
};
3466+
}
34223467

34233468
InvalidNestedHooksDispatcherOnRerenderInDEV = {
34243469
readContext<T>(context: ReactContext<T>): T {
@@ -3579,4 +3624,12 @@ if (__DEV__) {
35793624
return updateRefresh();
35803625
};
35813626
}
3627+
if (enableUseMemoCacheHook) {
3628+
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
3629+
size: number,
3630+
): Array<any> {
3631+
warnInvalidHookAccess();
3632+
return useMemoCache(size);
3633+
};
3634+
}
35823635
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
enableLazyContextPropagation,
3333
enableUseMutableSource,
3434
enableTransitionTracing,
35+
enableUseMemoCacheHook,
3536
} from 'shared/ReactFeatureFlags';
3637

3738
import {
@@ -721,6 +722,10 @@ function createFunctionComponentUpdateQueue(): FunctionComponentUpdateQueue {
721722
};
722723
}
723724

725+
function useMemoCache(size: number): Array<any> {
726+
throw new Error('Not implemented.');
727+
}
728+
724729
function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
725730
// $FlowFixMe: Flow doesn't like mixed types
726731
return typeof action === 'function' ? action(state) : action;
@@ -2416,6 +2421,9 @@ if (enableCache) {
24162421
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
24172422
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
24182423
}
2424+
if (enableUseMemoCacheHook) {
2425+
(ContextOnlyDispatcher: Dispatcher).useMemoCache = throwInvalidHookError;
2426+
}
24192427

24202428
const HooksDispatcherOnMount: Dispatcher = {
24212429
readContext,
@@ -2444,6 +2452,9 @@ if (enableCache) {
24442452
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
24452453
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
24462454
}
2455+
if (enableUseMemoCacheHook) {
2456+
(HooksDispatcherOnMount: Dispatcher).useMemoCache = useMemoCache;
2457+
}
24472458
const HooksDispatcherOnUpdate: Dispatcher = {
24482459
readContext,
24492460

@@ -2471,6 +2482,9 @@ if (enableCache) {
24712482
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
24722483
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
24732484
}
2485+
if (enableUseMemoCacheHook) {
2486+
(HooksDispatcherOnUpdate: Dispatcher).useMemoCache = useMemoCache;
2487+
}
24742488

24752489
const HooksDispatcherOnRerender: Dispatcher = {
24762490
readContext,
@@ -2499,6 +2513,9 @@ if (enableCache) {
24992513
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
25002514
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
25012515
}
2516+
if (enableUseMemoCacheHook) {
2517+
(HooksDispatcherOnRerender: Dispatcher).useMemoCache = useMemoCache;
2518+
}
25022519

25032520
let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
25042521
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
@@ -2674,6 +2691,9 @@ if (__DEV__) {
26742691
return mountRefresh();
26752692
};
26762693
}
2694+
if (enableUseMemoCacheHook) {
2695+
(HooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = useMemoCache;
2696+
}
26772697

26782698
HooksDispatcherOnMountWithHookTypesInDEV = {
26792699
readContext<T>(context: ReactContext<T>): T {
@@ -2816,6 +2836,9 @@ if (__DEV__) {
28162836
return mountRefresh();
28172837
};
28182838
}
2839+
if (enableUseMemoCacheHook) {
2840+
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useMemoCache = useMemoCache;
2841+
}
28192842

28202843
HooksDispatcherOnUpdateInDEV = {
28212844
readContext<T>(context: ReactContext<T>): T {
@@ -2958,6 +2981,9 @@ if (__DEV__) {
29582981
return updateRefresh();
29592982
};
29602983
}
2984+
if (enableUseMemoCacheHook) {
2985+
(HooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = useMemoCache;
2986+
}
29612987

29622988
HooksDispatcherOnRerenderInDEV = {
29632989
readContext<T>(context: ReactContext<T>): T {
@@ -3101,6 +3127,9 @@ if (__DEV__) {
31013127
return updateRefresh();
31023128
};
31033129
}
3130+
if (enableUseMemoCacheHook) {
3131+
(HooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = useMemoCache;
3132+
}
31043133

31053134
InvalidNestedHooksDispatcherOnMountInDEV = {
31063135
readContext<T>(context: ReactContext<T>): T {
@@ -3260,6 +3289,14 @@ if (__DEV__) {
32603289
return mountRefresh();
32613290
};
32623291
}
3292+
if (enableUseMemoCacheHook) {
3293+
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
3294+
size: number,
3295+
): Array<any> {
3296+
warnInvalidHookAccess();
3297+
return useMemoCache(size);
3298+
};
3299+
}
32633300

32643301
InvalidNestedHooksDispatcherOnUpdateInDEV = {
32653302
readContext<T>(context: ReactContext<T>): T {
@@ -3419,6 +3456,14 @@ if (__DEV__) {
34193456
return updateRefresh();
34203457
};
34213458
}
3459+
if (enableUseMemoCacheHook) {
3460+
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
3461+
size: number,
3462+
): Array<any> {
3463+
warnInvalidHookAccess();
3464+
return useMemoCache(size);
3465+
};
3466+
}
34223467

34233468
InvalidNestedHooksDispatcherOnRerenderInDEV = {
34243469
readContext<T>(context: ReactContext<T>): T {
@@ -3579,4 +3624,12 @@ if (__DEV__) {
35793624
return updateRefresh();
35803625
};
35813626
}
3627+
if (enableUseMemoCacheHook) {
3628+
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
3629+
size: number,
3630+
): Array<any> {
3631+
warnInvalidHookAccess();
3632+
return useMemoCache(size);
3633+
};
3634+
}
35823635
}

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ export type Dispatcher = {|
403403
): T,
404404
useId(): string,
405405
useCacheRefresh?: () => <T>(?() => T, ?T) => void,
406+
useMemoCache?: (size: number) => Array<any>,
406407

407408
unstable_isNewReconciler?: boolean,
408409
|};

packages/react-server/src/ReactFizzHooks.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {getTreeId} from './ReactFizzTreeContext';
2525

2626
import {makeId} from './ReactServerFormatConfig';
2727

28-
import {enableCache} from 'shared/ReactFeatureFlags';
28+
import {enableCache, enableUseMemoCacheHook} from 'shared/ReactFeatureFlags';
2929
import is from 'shared/objectIs';
3030

3131
type BasicStateAction<S> = (S => S) | S;
@@ -537,6 +537,10 @@ function useCacheRefresh(): <T>(?() => T, ?T) => void {
537537
return unsupportedRefresh;
538538
}
539539

540+
function useMemoCache(size: number): Array<any> {
541+
return new Array(size);
542+
}
543+
540544
function noop(): void {}
541545

542546
export const Dispatcher: DispatcherType = {
@@ -567,6 +571,9 @@ if (enableCache) {
567571
Dispatcher.getCacheForType = getCacheForType;
568572
Dispatcher.useCacheRefresh = useCacheRefresh;
569573
}
574+
if (enableUseMemoCacheHook) {
575+
Dispatcher.useMemoCache = useMemoCache;
576+
}
570577

571578
export let currentResponseState: null | ResponseState = (null: any);
572579
export function setCurrentResponseState(

packages/react-server/src/ReactFlightHooks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export const Dispatcher: DispatcherType = {
7878
useCacheRefresh(): <T>(?() => T, ?T) => void {
7979
return unsupportedRefresh;
8080
},
81+
useMemoCache(size: number): Array<any> {
82+
return new Array(size);
83+
},
8184
};
8285

8386
function unsupportedHook(): void {

packages/react-suspense-test-utils/src/ReactSuspenseTestUtils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function waitForSuspense<T>(fn: () => T): Promise<T> {
4646
useMutableSource: unsupported,
4747
useSyncExternalStore: unsupported,
4848
useCacheRefresh: unsupported,
49+
useMemoCache: unsupported,
4950
};
5051
// Not using async/await because we don't compile it.
5152
return new Promise((resolve, reject) => {

packages/react/index.classic.fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export {
4242
unstable_getCacheSignal,
4343
unstable_getCacheForType,
4444
unstable_useCacheRefresh,
45+
unstable_useMemoCache,
4546
useId,
4647
useCallback,
4748
useContext,

packages/react/index.experimental.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export {
3535
unstable_getCacheSignal,
3636
unstable_getCacheForType,
3737
unstable_useCacheRefresh,
38+
unstable_useMemoCache,
3839
useId,
3940
useCallback,
4041
useContext,

packages/react/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export {
6363
unstable_getCacheSignal,
6464
unstable_getCacheForType,
6565
unstable_useCacheRefresh,
66+
unstable_useMemoCache,
6667
useId,
6768
useCallback,
6869
useContext,

packages/react/index.modern.fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export {
4040
unstable_getCacheSignal,
4141
unstable_getCacheForType,
4242
unstable_useCacheRefresh,
43+
unstable_useMemoCache,
4344
useId,
4445
useCallback,
4546
useContext,

packages/react/src/React.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
useDeferredValue,
5656
useId,
5757
useCacheRefresh,
58+
useMemoCache,
5859
} from './ReactHooks';
5960
import {
6061
createElementWithValidation,
@@ -127,6 +128,7 @@ export {
127128
getCacheForType as unstable_getCacheForType,
128129
useCacheRefresh as unstable_useCacheRefresh,
129130
REACT_CACHE_TYPE as unstable_Cache,
131+
useMemoCache as unstable_useMemoCache,
130132
// enableScopeAPI
131133
REACT_SCOPE_TYPE as unstable_Scope,
132134
// enableTransitionTracing

packages/react/src/ReactHooks.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,9 @@ export function useCacheRefresh(): <T>(?() => T, ?T) => void {
204204
// $FlowFixMe This is unstable, thus optional
205205
return dispatcher.useCacheRefresh();
206206
}
207+
208+
export function useMemoCache(size: number): Array<any> {
209+
const dispatcher = resolveDispatcher();
210+
// $FlowFixMe This is unstable, thus optional
211+
return dispatcher.useMemoCache(size);
212+
}

0 commit comments

Comments
 (0)