This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
attachRendererFiber.js
291 lines (272 loc) · 8.86 KB
/
attachRendererFiber.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
import type {Hook, ReactRenderer, Helpers} from './types';
var getDataFiber = require('./getDataFiber');
var {
ClassComponent,
ContextConsumer,
HostRoot,
} = require('./ReactTypeOfWork');
// Inlined from ReactTypeOfSideEffect
var PerformedWork = 1;
function attachRendererFiber(hook: Hook, rid: string, renderer: ReactRenderer): Helpers {
// This is a slightly annoying indirection.
// It is currently necessary because DevTools wants
// to use unique objects as keys for instances.
// However fibers have two versions.
// We use this set to remember first encountered fiber for
// each conceptual instance.
const opaqueNodes = new Set();
function getOpaqueNode(fiber) {
if (opaqueNodes.has(fiber)) {
return fiber;
}
const {alternate} = fiber;
if (alternate != null && opaqueNodes.has(alternate)) {
return alternate;
}
opaqueNodes.add(fiber);
return fiber;
}
function hasDataChanged(prevFiber, nextFiber) {
if (prevFiber.tag === ClassComponent) {
// Skip if the class performed no work (shouldComponentUpdate bailout).
// eslint-disable-next-line no-bitwise
if ((nextFiber.effectTag & PerformedWork) !== PerformedWork) {
return false;
}
// Only classes have context.
if (prevFiber.stateNode.context !== nextFiber.stateNode.context) {
return true;
}
// Force updating won't update state or props.
if (nextFiber.updateQueue != null && nextFiber.updateQueue.hasForceUpdate) {
return true;
}
}
// Always treat context consumers as changed.
// This ensures we don't skip highlighting their updates.
if (prevFiber.tag === ContextConsumer) {
return true;
}
// Compare the fields that would result in observable changes in DevTools.
// We don't compare type, tag, index, and key, because these are known to match.
return (
prevFiber.memoizedProps !== nextFiber.memoizedProps ||
prevFiber.memoizedState !== nextFiber.memoizedState ||
prevFiber.ref !== nextFiber.ref ||
prevFiber._debugSource !== nextFiber._debugSource
);
}
let pendingEvents = [];
function flushPendingEvents() {
const events = pendingEvents;
pendingEvents = [];
for (let i = 0; i < events.length; i++) {
const event = events[i];
hook.emit(event.type, event);
}
}
function enqueueMount(fiber) {
pendingEvents.push({
internalInstance: getOpaqueNode(fiber),
data: getDataFiber(fiber, getOpaqueNode),
renderer: rid,
type: 'mount',
});
const isRoot = fiber.tag === HostRoot;
if (isRoot) {
pendingEvents.push({
internalInstance: getOpaqueNode(fiber),
renderer: rid,
type: 'root',
});
}
}
function enqueueUpdateIfNecessary(fiber, hasChildOrderChanged) {
if (!hasChildOrderChanged && !hasDataChanged(fiber.alternate, fiber)) {
return;
}
pendingEvents.push({
internalInstance: getOpaqueNode(fiber),
data: getDataFiber(fiber, getOpaqueNode),
renderer: rid,
type: 'update',
});
}
function enqueueUnmount(fiber) {
const isRoot = fiber.tag === HostRoot;
const opaqueNode = getOpaqueNode(fiber);
const event = {
internalInstance: opaqueNode,
renderer: rid,
type: 'unmount',
};
if (isRoot) {
pendingEvents.push(event);
} else {
// Non-root fibers are deleted during the commit phase.
// They are deleted in the child-first order. However
// DevTools currently expects deletions to be parent-first.
// This is why we unshift deletions rather than push them.
pendingEvents.unshift(event);
}
opaqueNodes.delete(opaqueNode);
}
function mountFiber(fiber) {
// Depth-first.
// Logs mounting of children first, parents later.
let node = fiber;
outer: while (true) {
if (node.child) {
node.child.return = node;
node = node.child;
continue;
}
enqueueMount(node);
if (node == fiber) {
return;
}
if (node.sibling) {
node.sibling.return = node.return;
node = node.sibling;
continue;
}
while (node.return) {
node = node.return;
enqueueMount(node);
if (node == fiber) {
return;
}
if (node.sibling) {
node.sibling.return = node.return;
node = node.sibling;
continue outer;
}
}
return;
}
}
function updateFiber(nextFiber, prevFiber) {
let hasChildOrderChanged = false;
if (nextFiber.child !== prevFiber.child) {
// If the first child is different, we need to traverse them.
// Each next child will be either a new child (mount) or an alternate (update).
let nextChild = nextFiber.child;
let prevChildAtSameIndex = prevFiber.child;
while (nextChild) {
// We already know children will be referentially different because
// they are either new mounts or alternates of previous children.
// Schedule updates and mounts depending on whether alternates exist.
// We don't track deletions here because they are reported separately.
if (nextChild.alternate) {
const prevChild = nextChild.alternate;
updateFiber(nextChild, prevChild);
// However we also keep track if the order of the children matches
// the previous order. They are always different referentially, but
// if the instances line up conceptually we'll want to know that.
if (!hasChildOrderChanged && prevChild !== prevChildAtSameIndex) {
hasChildOrderChanged = true;
}
} else {
mountFiber(nextChild);
if (!hasChildOrderChanged) {
hasChildOrderChanged = true;
}
}
// Try the next child.
nextChild = nextChild.sibling;
// Advance the pointer in the previous list so that we can
// keep comparing if they line up.
if (!hasChildOrderChanged && prevChildAtSameIndex != null) {
prevChildAtSameIndex = prevChildAtSameIndex.sibling;
}
}
// If we have no more children, but used to, they don't line up.
if (!hasChildOrderChanged && prevChildAtSameIndex != null) {
hasChildOrderChanged = true;
}
}
enqueueUpdateIfNecessary(nextFiber, hasChildOrderChanged);
}
function walkTree() {
hook.getFiberRoots(rid).forEach(root => {
// Hydrate all the roots for the first time.
mountFiber(root.current);
});
flushPendingEvents();
}
function cleanup() {
// We don't patch any methods so there is no cleanup.
}
function handleCommitFiberUnmount(fiber) {
// This is not recursive.
// We can't traverse fibers after unmounting so instead
// we rely on React telling us about each unmount.
// It will be flushed after the root is committed.
enqueueUnmount(fiber);
}
function handleCommitFiberRoot(root) {
const current = root.current;
const alternate = current.alternate;
if (alternate) {
// TODO: relying on this seems a bit fishy.
const wasMounted = alternate.memoizedState != null && alternate.memoizedState.element != null;
const isMounted = current.memoizedState != null && current.memoizedState.element != null;
if (!wasMounted && isMounted) {
// Mount a new root.
mountFiber(current);
} else if (wasMounted && isMounted) {
// Update an existing root.
updateFiber(current, alternate);
} else if (wasMounted && !isMounted) {
// Unmount an existing root.
enqueueUnmount(current);
}
} else {
// Mount a new root.
mountFiber(current);
}
// We're done here.
flushPendingEvents();
}
// The naming is confusing.
// They deal with opaque nodes (fibers), not elements.
function getNativeFromReactElement(fiber) {
try {
const opaqueNode = fiber;
const hostInstance = renderer.findHostInstanceByFiber(opaqueNode);
return hostInstance;
} catch (err) {
// The fiber might have unmounted by now.
return null;
}
}
function getReactElementFromNative(hostInstance) {
const fiber = renderer.findFiberByHostInstance(hostInstance);
if (fiber != null) {
// TODO: type fibers.
const opaqueNode = getOpaqueNode((fiber: any));
return opaqueNode;
}
return null;
}
return {
getNativeFromReactElement,
getReactElementFromNative,
handleCommitFiberRoot,
handleCommitFiberUnmount,
cleanup,
walkTree,
};
}
module.exports = attachRendererFiber;