Skip to content

Commit

Permalink
Scaffolding for useMemoCache hook (#25123)
Browse files Browse the repository at this point in the history
* Scaffolding for useMemoCache hook
* cleanup leftovers from copy/paste of use() diff

Co-authored-by: Andrew Clark <git@andrewclark.io>
  • Loading branch information
2 people authored and rickhanlonii committed Oct 5, 2022
1 parent e25648b commit 2c35dc9
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 1 deletion.
53 changes: 53 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
enableLazyContextPropagation,
enableUseMutableSource,
enableTransitionTracing,
enableUseMemoCacheHook,
} from 'shared/ReactFeatureFlags';

import {
Expand Down Expand Up @@ -721,6 +722,10 @@ function createFunctionComponentUpdateQueue(): FunctionComponentUpdateQueue {
};
}

function useMemoCache(size: number): Array<any> {
throw new Error('Not implemented.');
}

function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
// $FlowFixMe: Flow doesn't like mixed types
return typeof action === 'function' ? action(state) : action;
Expand Down Expand Up @@ -2416,6 +2421,9 @@ if (enableCache) {
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
}
if (enableUseMemoCacheHook) {
(ContextOnlyDispatcher: Dispatcher).useMemoCache = throwInvalidHookError;
}

const HooksDispatcherOnMount: Dispatcher = {
readContext,
Expand Down Expand Up @@ -2444,6 +2452,9 @@ if (enableCache) {
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMount: Dispatcher).useMemoCache = useMemoCache;
}
const HooksDispatcherOnUpdate: Dispatcher = {
readContext,

Expand Down Expand Up @@ -2471,6 +2482,9 @@ if (enableCache) {
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnUpdate: Dispatcher).useMemoCache = useMemoCache;
}

const HooksDispatcherOnRerender: Dispatcher = {
readContext,
Expand Down Expand Up @@ -2499,6 +2513,9 @@ if (enableCache) {
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerender: Dispatcher).useMemoCache = useMemoCache;
}

let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
Expand Down Expand Up @@ -2674,6 +2691,9 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = useMemoCache;
}

HooksDispatcherOnMountWithHookTypesInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -2816,6 +2836,9 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useMemoCache = useMemoCache;
}

HooksDispatcherOnUpdateInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -2958,6 +2981,9 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = useMemoCache;
}

HooksDispatcherOnRerenderInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3101,6 +3127,9 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = useMemoCache;
}

InvalidNestedHooksDispatcherOnMountInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3260,6 +3289,14 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
size: number,
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
}

InvalidNestedHooksDispatcherOnUpdateInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3419,6 +3456,14 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
size: number,
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
}

InvalidNestedHooksDispatcherOnRerenderInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3579,4 +3624,12 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
size: number,
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
}
}
53 changes: 53 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
enableLazyContextPropagation,
enableUseMutableSource,
enableTransitionTracing,
enableUseMemoCacheHook,
} from 'shared/ReactFeatureFlags';

import {
Expand Down Expand Up @@ -721,6 +722,10 @@ function createFunctionComponentUpdateQueue(): FunctionComponentUpdateQueue {
};
}

function useMemoCache(size: number): Array<any> {
throw new Error('Not implemented.');
}

function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
// $FlowFixMe: Flow doesn't like mixed types
return typeof action === 'function' ? action(state) : action;
Expand Down Expand Up @@ -2416,6 +2421,9 @@ if (enableCache) {
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
}
if (enableUseMemoCacheHook) {
(ContextOnlyDispatcher: Dispatcher).useMemoCache = throwInvalidHookError;
}

const HooksDispatcherOnMount: Dispatcher = {
readContext,
Expand Down Expand Up @@ -2444,6 +2452,9 @@ if (enableCache) {
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMount: Dispatcher).useMemoCache = useMemoCache;
}
const HooksDispatcherOnUpdate: Dispatcher = {
readContext,

Expand Down Expand Up @@ -2471,6 +2482,9 @@ if (enableCache) {
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnUpdate: Dispatcher).useMemoCache = useMemoCache;
}

const HooksDispatcherOnRerender: Dispatcher = {
readContext,
Expand Down Expand Up @@ -2499,6 +2513,9 @@ if (enableCache) {
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerender: Dispatcher).useMemoCache = useMemoCache;
}

let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
Expand Down Expand Up @@ -2674,6 +2691,9 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = useMemoCache;
}

HooksDispatcherOnMountWithHookTypesInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -2816,6 +2836,9 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useMemoCache = useMemoCache;
}

HooksDispatcherOnUpdateInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -2958,6 +2981,9 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = useMemoCache;
}

HooksDispatcherOnRerenderInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3101,6 +3127,9 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = useMemoCache;
}

InvalidNestedHooksDispatcherOnMountInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3260,6 +3289,14 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
size: number,
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
}

InvalidNestedHooksDispatcherOnUpdateInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3419,6 +3456,14 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
size: number,
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
}

InvalidNestedHooksDispatcherOnRerenderInDEV = {
readContext<T>(context: ReactContext<T>): T {
Expand Down Expand Up @@ -3579,4 +3624,12 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
size: number,
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
}
}
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ export type Dispatcher = {|
): T,
useId(): string,
useCacheRefresh?: () => <T>(?() => T, ?T) => void,
useMemoCache?: (size: number) => Array<any>,

unstable_isNewReconciler?: boolean,
|};
9 changes: 8 additions & 1 deletion packages/react-server/src/ReactFizzHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {getTreeId} from './ReactFizzTreeContext';

import {makeId} from './ReactServerFormatConfig';

import {enableCache} from 'shared/ReactFeatureFlags';
import {enableCache, enableUseMemoCacheHook} from 'shared/ReactFeatureFlags';
import is from 'shared/objectIs';

type BasicStateAction<S> = (S => S) | S;
Expand Down Expand Up @@ -537,6 +537,10 @@ function useCacheRefresh(): <T>(?() => T, ?T) => void {
return unsupportedRefresh;
}

function useMemoCache(size: number): Array<any> {
return new Array(size);
}

function noop(): void {}

export const Dispatcher: DispatcherType = {
Expand Down Expand Up @@ -567,6 +571,9 @@ if (enableCache) {
Dispatcher.getCacheForType = getCacheForType;
Dispatcher.useCacheRefresh = useCacheRefresh;
}
if (enableUseMemoCacheHook) {
Dispatcher.useMemoCache = useMemoCache;
}

export let currentResponseState: null | ResponseState = (null: any);
export function setCurrentResponseState(
Expand Down
3 changes: 3 additions & 0 deletions packages/react-server/src/ReactFlightHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export const Dispatcher: DispatcherType = {
useCacheRefresh(): <T>(?() => T, ?T) => void {
return unsupportedRefresh;
},
useMemoCache(size: number): Array<any> {
return new Array(size);
},
};

function unsupportedHook(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function waitForSuspense<T>(fn: () => T): Promise<T> {
useMutableSource: unsupported,
useSyncExternalStore: unsupported,
useCacheRefresh: unsupported,
useMemoCache: unsupported,
};
// Not using async/await because we don't compile it.
return new Promise((resolve, reject) => {
Expand Down
1 change: 1 addition & 0 deletions packages/react/index.classic.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
unstable_getCacheSignal,
unstable_getCacheForType,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand Down
1 change: 1 addition & 0 deletions packages/react/index.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export {
unstable_getCacheSignal,
unstable_getCacheForType,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand Down
1 change: 1 addition & 0 deletions packages/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export {
unstable_getCacheSignal,
unstable_getCacheForType,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand Down
1 change: 1 addition & 0 deletions packages/react/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export {
unstable_getCacheSignal,
unstable_getCacheForType,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
useDeferredValue,
useId,
useCacheRefresh,
useMemoCache,
} from './ReactHooks';
import {
createElementWithValidation,
Expand Down Expand Up @@ -127,6 +128,7 @@ export {
getCacheForType as unstable_getCacheForType,
useCacheRefresh as unstable_useCacheRefresh,
REACT_CACHE_TYPE as unstable_Cache,
useMemoCache as unstable_useMemoCache,
// enableScopeAPI
REACT_SCOPE_TYPE as unstable_Scope,
// enableTransitionTracing
Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,9 @@ export function useCacheRefresh(): <T>(?() => T, ?T) => void {
// $FlowFixMe This is unstable, thus optional
return dispatcher.useCacheRefresh();
}

export function useMemoCache(size: number): Array<any> {
const dispatcher = resolveDispatcher();
// $FlowFixMe This is unstable, thus optional
return dispatcher.useMemoCache(size);
}
Loading

0 comments on commit 2c35dc9

Please sign in to comment.