-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
FiberNavigator.ts
212 lines (167 loc) · 5.23 KB
/
FiberNavigator.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { ForwardRef } from 'react-is';
import { getReactFiberFromNode, Fiber } from '../../utils/getReactFiberFromNode';
const isDOMNode = e => e && typeof e.tagName === 'string' && e.nodeType === Node.ELEMENT_NODE;
export class FiberNavigator {
__fiber: Fiber;
// TODO: Fibers can become stale.
// The only current fiber is the one found on the DOM node.
// There is no way to start at a React Component fiber, go the DOM node,
// get the current fiber, and find your way back to the React Component fiber.
// Probably need to remove fromFiber and re-implement using only DOM node weak map.
static fromFiber = fiber => {
if (!fiber) return null;
const fiberNavigator = new FiberNavigator();
Object.defineProperty(fiberNavigator, '__fiber', {
value: fiber,
enumerable: false,
writable: false,
configurable: false,
});
return fiberNavigator;
};
static fromDOMNode = domNode => {
const fiber = getReactFiberFromNode(domNode);
if (!fiber) return null;
const fiberNavigator = new FiberNavigator();
Object.defineProperty(fiberNavigator, '__fiber', {
value: fiber,
enumerable: false,
writable: false,
configurable: false,
});
return fiberNavigator;
};
get key() {
return this.__fiber.key;
}
get name() {
if (this.isClassComponent || this.isFunctionComponent) {
return this.__fiber.type.displayName || this.__fiber.type.name;
}
if (this.isForwardRef) {
return (
this.__fiber.type.displayName ||
this.__fiber.type.name ||
this.__fiber.type.return?.displayName ||
this.__fiber.type.return?.name
);
}
if (this.isHostComponent) {
return this.__fiber.stateNode.constructor.name;
}
return null;
}
get parent(): FiberNavigator {
return FiberNavigator.fromFiber(this.__fiber.return);
}
get owner() {
return FiberNavigator.fromFiber(this.__fiber._debugOwner);
}
get domNode() {
let fiber = this.__fiber;
do {
if (isDOMNode(fiber.stateNode)) {
return fiber.stateNode;
}
fiber = fiber.child;
} while (fiber);
return null;
}
get instance() {
if (this.isClassComponent) {
return this.__fiber.stateNode;
}
if (this.isFunctionComponent || this.isForwardRef) {
// assumes functional component w/useRef
return this.findDebugHookState(this.__fiber.memoizedState);
}
return null;
}
get props() {
return this.__fiber.memoizedProps;
}
get state() {
return this.__fiber.memoizedState;
}
/**
* Hooks state is represented by a recursive structure where:
* - `memoizedState` is a current value if applicable
* - `next` is next hook in order
* @param node - fiber
*/
findDebugHookState(node) {
if (node && node.memoizedState && node.memoizedState.current && node.memoizedState.current.fluentUIDebug) {
return node.memoizedState.current;
}
if (node === null || node.next === null) {
return null;
}
return this.findDebugHookState(node.next);
}
get reactComponent() {
return this.isHostComponent ? this.owner.elementType : this.elementType;
}
get elementType() {
return this.__fiber.elementType;
}
get fluentUIDebug() {
return this.instance && this.instance.fluentUIDebug ? this.instance.fluentUIDebug : null;
}
get jsxString() {
return `<${this.name} />`;
}
//
// Methods
//
isEqual(fiberNav: FiberNavigator) {
// TODO: do equality check on __fiber instead, however, see fromFiber TODO :/
return !!fiberNav && fiberNav.instance === this.instance;
}
find(condition, move) {
let fiber: FiberNavigator = FiberNavigator.fromFiber(this.__fiber);
while (fiber) {
if (condition(fiber)) {
return fiber;
}
fiber = move(fiber);
}
return null;
}
findOwner(condition) {
return this.find(condition, fiber => fiber.owner);
}
findParent(condition) {
return this.find(condition, fiber => fiber.parent);
}
//
// Component Types
//
get isClassComponent() {
// React.Component subclasses have this flag
// https://reactjs.org/docs/implementation-notes.html
return typeof this.__fiber.type === 'function' && !!this.__fiber.type.prototype.isReactComponent;
}
get isFunctionComponent() {
// React.Component subclasses have this flag
// https://reactjs.org/docs/implementation-notes.html
return typeof this.__fiber.type === 'function' && !this.__fiber.type.prototype.isReactComponent;
}
get isForwardRef() {
return this.__fiber.type?.$$typeof === ForwardRef;
}
get isHostComponent() {
// Host components are platform components (i.e. 'div' on web)
// https://github.com/acdlite/react-fiber-architecture#type-and-key
return typeof this.__fiber.type === 'string';
}
//
// What this fiber component renders
//
get isDOMComponent() {
return !!this.__fiber.child && FiberNavigator.fromFiber(this.__fiber.child).isHostComponent;
}
// https://github.com/facebook/react/blob/16.8.6/packages/react-dom/src/test-utils/ReactTestUtils.js#L193
get isCompositeComponent() {
return this.isDOMComponent ? false : !!this.instance && !!this.instance.render && !!this.instance.setState;
}
}