forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ReactCurrentFiber.js
105 lines (94 loc) · 2.5 KB
/
ReactCurrentFiber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* 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 type {Fiber} from './ReactFiber';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
HostRoot,
HostPortal,
HostText,
Fragment,
ContextProvider,
ContextConsumer,
} from 'shared/ReactWorkTags';
import describeComponentFrame from 'shared/describeComponentFrame';
import getComponentName from 'shared/getComponentName';
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
function describeFiber(fiber: Fiber): string {
switch (fiber.tag) {
case HostRoot:
case HostPortal:
case HostText:
case Fragment:
case ContextProvider:
case ContextConsumer:
return '';
default:
const owner = fiber._debugOwner;
const source = fiber._debugSource;
const name = getComponentName(fiber.type);
let ownerName = null;
if (owner) {
ownerName = getComponentName(owner.type);
}
return describeComponentFrame(name, source, ownerName);
}
}
export function getStackByFiberInDevAndProd(workInProgress: Fiber): string {
let info = '';
let node = workInProgress;
do {
info += describeFiber(node);
node = node.return;
} while (node);
return info;
}
export let current: Fiber | null = null;
export let isRendering: boolean = false;
export function getCurrentFiberOwnerNameInDevOrNull(): string | null {
if (__DEV__) {
if (current === null) {
return null;
}
const owner = current._debugOwner;
if (owner !== null && typeof owner !== 'undefined') {
return getComponentName(owner.type);
}
}
return null;
}
export function getCurrentFiberStackInDev(): string {
if (__DEV__) {
if (current === null) {
return '';
}
// Safe because if current fiber exists, we are reconciling,
// and it is guaranteed to be the work-in-progress version.
return getStackByFiberInDevAndProd(current);
}
return '';
}
export function resetCurrentFiber() {
if (__DEV__) {
ReactDebugCurrentFrame.getCurrentStack = null;
current = null;
isRendering = false;
}
}
export function setCurrentFiber(fiber: Fiber) {
if (__DEV__) {
ReactDebugCurrentFrame.getCurrentStack = getCurrentFiberStackInDev;
current = fiber;
isRendering = false;
}
}
export function setIsRendering(rendering: boolean) {
if (__DEV__) {
isRendering = rendering;
}
}