react-native-macos/React/Base/RCTModuleRegistry.m

49 строки
1.2 KiB
Mathematica
Исходник Обычный вид История

Introduce RCTModuleRegistry Summary: ## Problem Many of our NativeModules are TurboModule-compatible. All our TurboModules currently use the bridge to require other NativeModules/TurboModules. As we migrate more surfaces to Venice, this lookup in many of these TurboModules will break, because the bridge will be nil. ## Initial Solution We migrate all these TurboModules over to the turboModuleRegistry. In FBiOS, this will work because all NativeModules are TurboModule-compatible, and should [have the turboModuleRegistry attached to them by the TurboModuleManager](https://fburl.com/diffusion/d747fbc3). However, this solution has a few problems: - If the TurboModule is used within a TurboModule-disabled app, it will break, because turboModuleRegistry will be nil on the module. Therefore, this solution isn't portable/flexible across apps. - Longer term, we should deprecate synthesize bridge inside NativeModules. With this approach, we can't remove the synthesize bridge = bridge call from the NativeModule. ## Suggested Solution Both NativeModules and TurboModules will be given access to an `RCTModuleRegistry` object. The RCTModuleRegistry object will use the TurboModuleManager when available and the Bridge when available to query for NativeModules and TurboModules. Otherwise, it'll just return nil. When we do a lookup via this RCTModuleRegistry: - In Venice, the bridge will be nil, so we won't search the bridge. - In FBiOS, the bridge and the TurboModuleManager will be non-nil, so we'll first search the bridge, and then the TurboModuleManager. - In standalone apps, the TurboModuleManager will be nil, so we'll only do a lookup on the bridge. Long story short, RCTModuleRegistry will do the right thing in all scenarios. Changelog: [Internal] Reviewed By: PeteTheHeat Differential Revision: D25412847 fbshipit-source-id: 13f41276158d90c68ab4925e518d71a2f369c210
2020-12-11 07:21:11 +03:00
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTBridge.h"
#import "RCTBridgeModule.h"
@implementation RCTModuleRegistry {
__weak id<RCTTurboModuleRegistry> _turboModuleRegistry;
__weak RCTBridge *_bridge;
}
- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
}
- (void)setTurboModuleRegistry:(id<RCTTurboModuleRegistry>)turboModuleRegistry
{
_turboModuleRegistry = turboModuleRegistry;
}
- (id)moduleForName:(const char *)moduleName
{
return [self moduleForName:moduleName lazilyLoadIfNecessary:YES];
}
- (id)moduleForName:(const char *)moduleName lazilyLoadIfNecessary:(BOOL)lazilyLoad
{
id<RCTBridgeModule> module = nil;
RCTBridge *bridge = _bridge;
if (bridge) {
module = [bridge moduleForName:[NSString stringWithUTF8String:moduleName] lazilyLoadIfNecessary:lazilyLoad];
}
id<RCTTurboModuleRegistry> turboModuleRegistry = _turboModuleRegistry;
if (module == nil && turboModuleRegistry && (lazilyLoad || [turboModuleRegistry moduleIsInitialized:moduleName])) {
module = [turboModuleRegistry moduleForName:moduleName];
}
return module;
}
@end