Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove 'worklets' dependency on NativeReanimatedModule #6357

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/fabric-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2125,10 +2125,10 @@ SPEC CHECKSUMS:
boost: 4cb898d0bf20404aab1850c656dcea009429d6c1
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
FBLazyVector: d08b51db67e61e1adaed7aefdb43b43f247ee46a
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: 69ef571f3de08433d766d614c73a9838a06bf7eb
fmt: 8083860262b879fc92bca720969c91bdb88ada93
glog: 79d13cd7c34b5d0cea5f4ca72c0e6b462ef8fb4d
hermes-engine: b205fccb3c7b52031e5bdb458a40f85f806bb7e8
RCT-Folly: 4464f4d875961fce86008d45f4ecf6cef6de0740
RCT-Folly: 62f9775ef5ef5819714c7923b4b3a3eb5bf32dab
RCTDeprecation: 8c3d64b4ab77cf28adefa261e04fd205c2715607
RCTRequired: 70f9b55e176be07e234e2efe43b31de14d7cd5ba
RCTTypeSafety: 570d25d58d8795b1a146f5dee4965a05b6fdf8ac
Expand Down Expand Up @@ -2191,7 +2191,7 @@ SPEC CHECKSUMS:
RNCPicker: 7973f617de8809ab9e7577b93ce23d3449fb1ec7
RNFlashList: 2381687e1305c20b7e173e9a980e2b9a7a3973f5
RNGestureHandler: 8781e2529230a1bc3ea8d75e5c3cd071b6c6aed7
RNReanimated: 9e218664699ae35b44889cfc7f58558f34d5409a
RNReanimated: 1ccd1c76398c3c599cf12ad152e8ef2b920aa6b9
RNScreens: de6e57426ba0e6cbc3fb5b4f496e7f08cb2773c2
RNSVG: 6b65086b51556fd9723d5570a3455e865e1304a3
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Expand Down
6 changes: 3 additions & 3 deletions apps/paper-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1915,10 +1915,10 @@ SPEC CHECKSUMS:
boost: 4cb898d0bf20404aab1850c656dcea009429d6c1
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
FBLazyVector: d08b51db67e61e1adaed7aefdb43b43f247ee46a
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: 69ef571f3de08433d766d614c73a9838a06bf7eb
fmt: 8083860262b879fc92bca720969c91bdb88ada93
glog: 79d13cd7c34b5d0cea5f4ca72c0e6b462ef8fb4d
hermes-engine: b205fccb3c7b52031e5bdb458a40f85f806bb7e8
RCT-Folly: 4464f4d875961fce86008d45f4ecf6cef6de0740
RCT-Folly: 62f9775ef5ef5819714c7923b4b3a3eb5bf32dab
RCTDeprecation: 8c3d64b4ab77cf28adefa261e04fd205c2715607
RCTRequired: 70f9b55e176be07e234e2efe43b31de14d7cd5ba
RCTTypeSafety: 570d25d58d8795b1a146f5dee4965a05b6fdf8ac
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/uimanager/UIManager.h>

#include <type_traits>
#include <memory>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand All @@ -15,8 +15,10 @@ using namespace react;

namespace reanimated {

using PropsMap = std::unordered_map<const ShadowNodeFamily *, std::vector<RawProps>>;
using ChildrenMap = std::unordered_map<const ShadowNodeFamily *, std::unordered_set<int>>;
using PropsMap =
std::unordered_map<const ShadowNodeFamily *, std::vector<RawProps>>;
using ChildrenMap =
std::unordered_map<const ShadowNodeFamily *, std::unordered_set<int>>;

RootShadowNode::Unshared cloneShadowTreeWithNewProps(
const RootShadowNode &oldRootNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "CollectionUtils.h"
#include "EventHandlerRegistry.h"
#include "FeaturesConfig.h"
#include "ReanimatedWorkletRuntimeDecorator.h"
#include "Shareables.h"
#include "UIRuntimeDecorator.h"
#include "WorkletEventHandler.h"
Expand All @@ -48,10 +49,12 @@ NativeReanimatedModule::NativeReanimatedModule(
const std::shared_ptr<UIScheduler> &uiScheduler,
const PlatformDepMethodsHolder &platformDepMethodsHolder,
const std::string &valueUnpackerCode,
const bool isBridgeless)
const bool isBridgeless,
const bool isReducedMotion)
: NativeReanimatedModuleSpec(
isBridgeless ? nullptr : jsScheduler->getJSCallInvoker()),
isBridgeless_(isBridgeless),
isReducedMotion_(isReducedMotion),
jsQueue_(jsQueue),
jsScheduler_(jsScheduler),
uiScheduler_(uiScheduler),
Expand Down Expand Up @@ -228,6 +231,7 @@ jsi::Value NativeReanimatedModule::createWorkletRuntime(
auto initializerShareable = extractShareableOrThrow<ShareableWorklet>(
rt, initializer, "[Reanimated] Initializer must be a worklet.");
workletRuntime->runGuarded(initializerShareable);
ReanimatedWorkletRuntimeDecorator::decorate(workletRuntime->getJSIRuntime());
return jsi::Object::createFromHostObject(rt, workletRuntime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
const std::shared_ptr<UIScheduler> &uiScheduler,
const PlatformDepMethodsHolder &platformDepMethodsHolder,
const std::string &valueUnpackerCode,
const bool isBridgeless);
const bool isBridgeless,
const bool isReducedMotion);

~NativeReanimatedModule();

Expand Down Expand Up @@ -164,14 +165,18 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
return *layoutAnimationsManager_;
}

