Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] Add getCacheForType() to the dispatcher #20315

Merged
merged 13 commits into from
Dec 3, 2020
6 changes: 5 additions & 1 deletion packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type PartialRenderer from './ReactPartialRenderer';
import {validateContextBounds} from './ReactPartialRendererContext';

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

type BasicStateAction<S> = (S => S) | S;
Expand Down Expand Up @@ -496,7 +497,6 @@ export function setCurrentPartialRenderer(renderer: PartialRenderer) {
}

export const Dispatcher: DispatcherType = {
getCacheForType,
readContext,
useContext,
useMemo,
Expand All @@ -517,3 +517,7 @@ export const Dispatcher: DispatcherType = {
// Subscriptions are not setup in a server environment.
useMutableSource,
};

if (enableCache) {
Dispatcher.getCacheForType = getCacheForType;
}
56 changes: 31 additions & 25 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
enableDebugTracing,
enableSchedulingProfiler,
enableNewReconciler,
enableCache,
decoupleUpdatePriorityFromScheduler,
enableUseRefAccessWarning,
} from 'shared/ReactFeatureFlags';
Expand Down Expand Up @@ -1820,7 +1821,6 @@ function getCacheForType<T>(resourceType: () => T): T {
}

export const ContextOnlyDispatcher: Dispatcher = {
getCacheForType,
readContext,

useCallback: throwInvalidHookError,
Expand All @@ -1840,9 +1840,11 @@ export const ContextOnlyDispatcher: Dispatcher = {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
ContextOnlyDispatcher.getCacheForType = getCacheForType;
}

const HooksDispatcherOnMount: Dispatcher = {
getCacheForType,
readContext,

useCallback: mountCallback,
Expand All @@ -1864,7 +1866,6 @@ const HooksDispatcherOnMount: Dispatcher = {
};

const HooksDispatcherOnUpdate: Dispatcher = {
getCacheForType,
readContext,

useCallback: updateCallback,
Expand All @@ -1884,9 +1885,11 @@ const HooksDispatcherOnUpdate: Dispatcher = {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnUpdate.getCacheForType = getCacheForType;
}

const HooksDispatcherOnRerender: Dispatcher = {
getCacheForType,
readContext,

useCallback: updateCallback,
Expand All @@ -1906,6 +1909,9 @@ const HooksDispatcherOnRerender: Dispatcher = {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnRerender.getCacheForType = getCacheForType;
}

let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
Expand Down Expand Up @@ -1935,9 +1941,6 @@ if (__DEV__) {
};

HooksDispatcherOnMountInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2063,11 +2066,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnMountInDEV.getCacheForType = getCacheForType;
}

HooksDispatcherOnMountWithHookTypesInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2188,11 +2191,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnMountWithHookTypesInDEV.getCacheForType = getCacheForType;
}

HooksDispatcherOnUpdateInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2313,11 +2316,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnUpdateInDEV.getCacheForType = getCacheForType;
}

HooksDispatcherOnRerenderInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2439,11 +2442,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnRerenderInDEV.getCacheForType = getCacheForType;
}

InvalidNestedHooksDispatcherOnMountInDEV = {
gaearon marked this conversation as resolved.
Show resolved Hide resolved
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2579,11 +2582,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
InvalidNestedHooksDispatcherOnMountInDEV.getCacheForType = getCacheForType;
}

InvalidNestedHooksDispatcherOnUpdateInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2719,11 +2722,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
InvalidNestedHooksDispatcherOnUpdateInDEV.getCacheForType = getCacheForType;
}

InvalidNestedHooksDispatcherOnRerenderInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2860,4 +2863,7 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
InvalidNestedHooksDispatcherOnRerenderInDEV.getCacheForType = getCacheForType;
}
}
59 changes: 34 additions & 25 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
enableDebugTracing,
enableSchedulingProfiler,
enableNewReconciler,
enableCache,
decoupleUpdatePriorityFromScheduler,
enableUseRefAccessWarning,
} from 'shared/ReactFeatureFlags';
Expand Down Expand Up @@ -1820,7 +1821,6 @@ function getCacheForType<T>(resourceType: () => T): T {
}

