From 81392991df1dd39d5a75a4c937328b7fc021beef Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 1 Dec 2020 18:56:23 +0000 Subject: [PATCH] readCache -> getCacheForType --- .../react-debug-tools/src/ReactDebugHooks.js | 5 +-- .../src/server/ReactPartialRendererHooks.js | 5 +-- packages/react-fetch/src/ReactFetchBrowser.js | 19 +++------ packages/react-fetch/src/ReactFetchNode.js | 20 +++------- .../src/ReactFiberHooks.new.js | 39 +++++++++---------- .../src/ReactFiberHooks.old.js | 39 +++++++++---------- .../src/ReactInternalTypes.js | 4 +- .../react-server/src/ReactFlightServer.js | 22 +++++------ packages/shared/ReactTypes.js | 5 --- 9 files changed, 66 insertions(+), 92 deletions(-) diff --git a/packages/react-debug-tools/src/ReactDebugHooks.js b/packages/react-debug-tools/src/ReactDebugHooks.js index 0f9b638bfeef2..ce62b5ce8b9ef 100644 --- a/packages/react-debug-tools/src/ReactDebugHooks.js +++ b/packages/react-debug-tools/src/ReactDebugHooks.js @@ -13,7 +13,6 @@ import type { MutableSourceSubscribeFn, ReactContext, ReactProviderType, - ReactCache, } from 'shared/ReactTypes'; import type { Fiber, @@ -102,7 +101,7 @@ function nextHook(): null | Hook { return hook; } -function readCache(): ReactCache { +function getCacheForType(resourceType: () => T): T { invariant(false, 'Not implemented.'); } @@ -304,7 +303,7 @@ function useOpaqueIdentifier(): OpaqueIDType | void { } const Dispatcher: DispatcherType = { - readCache, + getCacheForType, readContext, useCallback, useContext, diff --git a/packages/react-dom/src/server/ReactPartialRendererHooks.js b/packages/react-dom/src/server/ReactPartialRendererHooks.js index 8c48b917e28c6..1b0039e438cb3 100644 --- a/packages/react-dom/src/server/ReactPartialRendererHooks.js +++ b/packages/react-dom/src/server/ReactPartialRendererHooks.js @@ -14,7 +14,6 @@ import type { MutableSourceGetSnapshotFn, MutableSourceSubscribeFn, ReactContext, - ReactCache, } from 'shared/ReactTypes'; import type PartialRenderer from './ReactPartialRenderer'; @@ -215,7 +214,7 @@ export function resetHooksState(): void { workInProgressHook = null; } -function readCache(): ReactCache { +function getCacheForType(resourceType: () => T): T { invariant(false, 'Not implemented.'); } @@ -497,7 +496,7 @@ export function setCurrentPartialRenderer(renderer: PartialRenderer) { } export const Dispatcher: DispatcherType = { - readCache, + getCacheForType, readContext, useContext, useMemo, diff --git a/packages/react-fetch/src/ReactFetchBrowser.js b/packages/react-fetch/src/ReactFetchBrowser.js index b67e6319a2468..e6e3f369d5532 100644 --- a/packages/react-fetch/src/ReactFetchBrowser.js +++ b/packages/react-fetch/src/ReactFetchBrowser.js @@ -7,7 +7,7 @@ * @flow */ -import type {Wakeable, ReactCache} from 'shared/ReactTypes'; +import type {Wakeable} from 'shared/ReactTypes'; import * as React from 'react'; @@ -34,24 +34,17 @@ type Result = PendingResult | ResolvedResult | RejectedResult; // TODO: this is a browser-only version. Add a separate Node entry point. const nativeFetch = window.fetch; -const fetchKey = {}; const ReactCurrentDispatcher = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED .ReactCurrentDispatcher; -function readCache(): ReactCache { - return ReactCurrentDispatcher.current.readCache(); +function getResultMap(): Map { + return ReactCurrentDispatcher.current.getCacheForType(createResultMap); } -function readResultMap(): Map { - const resources = readCache().resources; - let map = resources.get(fetchKey); - if (map === undefined) { - map = new Map(); - resources.set(fetchKey, map); - } - return map; +function createResultMap(): Map { + return new Map(); } function toResult(thenable): Result { @@ -128,7 +121,7 @@ Response.prototype = { }; function preloadResult(url: string, options: mixed): Result { - const map = readResultMap(); + const map = getResultMap(); let entry = map.get(url); if (!entry) { if (options) { diff --git a/packages/react-fetch/src/ReactFetchNode.js b/packages/react-fetch/src/ReactFetchNode.js index 57f14ea40017c..3b8189e98e144 100644 --- a/packages/react-fetch/src/ReactFetchNode.js +++ b/packages/react-fetch/src/ReactFetchNode.js @@ -7,7 +7,7 @@ * @flow */ -import type {Wakeable, ReactCache} from 'shared/ReactTypes'; +import type {Wakeable} from 'shared/ReactTypes'; import * as http from 'http'; import * as https from 'https'; @@ -78,20 +78,12 @@ const ReactCurrentDispatcher = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED .ReactCurrentDispatcher; -function readCache(): ReactCache { - return ReactCurrentDispatcher.current.readCache(); +function getResultMap(): Map { + return ReactCurrentDispatcher.current.getCacheForType(createResultMap); } -const fetchKey = {}; - -function readResultMap(): Map> { - const resources = readCache().resources; - let map = resources.get(fetchKey); - if (map === undefined) { - map = new Map(); - resources.set(fetchKey, map); - } - return map; +function createResultMap(): Map { + return new Map(); } function readResult(result: Result): T { @@ -173,7 +165,7 @@ Response.prototype = { }; function preloadResult(url: string, options: mixed): Result { - const map = readResultMap(); + const map = getResultMap(); let entry = map.get(url); if (!entry) { if (options) { diff --git a/packages/react-reconciler/src/ReactFiberHooks.new.js b/packages/react-reconciler/src/ReactFiberHooks.new.js index 86a770442159f..97fe1e758a02b 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.new.js +++ b/packages/react-reconciler/src/ReactFiberHooks.new.js @@ -12,7 +12,6 @@ import type { MutableSourceGetSnapshotFn, MutableSourceSubscribeFn, ReactContext, - ReactCache, } from 'shared/ReactTypes'; import type {Fiber, Dispatcher, HookType} from './ReactInternalTypes'; import type {Lanes, Lane} from './ReactFiberLane'; @@ -1816,12 +1815,12 @@ function dispatchAction( } } -function readCache() { +function getCacheForType() { invariant(false, 'Not implemented.'); } export const ContextOnlyDispatcher: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: throwInvalidHookError, @@ -1843,7 +1842,7 @@ export const ContextOnlyDispatcher: Dispatcher = { }; const HooksDispatcherOnMount: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: mountCallback, @@ -1865,7 +1864,7 @@ const HooksDispatcherOnMount: Dispatcher = { }; const HooksDispatcherOnUpdate: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: updateCallback, @@ -1887,7 +1886,7 @@ const HooksDispatcherOnUpdate: Dispatcher = { }; const HooksDispatcherOnRerender: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: updateCallback, @@ -1936,8 +1935,8 @@ if (__DEV__) { }; HooksDispatcherOnMountInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2066,8 +2065,8 @@ if (__DEV__) { }; HooksDispatcherOnMountWithHookTypesInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2191,8 +2190,8 @@ if (__DEV__) { }; HooksDispatcherOnUpdateInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2316,8 +2315,8 @@ if (__DEV__) { }; HooksDispatcherOnRerenderInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2442,8 +2441,8 @@ if (__DEV__) { }; InvalidNestedHooksDispatcherOnMountInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2582,8 +2581,8 @@ if (__DEV__) { }; InvalidNestedHooksDispatcherOnUpdateInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2722,8 +2721,8 @@ if (__DEV__) { }; InvalidNestedHooksDispatcherOnRerenderInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, diff --git a/packages/react-reconciler/src/ReactFiberHooks.old.js b/packages/react-reconciler/src/ReactFiberHooks.old.js index f79f53fe9cef9..3f0017147ae76 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.old.js +++ b/packages/react-reconciler/src/ReactFiberHooks.old.js @@ -12,7 +12,6 @@ import type { MutableSourceGetSnapshotFn, MutableSourceSubscribeFn, ReactContext, - ReactCache, } from 'shared/ReactTypes'; import type {Fiber, Dispatcher, HookType} from './ReactInternalTypes'; import type {Lanes, Lane} from './ReactFiberLane'; @@ -1816,12 +1815,12 @@ function dispatchAction( } } -function readCache() { +function getCacheForType(resourceType: () => T): T { invariant(false, 'Not implemented.'); } export const ContextOnlyDispatcher: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: throwInvalidHookError, @@ -1843,7 +1842,7 @@ export const ContextOnlyDispatcher: Dispatcher = { }; const HooksDispatcherOnMount: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: mountCallback, @@ -1865,7 +1864,7 @@ const HooksDispatcherOnMount: Dispatcher = { }; const HooksDispatcherOnUpdate: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: updateCallback, @@ -1887,7 +1886,7 @@ const HooksDispatcherOnUpdate: Dispatcher = { }; const HooksDispatcherOnRerender: Dispatcher = { - readCache, + getCacheForType, readContext, useCallback: updateCallback, @@ -1936,8 +1935,8 @@ if (__DEV__) { }; HooksDispatcherOnMountInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2066,8 +2065,8 @@ if (__DEV__) { }; HooksDispatcherOnMountWithHookTypesInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2191,8 +2190,8 @@ if (__DEV__) { }; HooksDispatcherOnUpdateInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2316,8 +2315,8 @@ if (__DEV__) { }; HooksDispatcherOnRerenderInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2442,8 +2441,8 @@ if (__DEV__) { }; InvalidNestedHooksDispatcherOnMountInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2582,8 +2581,8 @@ if (__DEV__) { }; InvalidNestedHooksDispatcherOnUpdateInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, @@ -2722,8 +2721,8 @@ if (__DEV__) { }; InvalidNestedHooksDispatcherOnRerenderInDEV = { - readCache(): ReactCache { - return readCache(); + getCacheForType(resourceType: () => T): T { + return getCacheForType(resourceType); }, readContext( context: ReactContext, diff --git a/packages/react-reconciler/src/ReactInternalTypes.js b/packages/react-reconciler/src/ReactInternalTypes.js index d2a0c7a822708..bcfb1bf55ca1a 100644 --- a/packages/react-reconciler/src/ReactInternalTypes.js +++ b/packages/react-reconciler/src/ReactInternalTypes.js @@ -23,7 +23,7 @@ import type {Flags} from './ReactFiberFlags'; import type {Lane, LanePriority, Lanes, LaneMap} from './ReactFiberLane'; import type {RootTag} from './ReactRootTags'; import type {TimeoutHandle, NoTimeout} from './ReactFiberHostConfig'; -import type {Wakeable, ReactCache} from 'shared/ReactTypes'; +import type {Wakeable} from 'shared/ReactTypes'; import type {Interaction} from 'scheduler/src/Tracing'; // Unwind Circular: moved from ReactFiberHooks.old @@ -274,7 +274,7 @@ type BasicStateAction = (S => S) | S; type Dispatch = A => void; export type Dispatcher = {| - readCache(): ReactCache, + getCacheForType(resourceType: () => T): T, readContext( context: ReactContext, observedBits: void | number | boolean, diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index 8f1f0e37ddc7d..8e80dc569bad0 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -40,7 +40,6 @@ import { REACT_LAZY_TYPE, REACT_MEMO_TYPE, } from 'shared/ReactSymbols'; -import type {ReactCache} from 'shared/ReactTypes'; import ReactSharedInternals from 'shared/ReactSharedInternals'; import invariant from 'shared/invariant'; @@ -75,7 +74,7 @@ type Segment = { export type Request = { destination: Destination, bundlerConfig: BundlerConfig, - cache: ReactCache, + cache: Map, nextChunkId: number, pendingChunks: number, pingedSegments: Array, @@ -90,12 +89,6 @@ export type Request = { const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher; -function createCache(): ReactCache { - return { - resources: new Map(), - }; -} - export function createRequest( model: ReactModel, destination: Destination, @@ -105,7 +98,7 @@ export function createRequest( const request = { destination, bundlerConfig, - cache: createCache(), + cache: new Map(), nextChunkId: 0, pendingChunks: 0, pingedSegments: pingedSegments, @@ -755,7 +748,7 @@ function unsupportedHook(): void { invariant(false, 'This Hook is not supported in Server Components.'); } -let currentCache: ReactCache | null = null; +let currentCache: Map | null = null; const Dispatcher: DispatcherType = { useMemo(nextCreate: () => T): T { @@ -771,12 +764,17 @@ const Dispatcher: DispatcherType = { useTransition(): [(callback: () => void) => void, boolean] { return [() => {}, false]; }, - readCache() { + getCacheForType(resourceType: () => T): T { invariant( currentCache, 'Reading the cache is only supported while rendering.', ); - return currentCache; + if (currentCache.has(resourceType)) { + return currentCache.get(resourceType); + } + const entry = resourceType(); + currentCache.set(resourceType, entry); + return entry; }, readContext: (unsupportedHook: any), useContext: (unsupportedHook: any), diff --git a/packages/shared/ReactTypes.js b/packages/shared/ReactTypes.js index 7cb564f16a211..c03ab5056f6f4 100644 --- a/packages/shared/ReactTypes.js +++ b/packages/shared/ReactTypes.js @@ -216,8 +216,3 @@ export interface Thenable<+R> { onReject: (error: mixed) => void | Thenable | U, ): void | Thenable; } - -export type ReactCache = {| - // TODO: a different API. - resources: Map, -|};