Skip to content

Commit 3eb57fc

Browse files
authored
fix(react-native-host): notify when the runtime is ready (#3604)
1 parent 89ad5d8 commit 3eb57fc

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

.changeset/calm-dingos-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rnx-kit/react-native-host": patch
3+
---
4+
5+
Post a notification when the React runtime is ready

packages/react-native-host/cocoa/RNXTurboModuleAdapter.mm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,26 @@
5353
@implementation RNXTurboModuleAdapter {
5454
#if USE_FABRIC
5555
RCTTurboModuleManager *_turboModuleManager;
56+
std::weak_ptr<facebook::react::CallInvoker> _jsInvoker;
5657
#endif // USE_FABRIC
5758
#if USE_RUNTIME_SCHEDULER
5859
std::shared_ptr<facebook::react::RuntimeScheduler> _runtimeScheduler;
5960
#endif // USE_RUNTIME_SCHEDULER
6061
}
6162

63+
#if USE_FABRIC
64+
- (instancetype)init
65+
{
66+
if (self = [super init]) {
67+
[NSNotificationCenter.defaultCenter addObserver:self
68+
selector:@selector(onRuntimeReady:)
69+
name:@"RCTInstanceDidLoadBundle"
70+
object:nil];
71+
}
72+
return self;
73+
}
74+
#endif // USE_FABRIC
75+
6276
- (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:
6377
(RCTBridge *)bridge
6478
{
@@ -90,6 +104,7 @@ - (Class)getModuleClassFromName:(char const *)name
90104
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
91105
{
92106
#if __has_include(<react/nativemodule/defaults/DefaultTurboModules.h>) // >= 0.75
107+
_jsInvoker = jsInvoker;
93108
return facebook::react::DefaultTurboModules::getTurboModule(name, jsInvoker);
94109
#else
95110
return nullptr;
@@ -127,6 +142,18 @@ - (Class)getModuleClassFromName:(char const *)name
127142
#endif // USE_RUNTIME_SCHEDULER
128143
}
129144

145+
- (void)onRuntimeReady:(NSNotification *)note
146+
{
147+
if (auto jsInvoker = _jsInvoker.lock()) {
148+
jsInvoker->invokeAsync([](facebook::jsi::Runtime &runtime) {
149+
NSDictionary *userInfo = @{@"runtime": [NSValue valueWithPointer:&runtime]};
150+
[NSNotificationCenter.defaultCenter postNotificationName:@"ReactAppRuntimeReady"
151+
object:nil
152+
userInfo:userInfo];
153+
});
154+
}
155+
}
156+
130157
#endif // USE_FABRIC
131158

132159
@end

0 commit comments

Comments
 (0)