export const ContextOnlyDispatcher: Dispatcher = {
getCacheForType,
readContext,

useCallback: throwInvalidHookError,
Expand All @@ -1840,9 +1840,11 @@ export const ContextOnlyDispatcher: Dispatcher = {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
ContextOnlyDispatcher.getCacheForType = getCacheForType;
}

const HooksDispatcherOnMount: Dispatcher = {
getCacheForType,
readContext,

useCallback: mountCallback,
Expand All @@ -1862,9 +1864,11 @@ const HooksDispatcherOnMount: Dispatcher = {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnMount.getCacheForType = getCacheForType;
}

const HooksDispatcherOnUpdate: Dispatcher = {
getCacheForType,
readContext,

useCallback: updateCallback,
Expand All @@ -1884,9 +1888,11 @@ const HooksDispatcherOnUpdate: Dispatcher = {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnUpdate.getCacheForType = getCacheForType;
}

const HooksDispatcherOnRerender: Dispatcher = {
getCacheForType,
readContext,

useCallback: updateCallback,
Expand All @@ -1906,6 +1912,9 @@ const HooksDispatcherOnRerender: Dispatcher = {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnRerender.getCacheForType = getCacheForType;
}

let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
Expand Down Expand Up @@ -1935,9 +1944,6 @@ if (__DEV__) {
};

HooksDispatcherOnMountInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2063,11 +2069,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnMountInDEV.getCacheForType = getCacheForType;
}

HooksDispatcherOnMountWithHookTypesInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2188,11 +2194,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnMountWithHookTypesInDEV.getCacheForType = getCacheForType;
}

HooksDispatcherOnUpdateInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2313,11 +2319,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnUpdateInDEV.getCacheForType = getCacheForType;
}

HooksDispatcherOnRerenderInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2439,11 +2445,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
HooksDispatcherOnRerenderInDEV.getCacheForType = getCacheForType;
}

InvalidNestedHooksDispatcherOnMountInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2579,11 +2585,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
InvalidNestedHooksDispatcherOnMountInDEV.getCacheForType = getCacheForType;
}

InvalidNestedHooksDispatcherOnUpdateInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2719,11 +2725,11 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
InvalidNestedHooksDispatcherOnUpdateInDEV.getCacheForType = getCacheForType;
}

InvalidNestedHooksDispatcherOnRerenderInDEV = {
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down Expand Up @@ -2860,4 +2866,7 @@ if (__DEV__) {

unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
InvalidNestedHooksDispatcherOnRerenderInDEV.getCacheForType = getCacheForType;
}
}
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ type BasicStateAction<S> = (S => S) | S;
type Dispatch<A> = A => void;

export type Dispatcher = {|
getCacheForType<T>(resourceType: () => T): T,
getCacheForType?: <T>(resourceType: () => T) => T,
readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function resolveDispatcher() {

export function getCacheForType<T>(resourceType: () => T): T {
const dispatcher = resolveDispatcher();
// $FlowFixMe This is unstable, thus optional
return dispatcher.getCacheForType(resourceType);
}

Expand Down
1 change: 1 addition & 0 deletions packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const enableSelectiveHydration = __EXPERIMENTAL__;

// Flight experiments
export const enableLazyElements = __EXPERIMENTAL__;
export const enableCache = __EXPERIMENTAL__;

// Only used in www builds.
export const enableSchedulerDebugging = false;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.native-fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const enableSchedulerTracing = __PROFILE__;
export const enableSuspenseServerRenderer = false;
export const enableSelectiveHydration = false;
export const enableLazyElements = false;
export const enableCache = false;
export const enableSchedulerDebugging = false;
export const debugRenderPhaseSideEffectsForStrictMode = true;
export const disableJavaScriptURLs = false;
Expand Down
Loading