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

feat: duplicate worklets code the smart way #6827

Merged
merged 42 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2f8b9c4
feat: stub of node module react-native-worklets
tjzel Dec 11, 2024
fcfda4f
feat: preliminary use of react-native-worklets in reanimated
tjzel Dec 11, 2024
a9b6af2
chore: fix CMakeLists
tjzel Dec 12, 2024
96b76d7
Merge branch 'main' into @tjzel/worklets/node-module-with-rea-support
tjzel Dec 12, 2024
3ee4d4f
chore: add explanatory comments
tjzel Dec 17, 2024
1f40ace
feat: add linting to react-native-worklets
tjzel Dec 17, 2024
7fccc99
chore: add missing dependency
tjzel Dec 17, 2024
661706a
chore: add external-worklets-example
tjzel Dec 12, 2024
b127bea
chore: fix FabricExample occurences
tjzel Dec 17, 2024
a507fdc
chore: update github actions
tjzel Dec 17, 2024
afd030d
chore: fix CI
tjzel Dec 17, 2024
2634199
chore: remove excessive changes
tjzel Dec 12, 2024
64aea46
chore: remove external worklets
tjzel Jan 20, 2025
3701183
chore: cleanup
tjzel Jan 20, 2025
ceee2f7
chore: draft duplication
tjzel Dec 17, 2024
62c9488
chore: cleanup
tjzel Jan 21, 2025
50d3f00
chore: improve react-native-worklets detection script
tjzel Jan 21, 2025
7e75fb3
refactor: move ReanimatedVersion to Reanimated
tjzel Jan 21, 2025
88e38c0
Merge branch 'main' into @tjzel/worklets/restructure-worklets-source-…
tjzel Jan 23, 2025
08ad6ab
chore: cleanup
tjzel Jan 23, 2025
6a35149
chore: cleanup
tjzel Jan 23, 2025
45a4c27
Merge branch 'main' into @tjzel/worklets/restructure-worklets-source-…
tjzel Jan 23, 2025
1bcb1a9
Merge remote-tracking branch 'origin/@tjzel/worklets/restructure-work…
tjzel Jan 24, 2025
077304b
chore: fix CI issues
tjzel Jan 24, 2025
2017a8e
refactor: actually copy from worklets
tjzel Jan 24, 2025
6a85786
chore: fix dir
tjzel Jan 24, 2025
48669d5
chore: remove worklets
tjzel Jan 24, 2025
cb09fa5
refactor: major
tjzel Jan 27, 2025
8604d29
chore: apple
tjzel Jan 27, 2025
627827d
refactor: adjust to downstream changes
tjzel Jan 27, 2025
8d30b32
Merge branch 'main' into @tjzel/worklets/restructure-worklets-source-…
tjzel Jan 27, 2025
5585839
chore: update CI
tjzel Jan 27, 2025
8b9319c
chore: dont fire CIs twice
tjzel Jan 27, 2025
55301e8
chore: fix java package
tjzel Jan 27, 2025
3b9eac8
Merge branch '@tjzel/worklets/restructure-worklets-source-sets' into …
tjzel Jan 27, 2025
2cd50bc
chore: update CI
tjzel Jan 27, 2025
15c4fba
Merge branch '@tjzel/worklets/restructure-worklets-source-sets' into …
tjzel Jan 28, 2025
70ededc
chore: cleanup
tjzel Jan 28, 2025
85b3e07
chore: apply formatting
tjzel Jan 28, 2025
daad12a
Merge branch 'main' into @tjzel/worklets/duplicate-reanimated-code
tjzel Jan 28, 2025
8aa02a7
chore: add targets to build gradle`
tjzel Jan 28, 2025
510cbff
chore: restore order
tjzel Jan 29, 2025
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
9 changes: 9 additions & 0 deletions .github/workflows/example-android-build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
env:
WORKING_DIRECTORY: apps/fabric-example
COMMON_APP_DIR: apps/common-app
REANIMATED_DIR: packages/react-native-reanimated
concurrency:
group: android-${{ github.ref }}-${{ inputs.use-external-worklets }}
cancel-in-progress: true
Expand All @@ -59,6 +60,14 @@ jobs:
# TODO: Add caching for node_modules and artifacts that will work with monorepo setup.
- name: Install monorepo node dependencies
run: yarn install --immutable
- name: Build Reanimated package
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn build

- name: Use external worklets
if: ${{ inputs.use-external-worklets == 'true' }}
working-directory: ${{ env.COMMON_APP_DIR }}
run: yarn add react-native-worklets@workspace:"*"

- name: Use external worklets
if: ${{ inputs.use-external-worklets == 'true' }}
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/example-ios-build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
env:
WORKING_DIRECTORY: apps/fabric-example
COMMON_APP_DIR: apps/common-app
REANIMATED_DIR: packages/react-native-reanimated
concurrency:
group: ios-${{ github.ref }}-${{ inputs.use-external-worklets }}
cancel-in-progress: true
Expand All @@ -60,6 +61,9 @@ jobs:

- name: Install monorepo node dependencies
run: yarn install --immutable
- name: Build Reanimated package
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn build

- name: Use external worklets
if: ${{ inputs.use-external-worklets == 'true' }}
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/example-macos-build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
env:
WORKING_DIRECTORY: apps/macos-example
COMMON_APP_DIR: apps/common-app
REANIMATED_DIR: packages/react-native-reanimated
concurrency:
group: macos-${{ github.ref }}-${{ inputs.use-external-worklets }}
cancel-in-progress: true
Expand All @@ -63,6 +64,14 @@ jobs:
# TODO: Add caching for node_modules and artifacts that will work with monorepo setup.
- name: Install monorepo node dependencies
run: yarn install --immutable
- name: Build Reanimated package
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn build

- name: Use external worklets
if: ${{ inputs.use-external-worklets == 'true' }}
working-directory: ${{ env.COMMON_APP_DIR }}
run: yarn add react-native-worklets@workspace:"*"

- name: Use external worklets
if: ${{ inputs.use-external-worklets == 'true' }}
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/example-tvos-build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
env:
WORKING_DIRECTORY: apps/tvos-example
COMMON_APP_DIR: apps/common-app
REANIMATED_DIR: packages/react-native-reanimated
concurrency:
group: tvos-${{ github.ref }}-${{ inputs.use-external-worklets }}
cancel-in-progress: true
Expand All @@ -56,14 +57,14 @@ jobs:

- name: Install monorepo node dependencies
run: yarn install --immutable
# TODO: Add caching for node_modules and artifacts that will work with monorepo setup.
- name: Install Reanimated node_modules
working-directory: packages/react-native-reanimated
run: yarn install --immutable
- name: Build Reanimated package
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn build

- name: Install app node_modules
working-directory: ${{ env.WORKING_DIRECTORY }}
run: yarn install --immutable
- name: Use external worklets
if: ${{ inputs.use-external-worklets == 'true' }}
working-directory: ${{ env.COMMON_APP_DIR }}
run: yarn add react-native-worklets@workspace:"*"

- name: Use external worklets
if: ${{ inputs.use-external-worklets == 'true' }}
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/reanimated-android-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
concurrency:
group: validate-java-${{ github.ref }}
cancel-in-progress: true
env:
REANIMATED_DIR: packages/react-native-reanimated
steps:
- name: checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -62,7 +64,10 @@ jobs:

- name: Install monorepo node dependencies
run: yarn install --immutable
- name: Build Reanimated package
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn build

- name: Lint Android
working-directory: packages/react-native-reanimated
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn lint:android
7 changes: 6 additions & 1 deletion .github/workflows/reanimated-apple-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ on:
jobs:
check:
if: github.repository == 'software-mansion/react-native-reanimated'
env:
REANIMATED_DIR: packages/react-native-reanimated
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install monorepo node dependencies
run: yarn install --immutable
- name: Build Reanimated package
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn build

- name: Lint apple
working-directory: packages/react-native-reanimated
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn lint:apple
11 changes: 8 additions & 3 deletions .github/workflows/reanimated-common-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ jobs:
strategy:
matrix:
python-version: [3.13]

env:
REANIMATED_DIR: packages/react-native-reanimated
steps:
- uses: actions/checkout@v4

Expand All @@ -42,11 +43,15 @@ jobs:
pip install cpplint==1.6.1
- name: Install monorepo node dependencies
run: yarn install --immutable
- name: Build Reanimated
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn build

- name: Lint Common
working-directory: packages/react-native-reanimated
working-directory: ${{ env.REANIMATED_DIR }}
run: yarn lint:common

- name: Disallow DEBUG macros
working-directory: ${{ env.REANIMATED_DIR }}
run: |
! egrep -r '(#if DEBUG|#ifdef DEBUG)' packages/react-native-reanimated/Common/cpp/ packages/react-native-reanimated/apple/ packages/react-native-reanimated/android/src/main/cpp/
! egrep -r '(#if DEBUG|#ifdef DEBUG)' Common/cpp/ apple/ android/src/main/cpp/
6 changes: 6 additions & 0 deletions packages/react-native-reanimated/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ plugin/types

# library bundle
*.tgz

# Worklets code (duplicated)
apple/worklets
Common/cpp/worklets
android/src/worklets
android/src/main/cpp/worklets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <reanimated/RuntimeDecorators/RNRuntimeDecorator.h>
#include <worklets/Tools/ReanimatedVersion.h>
#include <reanimated/Tools/ReanimatedVersion.h>

namespace reanimated {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <reanimated/Tools/ReanimatedVersion.h>
#include <worklets/Tools/JSLogger.h>
#include <worklets/Tools/ReanimatedVersion.h>

#include <memory>
#include <regex>
Expand All @@ -13,7 +13,7 @@

using namespace facebook;

namespace worklets {
namespace reanimated {

std::string getReanimatedCppVersion() {
return std::string(REANIMATED_VERSION_STRING);
Expand Down Expand Up @@ -54,7 +54,7 @@ bool matchVersion(const std::string &version1, const std::string &version2) {

void checkJSVersion(
jsi::Runtime &rnRuntime,
const std::shared_ptr<JSLogger> &jsLogger) {
const std::shared_ptr<worklets::JSLogger> &jsLogger) {
auto cppVersion = getReanimatedCppVersion();

auto maybeJSVersion =
Expand Down Expand Up @@ -92,4 +92,4 @@ bool matchVersion(const std::string &version1, const std::string &version2) {
}
#endif // NDEBUG

}; // namespace worklets
}; // namespace reanimated
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

using namespace facebook;

namespace worklets {
namespace reanimated {

std::string getReanimatedCppVersion();
void injectReanimatedCppVersion(jsi::Runtime &);
bool matchVersion(const std::string &, const std::string &);
void checkJSVersion(jsi::Runtime &, const std::shared_ptr<JSLogger> &);
void checkJSVersion(
jsi::Runtime &,
const std::shared_ptr<worklets::JSLogger> &);

}; // namespace worklets
}; // namespace reanimated
10 changes: 4 additions & 6 deletions packages/react-native-reanimated/RNReanimated.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ Pod::Spec.new do |s|
s.platforms = { :ios => ios_min_version, :tvos => "9.0", :osx => "10.14", :visionos => "1.0" }
s.source = { :git => "https://github.com/software-mansion/react-native-reanimated.git", :tag => "#{s.version}" }

# TODO: Uncomment me when dynamic worklets linking is ready
# if File.directory?(File.join(__dir__, "../react-native-worklets"))
# # This condition is really naïve...
# s.dependency "RNWorklets"
# else
if $config[:has_external_worklets]
s.dependency "RNWorklets"
else
s.subspec "worklets" do |ss|
ss.source_files = "Common/cpp/worklets/**/*.{cpp,h}"
ss.header_dir = "worklets"
Expand All @@ -95,7 +93,7 @@ Pod::Spec.new do |s|
sss.source_files = "apple/worklets/**/*.{mm,h,m}"
sss.header_dir = "worklets"
sss.header_mappings_dir = "apple/worklets"
# end
end
end
end

Expand Down
12 changes: 7 additions & 5 deletions packages/react-native-reanimated/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def thirdPartyNdkDir = new File("$buildDir/third-party-ndk")
def reactNativeThirdParty = new File("$reactNativeRootDir/ReactAndroid/src/main/jni/third-party")
def reactNativeAndroidDownloadDir = new File("$reactNativeRootDir/ReactAndroid/build/downloads")

// TODO: Uncomment me when when dynamic worklets linking is ready
// def hasExternalWorklets = rootProject.subprojects.find { it.name == 'react-native-worklets' } != null
def hasExternalWorklets = false
def hasExternalWorklets = rootProject.subprojects.find { it.name == 'react-native-worklets' } != null

def workletsPrefabHeadersDir = project.file("$buildDir/prefab-headers/worklets")
def reanimatedPrefabHeadersDir = project.file("$buildDir/prefab-headers/reanimated")
Expand Down Expand Up @@ -260,7 +258,11 @@ android {
"-DREANIMATED_VERSION=${REANIMATED_VERSION}",
"-DHAS_EXTERNAL_WORKLETS=${hasExternalWorklets}"
abiFilters (*reactNativeArchitectures())
targets("reanimated", "worklets")
if(hasExternalWorklets){
targets("reanimated")
} else {
targets("reanimated", "worklets")
}
}
}

Expand Down Expand Up @@ -343,7 +345,7 @@ android {
sourceSets.main {
java {
if(hasExternalWorklets){
// Nothing for now.
srcDirs += "src/externalWorklets/with"
} else {
srcDirs += "src/worklets/main"
srcDirs += "src/externalWorklets/without"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.swmansion.reanimated;

import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END;
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_START;

import androidx.annotation.NonNull;
import com.facebook.react.BaseReactPackage;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.module.annotations.ReactModuleList;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.uimanager.ReanimatedUIManager;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.systrace.Systrace;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@ReactModuleList(
nativeModules = {
ReanimatedModule.class,
ReanimatedUIManager.class,
})
public class ReanimatedPackage extends BaseReactPackage implements ReactPackage {
@Override
public NativeModule getModule(
@NonNull String name, @NonNull ReactApplicationContext reactContext) {
return switch (name) {
case ReanimatedModule.NAME -> new ReanimatedModule(reactContext);
case ReanimatedUIManager.NAME -> createUIManager(reactContext);
default -> null;
};
}

@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
Class<? extends NativeModule>[] moduleList =
new Class[] {
ReanimatedModule.class, ReanimatedUIManager.class,
};

final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
for (Class<? extends NativeModule> moduleClass : moduleList) {
ReactModule reactModule =
Objects.requireNonNull(moduleClass.getAnnotation(ReactModule.class));

reactModuleInfoMap.put(
reactModule.name(),
new ReactModuleInfo(
reactModule.name(),
moduleClass.getName(),
true, // override UIManagerModule
reactModule.needsEagerInit(),
reactModule.isCxxModule(),
BuildConfig.IS_NEW_ARCHITECTURE_ENABLED));
}

return () -> reactModuleInfoMap;
}

private UIManagerModule createUIManager(final ReactApplicationContext reactContext) {
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_START);
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule");
final ReactInstanceManager reactInstanceManager = getReactInstanceManager(reactContext);
List<ViewManager> viewManagers = reactInstanceManager.getOrCreateViewManagers(reactContext);
int minTimeLeftInFrameForNonBatchedOperationMs = -1;
try {
return ReanimatedUIManagerFactory.create(
reactContext, viewManagers, minTimeLeftInFrameForNonBatchedOperationMs);
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END);
}
}

/**
* Get the {@link ReactInstanceManager} used by this app. By default, assumes {@link
* ReactApplicationContext#getApplicationContext()} is an instance of {@link ReactApplication} and
* calls {@link ReactApplication#getReactNativeHost().getReactInstanceManager()}. Override this
* method if your application class does not implement {@code ReactApplication} or you simply have
* a different mechanism for storing a {@code ReactInstanceManager}, e.g. as a static field
* somewhere.
*/
public ReactInstanceManager getReactInstanceManager(ReactApplicationContext reactContext) {
return ((ReactApplication) reactContext.getApplicationContext())
.getReactNativeHost()
.getReactInstanceManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
import java.util.Map;
import java.util.Objects;

// `WorkletsModule` should be included from a separate Java package, called `WorkletsPackage`.
// However, it's not possible with the current state of the Gradle Tools provided by
// RNC CLI - all packages besides the first found are ignored.
// Therefore, until we extract `react-native-worklets` to a separate package,
// we will host this module in Reanimated's package.
// `WorkletsPackage` must be registered within `ReanimatedPackage` when
// `react-native-worklets` isn't installed . Extra `...Package` files
// are ignored by React Native tools.
@ReactModuleList(
nativeModules = {
WorkletsModule.class,
Expand Down
Loading
Loading