Separate current owner and dispatcher (#14548)

This commit is contained in:
Brian Vaughn
2019-01-08 14:39:52 -08:00
committed by GitHub
parent a9b035b0c2
commit 19ef0ec116
12 changed files with 56 additions and 32 deletions

View File

@@ -46,11 +46,12 @@ const Pending = 0;
const Resolved = 1;
const Rejected = 2;
const currentOwner =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;
const ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher;
function readContext(Context, observedBits) {
const dispatcher = currentOwner.currentDispatcher;
const dispatcher = ReactCurrentDispatcher.current;
if (dispatcher === null) {
throw new Error(
'react-cache: read and preload may only be called from within a ' +

View File

@@ -20,7 +20,7 @@ import {
ForwardRef,
} from 'shared/ReactWorkTags';
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
// Used to track hooks called during a render
@@ -409,9 +409,9 @@ export function inspectHooks<Props>(
renderFunction: Props => React$Node,
props: Props,
): HooksTree {
let previousDispatcher = ReactCurrentOwner.currentDispatcher;
let previousDispatcher = ReactCurrentDispatcher.current;
let readHookLog;
ReactCurrentOwner.currentDispatcher = Dispatcher;
ReactCurrentDispatcher.current = Dispatcher;
let ancestorStackError;
try {
ancestorStackError = new Error();
@@ -419,7 +419,7 @@ export function inspectHooks<Props>(
} finally {
readHookLog = hookLog;
hookLog = [];
ReactCurrentOwner.currentDispatcher = previousDispatcher;
ReactCurrentDispatcher.current = previousDispatcher;
}
let rootStack = ErrorStackParser.parse(ancestorStackError);
return buildTree(rootStack, readHookLog);
@@ -451,9 +451,9 @@ function inspectHooksOfForwardRef<Props, Ref>(
props: Props,
ref: Ref,
): HooksTree {
let previousDispatcher = ReactCurrentOwner.currentDispatcher;
let previousDispatcher = ReactCurrentDispatcher.current;
let readHookLog;
ReactCurrentOwner.currentDispatcher = Dispatcher;
ReactCurrentDispatcher.current = Dispatcher;
let ancestorStackError;
try {
ancestorStackError = new Error();
@@ -461,7 +461,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
} finally {
readHookLog = hookLog;
hookLog = [];
ReactCurrentOwner.currentDispatcher = previousDispatcher;
ReactCurrentDispatcher.current = previousDispatcher;
}
let rootStack = ErrorStackParser.parse(ancestorStackError);
return buildTree(rootStack, readHookLog);

View File

@@ -13,7 +13,7 @@
let React;
let ReactDOMServer;
let PropTypes;
let ReactCurrentOwner;
let ReactCurrentDispatcher;
function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
@@ -25,9 +25,9 @@ describe('ReactDOMServer', () => {
React = require('react');
PropTypes = require('prop-types');
ReactDOMServer = require('react-dom/server');
ReactCurrentOwner =
ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentOwner;
.ReactCurrentDispatcher;
});
describe('renderToString', () => {
@@ -439,7 +439,7 @@ describe('ReactDOMServer', () => {
const Context = React.createContext(0);
function readContext(context) {
return ReactCurrentOwner.currentDispatcher.readContext(context);
return ReactCurrentDispatcher.current.readContext(context);
}
function Consumer(props) {

View File

@@ -87,7 +87,7 @@ const toArray = ((React.Children.toArray: any): toArrayType);
// Each stack is an array of frames which may contain nested stacks of elements.
let currentDebugStacks = [];
let ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
let ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
let ReactDebugCurrentFrame;
let prevGetCurrentStackImpl = null;
let getCurrentServerStackImpl = () => '';
@@ -785,11 +785,11 @@ class ReactDOMServerRenderer {
const prevThreadID = currentThreadID;
setCurrentThreadID(this.threadID);
const prevDispatcher = ReactCurrentOwner.currentDispatcher;
const prevDispatcher = ReactCurrentDispatcher.current;
if (enableHooks) {
ReactCurrentOwner.currentDispatcher = Dispatcher;
ReactCurrentDispatcher.current = Dispatcher;
} else {
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
ReactCurrentDispatcher.current = DispatcherWithoutHooks;
}
try {
// Markup generated within <Suspense> ends up buffered until we know
@@ -870,7 +870,7 @@ class ReactDOMServerRenderer {
}
return out[0];
} finally {
ReactCurrentOwner.currentDispatcher = prevDispatcher;
ReactCurrentDispatcher.current = prevDispatcher;
setCurrentThreadID(prevThreadID);
}
}

View File

@@ -55,10 +55,10 @@ import {
flushPassiveEffects,
} from './ReactFiberScheduler';
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
function readContext(contextType: any): any {
const dispatcher = ReactCurrentOwner.currentDispatcher;
const dispatcher = ReactCurrentDispatcher.current;
return dispatcher.readContext(contextType);
}

View File

@@ -171,7 +171,7 @@ export type Thenable = {
then(resolve: () => mixed, reject?: () => mixed): mixed,
};
const {ReactCurrentOwner} = ReactSharedInternals;
const {ReactCurrentDispatcher, ReactCurrentOwner} = ReactSharedInternals;
let didWarnAboutStateTransition;
let didWarnSetStateChildContext;
@@ -1209,9 +1209,9 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
isWorking = true;
if (enableHooks) {
ReactCurrentOwner.currentDispatcher = Dispatcher;
ReactCurrentDispatcher.current = Dispatcher;
} else {
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
ReactCurrentDispatcher.current = DispatcherWithoutHooks;
}
const expirationTime = root.nextExpirationTimeToWorkOn;
@@ -1373,7 +1373,7 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
// We're done performing work. Time to clean up.
isWorking = false;
ReactCurrentOwner.currentDispatcher = null;
ReactCurrentDispatcher.current = null;
resetContextDependences();
resetHooks();

View File

@@ -136,7 +136,7 @@ describe('memo', () => {
function readContext(Context) {
const dispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentOwner.currentDispatcher;
.ReactCurrentDispatcher.current;
return dispatcher.readContext(Context);
}

View File

@@ -39,8 +39,8 @@ describe('ReactNewContext', () => {
function readContext(Context, observedBits) {
const dispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner
.currentDispatcher;
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher.current;
return dispatcher.readContext(Context, observedBits);
}

View File

@@ -0,0 +1,23 @@
/**
* 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 typeof {Dispatcher} from 'react-reconciler/src/ReactFiberDispatcher';
/**
* Keeps track of the current dispatcher.
*/
const ReactCurrentDispatcher = {
/**
* @internal
* @type {ReactComponent}
*/
current: (null: null | Dispatcher),
};
export default ReactCurrentDispatcher;

View File

@@ -8,7 +8,6 @@
*/
import type {Fiber} from 'react-reconciler/src/ReactFiber';
import typeof {Dispatcher} from 'react-reconciler/src/ReactFiberDispatcher';
/**
* Keeps track of the current owner.
@@ -22,7 +21,6 @@ const ReactCurrentOwner = {
* @type {ReactComponent}
*/
current: (null: null | Fiber),
currentDispatcher: (null: null | Dispatcher),
};
export default ReactCurrentOwner;

View File

@@ -11,10 +11,10 @@ import type {ReactContext} from 'shared/ReactTypes';
import invariant from 'shared/invariant';
import warning from 'shared/warning';
import ReactCurrentOwner from './ReactCurrentOwner';
import ReactCurrentDispatcher from './ReactCurrentDispatcher';
function resolveDispatcher() {
const dispatcher = ReactCurrentOwner.currentDispatcher;
const dispatcher = ReactCurrentDispatcher.current;
invariant(
dispatcher !== null,
'Hooks can only be called inside the body of a function component.',

View File

@@ -29,10 +29,12 @@ import {
unstable_unsubscribe,
unstable_wrap,
} from 'scheduler/tracing';
import ReactCurrentDispatcher from './ReactCurrentDispatcher';
import ReactCurrentOwner from './ReactCurrentOwner';
import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
const ReactSharedInternals = {
ReactCurrentDispatcher,
ReactCurrentOwner,
// Used by renderers to avoid bundling object-assign twice in UMD bundles:
assign,