Skip to content

Commit

Permalink
Split Cache into its own Dispatcher (#25474)
Browse files Browse the repository at this point in the history
* Missing Hooks

* Remove www forks. These can use __SECRET... instead.

* Move cache to separate dispatcher

These will be available in more contexts than just render.
  • Loading branch information
sebmarkbage authored Oct 13, 2022
1 parent 2cf4352 commit a8c16a0
Show file tree
Hide file tree
Showing 24 changed files with 293 additions and 229 deletions.
5 changes: 0 additions & 5 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ function nextHook(): null | Hook {
return hook;
}

function getCacheForType<T>(resourceType: () => T): T {
throw new Error('Not implemented.');
}

function readContext<T>(context: ReactContext<T>): T {
// For now we don't expose readContext usage in the hooks debugging info.
return context._currentValue;
Expand Down Expand Up @@ -331,7 +327,6 @@ function useId(): string {
}

const Dispatcher: DispatcherType = {
getCacheForType,
readContext,
useCacheRefresh,
useCallback,
Expand Down
41 changes: 41 additions & 0 deletions packages/react-reconciler/src/ReactFiberCache.new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {CacheDispatcher} from './ReactInternalTypes';
import type {Cache} from './ReactFiberCacheComponent.new';

import {enableCache} from 'shared/ReactFeatureFlags';
import {readContext} from './ReactFiberNewContext.new';
import {CacheContext} from './ReactFiberCacheComponent.new';

function getCacheSignal(): AbortSignal {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
return cache.controller.signal;
}

function getCacheForType<T>(resourceType: () => T): T {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
let cacheForType: T | void = (cache.data.get(resourceType): any);
if (cacheForType === undefined) {
cacheForType = resourceType();
cache.data.set(resourceType, cacheForType);
}
return cacheForType;
}

export const DefaultCacheDispatcher: CacheDispatcher = {
getCacheSignal,
getCacheForType,
};
41 changes: 41 additions & 0 deletions packages/react-reconciler/src/ReactFiberCache.old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {CacheDispatcher} from './ReactInternalTypes';
import type {Cache} from './ReactFiberCacheComponent.old';

import {enableCache} from 'shared/ReactFeatureFlags';
import {readContext} from './ReactFiberNewContext.old';
import {CacheContext} from './ReactFiberCacheComponent.old';

function getCacheSignal(): AbortSignal {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
return cache.controller.signal;
}

function getCacheForType<T>(resourceType: () => T): T {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
let cacheForType: T | void = (cache.data.get(resourceType): any);
if (cacheForType === undefined) {
cacheForType = resourceType();
cache.data.set(resourceType, cacheForType);
}
return cacheForType;
}

export const DefaultCacheDispatcher: CacheDispatcher = {
getCacheSignal,
getCacheForType,
};
46 changes: 1 addition & 45 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type {
import type {Lanes, Lane} from './ReactFiberLane.new';
import type {HookFlags} from './ReactHookEffectTags';
import type {FiberRoot} from './ReactInternalTypes';
import type {Cache} from './ReactFiberCacheComponent.new';
import type {Flags} from './ReactFiberFlags';

import ReactSharedInternals from 'shared/ReactSharedInternals';
Expand Down Expand Up @@ -122,7 +121,7 @@ import {
} from './ReactMutableSource.new';
import {logStateUpdateScheduled} from './DebugTracing';
import {markStateUpdateScheduled} from './ReactFiberDevToolsHook.new';
import {createCache, CacheContext} from './ReactFiberCacheComponent.new';
import {createCache} from './ReactFiberCacheComponent.new';
import {
createUpdate as createLegacyQueueUpdate,
enqueueUpdate as enqueueLegacyQueueUpdate,
Expand Down Expand Up @@ -2600,27 +2599,6 @@ function markUpdateInDevTools<A>(fiber, lane, action: A) {
}
}

function getCacheSignal(): AbortSignal {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
return cache.controller.signal;
}

function getCacheForType<T>(resourceType: () => T): T {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
let cacheForType: T | void = (cache.data.get(resourceType): any);
if (cacheForType === undefined) {
cacheForType = resourceType();
cache.data.set(resourceType, cacheForType);
}
return cacheForType;
}

export const ContextOnlyDispatcher: Dispatcher = {
readContext,

Expand All @@ -2644,8 +2622,6 @@ export const ContextOnlyDispatcher: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(ContextOnlyDispatcher: Dispatcher).getCacheSignal = getCacheSignal;
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
}
if (enableUseHook) {
Expand Down Expand Up @@ -2681,8 +2657,6 @@ const HooksDispatcherOnMount: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnMount: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
// $FlowFixMe[escaped-generic] discovered when updating Flow
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
}
Expand Down Expand Up @@ -2718,8 +2692,6 @@ const HooksDispatcherOnUpdate: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnUpdate: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseMemoCacheHook) {
Expand Down Expand Up @@ -2755,8 +2727,6 @@ const HooksDispatcherOnRerender: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnRerender: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseHook) {
Expand Down Expand Up @@ -2935,8 +2905,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
mountHookTypesDev();
Expand Down Expand Up @@ -3094,8 +3062,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3253,8 +3219,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3413,8 +3377,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3589,8 +3551,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
mountHookTypesDev();
Expand Down Expand Up @@ -3776,8 +3736,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3964,8 +3922,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down
46 changes: 1 addition & 45 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type {
import type {Lanes, Lane} from './ReactFiberLane.old';
import type {HookFlags} from './ReactHookEffectTags';
import type {FiberRoot} from './ReactInternalTypes';
import type {Cache} from './ReactFiberCacheComponent.old';
import type {Flags} from './ReactFiberFlags';

import ReactSharedInternals from 'shared/ReactSharedInternals';
Expand Down Expand Up @@ -122,7 +121,7 @@ import {
} from './ReactMutableSource.old';
import {logStateUpdateScheduled} from './DebugTracing';
import {markStateUpdateScheduled} from './ReactFiberDevToolsHook.old';
import {createCache, CacheContext} from './ReactFiberCacheComponent.old';
import {createCache} from './ReactFiberCacheComponent.old';
import {
createUpdate as createLegacyQueueUpdate,
enqueueUpdate as enqueueLegacyQueueUpdate,
Expand Down Expand Up @@ -2600,27 +2599,6 @@ function markUpdateInDevTools<A>(fiber, lane, action: A) {
}
}

function getCacheSignal(): AbortSignal {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
return cache.controller.signal;
}

function getCacheForType<T>(resourceType: () => T): T {
if (!enableCache) {
throw new Error('Not implemented.');
}
const cache: Cache = readContext(CacheContext);
let cacheForType: T | void = (cache.data.get(resourceType): any);
if (cacheForType === undefined) {
cacheForType = resourceType();
cache.data.set(resourceType, cacheForType);
}
return cacheForType;
}

export const ContextOnlyDispatcher: Dispatcher = {
readContext,

Expand All @@ -2644,8 +2622,6 @@ export const ContextOnlyDispatcher: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(ContextOnlyDispatcher: Dispatcher).getCacheSignal = getCacheSignal;
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
}
if (enableUseHook) {
Expand Down Expand Up @@ -2681,8 +2657,6 @@ const HooksDispatcherOnMount: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnMount: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
// $FlowFixMe[escaped-generic] discovered when updating Flow
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
}
Expand Down Expand Up @@ -2718,8 +2692,6 @@ const HooksDispatcherOnUpdate: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnUpdate: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseMemoCacheHook) {
Expand Down Expand Up @@ -2755,8 +2727,6 @@ const HooksDispatcherOnRerender: Dispatcher = {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnRerender: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseHook) {
Expand Down Expand Up @@ -2935,8 +2905,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
mountHookTypesDev();
Expand Down Expand Up @@ -3094,8 +3062,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3253,8 +3219,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3413,8 +3377,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3589,8 +3551,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
mountHookTypesDev();
Expand Down Expand Up @@ -3776,8 +3736,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down Expand Up @@ -3964,8 +3922,6 @@ if (__DEV__) {
unstable_isNewReconciler: enableNewReconciler,
};
if (enableCache) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
currentHookNameInDev = 'useCacheRefresh';
updateHookTypesDev();
Expand Down
Loading

0 comments on commit a8c16a0

Please sign in to comment.