Separate current owner and dispatcher (#14548)
Brian Vaughn committed
Jan 8, 2019 at 14:39 UTC
19ef0ec116a2c5fd1a8dc86d38c2185d62abc6a1
12 files changed
+56
-32
packages/react-cache/src/ReactCache.js
+4
-3
@@ -46,11 +46,12 @@ const Pending = 0;
46
const Resolved = 1;
47
const Rejected = 2;
48
49
-const currentOwner =
50
- React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;
49
+const ReactCurrentDispatcher =
50
+ React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
51
+ .ReactCurrentDispatcher;
52
53
function readContext(Context, observedBits) {
53
- const dispatcher = currentOwner.currentDispatcher;
54
+ const dispatcher = ReactCurrentDispatcher.current;
55
if (dispatcher === null) {
56
throw new Error(
57
'react-cache: read and preload may only be called from within a ' +
packages/react-debug-tools/src/ReactDebugHooks.js
+7
-7
@@ -20,7 +20,7 @@ import {
20
ForwardRef,
21
} from 'shared/ReactWorkTags';
22
23
-const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
23
+const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
24
25
// Used to track hooks called during a render
26
@@ -409,9 +409,9 @@ export function inspectHooks<Props>(
409
renderFunction: Props => React$Node,
410
props: Props,
411
): HooksTree {
412
- let previousDispatcher = ReactCurrentOwner.currentDispatcher;
412
+ let previousDispatcher = ReactCurrentDispatcher.current;
413
let readHookLog;
414
- ReactCurrentOwner.currentDispatcher = Dispatcher;
414
+ ReactCurrentDispatcher.current = Dispatcher;
415
let ancestorStackError;
416
try {
417
ancestorStackError = new Error();
@@ -419,7 +419,7 @@ export function inspectHooks<Props>(
419
} finally {
420
readHookLog = hookLog;
421
hookLog = [];
422
- ReactCurrentOwner.currentDispatcher = previousDispatcher;
422
+ ReactCurrentDispatcher.current = previousDispatcher;
423
}
424
let rootStack = ErrorStackParser.parse(ancestorStackError);
425
return buildTree(rootStack, readHookLog);
@@ -451,9 +451,9 @@ function inspectHooksOfForwardRef<Props, Ref>(
451
props: Props,
452
ref: Ref,
453
): HooksTree {
454
- let previousDispatcher = ReactCurrentOwner.currentDispatcher;
454
+ let previousDispatcher = ReactCurrentDispatcher.current;
455
let readHookLog;
456
- ReactCurrentOwner.currentDispatcher = Dispatcher;
456
+ ReactCurrentDispatcher.current = Dispatcher;
457
let ancestorStackError;
458
try {
459
ancestorStackError = new Error();
@@ -461,7 +461,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
461
} finally {
462
readHookLog = hookLog;
463
hookLog = [];
464
- ReactCurrentOwner.currentDispatcher = previousDispatcher;
464
+ ReactCurrentDispatcher.current = previousDispatcher;
465
}
466
let rootStack = ErrorStackParser.parse(ancestorStackError);
467
return buildTree(rootStack, readHookLog);
packages/react-dom/src/__tests__/ReactServerRendering-test.js
+4
-4
@@ -13,7 +13,7 @@
13
let React;
14
let ReactDOMServer;
15
let PropTypes;
16
-let ReactCurrentOwner;
16
+let ReactCurrentDispatcher;
17
18
function normalizeCodeLocInfo(str) {
19
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
@@ -25,9 +25,9 @@ describe('ReactDOMServer', () => {
25
React = require('react');
26
PropTypes = require('prop-types');
27
ReactDOMServer = require('react-dom/server');
28
- ReactCurrentOwner =
28
+ ReactCurrentDispatcher =
29
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
30
- .ReactCurrentOwner;
30
+ .ReactCurrentDispatcher;
31
});
32
33
describe('renderToString', () => {
@@ -439,7 +439,7 @@ describe('ReactDOMServer', () => {
439
const Context = React.createContext(0);
440
441
function readContext(context) {
442
- return ReactCurrentOwner.currentDispatcher.readContext(context);
442
+ return ReactCurrentDispatcher.current.readContext(context);
443
}
444
445
function Consumer(props) {
packages/react-dom/src/server/ReactPartialRenderer.js
+5
-5
@@ -87,7 +87,7 @@ const toArray = ((React.Children.toArray: any): toArrayType);
87
// Each stack is an array of frames which may contain nested stacks of elements.
88
let currentDebugStacks = [];
89
90
-let ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
90
+let ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
91
let ReactDebugCurrentFrame;
92
let prevGetCurrentStackImpl = null;
93
let getCurrentServerStackImpl = () => '';
@@ -785,11 +785,11 @@ class ReactDOMServerRenderer {
785
786
const prevThreadID = currentThreadID;
787
setCurrentThreadID(this.threadID);
788
- const prevDispatcher = ReactCurrentOwner.currentDispatcher;
788
+ const prevDispatcher = ReactCurrentDispatcher.current;
789
if (enableHooks) {
790
- ReactCurrentOwner.currentDispatcher = Dispatcher;
790
+ ReactCurrentDispatcher.current = Dispatcher;
791
} else {
792
- ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
792
+ ReactCurrentDispatcher.current = DispatcherWithoutHooks;
793
}
794
try {
795
// Markup generated within <Suspense> ends up buffered until we know
@@ -870,7 +870,7 @@ class ReactDOMServerRenderer {
870
}
871
return out[0];
872
} finally {
873
- ReactCurrentOwner.currentDispatcher = prevDispatcher;
873
+ ReactCurrentDispatcher.current = prevDispatcher;
874
setCurrentThreadID(prevThreadID);
875
}
876
}
packages/react-reconciler/src/ReactFiberClassComponent.js
+2
-2
@@ -55,10 +55,10 @@ import {
55
flushPassiveEffects,
56
} from './ReactFiberScheduler';
57
58
-const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
58
+const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
59
60
function readContext(contextType: any): any {
61
- const dispatcher = ReactCurrentOwner.currentDispatcher;
61
+ const dispatcher = ReactCurrentDispatcher.current;
62
return dispatcher.readContext(contextType);
63
}
64
packages/react-reconciler/src/ReactFiberScheduler.js
+4
-4
@@ -171,7 +171,7 @@ export type Thenable = {
171
then(resolve: () => mixed, reject?: () => mixed): mixed,
172
};
173
174
-const {ReactCurrentOwner} = ReactSharedInternals;
174
+const {ReactCurrentDispatcher, ReactCurrentOwner} = ReactSharedInternals;
175
176
let didWarnAboutStateTransition;
177
let didWarnSetStateChildContext;
@@ -1209,9 +1209,9 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
1209
1210
isWorking = true;
1211
if (enableHooks) {
1212
- ReactCurrentOwner.currentDispatcher = Dispatcher;
1212
+ ReactCurrentDispatcher.current = Dispatcher;
1213
} else {
1214
- ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
1214
+ ReactCurrentDispatcher.current = DispatcherWithoutHooks;
1215
}
1216
1217
const expirationTime = root.nextExpirationTimeToWorkOn;
@@ -1373,7 +1373,7 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
1373
1374
// We're done performing work. Time to clean up.
1375
isWorking = false;
1376
- ReactCurrentOwner.currentDispatcher = null;
1376
+ ReactCurrentDispatcher.current = null;
1377
resetContextDependences();
1378
resetHooks();
1379
packages/react-reconciler/src/__tests__/ReactMemo-test.internal.js
+1
-1
@@ -136,7 +136,7 @@ describe('memo', () => {
136
function readContext(Context) {
137
const dispatcher =
138
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
139
- .ReactCurrentOwner.currentDispatcher;
139
+ .ReactCurrentDispatcher.current;
140
return dispatcher.readContext(Context);
141
}
142
packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js
+2
-2
@@ -39,8 +39,8 @@ describe('ReactNewContext', () => {
39
40
function readContext(Context, observedBits) {
41
const dispatcher =
42
- React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner
43
- .currentDispatcher;
42
+ React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
43
+ .ReactCurrentDispatcher.current;
44
return dispatcher.readContext(Context, observedBits);
45
}
46
packages/react/src/ReactCurrentDispatcher.js
new
+23
@@ -0,0 +1,23 @@
1
+/**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ */
9
+
10
+import typeof {Dispatcher} from 'react-reconciler/src/ReactFiberDispatcher';
11
+
12
+/**
13
+ * Keeps track of the current dispatcher.
14
+ */
15
+const ReactCurrentDispatcher = {
16
+ /**
17
+ * @internal
18
+ * @type {ReactComponent}
19
+ */
20
+ current: (null: null | Dispatcher),
21
+};
22
+
23
+export default ReactCurrentDispatcher;
packages/react/src/ReactCurrentOwner.js
-2
@@ -8,7 +8,6 @@
8
*/
9
10
import type {Fiber} from 'react-reconciler/src/ReactFiber';
11
-import typeof {Dispatcher} from 'react-reconciler/src/ReactFiberDispatcher';
11
12
/**
13
* Keeps track of the current owner.
@@ -22,7 +21,6 @@ const ReactCurrentOwner = {
21
* @type {ReactComponent}
22
*/
23
current: (null: null | Fiber),
25
- currentDispatcher: (null: null | Dispatcher),
24
};
25
26
export default ReactCurrentOwner;
packages/react/src/ReactHooks.js
+2
-2
@@ -11,10 +11,10 @@ import type {ReactContext} from 'shared/ReactTypes';
11
import invariant from 'shared/invariant';
12
import warning from 'shared/warning';
13
14
-import ReactCurrentOwner from './ReactCurrentOwner';
14
+import ReactCurrentDispatcher from './ReactCurrentDispatcher';
15
16
function resolveDispatcher() {
17
- const dispatcher = ReactCurrentOwner.currentDispatcher;
17
+ const dispatcher = ReactCurrentDispatcher.current;
18
invariant(
19
dispatcher !== null,
20
'Hooks can only be called inside the body of a function component.',
packages/react/src/ReactSharedInternals.js
+2
@@ -29,10 +29,12 @@ import {
29
unstable_unsubscribe,
30
unstable_wrap,
31
} from 'scheduler/tracing';
32
+import ReactCurrentDispatcher from './ReactCurrentDispatcher';
33
import ReactCurrentOwner from './ReactCurrentOwner';
34
import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
35
36
const ReactSharedInternals = {
37
+ ReactCurrentDispatcher,
38
ReactCurrentOwner,
39
// Used by renderers to avoid bundling object-assign twice in UMD bundles:
40
assign,