From 52a4e63d3ad7a34dbb03e9a72602f92582d3fe68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Mon, 7 Oct 2024 11:40:58 +0200 Subject: [PATCH] feat: move to Cxx Turbo Modules --- cpp/react-native-rapier.cpp | 21 ++++++--------------- cpp/react-native-rapier.h | 23 ++++++++++++++++++----- ios/ReactNativeRapier.h | 9 +-------- ios/ReactNativeRapier.mm | 28 +++++++--------------------- src/NativeReactNativeRapier.ts | 4 +++- src/index.tsx | 14 +------------- 6 files changed, 36 insertions(+), 63 deletions(-) diff --git a/cpp/react-native-rapier.cpp b/cpp/react-native-rapier.cpp index 1380be0..f9d7df0 100644 --- a/cpp/react-native-rapier.cpp +++ b/cpp/react-native-rapier.cpp @@ -1,21 +1,12 @@ #include "react-native-rapier.h" -#include "macros.h" +namespace facebook::react { -namespace callstack { -namespace react_native_rapier { -void install(jsi::Runtime& rt) { - auto multiply = HOSTFN("multiply", 2) { - auto num1 = args[0].asNumber(); - auto num2 = args[1].asNumber(); - return jsi::Value(num1 + num2); - }); +ReactNativeRapier::ReactNativeRapier(std::shared_ptr jsInvoker) +: NativeReactNativeRapierCxxSpecJSI(jsInvoker), _callInvoker(jsInvoker) {} - auto rapier = jsi::Object { rt }; - rapier.setProperty(rt, "multiply", std::move(multiply)); - - rt.global().setProperty(rt, "__RapierProxy", std::move(rapier)); -} -} +double ReactNativeRapier::multiply(jsi::Runtime &rt, double a, double b) { + return a * b; } +} diff --git a/cpp/react-native-rapier.h b/cpp/react-native-rapier.h index 0faf7dc..766a2c1 100644 --- a/cpp/react-native-rapier.h +++ b/cpp/react-native-rapier.h @@ -1,9 +1,22 @@ +#pragma once + #include +#include + +namespace facebook::react { using namespace facebook; -namespace callstack { - namespace react_native_rapier { - void install(jsi::Runtime& rt); - } -} +class ReactNativeRapier : public NativeReactNativeRapierCxxSpecJSI { +public: + explicit ReactNativeRapier(std::shared_ptr jsInvoker); + +public: + double multiply(jsi::Runtime &rt, double a, double b) override; + constexpr static auto kModuleName = "ReactNativeRapier"; + +private: + std::shared_ptr _callInvoker; +}; + +} // namespace facebook::react diff --git a/ios/ReactNativeRapier.h b/ios/ReactNativeRapier.h index 199d54f..9ab39b2 100644 --- a/ios/ReactNativeRapier.h +++ b/ios/ReactNativeRapier.h @@ -2,14 +2,7 @@ #import "react-native-rapier.h" #endif -#ifdef RCT_NEW_ARCH_ENABLED -#import "RNReactNativeRapierSpec.h" -@interface ReactNativeRapier : NSObject -#else -#import - -@interface ReactNativeRapier : NSObject -#endif +@interface ReactNativeRapier : NSObject @end diff --git a/ios/ReactNativeRapier.mm b/ios/ReactNativeRapier.mm index 1f9c2f3..6ddc733 100644 --- a/ios/ReactNativeRapier.mm +++ b/ios/ReactNativeRapier.mm @@ -1,28 +1,14 @@ #import "ReactNativeRapier.h" -#import - - -@interface ReactNativeRapier () -@end +#include @implementation ReactNativeRapier -RCT_EXPORT_MODULE() - -#pragma mark - RCTTurboModuleWithJSIBindings - -- (void)installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)runtime -{ - callstack::react_native_rapier::install(runtime); -} - -// Don't compile this code when we build for the old architecture. -#ifdef RCT_NEW_ARCH_ENABLED -- (std::shared_ptr)getTurboModule: -(const facebook::react::ObjCTurboModule::InitParams &)params -{ - return std::make_shared(params); ++ (void)load { + facebook::react::registerCxxModuleToGlobalModuleMap( + std::string(facebook::react::ReactNativeRapier::kModuleName), + [&](std::shared_ptr jsInvoker) { + return std::make_shared(jsInvoker); +}); } -#endif @end diff --git a/src/NativeReactNativeRapier.ts b/src/NativeReactNativeRapier.ts index a1771a1..ed3734c 100644 --- a/src/NativeReactNativeRapier.ts +++ b/src/NativeReactNativeRapier.ts @@ -1,6 +1,8 @@ import type { TurboModule } from 'react-native'; import { TurboModuleRegistry } from 'react-native'; -export interface Spec extends TurboModule {} +export interface Spec extends TurboModule { + multiply(a: number, b: number): number; +} export default TurboModuleRegistry.getEnforcing('ReactNativeRapier'); diff --git a/src/index.tsx b/src/index.tsx index 151e590..196a1e3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -13,7 +13,7 @@ const ReactNativeRapierModule = isTurboModuleEnabled ? require('./NativeReactNativeRapier').default : NativeModules.ReactNativeRapier; -ReactNativeRapierModule +const Rapier = ReactNativeRapierModule ? ReactNativeRapierModule : new Proxy( {}, @@ -24,16 +24,4 @@ ReactNativeRapierModule } ); -declare global { - var __RapierProxy: object | undefined; -} - -if (global.__RapierProxy == null) { - throw new Error(LINKING_ERROR); -} - -const proxy = global.__RapierProxy; -// Type cast this to Rapier types -const Rapier = proxy as any; - export default Rapier;