-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathNativeNitroModules+NewArch.mm
More file actions
90 lines (74 loc) 路 2.68 KB
/
Copy pathNativeNitroModules+NewArch.mm
File metadata and controls
90 lines (74 loc) 路 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// NativeNitroModules+NewArch.mm
// react-native-nitro
//
// Created by Marc Rousavy on 21.06.24.
//
#import "NativeNitroModules.h"
#ifdef RCT_NEW_ARCH_ENABLED
#import "CallInvokerDispatcher.hpp"
#import "InstallNitro.hpp"
#import "NitroLogger.hpp"
#import <ReactCommon/CallInvoker.h>
#import <ReactCommon/RCTTurboModuleWithJSIBindings.h>
using namespace facebook;
using namespace margelo;
// Make NativeNitroModules comply to RCTTurboModuleWithJSIBindings
@interface NativeNitroModules () <RCTTurboModuleWithJSIBindings>
@end
/**
* NativeNitroModules implementation for the new architecture.
* This uses `installJSIBindingsWithRuntime:` to install the `global.NitroModulesProxy` into the JS Runtime.
*/
@implementation NativeNitroModules {
bool _didInstall;
NSString* _Nullable _errorMessage;
std::weak_ptr<react::CallInvoker> _callInvoker;
}
RCT_EXPORT_MODULE(NitroModules)
// React Native >= 0.87+
- (void)installJSIBindingsWithRuntime:(jsi::Runtime&)runtime callInvoker:(const std::shared_ptr<react::CallInvoker>&)callInvoker {
_callInvoker = callInvoker;
[self installNitroInRuntime:runtime];
}
// TODO: Delete this old overload when we drop support for RN 0.86
// React Native < 0.87
- (void)installJSIBindingsWithRuntime:(jsi::Runtime&)runtime {
[self installNitroInRuntime:runtime];
}
- (void)installNitroInRuntime:(jsi::Runtime&)runtime {
// 1. Get CallInvoker we cached statically
std::shared_ptr<react::CallInvoker> callInvoker = _callInvoker.lock();
if (callInvoker == nullptr) {
throw std::runtime_error("Cannot install `global.NitroModulesProxy` - CallInvoker was null!");
}
// 2. Wrap CallInvoker as Dispatcher
auto dispatcher = std::make_shared<nitro::CallInvokerDispatcher>(callInvoker);
// 3. Install Nitro
try {
nitro::install(runtime, dispatcher);
_didInstall = true;
} catch (const std::exception& exc) {
nitro::Logger::log(nitro::LogLevel::Error, "NitroModules", "Failed to install Nitro! %s", exc.what());
_errorMessage = [NSString stringWithCString:exc.what() encoding:kCFStringEncodingUTF8];
_didInstall = false;
}
}
- (NSString*)install {
if (_didInstall) {
// installJSIBindingsWithRuntime ran successfully.
return nil;
} else {
if (_errorMessage != nil) {
return _errorMessage;
}
return @"installJSIBindingsWithRuntime: was not called - JSI Bindings could not be installed!";
}
}
- (std::shared_ptr<react::TurboModule>)getTurboModule:(const react::ObjCTurboModule::InitParams&)params {
// Cache the CallInvoker statically (weak) - we use it later in `installJSIBindingsWithRuntime:`.
_callInvoker = params.jsInvoker;
return std::make_shared<react::NativeNitroModulesSpecJSI>(params);
}
@end
#endif