inline jsi::Runtime &getUIRuntime() {
inline jsi::Runtime &getUIRuntime() const {
return uiWorkletRuntime_->getJSIRuntime();
}

inline bool isBridgeless() const {
return isBridgeless_;
}

inline bool isReducedMotion() const {
return isReducedMotion_;
}

private:
void commonInit(const PlatformDepMethodsHolder &platformDepMethodsHolder);

Expand All @@ -185,6 +190,7 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
#endif // RCT_NEW_ARCH_ENABLED

const bool isBridgeless_;
const bool isReducedMotion_;
const std::shared_ptr<MessageQueueThread> jsQueue_;
const std::shared_ptr<JSScheduler> jsScheduler_;
const std::shared_ptr<UIScheduler> uiScheduler_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace reanimated {

void RNRuntimeDecorator::decorate(
jsi::Runtime &rnRuntime,
const std::shared_ptr<NativeReanimatedModule> &nativeReanimatedModule,
const bool isReducedMotion) {
const std::shared_ptr<NativeReanimatedModule> &nativeReanimatedModule) {
rnRuntime.global().setProperty(rnRuntime, "_WORKLET", false);

jsi::Runtime &uiRuntime = nativeReanimatedModule->getUIRuntime();
Expand Down Expand Up @@ -38,7 +37,9 @@ void RNRuntimeDecorator::decorate(
injectReanimatedCppVersion(rnRuntime);

rnRuntime.global().setProperty(
rnRuntime, "_REANIMATED_IS_REDUCED_MOTION", isReducedMotion);
rnRuntime,
"_REANIMATED_IS_REDUCED_MOTION",
nativeReanimatedModule->isReducedMotion());
tjzel marked this conversation as resolved.
Show resolved Hide resolved

rnRuntime.global().setProperty(
rnRuntime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class RNRuntimeDecorator {
public:
static void decorate(
jsi::Runtime &rnRuntime,
const std::shared_ptr<NativeReanimatedModule> &nativeReanimatedModule,
const bool isReducedMotion);
const std::shared_ptr<NativeReanimatedModule> &nativeReanimatedModule);
};

} // namespace reanimated
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "ReanimatedWorkletRuntimeDecorator.h"
#include "JSISerializer.h"
#include "PlatformLogger.h"
#include "ReanimatedJSIUtils.h"
#include "WorkletRuntime.h"

namespace reanimated {

void ReanimatedWorkletRuntimeDecorator::decorate(jsi::Runtime &rt) {
jsi_utils::installJsiFunction(
rt, "_log", [](jsi::Runtime &rt, const jsi::Value &value) {
PlatformLogger::log(stringifyJSIValue(rt, value));
});
}

} // namespace reanimated
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <jsi/jsi.h>

using namespace facebook;

namespace reanimated {

class ReanimatedWorkletRuntimeDecorator {
public:
static void decorate(jsi::Runtime &rt);
};

} // namespace reanimated
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "WorkletRuntimeDecorator.h"
#include "JSISerializer.h"
#include "PlatformLogger.h"
#include "ReanimatedJSIUtils.h"
#include "Shareables.h"
#include "WorkletRuntime.h"
Expand Down Expand Up @@ -66,11 +65,6 @@ void WorkletRuntimeDecorator::decorate(
return jsi::String::createFromUtf8(rt, stringifyJSIValue(rt, value));
});

jsi_utils::installJsiFunction(
rt, "_log", [](jsi::Runtime &rt, const jsi::Value &value) {
PlatformLogger::log(stringifyJSIValue(rt, value));
});

jsi_utils::installJsiFunction(
rt,
"_makeShareableClone",
Expand Down Expand Up @@ -111,7 +105,8 @@ void WorkletRuntimeDecorator::decorate(
for (size_t i = 0; i < argsSize; i++) {
args[i] = argsArray.getValueAtIndex(rt, i);
}
remoteFun.asObject(rt).asFunction(rt).call(rt, const_cast<const jsi::Value *>(args.data()), args.size());
remoteFun.asObject(rt).asFunction(rt).call(
rt, const_cast<const jsi::Value *>(args.data()), args.size());
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ NativeProxy::NativeProxy(
uiScheduler,
getPlatformDependentMethods(),
valueUnpackerCode,
/* isBridgeless */ false)),
/* isBridgeless */ false,
getIsReducedMotion())),
layoutAnimations_(std::move(layoutAnimations)) {
#ifdef RCT_NEW_ARCH_ENABLED
commonInit(fabricUIManager);
Expand All @@ -76,7 +77,8 @@ NativeProxy::NativeProxy(
uiScheduler,
getPlatformDependentMethods(),
valueUnpackerCode,
/* isBridgeless */ true)),
/* isBridgeless */ true,
getIsReducedMotion())),
layoutAnimations_(std::move(layoutAnimations)) {
commonInit(fabricUIManager);
}
Expand Down Expand Up @@ -206,9 +208,7 @@ void NativeProxy::injectCppVersion() {
void NativeProxy::installJSIBindings() {
jsi::Runtime &rnRuntime = *rnRuntime_;
WorkletRuntimeCollector::install(rnRuntime);
auto isReducedMotion = getIsReducedMotion();
RNRuntimeDecorator::decorate(
rnRuntime, nativeReanimatedModule_, isReducedMotion);
RNRuntimeDecorator::decorate(rnRuntime, nativeReanimatedModule_);
#ifndef NDEBUG
checkJavaVersion(rnRuntime);
injectCppVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
// removed temporarily, event listener mechanism needs to be fixed on RN side
// std::shared_ptr<facebook::react::Scheduler> reactScheduler_;
// std::shared_ptr<EventListener> eventListener_;
#endif
#endif // RCT_NEW_ARCH_ENABLED
void installJSIBindings();
#ifdef RCT_NEW_ARCH_ENABLED
void synchronouslyUpdateUIProps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(
SHARED
${REANIMATED_COMMON_SOURCES}
${SOURCES_ANDROID}
"${ANDROID_SRC_DIR}/main/cpp/PlatformLogger.cpp"
tjzel marked this conversation as resolved.
Show resolved Hide resolved
)

target_include_directories(
Expand All @@ -18,6 +19,7 @@ target_include_directories(
"${REANIMATED_COMMON_DIR}/LayoutAnimations"
"${REANIMATED_COMMON_DIR}/NativeModules"
"${REANIMATED_COMMON_DIR}/Tools"
"${REANIMATED_COMMON_DIR}/RuntimeDecorators"
)

target_include_directories(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ add_library(
worklets
SHARED
${WORKLETS_COMMON_SOURCES}
"${ANDROID_SRC_DIR}/main/cpp/PlatformLogger.cpp"
)

# includes
Expand All @@ -27,22 +26,6 @@ target_include_directories(
"${WORKLETS_COMMON_DIR}/WorkletRuntime"
)

target_include_directories(
worklets
PRIVATE
"${ANDROID_SRC_DIR}/main/cpp"
)

target_include_directories(
worklets
PRIVATE
"${REANIMATED_COMMON_DIR}/NativeModules"
"${REANIMATED_COMMON_DIR}/AnimatedSensor"
"${REANIMATED_COMMON_DIR}/Fabric"
"${REANIMATED_COMMON_DIR}/LayoutAnimations"
"${REANIMATED_COMMON_DIR}/Tools"
)

target_include_directories(
worklets
PRIVATE
Expand Down
9 changes: 2 additions & 7 deletions packages/react-native-reanimated/apple/REAModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body

if (jsiRuntime) {
auto nativeReanimatedModule = reanimated::createReanimatedModule(
self.bridge, self.bridge.jsCallInvoker, std::string([valueUnpackerCode UTF8String]));
self, self.bridge, self.bridge.jsCallInvoker, std::string([valueUnpackerCode UTF8String]));
jsi::Runtime &rnRuntime = *jsiRuntime;

[self commonInit:nativeReanimatedModule withRnRuntime:rnRuntime];
Expand All @@ -325,13 +325,8 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body
- (void)commonInit:(std::shared_ptr<NativeReanimatedModule>)nativeReanimatedModule
withRnRuntime:(jsi::Runtime &)rnRuntime
{
#if __has_include(<UIKit/UIAccessibility.h>)
auto isReducedMotion = UIAccessibilityIsReduceMotionEnabled();
#else
auto isReducedMotion = NSWorkspace.sharedWorkspace.accessibilityDisplayShouldReduceMotion;
#endif
WorkletRuntimeCollector::install(rnRuntime);
RNRuntimeDecorator::decorate(rnRuntime, nativeReanimatedModule, isReducedMotion);
RNRuntimeDecorator::decorate(rnRuntime, nativeReanimatedModule);
#ifdef RCT_NEW_ARCH_ENABLED
weakNativeReanimatedModule_ = nativeReanimatedModule;
if (self->_surfacePresenter != nil) {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-reanimated/apple/native/NativeProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace reanimated {

static inline bool getIsReducedMotion();

std::shared_ptr<reanimated::NativeReanimatedModule> createReanimatedModule(
REAModule *reaModule,
RCTBridge *bridge,
const std::shared_ptr<facebook::react::CallInvoker> &jsInvoker,
const std::string &valueUnpackerCode);
Expand Down
Loading
Loading