Skip to content

Commit e7d4ba8

Browse files
authored
feat(Worklets): Create a stub of Worklets Module (software-mansion#6539)
This pull request is one of many which replace - software-mansion#6378 since that PR is extensive and too difficult to review. ## Summary Adding a second Native Module for Worklets. It's initial responsibility for now is forwarding `valueUnpackerCode` to Reanimated Module. While it seems silly, it's a good start since it sets up the whole pipeline of: ```tree typescript ├── android │ └── cpp └── ios └── cpp ``` (I was too lazy to use mermaid, long live `mkdir -p`) Follow up PRs will move more and more responsibilities from Reanimated to Worklets. Requires: - software-mansion#6556 - software-mansion#6557 ## Test plan - [x] All GitHub Actions pass - [x] Compatibility Github Action passes - [x] debug Android works - [x] release Android works - [x] debug iOS works - [x] release iOS works
1 parent 9114183 commit e7d4ba8

File tree

43 files changed

+566
-111
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+566
-111
lines changed

Common/cpp/reanimated/NativeModules/NativeReanimatedModule.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,30 @@ using namespace facebook;
5252
namespace reanimated {
5353

5454
NativeReanimatedModule::NativeReanimatedModule(
55+
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule,
5556
jsi::Runtime &rnRuntime,
5657
const std::shared_ptr<JSScheduler> &jsScheduler,
5758
const std::shared_ptr<MessageQueueThread> &jsQueue,
5859
const std::shared_ptr<UIScheduler> &uiScheduler,
5960
const PlatformDepMethodsHolder &platformDepMethodsHolder,
60-
const std::string &valueUnpackerCode,
6161
const bool isBridgeless,
6262
const bool isReducedMotion)
6363
: NativeReanimatedModuleSpec(
6464
isBridgeless ? nullptr : jsScheduler->getJSCallInvoker()),
6565
isBridgeless_(isBridgeless),
6666
isReducedMotion_(isReducedMotion),
6767
jsQueue_(jsQueue),
68+
nativeWorkletsModule_(nativeWorkletsModule),
6869
jsScheduler_(jsScheduler),
6970
uiScheduler_(uiScheduler),
71+
valueUnpackerCode_(nativeWorkletsModule->getValueUnpackerCode()),
7072
uiWorkletRuntime_(std::make_shared<WorkletRuntime>(
7173
rnRuntime,
7274
jsQueue,
7375
jsScheduler_,
7476
"Reanimated UI runtime",
7577
true /* supportsLocking */,
76-
valueUnpackerCode)),
77-
valueUnpackerCode_(valueUnpackerCode),
78+
valueUnpackerCode_)),
7879
eventHandlerRegistry_(std::make_unique<EventHandlerRegistry>()),
7980
requestRender_(platformDepMethodsHolder.requestRender),
8081
onRenderCallback_([this](const double timestampMs) {

Common/cpp/reanimated/NativeModules/NativeReanimatedModule.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <reanimated/LayoutAnimations/LayoutAnimationsProxy.h>
1414
#endif // RCT_NEW_ARCH_ENABLED
1515

16+
#include <worklets/NativeModules/NativeWorkletsModule.h>
1617
#include <worklets/Registries/EventHandlerRegistry.h>
1718
#include <worklets/Tools/JSScheduler.h>
1819
#include <worklets/Tools/UIScheduler.h>
@@ -32,12 +33,12 @@ namespace reanimated {
3233
class NativeReanimatedModule : public NativeReanimatedModuleSpec {
3334
public:
3435
NativeReanimatedModule(
36+
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule,
3537
jsi::Runtime &rnRuntime,
3638
const std::shared_ptr<JSScheduler> &jsScheduler,
3739
const std::shared_ptr<MessageQueueThread> &jsQueue,
3840
const std::shared_ptr<UIScheduler> &uiScheduler,
3941
const PlatformDepMethodsHolder &platformDepMethodsHolder,
40-
const std::string &valueUnpackerCode,
4142
const bool isBridgeless,
4243
const bool isReducedMotion);
4344

@@ -168,18 +169,23 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
168169
return *layoutAnimationsManager_;
169170
}
170171

171-
inline jsi::Runtime &getUIRuntime() const {
172+
[[nodiscard]] inline jsi::Runtime &getUIRuntime() const {
172173
return uiWorkletRuntime_->getJSIRuntime();
173174
}
174175

175-
inline bool isBridgeless() const {
176+
[[nodiscard]] inline bool isBridgeless() const {
176177
return isBridgeless_;
177178
}
178179

179-
inline bool isReducedMotion() const {
180+
[[nodiscard]] inline bool isReducedMotion() const {
180181
return isReducedMotion_;
181182
}
182183

184+
[[nodiscard]] inline std::shared_ptr<NativeWorkletsModule>
185+
getNativeWorkletsModule() const {
186+
return nativeWorkletsModule_;
187+
}
188+
183189
private:
184190
void commonInit(const PlatformDepMethodsHolder &platformDepMethodsHolder);
185191

@@ -195,10 +201,11 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
195201
const bool isBridgeless_;
196202
const bool isReducedMotion_;
197203
const std::shared_ptr<MessageQueueThread> jsQueue_;
204+
const std::shared_ptr<NativeWorkletsModule> nativeWorkletsModule_;
198205
const std::shared_ptr<JSScheduler> jsScheduler_;
199206
const std::shared_ptr<UIScheduler> uiScheduler_;
207+
const std::string valueUnpackerCode_;
200208
std::shared_ptr<WorkletRuntime> uiWorkletRuntime_;
201-
std::string valueUnpackerCode_;
202209

203210
std::unique_ptr<EventHandlerRegistry> eventHandlerRegistry_;
204211
const RequestRenderFunction requestRender_;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <string>
2+
3+
#ifdef RCT_NEW_ARCH_ENABLED
4+
#include <react/renderer/uimanager/UIManagerBinding.h>
5+
#include <react/renderer/uimanager/primitives.h>
6+
#endif // RCT_NEW_ARCH_ENABLED
7+
8+
#include <worklets/NativeModules/NativeWorkletsModule.h>
9+
10+
#ifdef __ANDROID__
11+
#include <fbjni/fbjni.h>
12+
#endif // __ANDROID__
13+
14+
#include <jsi/jsi.h>
15+
16+
using namespace facebook;
17+
18+
namespace worklets {
19+
20+
NativeWorkletsModule::NativeWorkletsModule(const std::string &valueUnpackerCode)
21+
: NativeWorkletsModuleSpec(nullptr),
22+
valueUnpackerCode_(valueUnpackerCode) {}
23+
24+
NativeWorkletsModule::~NativeWorkletsModule() {}
25+
} // namespace worklets
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <cxxreact/MessageQueueThread.h>
4+
#include <worklets/NativeModules/NativeWorkletsModuleSpec.h>
5+
#include <string>
6+
7+
namespace worklets {
8+
9+
class NativeWorkletsModule : public NativeWorkletsModuleSpec {
10+
public:
11+
explicit NativeWorkletsModule(const std::string &valueUnpackerCode);
12+
13+
~NativeWorkletsModule();
14+
15+
[[nodiscard]] inline std::string getValueUnpackerCode() const {
16+
return valueUnpackerCode_;
17+
}
18+
19+
private:
20+
const std::string valueUnpackerCode_;
21+
};
22+
23+
} // namespace worklets
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <worklets/NativeModules/NativeWorkletsModuleSpec.h>
2+
3+
namespace worklets {
4+
5+
NativeWorkletsModuleSpec::NativeWorkletsModuleSpec(
6+
const std::shared_ptr<CallInvoker> jsInvoker)
7+
: TurboModule("NativeWorklets", jsInvoker) {}
8+
9+
} // namespace worklets
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <ReactCommon/CallInvoker.h>
4+
#include <ReactCommon/TurboModule.h>
5+
#include <memory>
6+
7+
using namespace facebook;
8+
using namespace react;
9+
10+
namespace worklets {
11+
12+
class JSI_EXPORT NativeWorkletsModuleSpec : public TurboModule {
13+
protected:
14+
explicit NativeWorkletsModuleSpec(
15+
const std::shared_ptr<CallInvoker> jsInvoker);
16+
};
17+
18+
} // namespace worklets
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h>
2+
3+
namespace worklets {
4+
5+
void RNRuntimeWorkletDecorator::decorate(
6+
jsi::Runtime &rnRuntime,
7+
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule) {
8+
rnRuntime.global().setProperty(
9+
rnRuntime,
10+
"__workletsModuleProxy",
11+
jsi::Object::createFromHostObject(rnRuntime, nativeWorkletsModule));
12+
}
13+
14+
} // namespace worklets
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <jsi/jsi.h>
4+
#include <worklets/NativeModules/NativeWorkletsModule.h>
5+
#include <memory>
6+
7+
using namespace facebook;
8+
9+
namespace worklets {
10+
11+
class RNRuntimeWorkletDecorator {
12+
// TODO: Rename to `RNRuntimeWorkletsDecorator` or something more suitable.
13+
public:
14+
static void decorate(
15+
jsi::Runtime &rnRuntime,
16+
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule);
17+
};
18+
19+
} // namespace worklets

RNReanimated.podspec

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ Pod::Spec.new do |s|
7878
s.platforms = { :ios => ios_min_version, :tvos => "9.0", :osx => "10.14", :visionos => "1.0" }
7979
s.source = { :git => "https://github.com/software-mansion/react-native-reanimated.git", :tag => "#{s.version}" }
8080

81+
s.subspec "worklets" do |ss|
82+
ss.source_files = "Common/cpp/worklets/**/*.{cpp,h}"
83+
ss.header_dir = "worklets"
84+
ss.header_mappings_dir = "Common/cpp/worklets"
85+
86+
ss.subspec "apple" do |sss|
87+
# Please be careful with the snakes.
88+
# 🐍🐍🐍
89+
# Thank you for your understanding.
90+
sss.source_files = "apple/worklets/**/*.{mm,h,m}"
91+
sss.header_dir = "worklets"
92+
sss.header_mappings_dir = "apple/worklets"
93+
end
94+
end
95+
8196
s.subspec "reanimated" do |ss|
8297
ss.source_files = "Common/cpp/reanimated/**/*.{cpp,h}"
8398
ss.header_dir = "reanimated"
@@ -90,12 +105,6 @@ Pod::Spec.new do |s|
90105
end
91106
end
92107

93-
s.subspec "worklets" do |ss|
94-
ss.source_files = "Common/cpp/worklets/**/*.{cpp,h}"
95-
ss.header_dir = "worklets"
96-
ss.header_mappings_dir = "Common/cpp/worklets"
97-
end
98-
99108
gcc_debug_definitions = "$(inherited)"
100109
if !is_release
101110
gcc_debug_definitions << " HERMES_ENABLE_DEBUGGER=1"

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ task createNativeDepsDirectories() {
461461

462462
task packageNdkLibs(type: Copy) {
463463
from("$buildDir/reanimated-ndk/all")
464-
include("**/libreanimated.so")
465464
include("**/libworklets.so")
465+
include("**/libreanimated.so")
466466
into("$projectDir/src/main/jniLibs")
467467
}
468468

0 commit comments

Comments
 (0)