mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 20:25:33 +08:00
Refactored module access to allow for lazy loading
Summary: public The `bridge.modules` dictionary provides access to all native modules, but this API requires that every module is initialized in advance so that any module can be accessed. This diff introduces a better API that will allow modules to be initialized lazily as they are needed, and deprecates `bridge.modules` (modules that use it will still work, but should be rewritten to use `bridge.moduleClasses` or `-[bridge moduleForName/Class:` instead. The rules are now as follows: * Any module that overrides `init` or `setBridge:` will be initialized on the main thread when the bridge is created * Any module that implements `constantsToExport:` will be initialized later when the config is exported (the module itself will be initialized on a background queue, but `constantsToExport:` will still be called on the main thread. * All other modules will be initialized lazily when a method is first called on them. These rules may seem slightly arcane, but they have the advantage of not violating any assumptions that may have been made by existing code - any module written under the original assumption that it would be initialized synchronously on the main thread when the bridge is created should still function exactly the same, but modules that avoid overriding `init` or `setBridge:` will now be loaded lazily. I've rewritten most of the standard modules to take advantage of this new lazy loading, with the following results: Out of the 65 modules included in UIExplorer: * 16 are initialized on the main thread when the bridge is created * A further 8 are initialized when the config is exported to JS * The remaining 41 will be initialized lazily on-demand Reviewed By: jspahrsummers Differential Revision: D2677695 fb-gh-sync-id: 507ae7e9fd6b563e89292c7371767c978e928f33
This commit is contained in:
committed by
facebook-github-bot-7
parent
bba71f146d
commit
060664fd3d
@@ -44,16 +44,21 @@ typedef void (^RCTPropBlock)(id<RCTComponent> view, id json);
|
||||
RCTShadowView *_defaultShadowView;
|
||||
NSMutableDictionary<NSString *, RCTPropBlock> *_viewPropBlocks;
|
||||
NSMutableDictionary<NSString *, RCTPropBlock> *_shadowPropBlocks;
|
||||
RCTBridge *_bridge;
|
||||
}
|
||||
|
||||
- (instancetype)initWithManager:(RCTViewManager *)manager
|
||||
@synthesize manager = _manager;
|
||||
|
||||
- (instancetype)initWithManagerClass:(Class)managerClass
|
||||
bridge:(RCTBridge *)bridge
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_manager = manager;
|
||||
_bridge = bridge;
|
||||
_managerClass = managerClass;
|
||||
_viewPropBlocks = [NSMutableDictionary new];
|
||||
_shadowPropBlocks = [NSMutableDictionary new];
|
||||
|
||||
_name = RCTBridgeModuleNameForClass([manager class]);
|
||||
_name = RCTBridgeModuleNameForClass(_managerClass);
|
||||
RCTAssert(_name.length, @"Invalid moduleName '%@'", _name);
|
||||
if ([_name hasSuffix:@"Manager"]) {
|
||||
_name = [_name substringToIndex:_name.length - @"Manager".length];
|
||||
@@ -62,13 +67,21 @@ typedef void (^RCTPropBlock)(id<RCTComponent> view, id json);
|
||||
return self;
|
||||
}
|
||||
|
||||
- (RCTViewManager *)manager
|
||||
{
|
||||
if (!_manager) {
|
||||
_manager = [_bridge moduleForClass:_managerClass];
|
||||
}
|
||||
return _manager;
|
||||
}
|
||||
|
||||
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
- (UIView *)createViewWithTag:(NSNumber *)tag props:(NSDictionary<NSString *, id> *)props
|
||||
{
|
||||
RCTAssertMainThread();
|
||||
|
||||
UIView *view = (UIView *)(props ? [_manager viewWithProps:props] : [_manager view]);
|
||||
UIView *view = (UIView *)(props ? [self.manager viewWithProps:props] : [_manager view]);
|
||||
view.reactTag = tag;
|
||||
view.multipleTouchEnabled = YES;
|
||||
view.userInteractionEnabled = YES; // required for touch handling
|
||||
@@ -78,7 +91,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
- (RCTShadowView *)createShadowViewWithTag:(NSNumber *)tag
|
||||
{
|
||||
RCTShadowView *shadowView = [_manager shadowView];
|
||||
RCTShadowView *shadowView = [self.manager shadowView];
|
||||
shadowView.reactTag = tag;
|
||||
shadowView.viewName = _name;
|
||||
return shadowView;
|
||||
@@ -310,15 +323,13 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
- (NSDictionary<NSString *, id> *)viewConfig
|
||||
{
|
||||
Class managerClass = [_manager class];
|
||||
|
||||
NSMutableArray<NSString *> *directEvents = [NSMutableArray new];
|
||||
if (RCTClassOverridesInstanceMethod(managerClass, @selector(customDirectEventTypes))) {
|
||||
NSArray<NSString *> *events = [_manager customDirectEventTypes];
|
||||
if (RCTClassOverridesInstanceMethod(_managerClass, @selector(customDirectEventTypes))) {
|
||||
NSArray<NSString *> *events = [self.manager customDirectEventTypes];
|
||||
if (RCT_DEBUG) {
|
||||
RCTAssert(!events || [events isKindOfClass:[NSArray class]],
|
||||
@"customDirectEventTypes must return an array, but %@ returned %@",
|
||||
managerClass, [events class]);
|
||||
_managerClass, [events class]);
|
||||
}
|
||||
for (NSString *event in events) {
|
||||
[directEvents addObject:RCTNormalizeInputEventName(event)];
|
||||
@@ -326,12 +337,12 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
}
|
||||
|
||||
NSMutableArray<NSString *> *bubblingEvents = [NSMutableArray new];
|
||||
if (RCTClassOverridesInstanceMethod(managerClass, @selector(customBubblingEventTypes))) {
|
||||
NSArray<NSString *> *events = [_manager customBubblingEventTypes];
|
||||
if (RCTClassOverridesInstanceMethod(_managerClass, @selector(customBubblingEventTypes))) {
|
||||
NSArray<NSString *> *events = [self.manager customBubblingEventTypes];
|
||||
if (RCT_DEBUG) {
|
||||
RCTAssert(!events || [events isKindOfClass:[NSArray class]],
|
||||
@"customBubblingEventTypes must return an array, but %@ returned %@",
|
||||
managerClass, [events class]);
|
||||
_managerClass, [events class]);
|
||||
}
|
||||
for (NSString *event in events) {
|
||||
[bubblingEvents addObject:RCTNormalizeInputEventName(event)];
|
||||
@@ -340,7 +351,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
unsigned int count = 0;
|
||||
NSMutableDictionary *propTypes = [NSMutableDictionary new];
|
||||
Method *methods = class_copyMethodList(object_getClass(managerClass), &count);
|
||||
Method *methods = class_copyMethodList(object_getClass(_managerClass), &count);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
Method method = methods[i];
|
||||
SEL selector = method_getName(method);
|
||||
@@ -349,7 +360,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
NSRange nameRange = [methodName rangeOfString:@"_"];
|
||||
if (nameRange.length) {
|
||||
NSString *name = [methodName substringFromIndex:nameRange.location + 1];
|
||||
NSString *type = ((NSArray<NSString *> *(*)(id, SEL))objc_msgSend)(managerClass, selector)[0];
|
||||
NSString *type = ((NSArray<NSString *> *(*)(id, SEL))objc_msgSend)(_managerClass, selector)[0];
|
||||
if (RCT_DEBUG && propTypes[name] && ![propTypes[name] isEqualToString:type]) {
|
||||
RCTLogError(@"Property '%@' of component '%@' redefined from '%@' "
|
||||
"to '%@'", name, _name, propTypes[name], type);
|
||||
|
||||
Reference in New Issue
Block a user