Repository navigation
Expand file tree
/
Copy pathShadowNode.cpp
More file actions
537 lines (439 loc) · 15.9 KB
/
Copy pathShadowNode.cpp
File metadata and controls
537 lines (439 loc) · 15.9 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* 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.
*/
#include "ShadowNode.h"
#include "DynamicPropsUtilities.h"
#include "ShadowNodeFragment.h"
#include <react/debug/react_native_assert.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/renderer/core/ComponentDescriptor.h>
#include <react/renderer/core/ShadowNodeFragment.h>
#include <react/renderer/debug/DebugStringConvertible.h>
#include <react/renderer/debug/debugStringConvertibleUtils.h>
#include <utility>
namespace facebook::react {
/*
* Runtime shadow node reference updates should only run from one thread at all
* times to avoid having more than one shadow tree updating the current fiber
* tree simultaneously. This thread_local flag allows enabling the updates for
* choses threads.
*/
thread_local bool useRuntimeShadowNodeReferenceUpdateOnThread{false}; // NOLINT
/* static */ void ShadowNode::setUseRuntimeShadowNodeReferenceUpdateOnThread(
bool isEnabled) {
useRuntimeShadowNodeReferenceUpdateOnThread = isEnabled;
}
ShadowNode::SharedListOfShared ShadowNode::emptySharedShadowNodeSharedList() {
static const auto emptySharedShadowNodeSharedList =
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>();
return emptySharedShadowNodeSharedList;
}
/*
* On iOS, this method returns `props` if provided, `sourceShadowNode`'s props
* otherwise. On Android, we forward props in case `sourceShadowNode` hasn't
* been mounted. `Props::rawProps` are merged from `props` to a copy of
* `sourceShadowNode.props_` and returned. This is necessary to enable
* Background Executor and should be removed once reimplementation of JNI layer
* is finished.
*/
Props::Shared ShadowNode::propsForClonedShadowNode(
const ShadowNode& sourceShadowNode,
const Props::Shared& props) {
#ifdef RN_SERIALIZABLE_STATE
bool hasBeenMounted = sourceShadowNode.hasBeenMounted_;
bool sourceNodeHasRawProps = !sourceShadowNode.getProps()->rawProps.empty();
if (!hasBeenMounted && sourceNodeHasRawProps && props) {
auto& castedProps = const_cast<Props&>(*props);
castedProps.rawProps = mergeDynamicProps(
sourceShadowNode.getProps()->rawProps,
props->rawProps,
NullValueStrategy::Override);
return props;
}
#endif
return props ? props : sourceShadowNode.getProps();
}
bool ShadowNode::sameFamily(const ShadowNode& first, const ShadowNode& second) {
return first.family_ == second.family_;
}
#pragma mark - Constructors
ShadowNode::ShadowNode(
const ShadowNodeFragment& fragment,
ShadowNodeFamily::Shared family,
ShadowNodeTraits traits)
:
#if RN_DEBUG_STRING_CONVERTIBLE
revision_(1),
#endif
props_(fragment.props),
children_(
fragment.children ? fragment.children
: emptySharedShadowNodeSharedList()),
state_(fragment.state),
orderIndex_(0),
family_(std::move(family)),
traits_(traits) {
react_native_assert(props_);
react_native_assert(children_);
traits_.set(ShadowNodeTraits::Trait::ChildrenAreShared);
for (const auto& child : *children_) {
child->family_->setParent(family_);
}
updateTraitsIfNeccessary();
// The first node of the family gets its state committed automatically.
family_->setMostRecentState(state_);
}
ShadowNode::ShadowNode(
const ShadowNode& sourceShadowNode,
const ShadowNodeFragment& fragment)
:
#if RN_DEBUG_STRING_CONVERTIBLE
revision_(sourceShadowNode.revision_ + 1),
#endif
props_(propsForClonedShadowNode(sourceShadowNode, fragment.props)),
children_(
fragment.children ? fragment.children : sourceShadowNode.children_),
state_(
fragment.state ? fragment.state
: (ReactNativeFeatureFlags::useShadowNodeStateOnClone()
? sourceShadowNode.state_
: sourceShadowNode.getMostRecentState())),
orderIndex_(sourceShadowNode.orderIndex_),
family_(sourceShadowNode.family_),
traits_(sourceShadowNode.traits_) {
react_native_assert(props_);
react_native_assert(children_);
// State could have been progressed above by checking
// `sourceShadowNode.getMostRecentState()`.
traits_.set(ShadowNodeTraits::Trait::ChildrenAreShared);
if (fragment.children) {
for (const auto& child : *children_) {
child->family_->setParent(family_);
}
updateTraitsIfNeccessary();
}
}
// [macOS Backport react/react-native@923e7ddcaf4d (#52646).
std::shared_ptr<ShadowNode> ShadowNode::clone(
const ShadowNodeFragment& fragment) const {
const auto& family = *family_;
const auto& componentDescriptor = family.componentDescriptor_;
std::optional<RawProps> propsOverride;
{
std::lock_guard<std::mutex> lock(family.nativePropsMutex);
if (family.nativeProps_DEPRECATED != nullptr) {
if (fragment.props == ShadowNodeFragment::propsPlaceholder()) {
propsOverride.emplace(*family.nativeProps_DEPRECATED);
} else {
// TODO: We might need to merge fragment.props with
// `family.nativeProps_DEPRECATED`.
}
}
}
if (propsOverride) {
// Clone existing `props_` with `family.nativeProps_DEPRECATED` to
// apply previously set props via `setNativeProps` API. The parsed
// result escapes the lock so the rest of the clone work runs
// unblocked.
auto propsParserContext = PropsParserContext{family_->getSurfaceId(), {}};
return componentDescriptor.cloneShadowNode(
*this,
{.props = componentDescriptor.cloneProps(
propsParserContext, props_, std::move(*propsOverride)),
.children = fragment.children,
.state = fragment.state});
}
return componentDescriptor.cloneShadowNode(*this, fragment);
}
// macOS]
std::shared_ptr<const ContextContainer> ShadowNode::getContextContainer()
const {
return family_->componentDescriptor_.getContextContainer();
}
#pragma mark - Getters
ComponentName ShadowNode::getComponentName() const {
return family_->getComponentName();
}
ComponentHandle ShadowNode::getComponentHandle() const {
return family_->getComponentHandle();
}
const std::vector<std::shared_ptr<const ShadowNode>>& ShadowNode::getChildren()
const {
return *children_;
}
ShadowNodeTraits ShadowNode::getTraits() const {
return traits_;
}
const Props::Shared& ShadowNode::getProps() const {
return props_;
}
const SharedEventEmitter& ShadowNode::getEventEmitter() const {
return family_->eventEmitter_;
}
jsi::Value ShadowNode::getInstanceHandle(jsi::Runtime& runtime) const {
return family_->getInstanceHandle(runtime);
}
Tag ShadowNode::getTag() const {
return family_->tag_;
}
SurfaceId ShadowNode::getSurfaceId() const {
return family_->surfaceId_;
}
const ComponentDescriptor& ShadowNode::getComponentDescriptor() const {
return family_->componentDescriptor_;
}
const State::Shared& ShadowNode::getState() const {
return state_;
}
State::Shared ShadowNode::getMostRecentState() const {
return family_->getMostRecentState();
}
int ShadowNode::getOrderIndex() const {
return orderIndex_;
}
void ShadowNode::sealRecursive() const {
if (getSealed()) {
return;
}
seal();
props_->seal();
for (const auto& child : *children_) {
child->sealRecursive();
}
}
#pragma mark - Mutating Methods
void ShadowNode::appendChild(const std::shared_ptr<const ShadowNode>& child) {
ensureUnsealed();
cloneChildrenIfShared();
auto& children =
const_cast<std::vector<std::shared_ptr<const ShadowNode>>&>(*children_);
children.push_back(child);
child->family_->setParent(family_);
updateTraitsIfNeccessary();
}
void ShadowNode::replaceChild(
const ShadowNode& oldChild,
const std::shared_ptr<const ShadowNode>& newChild,
size_t suggestedIndex) {
ensureUnsealed();
cloneChildrenIfShared();
newChild->family_->setParent(family_);
auto& children =
const_cast<std::vector<std::shared_ptr<const ShadowNode>>&>(*children_);
auto size = children.size();
if (suggestedIndex != std::numeric_limits<size_t>::max() &&
suggestedIndex < size) {
// If provided `suggestedIndex` is accurate,
// replacing in place using the index.
if (children.at(suggestedIndex).get() == &oldChild) {
children[suggestedIndex] = newChild;
return;
}
}
for (size_t index = 0; index < size; index++) {
if (children.at(index).get() == &oldChild) {
children[index] = newChild;
return;
}
}
react_native_assert(false && "Child to replace was not found.");
updateTraitsIfNeccessary();
}
void ShadowNode::cloneChildrenIfShared() {
if (!traits_.check(ShadowNodeTraits::Trait::ChildrenAreShared)) {
return;
}
traits_.unset(ShadowNodeTraits::Trait::ChildrenAreShared);
children_ = std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
*children_);
}
void ShadowNode::updateTraitsIfNeccessary() {
if (ReactNativeFeatureFlags::enableViewCulling()) {
if (traits_.check(ShadowNodeTraits::Trait::Unstable_uncullableView)) {
return;
}
for (const auto& child : *children_) {
if (child->getTraits().check(
ShadowNodeTraits::Trait::Unstable_uncullableView) ||
child->getTraits().check(
ShadowNodeTraits::Trait::Unstable_uncullableTrace)) {
traits_.set(ShadowNodeTraits::Trait::Unstable_uncullableTrace);
return;
}
}
traits_.unset(ShadowNodeTraits::Trait::Unstable_uncullableTrace);
}
}
void ShadowNode::setMounted(bool mounted) const {
if (mounted) {
family_->setMostRecentState(getState());
family_->setMounted();
hasBeenMounted_ = mounted;
}
family_->eventEmitter_->setEnabled(mounted);
}
bool ShadowNode::getHasBeenPromoted() const {
return hasBeenMounted_.load();
}
void ShadowNode::setRuntimeShadowNodeReference(
const std::shared_ptr<ShadowNodeWrapper>& runtimeShadowNodeReference)
const {
runtimeShadowNodeReference_ = runtimeShadowNodeReference;
}
void ShadowNode::updateRuntimeShadowNodeReference(
const std::shared_ptr<const ShadowNode>& destinationShadowNode) const {
if (auto reference = runtimeShadowNodeReference_.lock()) {
reference->shadowNode = destinationShadowNode;
}
}
void ShadowNode::transferRuntimeShadowNodeReference(
const std::shared_ptr<const ShadowNode>& destinationShadowNode) const {
destinationShadowNode->runtimeShadowNodeReference_ =
runtimeShadowNodeReference_;
if (!ReactNativeFeatureFlags::updateRuntimeShadowNodeReferencesOnCommit()) {
updateRuntimeShadowNodeReference(destinationShadowNode);
}
}
void ShadowNode::transferRuntimeShadowNodeReference(
const std::shared_ptr<const ShadowNode>& destinationShadowNode,
const ShadowNodeFragment& fragment) const {
if ((ReactNativeFeatureFlags::updateRuntimeShadowNodeReferencesOnCommit() ||
useRuntimeShadowNodeReferenceUpdateOnThread) &&
fragment.runtimeShadowNodeReference) {
transferRuntimeShadowNodeReference(destinationShadowNode);
}
}
const ShadowNodeFamily& ShadowNode::getFamily() const {
return *family_;
}
ShadowNodeFamily::Shared ShadowNode::getFamilyShared() const {
return family_;
}
std::shared_ptr<ShadowNode> ShadowNode::cloneTree(
const ShadowNodeFamily& shadowNodeFamily,
const std::function<std::shared_ptr<ShadowNode>(
const ShadowNode& oldShadowNode)>& callback) const {
auto ancestors = shadowNodeFamily.getAncestors(*this);
if (ancestors.empty()) {
return std::shared_ptr<ShadowNode>{nullptr};
}
auto& parent = ancestors.back();
auto& oldShadowNode = parent.first.get().getChildren().at(parent.second);
auto newShadowNode = callback(*oldShadowNode);
react_native_assert(
newShadowNode &&
"`callback` returned `nullptr` which is not allowed value.");
auto childNode = newShadowNode;
for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it) {
auto& parentNode = it->first.get();
auto childIndex = it->second;
auto children = parentNode.getChildren();
react_native_assert(
ShadowNode::sameFamily(*children.at(childIndex), *childNode));
children[childIndex] = childNode;
childNode = parentNode.clone(
{.children =
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
children)});
}
return std::const_pointer_cast<ShadowNode>(childNode);
}
namespace {
std::shared_ptr<ShadowNode> cloneMultipleRecursive(
const ShadowNode& shadowNode,
const std::unordered_set<const ShadowNodeFamily*>& familiesToUpdate,
const std::unordered_map<const ShadowNodeFamily*, int>& childrenCount,
const std::function<std::shared_ptr<
ShadowNode>(const ShadowNode&, const ShadowNodeFragment&)>& callback) {
const auto* family = &shadowNode.getFamily();
auto& children = shadowNode.getChildren();
std::shared_ptr<std::vector<std::shared_ptr<const ShadowNode>>> newChildren;
auto count = childrenCount.at(family);
for (size_t i = 0; count > 0 && i < children.size(); i++) {
const auto childFamily = &children[i]->getFamily();
if (childrenCount.contains(childFamily)) {
count--;
if (!newChildren) {
newChildren =
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
children);
}
(*newChildren)[i] = cloneMultipleRecursive(
*children[i], familiesToUpdate, childrenCount, callback);
}
}
ShadowNodeFragment fragment{.children = newChildren};
if (familiesToUpdate.contains(family)) {
return callback(shadowNode, fragment);
}
return shadowNode.clone(fragment);
}
} // namespace
std::shared_ptr<ShadowNode> ShadowNode::cloneMultiple(
const std::unordered_set<const ShadowNodeFamily*>& familiesToUpdate,
const std::function<std::shared_ptr<ShadowNode>(
const ShadowNode& oldShadowNode,
const ShadowNodeFragment& fragment)>& callback) const {
std::unordered_map<const ShadowNodeFamily*, int> childrenCount;
for (const auto& family : familiesToUpdate) {
if (childrenCount.contains(family)) {
continue;
}
childrenCount[family] = 0;
auto ancestor = family->parent_.lock();
while ((ancestor != nullptr) && ancestor != family_) {
auto ancestorIt = childrenCount.find(ancestor.get());
if (ancestorIt != childrenCount.end()) {
ancestorIt->second++;
break;
}
childrenCount[ancestor.get()] = 1;
ancestor = ancestor->parent_.lock();
}
if (ancestor == family_) {
childrenCount[ancestor.get()]++;
}
}
if (childrenCount.empty()) {
return nullptr;
}
return cloneMultipleRecursive(
*this, familiesToUpdate, childrenCount, callback);
}
#pragma mark - DebugStringConvertible
#if RN_DEBUG_STRING_CONVERTIBLE
std::string ShadowNode::getDebugName() const {
return getComponentName();
}
std::string ShadowNode::getDebugValue() const {
return "r" + std::to_string(revision_) + "/sr" +
std::to_string(state_ ? state_->getRevision() : 0) +
(getSealed() ? "/sealed" : "") +
(getProps()->nativeId.empty() ? "" : "/id=" + getProps()->nativeId);
}
SharedDebugStringConvertibleList ShadowNode::getDebugChildren() const {
auto debugChildren = SharedDebugStringConvertibleList{};
for (const auto& child : *children_) {
auto debugChild =
std::dynamic_pointer_cast<const DebugStringConvertible>(child);
if (debugChild) {
debugChildren.push_back(debugChild);
}
}
return debugChildren;
}
SharedDebugStringConvertibleList ShadowNode::getDebugProps() const {
return props_->getDebugProps() +
SharedDebugStringConvertibleList{
debugStringConvertibleItem("tag", std::to_string(getTag()))};
}
#endif
// Explicitly define destructors here, as they have to exist in order to act as
// a "key function" for the ShadowNodeWrapper class -- this allows for RTTI to
// work properly across dynamic library boundaries (i.e. dynamic_cast that is
// used by getNativeState method)
ShadowNodeWrapper::~ShadowNodeWrapper() = default;
} // namespace facebook::react