-
Notifications
You must be signed in to change notification settings - Fork 48.4k
/
Copy pathReactFiberScope.js
214 lines (195 loc) · 5.18 KB
/
ReactFiberScope.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
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
213
214
/**
* Copyright (c) Meta Platforms, Inc. and 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 './ReactInternalTypes';
import type {
ReactScopeInstance,
ReactContext,
ReactScopeQuery,
} from 'shared/ReactTypes';
import {
getPublicInstance,
getInstanceFromNode,
getInstanceFromScope,
} from './ReactFiberConfig';
import {isFiberSuspenseAndTimedOut} from './ReactFiberTreeReflection';
import {HostComponent, ScopeComponent, ContextProvider} from './ReactWorkTags';
import {
enableScopeAPI,
enableRenderableContext,
} from 'shared/ReactFeatureFlags';
function getSuspenseFallbackChild(fiber: Fiber): Fiber | null {
return ((((fiber.child: any): Fiber).sibling: any): Fiber).child;
}
const emptyObject = {};
function collectScopedNodes(
node: Fiber,
fn: ReactScopeQuery,
scopedNodes: Array<any>,
): void {
if (enableScopeAPI) {
if (node.tag === HostComponent) {
const {type, memoizedProps, stateNode} = node;
const instance = getPublicInstance(stateNode);
if (
instance !== null &&
fn(type, memoizedProps || emptyObject, instance) === true
) {
scopedNodes.push(instance);
}
}
let child = node.child;
if (isFiberSuspenseAndTimedOut(node)) {
child = getSuspenseFallbackChild(node);
}
if (child !== null) {
collectScopedNodesFromChildren(child, fn, scopedNodes);
}
}
}
function collectFirstScopedNode(
node: Fiber,
fn: ReactScopeQuery,
): null | Object {
if (enableScopeAPI) {
if (node.tag === HostComponent) {
const {type, memoizedProps, stateNode} = node;
const instance = getPublicInstance(stateNode);
if (instance !== null && fn(type, memoizedProps, instance) === true) {
return instance;
}
}
let child = node.child;
if (isFiberSuspenseAndTimedOut(node)) {
child = getSuspenseFallbackChild(node);
}
if (child !== null) {
return collectFirstScopedNodeFromChildren(child, fn);
}
}
return null;
}
function collectScopedNodesFromChildren(
startingChild: Fiber,
fn: ReactScopeQuery,
scopedNodes: Array<any>,
): void {
let child: null | Fiber = startingChild;
while (child !== null) {
collectScopedNodes(child, fn, scopedNodes);
child = child.sibling;
}
}
function collectFirstScopedNodeFromChildren(
startingChild: Fiber,
fn: ReactScopeQuery,
): Object | null {
let child: null | Fiber = startingChild;
while (child !== null) {
const scopedNode = collectFirstScopedNode(child, fn);
if (scopedNode !== null) {
return scopedNode;
}
child = child.sibling;
}
return null;
}
function collectNearestContextValues<T>(
node: Fiber,
context: ReactContext<T>,
childContextValues: Array<T>,
): void {
if (
node.tag === ContextProvider &&
(enableRenderableContext ? node.type : node.type._context) === context
) {
const contextValue = node.memoizedProps.value;
childContextValues.push(contextValue);
} else {
let child = node.child;
if (isFiberSuspenseAndTimedOut(node)) {
child = getSuspenseFallbackChild(node);
}
if (child !== null) {
collectNearestChildContextValues(child, context, childContextValues);
}
}
}
function collectNearestChildContextValues<T>(
startingChild: Fiber | null,
context: ReactContext<T>,
childContextValues: Array<T>,
): void {
let child = startingChild;
while (child !== null) {
collectNearestContextValues(child, context, childContextValues);
child = child.sibling;
}
}
function DO_NOT_USE_queryAllNodes(
this: $FlowFixMe,
fn: ReactScopeQuery,
): null | Array<Object> {
const currentFiber = getInstanceFromScope(this);
if (currentFiber === null) {
return null;
}
const child = currentFiber.child;
const scopedNodes: Array<any> = [];
if (child !== null) {
collectScopedNodesFromChildren(child, fn, scopedNodes);
}
return scopedNodes.length === 0 ? null : scopedNodes;
}
function DO_NOT_USE_queryFirstNode(
this: $FlowFixMe,
fn: ReactScopeQuery,
): null | Object {
const currentFiber = getInstanceFromScope(this);
if (currentFiber === null) {
return null;
}
const child = currentFiber.child;
if (child !== null) {
return collectFirstScopedNodeFromChildren(child, fn);
}
return null;
}
function containsNode(this: $FlowFixMe, node: Object): boolean {
let fiber = getInstanceFromNode(node);
while (fiber !== null) {
if (fiber.tag === ScopeComponent && fiber.stateNode === this) {
return true;
}
fiber = fiber.return;
}
return false;
}
function getChildContextValues<T>(
this: $FlowFixMe,
context: ReactContext<T>,
): Array<T> {
const currentFiber = getInstanceFromScope(this);
if (currentFiber === null) {
return [];
}
const child = currentFiber.child;
const childContextValues: Array<T> = [];
if (child !== null) {
collectNearestChildContextValues(child, context, childContextValues);
}
return childContextValues;
}
export function createScopeInstance(): ReactScopeInstance {
return {
DO_NOT_USE_queryAllNodes,
DO_NOT_USE_queryFirstNode,
containsNode,
getChildContextValues,
};
}