Skip to content

Commit

Permalink
readCache -> getCacheForType<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 1, 2020
1 parent 38de6e6 commit 8139299
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 92 deletions.
5 changes: 2 additions & 3 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
MutableSourceSubscribeFn,
ReactContext,
ReactProviderType,
ReactCache,
} from 'shared/ReactTypes';
import type {
Fiber,
Expand Down Expand Up @@ -102,7 +101,7 @@ function nextHook(): null | Hook {
return hook;
}

function readCache(): ReactCache {
function getCacheForType<T>(resourceType: () => T): T {
invariant(false, 'Not implemented.');
}

Expand Down Expand Up @@ -304,7 +303,7 @@ function useOpaqueIdentifier(): OpaqueIDType | void {
}

const Dispatcher: DispatcherType = {
readCache,
getCacheForType,
readContext,
useCallback,
useContext,
Expand Down
5 changes: 2 additions & 3 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
MutableSourceGetSnapshotFn,
MutableSourceSubscribeFn,
ReactContext,
ReactCache,
} from 'shared/ReactTypes';
import type PartialRenderer from './ReactPartialRenderer';

Expand Down Expand Up @@ -215,7 +214,7 @@ export function resetHooksState(): void {
workInProgressHook = null;
}

function readCache(): ReactCache {
function getCacheForType(resourceType: () => T): T {
invariant(false, 'Not implemented.');
}

Expand Down Expand Up @@ -497,7 +496,7 @@ export function setCurrentPartialRenderer(renderer: PartialRenderer) {
}

export const Dispatcher: DispatcherType = {
readCache,
getCacheForType,
readContext,
useContext,
useMemo,
Expand Down
19 changes: 6 additions & 13 deletions packages/react-fetch/src/ReactFetchBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import type {Wakeable, ReactCache} from 'shared/ReactTypes';
import type {Wakeable} from 'shared/ReactTypes';

import * as React from 'react';

Expand All @@ -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<string, Result> {
return ReactCurrentDispatcher.current.getCacheForType(createResultMap);
}

function readResultMap(): Map<string, Result> {
const resources = readCache().resources;
let map = resources.get(fetchKey);
if (map === undefined) {
map = new Map();
resources.set(fetchKey, map);
}
return map;
function createResultMap(): Map<string, Result> {
return new Map();
}

function toResult(thenable): Result {
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 6 additions & 14 deletions packages/react-fetch/src/ReactFetchNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, Result> {
return ReactCurrentDispatcher.current.getCacheForType(createResultMap);
}

const fetchKey = {};

function readResultMap(): Map<string, Result<FetchResponse>> {
const resources = readCache().resources;
let map = resources.get(fetchKey);
if (map === undefined) {
map = new Map();
resources.set(fetchKey, map);
}
return map;
function createResultMap(): Map<string, Result> {
return new Map();
}

function readResult<T>(result: Result<T>): T {
Expand Down Expand Up @@ -173,7 +165,7 @@ Response.prototype = {
};

function preloadResult(url: string, options: mixed): Result<FetchResponse> {
const map = readResultMap();
const map = getResultMap();
let entry = map.get(url);
if (!entry) {
if (options) {
Expand Down
39 changes: 19 additions & 20 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1816,12 +1815,12 @@ function dispatchAction<S, A>(
}
}

function readCache() {
function getCacheForType() {
invariant(false, 'Not implemented.');
}

export const ContextOnlyDispatcher: Dispatcher = {
readCache,
getCacheForType,
readContext,

useCallback: throwInvalidHookError,
Expand All @@ -1843,7 +1842,7 @@ export const ContextOnlyDispatcher: Dispatcher = {
};

const HooksDispatcherOnMount: Dispatcher = {
readCache,
getCacheForType,
readContext,

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

const HooksDispatcherOnUpdate: Dispatcher = {
readCache,
getCacheForType,
readContext,

useCallback: updateCallback,
Expand All @@ -1887,7 +1886,7 @@ const HooksDispatcherOnUpdate: Dispatcher = {
};

const HooksDispatcherOnRerender: Dispatcher = {
readCache,
getCacheForType,
readContext,

useCallback: updateCallback,
Expand Down Expand Up @@ -1936,8 +1935,8 @@ if (__DEV__) {
};

HooksDispatcherOnMountInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2066,8 +2065,8 @@ if (__DEV__) {
};

HooksDispatcherOnMountWithHookTypesInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2191,8 +2190,8 @@ if (__DEV__) {
};

HooksDispatcherOnUpdateInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2316,8 +2315,8 @@ if (__DEV__) {
};

HooksDispatcherOnRerenderInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2442,8 +2441,8 @@ if (__DEV__) {
};

InvalidNestedHooksDispatcherOnMountInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2582,8 +2581,8 @@ if (__DEV__) {
};

InvalidNestedHooksDispatcherOnUpdateInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2722,8 +2721,8 @@ if (__DEV__) {
};

InvalidNestedHooksDispatcherOnRerenderInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down
39 changes: 19 additions & 20 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1816,12 +1815,12 @@ function dispatchAction<S, A>(
}
}

function readCache() {
function getCacheForType<T>(resourceType: () => T): T {
invariant(false, 'Not implemented.');
}

export const ContextOnlyDispatcher: Dispatcher = {
readCache,
getCacheForType,
readContext,

useCallback: throwInvalidHookError,
Expand All @@ -1843,7 +1842,7 @@ export const ContextOnlyDispatcher: Dispatcher = {
};

const HooksDispatcherOnMount: Dispatcher = {
readCache,
getCacheForType,
readContext,

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

const HooksDispatcherOnUpdate: Dispatcher = {
readCache,
getCacheForType,
readContext,

useCallback: updateCallback,
Expand All @@ -1887,7 +1886,7 @@ const HooksDispatcherOnUpdate: Dispatcher = {
};

const HooksDispatcherOnRerender: Dispatcher = {
readCache,
getCacheForType,
readContext,

useCallback: updateCallback,
Expand Down Expand Up @@ -1936,8 +1935,8 @@ if (__DEV__) {
};

HooksDispatcherOnMountInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2066,8 +2065,8 @@ if (__DEV__) {
};

HooksDispatcherOnMountWithHookTypesInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2191,8 +2190,8 @@ if (__DEV__) {
};

HooksDispatcherOnUpdateInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2316,8 +2315,8 @@ if (__DEV__) {
};

HooksDispatcherOnRerenderInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2442,8 +2441,8 @@ if (__DEV__) {
};

InvalidNestedHooksDispatcherOnMountInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2582,8 +2581,8 @@ if (__DEV__) {
};

InvalidNestedHooksDispatcherOnUpdateInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down Expand Up @@ -2722,8 +2721,8 @@ if (__DEV__) {
};

InvalidNestedHooksDispatcherOnRerenderInDEV = {
readCache(): ReactCache {
return readCache();
getCacheForType<T>(resourceType: () => T): T {
return getCacheForType(resourceType);
},
readContext<T>(
context: ReactContext<T>,
Expand Down
Loading

0 comments on commit 8139299

Please sign in to comment.