mirror of
https://github.com/status-im/react-native.git
synced 2025-01-30 19:25:11 +00:00
c25d5948a5
Summary: As we did in the previous diff, here we implemented `EventEmitter`'s ownership model as a `shared_ptr`. This change fixes problem with leaking `WeakObject`s which happens on hot-reload. So, in short: * `EventTargetWrapper` object owns `jsi::WeakObject` that can be converted to actual `jsi::Object` that represent event target in JavaScript realm; * `EventTargetWrapper` and `jsi::WeakObject` objects must be deallocated as soon as native part does not need them anymore; * `EventEmitter` objects retain `EventTarget` objects; * `EventEmitter` can loose event target object in case if assosiated `ShadowNode` got unmounted (not deallocated); in this case `EventEmitter` is loosing possibility to dispatch event even if some mounting-layer code is still retaining it. Reviewed By: mdvacca Differential Revision: D9762755 fbshipit-source-id: 96e989767a32914db9f4627fce51b044c71f257a
245 lines
9.8 KiB
C++
245 lines
9.8 KiB
C++
/**
|
|
* 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.
|
|
*/
|
|
|
|
#include "FabricUIManager.h"
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <fabric/components/view/ViewComponentDescriptor.h>
|
|
#include <fabric/components/view/ViewProps.h>
|
|
#include <fabric/components/view/ViewShadowNode.h>
|
|
#include <fabric/core/componentDescriptor.h>
|
|
#include <fabric/core/LayoutContext.h>
|
|
#include <fabric/core/ShadowNodeFragment.h>
|
|
#include <fabric/debug/DebugStringConvertible.h>
|
|
#include <fabric/debug/DebugStringConvertibleItem.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
// TODO: Kill this flag and remove debug logging.
|
|
const bool isLoggingEnabled = false;
|
|
|
|
static const RawProps rawPropsFromDynamic(const folly::dynamic object) {
|
|
// TODO: Convert this to something smarter, probably returning `std::iterator`.
|
|
RawProps result;
|
|
|
|
if (object.isNull()) {
|
|
return result;
|
|
}
|
|
|
|
assert(object.isObject());
|
|
|
|
for (const auto &pair : object.items()) {
|
|
assert(pair.first.isString());
|
|
result[pair.first.asString()] = pair.second;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
static const std::string componentNameByReactViewName(std::string viewName) {
|
|
// We need this function only for the transition period;
|
|
// eventually, all names will be unified.
|
|
|
|
std::string rctPrefix("RCT");
|
|
if (std::mismatch(rctPrefix.begin(), rctPrefix.end(), viewName.begin()).first == rctPrefix.end()) {
|
|
// If `viewName` has "RCT" prefix, remove it.
|
|
viewName.erase(0, rctPrefix.length());
|
|
}
|
|
|
|
// Fabric uses slightly new names for Text components because of differences
|
|
// in semantic.
|
|
if (viewName == "Text") {
|
|
return "Paragraph";
|
|
}
|
|
if (viewName == "VirtualText") {
|
|
return "Text";
|
|
}
|
|
|
|
if (viewName == "ImageView") {
|
|
return "Image";
|
|
}
|
|
|
|
if (viewName == "AndroidHorizontalScrollView") {
|
|
return "ScrollView";
|
|
}
|
|
|
|
// We need this temporarly for testing purposes until we have proper
|
|
// implementation of core components.
|
|
if (
|
|
viewName == "SinglelineTextInputView" ||
|
|
viewName == "MultilineTextInputView" ||
|
|
viewName == "RefreshControl" ||
|
|
viewName == "SafeAreaView" ||
|
|
viewName == "ScrollContentView" ||
|
|
viewName == "AndroidHorizontalScrollContentView" // Android
|
|
) {
|
|
return "View";
|
|
}
|
|
|
|
return viewName;
|
|
}
|
|
|
|
void FabricUIManager::setComponentDescriptorRegistry(const SharedComponentDescriptorRegistry &componentDescriptorRegistry) {
|
|
componentDescriptorRegistry_ = componentDescriptorRegistry;
|
|
}
|
|
|
|
void FabricUIManager::setDelegate(UIManagerDelegate *delegate) {
|
|
delegate_ = delegate;
|
|
}
|
|
|
|
UIManagerDelegate *FabricUIManager::getDelegate() {
|
|
return delegate_;
|
|
}
|
|
|
|
void FabricUIManager::setDispatchEventToEmptyTargetFunction(std::function<DispatchEventToEmptyTargetFunction> dispatchEventFunction) {
|
|
dispatchEventToEmptyTargetFunction_ = dispatchEventFunction;
|
|
}
|
|
|
|
void FabricUIManager::setDispatchEventToTargetFunction(std::function<DispatchEventToTargetFunction> dispatchEventFunction) {
|
|
dispatchEventToTargetFunction_ = dispatchEventFunction;
|
|
}
|
|
|
|
void FabricUIManager::dispatchEventToTarget(const EventTarget *eventTarget, const std::string &type, const folly::dynamic &payload) const {
|
|
if (eventTarget) {
|
|
dispatchEventToTargetFunction_(
|
|
*eventHandler_,
|
|
*eventTarget,
|
|
const_cast<std::string &>(type),
|
|
const_cast<folly::dynamic &>(payload)
|
|
);
|
|
} else {
|
|
dispatchEventToEmptyTargetFunction_(
|
|
*eventHandler_,
|
|
const_cast<std::string &>(type),
|
|
const_cast<folly::dynamic &>(payload)
|
|
);
|
|
}
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int rootTag, folly::dynamic props, SharedEventTarget eventTarget) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::createNode(tag: " << tag << ", name: " << viewName << ", rootTag: " << rootTag << ", props: " << props << ")";
|
|
|
|
ComponentName componentName = componentNameByReactViewName(viewName);
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[componentName];
|
|
RawProps rawProps = rawPropsFromDynamic(props);
|
|
|
|
SharedShadowNode shadowNode =
|
|
componentDescriptor->createShadowNode({
|
|
.tag = tag,
|
|
.rootTag = rootTag,
|
|
.eventEmitter = componentDescriptor->createEventEmitter(std::move(eventTarget), tag),
|
|
.props = componentDescriptor->cloneProps(nullptr, rawProps)
|
|
});
|
|
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::createNode() -> " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
|
|
if (delegate_) {
|
|
delegate_->uiManagerDidCreateShadowNode(shadowNode);
|
|
}
|
|
|
|
return shadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &shadowNode) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNode(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(*shadowNode, {});
|
|
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNode() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNode &shadowNode) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildren(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
// Assuming semantic: Cloning with same props but empty children.
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(
|
|
*shadowNode,
|
|
{
|
|
.children = ShadowNode::emptySharedShadowNodeSharedList()
|
|
}
|
|
);
|
|
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildren() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode &shadowNode, folly::dynamic props) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewProps(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", props: " << props << ")";
|
|
// Assuming semantic: Cloning with same children and specified props.
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
RawProps rawProps = rawPropsFromDynamic(props);
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(
|
|
*shadowNode,
|
|
{
|
|
.props = componentDescriptor->cloneProps(shadowNode->getProps(), rawProps)
|
|
}
|
|
);
|
|
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewProps() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedShadowNode &shadowNode, folly::dynamic props) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildrenAndProps(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", props: " << props << ")";
|
|
// Assuming semantic: Cloning with empty children and specified props.
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
RawProps rawProps = rawPropsFromDynamic(props);
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(
|
|
*shadowNode,
|
|
{
|
|
.props = componentDescriptor->cloneProps(shadowNode->getProps(), rawProps),
|
|
.children = ShadowNode::emptySharedShadowNodeSharedList()
|
|
}
|
|
);
|
|
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildrenAndProps() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
void FabricUIManager::appendChild(const SharedShadowNode &parentShadowNode, const SharedShadowNode &childShadowNode) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::appendChild(parentShadowNode: " << parentShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", childShadowNode: " << childShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[parentShadowNode];
|
|
componentDescriptor->appendChild(parentShadowNode, childShadowNode);
|
|
}
|
|
|
|
SharedShadowNodeUnsharedList FabricUIManager::createChildSet(int rootTag) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::createChildSet(rootTag: " << rootTag << ")";
|
|
return std::make_shared<SharedShadowNodeList>(SharedShadowNodeList({}));
|
|
}
|
|
|
|
void FabricUIManager::appendChildToSet(const SharedShadowNodeUnsharedList &shadowNodeList, const SharedShadowNode &shadowNode) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::appendChildToSet(shadowNodeList: " << shadowNodeList << ", shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
shadowNodeList->push_back(shadowNode);
|
|
}
|
|
|
|
void FabricUIManager::completeRoot(int rootTag, const SharedShadowNodeUnsharedList &children) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::completeRoot(rootTag: " << rootTag << ", shadowNodeList: " << children << ")";
|
|
|
|
if (delegate_) {
|
|
delegate_->uiManagerDidFinishTransaction(rootTag, children);
|
|
}
|
|
}
|
|
|
|
void FabricUIManager::registerEventHandler(std::shared_ptr<EventHandler> eventHandler) {
|
|
isLoggingEnabled && LOG(INFO) << "FabricUIManager::registerEventHandler(eventHandler: " << eventHandler.get() << ")";
|
|
eventHandler_ = std::move(eventHandler);
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|