Skip to content

Commit

Permalink
Refactor: Rename TurboModuleHolder -> ModuleHolder (#37174)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37174

In the future, this will hold TurboModules, as well as legacy/interop modules.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D45404790

fbshipit-source-id: c0f3d597dfd0edc271f2e89c9ddb26e8a5542367
  • Loading branch information
RSNara authored and facebook-github-bot committed Apr 30, 2023
1 parent 34caa8d commit bd30131
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int32_t getUniqueId()
return counter++;
}

class TurboModuleHolder {
class ModuleHolder {
private:
const int32_t moduleId_;
id<RCTTurboModule> module_;
Expand All @@ -59,8 +59,7 @@ int32_t getUniqueId()
std::condition_variable cv_;

public:
TurboModuleHolder()
: moduleId_(getUniqueId()), module_(nil), isTryingToCreateModule_(false), isDoneCreatingModule_(false)
ModuleHolder() : moduleId_(getUniqueId()), module_(nil), isTryingToCreateModule_(false), isDoneCreatingModule_(false)
{
}

Expand Down Expand Up @@ -167,17 +166,17 @@ @implementation RCTTurboModuleManager {
* We need to come up with a mechanism to allow modules to specify whether
* they want to be long-lived or short-lived.
*
* All instances of TurboModuleHolder are owned by the _turboModuleHolders map.
* We only reference TurboModuleHolders via pointers to entries in the _turboModuleHolders map.
* All instances of ModuleHolder are owned by the _moduleHolders map.
* We only reference ModuleHolders via pointers to entries in the _moduleHolders map.
*/
std::unordered_map<std::string, TurboModuleHolder> _turboModuleHolders;
std::unordered_map<std::string, ModuleHolder> _moduleHolders;
std::unordered_map<std::string, std::shared_ptr<TurboModule>> _turboModuleCache;

// Enforce synchronous access into _delegate
std::mutex _turboModuleManagerDelegateMutex;

// Enforce synchronous access to _invalidating and _turboModuleHolders
std::mutex _turboModuleHoldersMutex;
// Enforce synchronous access to _invalidating and _moduleHolders
std::mutex _moduleHoldersMutex;
std::atomic<bool> _invalidating;
}

Expand Down Expand Up @@ -318,14 +317,14 @@ - (void)notifyAboutTurboModuleSetup:(const char *)name
return turboModule;
}

- (TurboModuleHolder *)_getOrCreateTurboModuleHolder:(const char *)moduleName
- (ModuleHolder *)_getOrCreateModuleHolder:(const char *)moduleName
{
std::lock_guard<std::mutex> guard(_turboModuleHoldersMutex);
std::lock_guard<std::mutex> guard(_moduleHoldersMutex);
if (_invalidating) {
return nullptr;
}

return &_turboModuleHolders[moduleName];
return &_moduleHolders[moduleName];
}

/**
Expand All @@ -342,7 +341,7 @@ - (TurboModuleHolder *)_getOrCreateTurboModuleHolder:(const char *)moduleName
moduleName = [[[NSString stringWithUTF8String:moduleName] substringFromIndex:3] UTF8String];
}

TurboModuleHolder *moduleHolder = [self _getOrCreateTurboModuleHolder:moduleName];
ModuleHolder *moduleHolder = [self _getOrCreateModuleHolder:moduleName];

if (!moduleHolder) {
return nil;
Expand All @@ -361,7 +360,7 @@ - (TurboModuleHolder *)_getOrCreateTurboModuleHolder:(const char *)moduleName
}

- (id<RCTTurboModule>)_provideRCTTurboModule:(const char *)moduleName
moduleHolder:(TurboModuleHolder *)moduleHolder
moduleHolder:(ModuleHolder *)moduleHolder
shouldPerfLog:(BOOL)shouldPerfLog
{
bool shouldCreateModule = false;
Expand Down Expand Up @@ -754,8 +753,8 @@ - (id)moduleForName:(const char *)moduleName warnOnLookupFailure:(BOOL)warnOnLoo

- (BOOL)moduleIsInitialized:(const char *)moduleName
{
std::unique_lock<std::mutex> guard(_turboModuleHoldersMutex);
return _turboModuleHolders.find(moduleName) != _turboModuleHolders.end();
std::unique_lock<std::mutex> guard(_moduleHoldersMutex);
return _moduleHolders.find(moduleName) != _moduleHolders.end();
}

#pragma mark Invalidation logic
Expand Down Expand Up @@ -788,8 +787,8 @@ - (void)invalidate

- (void)_enterInvalidatingState
{
// This should halt all insertions into _turboModuleHolders
std::lock_guard<std::mutex> guard(_turboModuleHoldersMutex);
// This should halt all insertions into _moduleHolders
std::lock_guard<std::mutex> guard(_moduleHoldersMutex);
_invalidating = true;
}

Expand All @@ -798,9 +797,9 @@ - (void)_invalidateModules
// Backward-compatibility: RCTInvalidating handling.
dispatch_group_t moduleInvalidationGroup = dispatch_group_create();

for (auto &pair : _turboModuleHolders) {
for (auto &pair : _moduleHolders) {
std::string moduleName = pair.first;
TurboModuleHolder *moduleHolder = &pair.second;
ModuleHolder *moduleHolder = &pair.second;

/**
* We could start tearing down ReactNative before a TurboModule is fully initialized. In this case, we should wait
Expand Down Expand Up @@ -844,7 +843,7 @@ - (void)_invalidateModules
RCTLogError(@"TurboModuleManager: Timed out waiting for modules to be invalidated");
}

_turboModuleHolders.clear();
_moduleHolders.clear();
_turboModuleCache.clear();
}

Expand Down

0 comments on commit bd30131

Please sign in to comment.