Skip to content

feat: try to enable bridgeless mode in the library #2708

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

Closed
wants to merge 9 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MainApplication : Application(), ReactApplication {
SoLoader.init(this, false)
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we load the native entry point for this app.
load()
load(bridgelessEnabled = true)
}
ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager)
}
Expand Down
9 changes: 9 additions & 0 deletions FabricExample/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ rootProject.name = 'FabricExample'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'
includeBuild('../node_modules/@react-native/gradle-plugin')

includeBuild('../node_modules/react-native') {
dependencySubstitution {
substitute(module("com.facebook.react:react-android")).using(project(":packages:react-native:ReactAndroid"))
substitute(module("com.facebook.react:react-native")).using(project(":packages:react-native:ReactAndroid"))
substitute(module("com.facebook.react:hermes-android")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
substitute(module("com.facebook.react:hermes-engine")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
}
}
6 changes: 4 additions & 2 deletions FabricExample/ios/FabricExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -679,7 +680,8 @@
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down
22 changes: 22 additions & 0 deletions FabricExample/ios/FabricExample/AppDelegate.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/CoreModulesPlugins.h>
#import <ReactCommon/RCTTurboModuleManager.h>
#import <RNGHTurboCppModule.h>

@interface AppDelegate () <RCTTurboModuleManagerDelegate> {}
@end

@implementation AppDelegate

Expand All @@ -19,6 +25,22 @@ - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
return [self getBundleURL];
}

- (BOOL)bridgelessEnabled
{
return YES;
}

#pragma mark RCTTurboModuleManagerDelegate

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
{
if (name == "RNGHTurboCppModule") {
return std::make_shared<facebook::react::RNGHTurboCppModule>(jsInvoker);
}
return nullptr;
}

- (NSURL *)getBundleURL
{
#if DEBUG
Expand Down
4 changes: 2 additions & 2 deletions FabricExample/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1439,9 +1439,9 @@ SPEC CHECKSUMS:
React-runtimescheduler: df8945a656356ff10f58f65a70820478bfcf33ad
React-utils: f5bc61e7ea3325c0732ae2d755f4441940163b85
ReactCommon: 45b5d4f784e869c44a6f5a8fad5b114ca8f78c53
RNGestureHandler: b028dd5cade762010a72ee9be3afac17a0f72787
RNGestureHandler: 76cb9f767ff56c3e77bcbbbc5ed9c1cc069bbe56
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: e64aa65de36c0832d04e8c7bd614396c77a80047
Yoga: 13c8ef87792450193e117976337b8527b49e8c03

PODFILE CHECKSUM: 792f7d0ed591c328474645afc856de4fd1732c31

Expand Down
6 changes: 5 additions & 1 deletion RNGestureHandler.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ Pod::Spec.new do |s|
s.license = "MIT"
s.author = { package["author"]["name"] => package["author"]["email"] }
s.source = { :git => "https://github.com/software-mansion/react-native-gesture-handler", :tag => "#{s.version}" }
s.source_files = "apple/**/*.{h,m,mm}"
if new_arch_enabled
s.source_files = ["apple/**/*.{h,m,mm}", "common/cpp/**/*.{cpp,h}"]
else
s.source_files = ["apple/**/*.{h,m,mm}"]
end
s.requires_arc = true
s.platforms = { ios: apple_platform, tvos: apple_platform, osx: '10.15' }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,20 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) :

@ReactMethod(isBlockingSynchronousMethod = true)
override fun install(): Boolean {
reactApplicationContext.runOnJSQueueThread {
try {
return try {
if (reactApplicationContext.javaScriptContextHolder == null) {
return false
}
reactApplicationContext.runOnJSQueueThread {
SoLoader.loadLibrary("gesturehandler")
val jsContext = reactApplicationContext.javaScriptContextHolder!!
decorateRuntime(jsContext.get())
} catch (exception: Exception) {
Log.w("[RNGestureHandler]", "Could not install JSI bindings.")
}
true
} catch (exception: Exception) {
Log.w("[RNGestureHandler]", "Could not install JSI bindings.")
false
}

return true
}

private external fun decorateRuntime(jsiPtr: Long)
Expand Down Expand Up @@ -645,6 +648,12 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) :
}

companion object {
init {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// we need here so it is called before cpp turbo module is requested
SoLoader.loadLibrary("gesturehandler")
}
}
const val MODULE_NAME = "RNGestureHandlerModule"
private const val KEY_SHOULD_CANCEL_WHEN_OUTSIDE = "shouldCancelWhenOutside"
private const val KEY_ENABLED = "enabled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.common.ReactConstants
import com.facebook.react.uimanager.RootView
import com.facebook.react.uimanager.ThemedReactContext
import com.swmansion.gesturehandler.core.GestureHandler
import com.swmansion.gesturehandler.core.GestureHandlerOrchestrator
import java.lang.Exception

class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView: ViewGroup) {
private val orchestrator: GestureHandlerOrchestrator?
Expand All @@ -24,7 +26,12 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
UiThreadUtil.assertOnUiThread()
val wrappedViewTag = wrappedView.id
check(wrappedViewTag >= 1) { "Expect view tag to be set for $wrappedView" }
val module = context.getNativeModule(RNGestureHandlerModule::class.java)!!

val module = try {
context.getNativeModule(RNGestureHandlerModule::class.java)!!
} catch (e: Exception) {
(context as ThemedReactContext).reactApplicationContext.getNativeModule(RNGestureHandlerModule::class.java)!!
}
val registry = module.registry
rootView = findRootViewTag(wrappedView)
Log.i(
Expand All @@ -49,7 +56,11 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
ReactConstants.TAG,
"[GESTURE HANDLER] Tearing down gesture handler registered for root view $rootView"
)
val module = context.getNativeModule(RNGestureHandlerModule::class.java)!!
val module = try {
context.getNativeModule(RNGestureHandlerModule::class.java)!!
} catch (e: Exception) {
(context as ThemedReactContext).reactApplicationContext.getNativeModule(RNGestureHandlerModule::class.java)!!
}
with(module) {
registry.dropHandler(jsGestureHandler!!.tag)
unregisterRootHelper(this@RNGestureHandlerRootHelper)
Expand Down
13 changes: 12 additions & 1 deletion android/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ set(REACT_ANDROID_DIR "${REACT_NATIVE_DIR}/ReactAndroid")
include(${REACT_ANDROID_DIR}/cmake-utils/folly-flags.cmake)
add_compile_options(${folly_FLAGS})

file(GLOB tm_SRC CONFIGURE_DEPENDS ${REACT_ANDROID_DIR}/../../../node_modules/react-native-gesture-handler/common/cpp/*.cpp)
file(GLOB tm_generated_SRC CONFIGURE_DEPENDS *.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../build/generated/source/codegen/jni/react/renderer/components/rngesturehandler_codegen/rngesturehandler_codegenJSI-generated.cpp)

add_library(${PACKAGE_NAME}
SHARED
cpp-adapter.cpp
${tm_SRC}
${tm_generated_SRC}
)

target_include_directories(
${PACKAGE_NAME}
PRIVATE
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/../../../../common/cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../../../build/generated/source/codegen/jni/react/renderer/components/rngesturehandler_codegen"
"${REACT_NATIVE_DIR}/ReactCommon"
)

find_package(ReactAndroid REQUIRED CONFIG)
find_package(fbjni REQUIRED CONFIG)

target_link_libraries(
gesturehandler
Expand All @@ -34,4 +42,7 @@ target_link_libraries(
ReactAndroid::react_render_graphics
ReactAndroid::jsi
ReactAndroid::react_nativemodule_core
fbjni::fbjni
ReactAndroid::turbomodulejsijni
ReactAndroid::react_newarchdefaults
)
66 changes: 32 additions & 34 deletions android/src/main/jni/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
#include <jni.h>
#include <jsi/jsi.h>
#include "RNGHTurboCppModule.h"
#include <DefaultTurboModuleManagerDelegate.h>
#include <fbjni/fbjni.h>

#include <react/renderer/uimanager/primitives.h>
std::function<std::shared_ptr<facebook::react::TurboModule>(
const std::string&,
const std::shared_ptr<facebook::react::CallInvoker>&)> cxxModuleProviderHolder;

using namespace facebook;
using namespace react;
namespace facebook::react {

void decorateRuntime(jsi::Runtime &runtime) {
auto isFormsStackingContext = jsi::Function::createFromHostFunction(
runtime,
jsi::PropNameID::forAscii(runtime, "isFormsStackingContext"),
1,
[](jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isObject()) {
return jsi::Value::null();
}

auto shadowNode = arguments[0]
.asObject(runtime)
.getHostObject<ShadowNodeWrapper>(runtime)
->shadowNode;
bool isFormsStackingContext = shadowNode->getTraits().check(
ShadowNodeTraits::FormsStackingContext);
extern "C" JNIEXPORT void JNICALL
Java_com_swmansion_gesturehandler_react_RNGestureHandlerModule_decorateRuntime(
JNIEnv *env,
jobject clazz,
jlong jsiPtr) {
jsi::Runtime *runtime = reinterpret_cast<jsi::Runtime *>(jsiPtr);
if (runtime) {
RNGHDecorateRuntime(*runtime);
}
}

return jsi::Value(isFormsStackingContext);
});
runtime.global().setProperty(
runtime, "isFormsStackingContext", std::move(isFormsStackingContext));
std::shared_ptr<TurboModule> cxxModuleProvider(
const std::string& name,
const std::shared_ptr<CallInvoker>& jsInvoker) {
if (name == "RNGHTurboCppModule") {
return std::make_shared<facebook::react::RNGHTurboCppModule>(jsInvoker);
}
return cxxModuleProviderHolder(name, jsInvoker);
}

extern "C" JNIEXPORT void JNICALL
Java_com_swmansion_gesturehandler_react_RNGestureHandlerModule_decorateRuntime(
JNIEnv *env,
jobject clazz,
jlong jsiPtr) {
jsi::Runtime *runtime = reinterpret_cast<jsi::Runtime *>(jsiPtr);
if (runtime) {
decorateRuntime(*runtime);
}
} // namespace facebook::react

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
cxxModuleProviderHolder = facebook::react::DefaultTurboModuleManagerDelegate::cxxModuleProvider;
return facebook::jni::initialize(vm, [] {
facebook::react::DefaultTurboModuleManagerDelegate::cxxModuleProvider =
&facebook::react::cxxModuleProvider;
});
}
5 changes: 5 additions & 0 deletions apple/RNGestureHandlerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

@interface RNGestureHandlerManager : NSObject

#ifdef RCT_NEW_ARCH_ENABLED
- (nonnull instancetype)initWithModuleRegistry:(nonnull RCTModuleRegistry *)moduleRegistry
viewRegistry:(nonnull RCTViewRegistry *)viewRegistry;
#else
- (nonnull instancetype)initWithUIManager:(nonnull RCTUIManager *)uiManager
eventDispatcher:(nonnull id<RCTEventDispatcherProtocol>)eventDispatcher;
#endif // RCT_NEW_ARCH_ENABLED

- (void)createGestureHandler:(nonnull NSString *)handlerName
tag:(nonnull NSNumber *)handlerTag
Expand Down
38 changes: 32 additions & 6 deletions apple/RNGestureHandlerManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,48 @@ @interface RNGestureHandlerManager () <RNGestureHandlerEventEmitter, RNRootViewG

@implementation RNGestureHandlerManager {
RNGestureHandlerRegistry *_registry;
RCTUIManager *_uiManager;
NSHashTable<RNRootViewGestureRecognizer *> *_rootViewGestureRecognizers;
NSMutableDictionary<NSNumber *, NSNumber *> *_attachRetryCounter;
#ifdef RCT_NEW_ARCH_ENABLED
RCTModuleRegistry *_moduleRegistry;
RCTViewRegistry *_viewRegistry;
#else
RCTUIManager *_uiManager;
#endif // RCT_NEW_ARCH_ENABLED
id<RCTEventDispatcherProtocol> _eventDispatcher;
id _reanimatedModule;
}

#ifdef RCT_NEW_ARCH_ENABLED
- (instancetype)initWithModuleRegistry:(RCTModuleRegistry *)moduleRegistry viewRegistry:(RCTViewRegistry *)viewRegistry
{
if ((self = [super init])) {
_moduleRegistry = moduleRegistry;
_viewRegistry = viewRegistry;
_eventDispatcher = [_moduleRegistry moduleForName:"EventDispatcher"];
[self initCommonProps];
}
return self;
}
#else
- (instancetype)initWithUIManager:(RCTUIManager *)uiManager
eventDispatcher:(id<RCTEventDispatcherProtocol>)eventDispatcher
{
if ((self = [super init])) {
_uiManager = uiManager;
_eventDispatcher = eventDispatcher;
_registry = [RNGestureHandlerRegistry new];
_rootViewGestureRecognizers = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
_attachRetryCounter = [[NSMutableDictionary alloc] init];
_reanimatedModule = nil;
[self initCommonProps];
}
return self;
}
#endif // RCT_NEW_ARCH_ENABLED

- (void)initCommonProps
{
_registry = [RNGestureHandlerRegistry new];
_rootViewGestureRecognizers = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
_attachRetryCounter = [[NSMutableDictionary alloc] init];
}

- (void)createGestureHandler:(NSString *)handlerName tag:(NSNumber *)handlerTag config:(NSDictionary *)config
{
Expand Down Expand Up @@ -115,7 +137,11 @@ - (void)attachGestureHandler:(nonnull NSNumber *)handlerTag
toViewWithTag:(nonnull NSNumber *)viewTag
withActionType:(RNGestureHandlerActionType)actionType
{
#ifdef RCT_NEW_ARCH_ENABLED
RNGHUIView *view = [_viewRegistry viewForReactTag:viewTag];
#else
RNGHUIView *view = [_uiManager viewForReactTag:viewTag];
#endif // RCT_NEW_ARCH_ENABLED

#ifdef RCT_NEW_ARCH_ENABLED
if (view == nil || view.superview == nil) {
Expand Down Expand Up @@ -350,7 +376,7 @@ - (void)sendEventForReanimated:(RNGestureHandlerStateChange *)event
#ifdef RCT_NEW_ARCH_ENABLED
// Send event directly to Reanimated
if (_reanimatedModule == nil) {
_reanimatedModule = [_uiManager.bridge moduleForName:@"ReanimatedModule"];
_reanimatedModule = [_moduleRegistry moduleForName:"ReanimatedModule"];
}

[_reanimatedModule eventDispatcherWillDispatchEvent:event];
Expand Down
3 changes: 2 additions & 1 deletion apple/RNGestureHandlerModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#import <React/RCTUIManager.h>

#ifdef RN_FABRIC_ENABLED
#import <React/RCTInitializing.h>
#import <rngesturehandler_codegen/rngesturehandler_codegen.h>
#else
#import <React/RCTBridgeModule.h>
#endif

@interface RNGestureHandlerModule : RCTEventEmitter
#ifdef RN_FABRIC_ENABLED
<NativeRNGestureHandlerModuleSpec>
<NativeRNGestureHandlerModuleSpec, RCTInitializing>
#else
<RCTBridgeModule>
#endif
Expand Down
Loading