mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
b28ddede7b
Summary: This diff adds a new variable called "DisplayMode" into SurfaceHandler.cpp and FacebookAppRouteHandler.js. The purpose of DisplayMode is for the native pre-render system to notify React that the a surface is either being "pre-rendered" or "rendered" When the surface is being "pre-rendered" (displayMode == "SUSPENDED"), react will create and commit React Trees, but it will not execute use-effect callbacks When the surface is being "rendered" (displayMode == "VISIBLE"), react will create and commit React Trees and it will not execute all use-effect callbacks that weren't executed during "pre-rendering" By default surfaces are going to be rendered with displayMode == "VISIBLE". This diff should not create any change of behavior for now, this is the infra required to integrate the new offScreen API the react team is working on for pre-rendering system changelog: [internal] internal Reviewed By: yungsters Differential Revision: D27614664 fbshipit-source-id: f1f42fdf174c2ffa74174feb1873f1d5d46e7a95
153 lines
4.2 KiB
C++
153 lines
4.2 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <folly/dynamic.h>
|
|
#include <jsi/JSIDynamic.h>
|
|
#include <jsi/jsi.h>
|
|
#include <react/debug/react_native_assert.h>
|
|
#include <react/renderer/core/EventHandler.h>
|
|
#include <react/renderer/core/ShadowNode.h>
|
|
|
|
namespace facebook::react {
|
|
|
|
using BackgroundExecutor =
|
|
std::function<void(std::function<void()> &&callback)>;
|
|
|
|
struct EventHandlerWrapper : public EventHandler {
|
|
EventHandlerWrapper(jsi::Function eventHandler)
|
|
: callback(std::move(eventHandler)) {}
|
|
|
|
jsi::Function callback;
|
|
};
|
|
|
|
struct ShadowNodeWrapper : public jsi::HostObject {
|
|
ShadowNodeWrapper(SharedShadowNode shadowNode)
|
|
: shadowNode(std::move(shadowNode)) {}
|
|
|
|
ShadowNode::Shared shadowNode;
|
|
};
|
|
|
|
struct ShadowNodeListWrapper : public jsi::HostObject {
|
|
ShadowNodeListWrapper(SharedShadowNodeUnsharedList shadowNodeList)
|
|
: shadowNodeList(shadowNodeList) {}
|
|
|
|
SharedShadowNodeUnsharedList shadowNodeList;
|
|
};
|
|
|
|
inline static ShadowNode::Shared shadowNodeFromValue(
|
|
jsi::Runtime &runtime,
|
|
jsi::Value const &value) {
|
|
if (value.isNull()) {
|
|
return nullptr;
|
|
}
|
|
|
|
return value.getObject(runtime)
|
|
.getHostObject<ShadowNodeWrapper>(runtime)
|
|
->shadowNode;
|
|
}
|
|
|
|
inline static jsi::Value valueFromShadowNode(
|
|
jsi::Runtime &runtime,
|
|
const ShadowNode::Shared &shadowNode) {
|
|
return jsi::Object::createFromHostObject(
|
|
runtime, std::make_shared<ShadowNodeWrapper>(shadowNode));
|
|
}
|
|
|
|
inline static SharedShadowNodeUnsharedList shadowNodeListFromValue(
|
|
jsi::Runtime &runtime,
|
|
jsi::Value const &value) {
|
|
return value.getObject(runtime)
|
|
.getHostObject<ShadowNodeListWrapper>(runtime)
|
|
->shadowNodeList;
|
|
}
|
|
|
|
inline static ShadowNode::UnsharedListOfShared shadowNodeListFromWeakList(
|
|
ShadowNode::UnsharedListOfWeak const &weakShadowNodeList) {
|
|
auto result = std::make_shared<ShadowNode::ListOfShared>();
|
|
for (auto const &weakShadowNode : *weakShadowNodeList) {
|
|
auto sharedShadowNode = weakShadowNode.lock();
|
|
if (!sharedShadowNode) {
|
|
return nullptr;
|
|
}
|
|
result->push_back(sharedShadowNode);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
inline static ShadowNode::UnsharedListOfWeak weakShadowNodeListFromValue(
|
|
jsi::Runtime &runtime,
|
|
jsi::Value const &value) {
|
|
auto shadowNodeList = value.getObject(runtime)
|
|
.getHostObject<ShadowNodeListWrapper>(runtime)
|
|
->shadowNodeList;
|
|
|
|
auto weakShadowNodeList = std::make_shared<ShadowNode::ListOfWeak>();
|
|
for (auto const &shadowNode : *shadowNodeList) {
|
|
weakShadowNodeList->push_back(shadowNode);
|
|
}
|
|
|
|
return weakShadowNodeList;
|
|
}
|
|
|
|
inline static jsi::Value valueFromShadowNodeList(
|
|
jsi::Runtime &runtime,
|
|
const SharedShadowNodeUnsharedList &shadowNodeList) {
|
|
return jsi::Object::createFromHostObject(
|
|
runtime, std::make_unique<ShadowNodeListWrapper>(shadowNodeList));
|
|
}
|
|
|
|
inline static Tag tagFromValue(jsi::Value const &value) {
|
|
return (Tag)value.getNumber();
|
|
}
|
|
|
|
inline static SharedEventTarget eventTargetFromValue(
|
|
jsi::Runtime &runtime,
|
|
jsi::Value const &eventTargetValue,
|
|
jsi::Value const &tagValue) {
|
|
react_native_assert(!eventTargetValue.isNull());
|
|
if (eventTargetValue.isNull()) {
|
|
return nullptr;
|
|
}
|
|
return std::make_shared<EventTarget>(
|
|
runtime, eventTargetValue, tagFromValue(tagValue));
|
|
}
|
|
|
|
inline static SurfaceId surfaceIdFromValue(
|
|
jsi::Runtime &runtime,
|
|
jsi::Value const &value) {
|
|
return (SurfaceId)value.getNumber();
|
|
}
|
|
|
|
inline static int displayModeToInt(DisplayMode const value) {
|
|
// the result of this method should be in sync with
|
|
// Libraries/ReactNative/DisplayMode.js
|
|
switch (value) {
|
|
case DisplayMode::Visible:
|
|
return 1;
|
|
case DisplayMode::Suspended:
|
|
return 2;
|
|
case DisplayMode::Hidden:
|
|
return 3;
|
|
}
|
|
}
|
|
|
|
inline static std::string stringFromValue(
|
|
jsi::Runtime &runtime,
|
|
jsi::Value const &value) {
|
|
return value.getString(runtime).utf8(runtime);
|
|
}
|
|
|
|
inline static folly::dynamic commandArgsFromValue(
|
|
jsi::Runtime &runtime,
|
|
jsi::Value const &value) {
|
|
return jsi::dynamicFromValue(runtime, value);
|
|
}
|
|
|
|
} // namespace facebook::react
|