Skip to content

Commit

Permalink
Add React.unstable_getCacheForType
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 1, 2020
1 parent 1e7e5a9 commit 0102de6
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 12 deletions.
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 {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 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 {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 getCacheForType(createResultMap);
}

function createResultMap(): Map<string, Result<FetchResponse>> {
Expand Down
2 changes: 2 additions & 0 deletions packages/react/index.classic.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createFactory,
// exposeConcurrentModeAPIs
getCacheForType,
getCacheForType as unstable_getCacheForType,
useTransition,
useTransition as unstable_useTransition,
useDeferredValue,
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 @@ -40,6 +40,7 @@ export {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createFactory,
// exposeConcurrentModeAPIs
getCacheForType as unstable_getCacheForType,
useTransition as unstable_useTransition,
useDeferredValue as unstable_useDeferredValue,
startTransition as unstable_startTransition,
Expand Down
2 changes: 2 additions & 0 deletions packages/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export {
version,
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createFactory,
getCacheForType,
getCacheForType as unstable_getCacheForType,
useTransition,
useTransition as unstable_useTransition,
startTransition,
Expand Down
2 changes: 2 additions & 0 deletions packages/react/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export {
version,
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
// exposeConcurrentModeAPIs
getCacheForType,
getCacheForType as unstable_getCacheForType,
useTransition,
useTransition as unstable_useTransition,
useDeferredValue,
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 @@ -105,6 +106,7 @@ export {
// Deprecated behind disableCreateFactory
createFactory,
// Concurrent Mode
getCacheForType,
useTransition,
startTransition,
useDeferredValue,
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

0 comments on commit 0102de6

Please sign in to comment.