Skip to content

Commit

Permalink
Back out "Integrate TurboModuleManager with legacy module registration"
Browse files Browse the repository at this point in the history
Summary:
Original commit changeset: 785240c51eb9

Original Phabricator Diff: D44647858

## Changelog:
[iOS][Fixed] - Revert [TM][iOS] Integrate TurboModuleManager with legacy module registration

Reviewed By: jacdebug

Differential Revision: D45773196

fbshipit-source-id: 9d1e8c463e599d0a38b982377ceb5c7aad0c806f
  • Loading branch information
cipolleschi authored and facebook-github-bot committed May 11, 2023
1 parent ad116da commit f5759d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ RCT_EXTERN void RCTTurboModuleSetBindingMode(facebook::react::TurboModuleBinding
jsInvoker:
(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker;

/**
* Return a pre-initialized list of leagcy native modules.
* These modules shouldn't be TurboModule-compatible (i.e: they should not conform to RCTTurboModule).
*
* This method is only used by the TurboModule interop layer.
*
* It must match the signature of RCTBridgeDelegate extraModulesForBridge:
* - (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge;
*/
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
__attribute((deprecated("Please make all native modules returned from this method TurboModule-compatible.")));

@end

@interface RCTTurboModuleManager : NSObject <RCTTurboModuleRegistry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ @implementation RCTTurboModuleManager {
// Enforce synchronous access to _invalidating and _moduleHolders
std::mutex _moduleHoldersMutex;
std::atomic<bool> _invalidating;

NSDictionary<NSString *, id<RCTBridgeModule>> *_legacyEagerlyInitializedModules;
NSDictionary<NSString *, Class> *_legacyEagerlyRegisteredModuleClasses;
}

- (instancetype)initWithBridge:(RCTBridge *)bridge
Expand All @@ -193,27 +190,6 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
_bridge = bridge;
_invalidating = false;

if (RCTTurboModuleInteropEnabled()) {
NSMutableDictionary<NSString *, id<RCTBridgeModule>> *legacyInitializedModules = [NSMutableDictionary new];

if ([_delegate respondsToSelector:@selector(extraModulesForBridge:)]) {
for (id<RCTBridgeModule> module in [_delegate extraModulesForBridge:nil]) {
if (!RCT_IS_TURBO_MODULE_INSTANCE(module)) {
[legacyInitializedModules setObject:module forKey:RCTBridgeModuleNameForClass([module class])];
}
}
}
_legacyEagerlyInitializedModules = legacyInitializedModules;

NSMutableDictionary<NSString *, Class> *legacyEagerlyRegisteredModuleClasses = [NSMutableDictionary new];
for (Class moduleClass in RCTGetModuleClasses()) {
if (!RCT_IS_TURBO_MODULE_CLASS(moduleClass)) {
[legacyEagerlyRegisteredModuleClasses setObject:moduleClass forKey:RCTBridgeModuleNameForClass(moduleClass)];
}
}
_legacyEagerlyRegisteredModuleClasses = legacyEagerlyRegisteredModuleClasses;
}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(bridgeWillInvalidateModules:)
name:RCTBridgeWillInvalidateModulesNotification
Expand Down Expand Up @@ -413,10 +389,21 @@ - (ModuleHolder *)_getOrCreateModuleHolder:(const char *)moduleName
}

if (shouldCreateModule) {
Class moduleClass;

/**
* Step 2a: Resolve platform-specific class.
*/
Class moduleClass = [self _getModuleClassFromName:moduleName];
if (RCTTurboModuleManagerDelegateLockingDisabled()) {
moduleClass = [_delegate getModuleClassFromName:moduleName];
} else {
std::lock_guard<std::mutex> delegateGuard(_turboModuleManagerDelegateMutex);
moduleClass = [_delegate getModuleClassFromName:moduleName];
}

if (!moduleClass) {
moduleClass = getFallbackClassFromName(moduleName);
}

__block id<RCTBridgeModule> module = nil;

Expand Down Expand Up @@ -497,7 +484,15 @@ - (ModuleHolder *)_getOrCreateModuleHolder:(const char *)moduleName
*/

TurboModulePerfLogger::moduleCreateConstructStart(moduleName, moduleId);
module = [self _getModuleInstanceFromClass:moduleClass];
if (RCTTurboModuleManagerDelegateLockingDisabled()) {
module = (id<RCTBridgeModule>)[_delegate getModuleInstanceFromClass:moduleClass];
} else {
std::lock_guard<std::mutex> delegateGuard(_turboModuleManagerDelegateMutex);
module = (id<RCTBridgeModule>)[_delegate getModuleInstanceFromClass:moduleClass];
}
if (!module) {
module = [moduleClass new];
}
TurboModulePerfLogger::moduleCreateConstructEnd(moduleName, moduleId);

TurboModulePerfLogger::moduleCreateSetUpStart(moduleName, moduleId);
Expand Down Expand Up @@ -637,56 +632,6 @@ - (ModuleHolder *)_getOrCreateModuleHolder:(const char *)moduleName
return module;
}

- (Class)_getModuleClassFromName:(const char *)moduleName
{
NSString *moduleNameStr = @(moduleName);
if (_legacyEagerlyInitializedModules && _legacyEagerlyInitializedModules[moduleNameStr]) {
return [_legacyEagerlyInitializedModules[moduleNameStr] class];
}

if (_legacyEagerlyRegisteredModuleClasses && _legacyEagerlyRegisteredModuleClasses[moduleNameStr]) {
return _legacyEagerlyRegisteredModuleClasses[moduleNameStr];
}

Class moduleClass;
if (RCTTurboModuleManagerDelegateLockingDisabled()) {
moduleClass = [_delegate getModuleClassFromName:moduleName];
} else {
std::lock_guard<std::mutex> delegateGuard(_turboModuleManagerDelegateMutex);
moduleClass = [_delegate getModuleClassFromName:moduleName];
}

if (!moduleClass) {
moduleClass = getFallbackClassFromName(moduleName);
}
return moduleClass;
}

- (id<RCTBridgeModule>)_getModuleInstanceFromClass:(Class)moduleClass
{
NSString *moduleNameStr = RCTBridgeModuleNameForClass(moduleClass);
if (_legacyEagerlyInitializedModules && _legacyEagerlyInitializedModules[moduleNameStr]) {
return _legacyEagerlyInitializedModules[moduleNameStr];
}

if (_legacyEagerlyRegisteredModuleClasses && _legacyEagerlyRegisteredModuleClasses[moduleNameStr]) {
return [_legacyEagerlyRegisteredModuleClasses[moduleNameStr] new];
}

id<RCTBridgeModule> module = nil;
if (RCTTurboModuleManagerDelegateLockingDisabled()) {
module = (id<RCTBridgeModule>)[_delegate getModuleInstanceFromClass:moduleClass];
} else {
std::lock_guard<std::mutex> delegateGuard(_turboModuleManagerDelegateMutex);
module = (id<RCTBridgeModule>)[_delegate getModuleInstanceFromClass:moduleClass];
}
if (!module) {
module = [moduleClass new];
}

return module;
}

/**
* Should this NativeModule be created and initialized on the main queue?
*
Expand Down

0 comments on commit f5759d7

Please sign in to comment.