Skip to content

Commit

Permalink
install bindings earlier and prevent setup from main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudo committed May 9, 2024
1 parent 968bd95 commit a7b7bf6
Showing 1 changed file with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ static Class getFallbackClassFromName(const char *name)

@implementation RCTTurboModuleManager {
std::shared_ptr<CallInvoker> _jsInvoker;
facebook::jsi::Runtime *_runtime;
__weak id<RCTTurboModuleManagerDelegate> _delegate;
__weak RCTBridge *_bridge;

Expand Down Expand Up @@ -308,6 +307,7 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy
*/

- (std::shared_ptr<TurboModule>)provideTurboModule:(const char *)moduleName
withRuntime:(facebook::jsi::Runtime *)runtime
{
auto turboModuleLookup = _turboModuleCache.find(moduleName);
if (turboModuleLookup != _turboModuleCache.end()) {
Expand Down Expand Up @@ -411,6 +411,14 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy
RCTLogError(@"TurboModule \"%@\"'s getTurboModule: method returned nil.", moduleClass);
}
_turboModuleCache.insert({moduleName, turboModule});

if ([module respondsToSelector:@selector(createBindingsInstaller)]) {
RCTTurboModuleBindingsInstaller *installer =
(RCTTurboModuleBindingsInstaller *)[module performSelector:@selector(createBindingsInstaller)];
if (installer != nil) {
installer.get(*runtime);
}
}
return turboModule;
}

Expand Down Expand Up @@ -707,14 +715,6 @@ - (BOOL)_shouldCreateObjCModule:(Class)moduleClass
[(id<RCTCallInvokerModule>)module setCallInvoker:callInvoker];
}

if ([module respondsToSelector:@selector(createBindingsInstaller)]) {
RCTTurboModuleBindingsInstaller *installer =
(RCTTurboModuleBindingsInstaller *)[module performSelector:@selector(createBindingsInstaller)];
if (installer != nil && _runtime) {
installer.get(*_runtime);
}
}

/**
* Some modules need their own queues, but don't provide any, so we need to create it for them.
* These modules typically have the following:
Expand Down Expand Up @@ -924,16 +924,15 @@ - (BOOL)_requiresMainQueueSetup:(Class)moduleClass

- (void)installJSBindings:(facebook::jsi::Runtime &)runtime
{
_runtime = &runtime;

/**
* We keep TurboModuleManager alive until the JS VM is deleted.
* It is perfectly valid to only use/create TurboModules from JS.
* In such a case, we shouldn't dealloc TurboModuleManager if there
* aren't any strong references to it in ObjC. Hence, we give
* __turboModuleProxy a strong reference to TurboModuleManager.
*/
auto turboModuleProvider = [self](const std::string &name) -> std::shared_ptr<react::TurboModule> {
auto turboModuleProvider = [self,
runtime = &runtime](const std::string &name) -> std::shared_ptr<react::TurboModule> {
auto moduleName = name.c_str();

TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);
Expand All @@ -947,7 +946,7 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime
* Additionally, if a TurboModule with the name `name` isn't found, then we
* trigger an assertion failure.
*/
auto turboModule = [self provideTurboModule:moduleName];
auto turboModule = [self provideTurboModule:moduleName withRuntime:runtime];

if (moduleWasNotInitialized && [self moduleIsInitialized:moduleName]) {
[self->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup];
Expand Down Expand Up @@ -1038,14 +1037,12 @@ - (void)bridgeDidInvalidateModules:(NSNotification *)notification
}

[self _invalidateModules];
_runtime = nullptr;
}

- (void)invalidate
{
[self _enterInvalidatingState];
[self _invalidateModules];
_runtime = nullptr;
}

- (void)_enterInvalidatingState
Expand Down

0 comments on commit a7b7bf6

Please sign in to comment.