Skip to content

Commit

Permalink
feat: move to Cxx Turbo Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Oct 7, 2024
1 parent 5655a91 commit 52a4e63
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 63 deletions.
21 changes: 6 additions & 15 deletions cpp/react-native-rapier.cpp
Original file line number Diff line number Diff line change
@@ -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<CallInvoker> 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;
}

}
23 changes: 18 additions & 5 deletions cpp/react-native-rapier.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#pragma once

#include <ReactCommon/TurboModule.h>
#include <RNReactNativeRapierSpecJSI.h>

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<CallInvoker> jsInvoker);

public:
double multiply(jsi::Runtime &rt, double a, double b) override;
constexpr static auto kModuleName = "ReactNativeRapier";

private:
std::shared_ptr<CallInvoker> _callInvoker;
};

} // namespace facebook::react
9 changes: 1 addition & 8 deletions ios/ReactNativeRapier.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
#import "react-native-rapier.h"
#endif

#ifdef RCT_NEW_ARCH_ENABLED
#import "RNReactNativeRapierSpec.h"

@interface ReactNativeRapier : NSObject <NativeReactNativeRapierSpec>
#else
#import <React/RCTBridgeModule.h>

@interface ReactNativeRapier : NSObject <RCTBridgeModule>
#endif
@interface ReactNativeRapier : NSObject

@end
28 changes: 7 additions & 21 deletions ios/ReactNativeRapier.mm
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
#import "ReactNativeRapier.h"
#import <ReactCommon/RCTTurboModuleWithJSIBindings.h>


@interface ReactNativeRapier () <RCTTurboModuleWithJSIBindings>
@end
#include <ReactCommon/CxxTurboModuleUtils.h>

@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<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeReactNativeRapierSpecJSI>(params);
+ (void)load {
facebook::react::registerCxxModuleToGlobalModuleMap(
std::string(facebook::react::ReactNativeRapier::kModuleName),
[&](std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
return std::make_shared<facebook::react::ReactNativeRapier>(jsInvoker);
});
}
#endif

@end
4 changes: 3 additions & 1 deletion src/NativeReactNativeRapier.ts
Original file line number Diff line number Diff line change
@@ -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<Spec>('ReactNativeRapier');
14 changes: 1 addition & 13 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ReactNativeRapierModule = isTurboModuleEnabled
? require('./NativeReactNativeRapier').default
: NativeModules.ReactNativeRapier;

ReactNativeRapierModule
const Rapier = ReactNativeRapierModule
? ReactNativeRapierModule
: new Proxy(
{},
Expand All @@ -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;

0 comments on commit 52a4e63

Please sign in to comment.