Skip to content

Commit e9dc87e

Browse files
authored
Migrate from react-native-reanimated to react-native-worklets with backward compatibility (#731)
Co-authored-by: war-in <[email protected]>
1 parent 97097d5 commit e9dc87e

15 files changed

+284
-73
lines changed

RNLiveMarkdown.podspec

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ react_native_json = JSON.parse(File.read(File.join(react_native_node_modules_dir
55
react_native_minor_version = react_native_json['version'].split('.')[1].to_i
66

77
pods_root = Pod::Config.instance.project_pods_root
8-
react_native_reanimated_node_modules_dir = ENV['REACT_NATIVE_REANIMATED_NODE_MODULES_DIR'] || File.dirname(`cd "#{Pod::Config.instance.installation_root.to_s}" && node --print "require.resolve('react-native-reanimated/package.json')"`)
9-
react_native_reanimated_node_modules_dir_from_pods_root = Pathname.new(react_native_reanimated_node_modules_dir).relative_path_from(pods_root).to_s
8+
9+
worklets_installed = system(%Q[
10+
cd "#{Pod::Config.instance.installation_root}" &&
11+
node -e "require.resolve('react-native-worklets/package.json')" > /dev/null 2>&1
12+
])
13+
react_native_worklets_path = `cd "#{Pod::Config.instance.installation_root.to_s}" && node --print "require.resolve('react-native-worklets/package.json')"`
14+
worklets_installed = react_native_worklets_path != ""
15+
worklets_package_name = worklets_installed ? 'react-native-worklets' : 'react-native-reanimated'
16+
17+
react_native_worklets_or_reanimated_node_modules_dir = ENV['REACT_NATIVE_WORKLETS_NODE_MODULES_DIR'] || ENV['REACT_NATIVE_REANIMATED_NODE_MODULES_DIR'] ||
18+
File.dirname(`cd "#{Pod::Config.instance.installation_root.to_s}" && node --print "require.resolve('#{worklets_package_name}/package.json')"`)
19+
react_native_worklets_or_reanimated_node_modules_dir_from_pods_root = Pathname.new(react_native_worklets_or_reanimated_node_modules_dir).relative_path_from(pods_root).to_s
1020

1121
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
1222
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
@@ -24,15 +34,23 @@ Pod::Spec.new do |s|
2434

2535
s.source_files = "apple/**/*.{h,m,mm}", "cpp/**/*.{h,cpp}"
2636

27-
s.dependency "RNReanimated/worklets"
37+
if worklets_installed
38+
s.dependency "RNWorklets"
39+
else
40+
s.dependency "RNReanimated/worklets"
41+
end
2842

29-
s.xcconfig = {
43+
xcconfig = {
3044
"OTHER_CFLAGS" => "$(inherited) -DREACT_NATIVE_MINOR_VERSION=#{react_native_minor_version}",
3145
"HEADER_SEARCH_PATHS" => [
32-
"\"$(PODS_ROOT)/#{react_native_reanimated_node_modules_dir_from_pods_root}/apple\"",
33-
"\"$(PODS_ROOT)/#{react_native_reanimated_node_modules_dir_from_pods_root}/Common/cpp\"",
46+
"\"$(PODS_ROOT)/#{react_native_worklets_or_reanimated_node_modules_dir_from_pods_root}/apple\"",
47+
"\"$(PODS_ROOT)/#{react_native_worklets_or_reanimated_node_modules_dir_from_pods_root}/Common/cpp\"",
3448
].join(' '),
3549
}
50+
if worklets_installed
51+
xcconfig["OTHER_CFLAGS"] << " -DWORKLETS_INSTALLED=1"
52+
end
53+
s.xcconfig = xcconfig
3654

3755
s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/cpp\"" }
3856

android/build.gradle

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def getReactNativeMinorVersion() {
6767
}
6868

6969
def REACT_NATIVE_MINOR_VERSION = getReactNativeMinorVersion()
70+
def workletsInstalled = rootProject.findProject(':react-native-worklets') != null
7071

7172
android {
7273
if (supportsNamespace()) {
@@ -94,7 +95,9 @@ android {
9495
arguments "-DANDROID_STL=c++_shared",
9596
"-DANDROID_TOOLCHAIN=clang",
9697
"-DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION}",
97-
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
98+
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
99+
"-DREACT_NATIVE_ROOT_DIR=${resolveReactNativeDirectory().path}",
100+
"-DWORKLETS_INSTALLED=${workletsInstalled}"
98101
abiFilters (*reactNativeArchitectures())
99102
}
100103
}
@@ -177,14 +180,24 @@ repositories {
177180
dependencies {
178181
implementation "com.facebook.react:react-android" // version substituted by RNGP
179182
implementation "com.facebook.react:hermes-android" // version substituted by RNGP
180-
implementation project(":react-native-reanimated")
183+
184+
if (workletsInstalled) {
185+
implementation project(":react-native-worklets")
186+
} else {
187+
implementation project(":react-native-reanimated")
188+
}
181189
}
182190

183191
// This fixes linking errors due to undefined symbols from libworklets.so.
184192
// During Gradle Sync, Android Gradle Plugin runs Prefab and treats worklets
185193
// like a header-only library. During build, config files are not regenerated
186194
// because the cache key does not change and AGP thinks that they are up-to-date.
187195
afterEvaluate {
188-
prepareKotlinBuildScriptModel.dependsOn(":react-native-reanimated:prefabDebugPackage")
189-
prepareKotlinBuildScriptModel.dependsOn(":react-native-reanimated:prefabReleasePackage")
196+
if (workletsInstalled) {
197+
prepareKotlinBuildScriptModel.dependsOn(":react-native-worklets:prefabDebugPackage")
198+
prepareKotlinBuildScriptModel.dependsOn(":react-native-worklets:prefabReleasePackage")
199+
} else {
200+
prepareKotlinBuildScriptModel.dependsOn(":react-native-reanimated:prefabDebugPackage")
201+
prepareKotlinBuildScriptModel.dependsOn(":react-native-reanimated:prefabReleasePackage")
202+
}
190203
}

android/src/main/cpp/CMakeLists.txt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ cmake_minimum_required(VERSION 3.13)
44

55
set(CMAKE_VERBOSE_MAKEFILE on)
66

7+
if(WORKLETS_INSTALLED)
8+
include("${REACT_NATIVE_ROOT_DIR}/ReactAndroid/cmake-utils/folly-flags.cmake")
9+
add_compile_options(${folly_FLAGS})
10+
endif()
11+
712
add_compile_options(-fvisibility=hidden -fexceptions -frtti)
813

914
string(APPEND CMAKE_CXX_FLAGS " -DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION}")
@@ -15,16 +20,32 @@ file(GLOB CPP_SRC CONFIGURE_DEPENDS "${CPP_DIR}/*.cpp")
1520

1621
add_library(${CMAKE_PROJECT_NAME} SHARED ${ANDROID_SRC} ${CPP_SRC})
1722

18-
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CPP_DIR})
23+
if(WORKLETS_INSTALLED)
24+
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CPP_DIR} "${REACT_NATIVE_ROOT_DIR}/ReactCommon/jsiexecutor")
25+
else()
26+
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CPP_DIR})
27+
endif()
1928

