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

Separate current owner and dispatcher #14548

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/react-cache/src/ReactCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand Down
14 changes: 7 additions & 7 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -409,17 +409,17 @@ 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();
renderFunction(props);
} finally {
readHookLog = hookLog;
hookLog = [];
ReactCurrentOwner.currentDispatcher = previousDispatcher;
ReactCurrentDispatcher.current = previousDispatcher;
}
let rootStack = ErrorStackParser.parse(ancestorStackError);
return buildTree(rootStack, readHookLog);
Expand Down Expand Up @@ -451,17 +451,17 @@ 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();
renderFunction(props, ref);
} finally {
readHookLog = hookLog;
hookLog = [];
ReactCurrentOwner.currentDispatcher = previousDispatcher;
ReactCurrentDispatcher.current = previousDispatcher;
}
let rootStack = ErrorStackParser.parse(ancestorStackError);
return buildTree(rootStack, readHookLog);
Expand Down
8 changes: 4 additions & 4 deletions packages/react-dom/src/__tests__/ReactServerRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 **)');
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => '';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -870,7 +870,7 @@ class ReactDOMServerRenderer {
}
return out[0];
} finally {
ReactCurrentOwner.currentDispatcher = prevDispatcher;
ReactCurrentDispatcher.current = prevDispatcher;
setCurrentThreadID(prevThreadID);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export type Thenable = {
then(resolve: () => mixed, reject?: () => mixed): mixed,
};

const {ReactCurrentOwner} = ReactSharedInternals;
const {ReactCurrentDispatcher, ReactCurrentOwner} = ReactSharedInternals;

let didWarnAboutStateTransition;
let didWarnSetStateChildContext;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions packages/react/src/ReactCurrentDispatcher.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 0 additions & 2 deletions packages/react/src/ReactCurrentOwner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -22,7 +21,6 @@ const ReactCurrentOwner = {
* @type {ReactComponent}
*/
current: (null: null | Fiber),
currentDispatcher: (null: null | Dispatcher),
};

export default ReactCurrentOwner;
4 changes: 2 additions & 2 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/ReactSharedInternals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down