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
Prev Previous commit
Next Next commit
Add React.unstable_getCacheForType
  • Loading branch information
gaearon committed Dec 2, 2020
commit 300fe29a9dd8fa24b4b837285e167e49a66d1b1c
8 changes: 2 additions & 6 deletions packages/react-fetch/src/ReactFetchBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

import * as React from 'react';
import {unstable_getCacheForType} from 'react';

const Pending = 0;
const Resolved = 1;
Expand All @@ -35,12 +35,8 @@ type Result = PendingResult | ResolvedResult | RejectedResult;
// TODO: this is a browser-only version. Add a separate Node entry point.
const nativeFetch = window.fetch;

const ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher;

function getResultMap(): Map<string, Result> {
return ReactCurrentDispatcher.current.getCacheForType(createResultMap);
return unstable_getCacheForType(createResultMap);
}

function createResultMap(): Map<string, Result> {
Expand Down
8 changes: 2 additions & 6 deletions packages/react-fetch/src/ReactFetchNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {Wakeable} from 'shared/ReactTypes';

import * as http from 'http';
import * as https from 'https';
import * as React from 'react';
import {unstable_getCacheForType} from 'react';

type FetchResponse = {|
// Properties
Expand Down Expand Up @@ -74,12 +74,8 @@ type RejectedResult = {|

type Result<V> = PendingResult | ResolvedResult<V> | RejectedResult;

const ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher;

function getResultMap(): Map<string, Result<FetchResponse>> {
return ReactCurrentDispatcher.current.getCacheForType(createResultMap);
return unstable_getCacheForType(createResultMap);
}

function createResultMap(): Map<string, Result<FetchResponse>> {
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 @@ -50,6 +50,7 @@ export {
startTransition as unstable_startTransition,
SuspenseList,
SuspenseList as unstable_SuspenseList,
unstable_getCacheForType,
// enableScopeAPI
unstable_Scope,
unstable_useOpaqueIdentifier,
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 @@ -45,6 +45,7 @@ export {
startTransition as unstable_startTransition,
SuspenseList as unstable_SuspenseList,
unstable_useOpaqueIdentifier,
unstable_getCacheForType,
// enableDebugTracing
unstable_DebugTracingMode,
} from './src/React';
1 change: 1 addition & 0 deletions packages/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ export {
unstable_createFundamental,
unstable_Scope,
unstable_useOpaqueIdentifier,
unstable_getCacheForType,
} from './src/React';
1 change: 1 addition & 0 deletions packages/react/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export {
startTransition as unstable_startTransition,
SuspenseList,
SuspenseList as unstable_SuspenseList,
unstable_getCacheForType,
// enableScopeAPI
unstable_Scope,
unstable_useOpaqueIdentifier,
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 @@ -33,6 +33,7 @@ import {lazy} from './ReactLazy';
import {forwardRef} from './ReactForwardRef';
import {memo} from './ReactMemo';
import {
getCacheForType,
useCallback,
useContext,
useEffect,
Expand Down Expand Up @@ -110,6 +111,7 @@ export {
useDeferredValue,
REACT_SUSPENSE_LIST_TYPE as SuspenseList,
REACT_LEGACY_HIDDEN_TYPE as unstable_LegacyHidden,
getCacheForType as unstable_getCacheForType,
// enableFundamentalAPI
createFundamental as unstable_createFundamental,
// enableScopeAPI
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ function resolveDispatcher() {
return dispatcher;
}

export function getCacheForType<T>(resourceType: () => T): T {
const dispatcher = resolveDispatcher();
return dispatcher.getCacheForType(resourceType);
}

export function useContext<T>(
Context: ReactContext<T>,
unstable_observedBits: number | boolean | void,
Expand Down