2029
find_package(fbjni REQUIRED CONFIG)
2130
find_package(ReactAndroid REQUIRED CONFIG)
22-
find_package(react-native-reanimated REQUIRED CONFIG)
31+
32+
if(WORKLETS_INSTALLED)
33+
find_package(react-native-worklets REQUIRED CONFIG)
34+
add_definitions(-DWORKLETS_INSTALLED)
35+
else()
36+
find_package(react-native-reanimated REQUIRED CONFIG)
37+
endif()
38+
2339

2440
target_link_libraries(
2541
${CMAKE_PROJECT_NAME}
2642
fbjni::fbjni
2743
ReactAndroid::jsi
2844
ReactAndroid::reactnative
29-
react-native-reanimated::worklets
3045
)
46+
47+
if(WORKLETS_INSTALLED)
48+
target_link_libraries(${CMAKE_PROJECT_NAME} react-native-worklets::worklets)
49+
else()
50+
target_link_libraries(${CMAKE_PROJECT_NAME} react-native-reanimated::worklets)
51+
endif()

apple/MarkdownParser.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ @implementation MarkdownParser {
1919
const auto &markdownRuntime = expensify::livemarkdown::getMarkdownRuntime();
2020
jsi::Runtime &rt = markdownRuntime->getJSIRuntime();
2121

22+
#ifdef WORKLETS_INSTALLED
23+
std::shared_ptr<SerializableWorklet> markdownWorklet;
24+
#else
2225
std::shared_ptr<ShareableWorklet> markdownWorklet;
26+
#endif
2327
try {
2428
markdownWorklet = expensify::livemarkdown::getMarkdownWorklet([parserId intValue]);
2529
} catch (const std::out_of_range &error) {

cpp/MarkdownGlobal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ std::shared_ptr<WorkletRuntime> getMarkdownRuntime() {
1717
return globalMarkdownWorkletRuntime;
1818
}
1919

20-
std::unordered_map<int, std::shared_ptr<ShareableWorklet>> globalMarkdownShareableWorklets;
20+
std::unordered_map<int, std::shared_ptr<SerializableWorklet>> globalMarkdownShareableWorklets;
2121
std::mutex globalMarkdownShareableWorkletsMutex;
2222
int nextParserId = 1;
2323

24-
const int registerMarkdownWorklet(const std::shared_ptr<ShareableWorklet> &markdownWorklet) {
24+
const int registerMarkdownWorklet(const std::shared_ptr<SerializableWorklet> &markdownWorklet) {
2525
assert(markdownWorklet != nullptr);
2626
auto parserId = nextParserId++;
2727
std::unique_lock<std::mutex> lock(globalMarkdownShareableWorkletsMutex);
@@ -34,7 +34,7 @@ void unregisterMarkdownWorklet(const int parserId) {
3434
globalMarkdownShareableWorklets.erase(parserId);
3535
}
3636

37-
std::shared_ptr<ShareableWorklet> getMarkdownWorklet(const int parserId) {
37+
std::shared_ptr<SerializableWorklet> getMarkdownWorklet(const int parserId) {
3838
std::unique_lock<std::mutex> lock(globalMarkdownShareableWorkletsMutex);
3939
return globalMarkdownShareableWorklets.at(parserId);
4040
}

cpp/MarkdownGlobal.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ using namespace worklets;
1010
namespace expensify {
1111
namespace livemarkdown {
1212

13+
#ifndef WORKLETS_INSTALLED
14+
using SerializableWorklet = ShareableWorklet; // ShareableWorklet was renamed to SerializableWorklet
15+
#endif
16+
1317
void setMarkdownRuntime(const std::shared_ptr<WorkletRuntime> &markdownWorkletRuntime);
1418

1519
std::shared_ptr<WorkletRuntime> getMarkdownRuntime();
1620

17-
const int registerMarkdownWorklet(const std::shared_ptr<ShareableWorklet> &markdownWorklet);
21+
const int registerMarkdownWorklet(const std::shared_ptr<SerializableWorklet> &markdownWorklet);
1822

1923
void unregisterMarkdownWorklet(const int parserId);
2024

21-
std::shared_ptr<ShareableWorklet> getMarkdownWorklet(const int parserId);
25+
std::shared_ptr<SerializableWorklet> getMarkdownWorklet(const int parserId);
2226

2327
} // namespace livemarkdown
2428
} // namespace expensify

cpp/RuntimeDecorator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ void injectJSIBindings(jsi::Runtime &rt) {
2323
jsi::PropNameID::forAscii(rt, "jsi_registerMarkdownWorklet"),
2424
1,
2525
[](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value {
26-
auto parserId = registerMarkdownWorklet(extractShareableOrThrow<ShareableWorklet>(rt, args[0]));
26+
#ifdef WORKLETS_INSTALLED
27+
auto worklet = extractSerializableOrThrow<SerializableWorklet>(rt, args[0]);
28+
#else
29+
auto worklet = extractShareableOrThrow<SerializableWorklet>(rt, args[0]);
30+
#endif
31+
auto parserId = registerMarkdownWorklet(worklet);
2732
return jsi::Value(parserId);
2833
}));
2934

example/babel.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
const path = require('path');
22
const pak = require('../package.json');
33

4+
let workletsPlugin = null;
5+
try {
6+
require.resolve('react-native-worklets');
7+
workletsPlugin = 'react-native-worklets/plugin';
8+
} catch (e) {
9+
workletsPlugin = 'react-native-reanimated/plugin';
10+
}
11+
412
module.exports = {
513
presets: ['module:@react-native/babel-preset'],
614
plugins: [
@@ -13,6 +21,6 @@ module.exports = {
1321
},
1422
},
1523
],
16-
'react-native-reanimated/plugin',
24+
workletsPlugin,
1725
],
1826
};

example/ios/Podfile.lock

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,7 +2253,7 @@ PODS:
22532253
- React-perflogger (= 0.81.4)
22542254
- React-utils (= 0.81.4)
22552255
- SocketRocket
2256-
- RNLiveMarkdown (0.1.303):
2256+
- RNLiveMarkdown (0.1.305):
22572257
- boost
22582258
- DoubleConversion
22592259
- fast_float
@@ -2279,10 +2279,10 @@ PODS:
22792279
- ReactCodegen
22802280
- ReactCommon/turbomodule/bridging
22812281
- ReactCommon/turbomodule/core
2282-
- RNReanimated/worklets
2282+
- RNWorklets
22832283
- SocketRocket
22842284
- Yoga
2285-
- RNReanimated (3.19.0):
2285+
- RNReanimated (4.1.3):
22862286
- boost
22872287
- DoubleConversion
22882288
- fast_float
@@ -2309,11 +2309,11 @@ PODS:
23092309
- ReactCodegen
23102310
- ReactCommon/turbomodule/bridging
23112311
- ReactCommon/turbomodule/core
2312-
- RNReanimated/reanimated (= 3.19.0)
2313-
- RNReanimated/worklets (= 3.19.0)
2312+
- RNReanimated/reanimated (= 4.1.3)
2313+
- RNWorklets
23142314
- SocketRocket
23152315
- Yoga
2316-
- RNReanimated/reanimated (3.19.0):
2316+
- RNReanimated/reanimated (4.1.3):
23172317
- boost
23182318
- DoubleConversion
23192319
- fast_float
@@ -2340,10 +2340,11 @@ PODS:
23402340
- ReactCodegen
23412341
- ReactCommon/turbomodule/bridging
23422342
- ReactCommon/turbomodule/core
2343-
- RNReanimated/reanimated/apple (= 3.19.0)
2343+
- RNReanimated/reanimated/apple (= 4.1.3)
2344+
- RNWorklets
23442345
- SocketRocket
23452346
- Yoga
2346-
- RNReanimated/reanimated/apple (3.19.0):
2347+
- RNReanimated/reanimated/apple (4.1.3):
23472348
- boost
23482349
- DoubleConversion
23492350
- fast_float
@@ -2370,9 +2371,10 @@ PODS:
23702371
- ReactCodegen
23712372
- ReactCommon/turbomodule/bridging
23722373
- ReactCommon/turbomodule/core
2374+
- RNWorklets
23732375
- SocketRocket
23742376
- Yoga
2375-
- RNReanimated/worklets (3.19.0):
2377+
- RNWorklets (0.6.1):
23762378
- boost
23772379
- DoubleConversion
23782380
- fast_float
@@ -2399,10 +2401,40 @@ PODS:
23992401
- ReactCodegen
24002402
- ReactCommon/turbomodule/bridging
24012403
- ReactCommon/turbomodule/core
2402-
- RNReanimated/worklets/apple (= 3.19.0)
2404+
- RNWorklets/worklets (= 0.6.1)
24032405
- SocketRocket
24042406
- Yoga
2405-
- RNReanimated/worklets/apple (3.19.0):
2407+
- RNWorklets/worklets (0.6.1):
2408+
- boost
2409+
- DoubleConversion
2410+
- fast_float
2411+
- fmt
2412+
- glog
2413+
- hermes-engine
2414+
- RCT-Folly
2415+
- RCT-Folly/Fabric
2416+
- RCTRequired
2417+
- RCTTypeSafety
2418+
- React-Core
2419+
- React-debug
2420+
- React-Fabric
2421+
- React-featureflags
2422+
- React-graphics
2423+
- React-hermes
2424+
- React-ImageManager
2425+
- React-jsi
2426+
- React-NativeModulesApple
2427+
- React-RCTFabric
2428+
- React-renderercss
2429+
- React-rendererdebug
2430+
- React-utils
2431+
- ReactCodegen
2432+
- ReactCommon/turbomodule/bridging
2433+
- ReactCommon/turbomodule/core
2434+
- RNWorklets/worklets/apple (= 0.6.1)
2435+
- SocketRocket
2436+
- Yoga
2437+
- RNWorklets/worklets/apple (0.6.1):
24062438
- boost
24072439
- DoubleConversion
24082440
- fast_float
@@ -2508,6 +2540,7 @@ DEPENDENCIES:
25082540
- ReactCommon/turbomodule/core (from `../../node_modules/react-native/ReactCommon`)
25092541
- RNLiveMarkdown (from `../..`)
25102542
- RNReanimated (from `../../node_modules/react-native-reanimated`)
2543+
- RNWorklets (from `../../node_modules/react-native-worklets`)
25112544
- SocketRocket (~> 0.7.1)
25122545
- Yoga (from `../../node_modules/react-native/ReactCommon/yoga`)
25132546

@@ -2661,6 +2694,8 @@ EXTERNAL SOURCES:
26612694
:path: "../.."
26622695
RNReanimated:
26632696
:path: "../../node_modules/react-native-reanimated"
2697+
RNWorklets:
2698+
:path: "../../node_modules/react-native-worklets"
26642699
Yoga:
26652700
:path: "../../node_modules/react-native/ReactCommon/yoga"
26662701

@@ -2735,10 +2770,11 @@ SPEC CHECKSUMS:
27352770
ReactAppDependencyProvider: 433ddfb4536948630aadd5bd925aff8a632d2fe3
27362771
ReactCodegen: adf5027f30e34c68b5a09f0b68acb3e5ef9e1a5d
27372772
ReactCommon: 394c6b92765cf6d211c2c3f7f6bc601dffb316a6
2738-
RNLiveMarkdown: 0a98549fd315dc5fdc8b7ed937fdd6414d47565f
2739-
RNReanimated: 08fa7bb56d5f6d47c32245cf9cf53efb19befa0c
2773+
RNLiveMarkdown: 30f59122c1f27ea2ec2ad43935b1be214e10c697
2774+
RNReanimated: e04fc279d4e0d936a27f50c9a170cfa269c3434d
2775+
RNWorklets: 879b69a98caa893353b964b6fece154a819145af
27402776
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
2741-
Yoga: 922d794dce2af9c437f864bf4093abfa7a131adb
2777+
Yoga: a3ed390a19db0459bd6839823a6ac6d9c6db198d
27422778

27432779
PODFILE CHECKSUM: 3f53660915b3f926239de7f89ab29581306a2614
27442780

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"expensify-common": "2.0.148",
1313
"react": "19.1.0",
1414
"react-native": "0.81.4",
15-
"react-native-reanimated": "3.19.0"
15+
"react-native-reanimated": "4.1.3",
16+
"react-native-worklets": "0.6.1"
1617
},
1718
"devDependencies": {
1819
"@babel/core": "^7.25.2",

0 commit comments

Comments
 (0)