forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bridge] Scope the module loading and initialization to a single bridge
- Loading branch information
Showing
8 changed files
with
187 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
@import Foundation; | ||
|
||
@class RCTBridgeModuleLoader; | ||
|
||
@interface RCTBridgeModuleConfiguration : NSObject | ||
|
||
- (instancetype)initFromModuleLoader:(RCTBridgeModuleLoader *)moduleLoader; | ||
|
||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTBridgeModuleConfiguration.h" | ||
|
||
@implementation RCTBridgeModuleConfiguration | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
@import Foundation; | ||
|
||
@interface RCTBridgeModuleLoader : NSObject | ||
|
||
@property (nonatomic, strong, readonly) NSArray *moduleClassesByModuleID; | ||
|
||
+ (instancetype)sharedLoader; | ||
|
||
- (NSUInteger)addModuleClass:(Class)moduleClass; | ||
- (void)loadAllModuleClasses; | ||
|
||
- (NSUInteger)moduleIDForModuleName:(NSString *)moduleName; | ||
- (NSString *)moduleNameForModuleID:(NSUInteger)moduleID; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTBridgeModuleLoader.h" | ||
|
||
#import "RCTBridgeModule.h" | ||
#import "RCTUtils.h" | ||
|
||
@implementation RCTBridgeModuleLoader { | ||
NSMutableArray *_moduleNamesByID; | ||
NSMutableArray *_moduleClassesByID; | ||
NSMutableDictionary *_moduleIDsByName; | ||
} | ||
|
||
+ (instancetype)sharedLoader | ||
{ | ||
static RCTBridgeModuleLoader *instance; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
instance = [[RCTBridgeModuleLoader alloc] init]; | ||
}); | ||
return instance; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_moduleNamesByID = [NSMutableArray array]; | ||
_moduleClassesByID = [NSMutableArray array]; | ||
_moduleIDsByName = [NSMutableDictionary dictionary]; | ||
} | ||
return self; | ||
} | ||
|
||
- (NSArray *)moduleClassesByModuleID | ||
{ | ||
return _moduleClassesByID; | ||
} | ||
|
||
- (NSUInteger)addModuleClass:(Class)moduleClass | ||
{ | ||
NSString *moduleName = RCTModuleNameForClass(moduleClass); | ||
NSNumber *moduleIDNumber = _moduleIDsByName[moduleName]; | ||
if (moduleIDNumber) { | ||
return moduleIDNumber.unsignedIntegerValue; | ||
} | ||
|
||
NSUInteger moduleID = _moduleNamesByID.count; | ||
_moduleIDsByName[moduleName] = @(moduleID); | ||
[_moduleNamesByID addObject:moduleName]; | ||
[_moduleClassesByID addObject:moduleClass]; | ||
return moduleID; | ||
} | ||
|
||
- (void)loadAllModuleClasses | ||
{ | ||
RCTEnumerateClasses(^(__unsafe_unretained Class cls) { | ||
if ([cls conformsToProtocol:@protocol(RCTBridgeModule)]) { | ||
[self addModuleClass:cls]; | ||
} | ||
}); | ||
} | ||
|
||
- (NSUInteger)moduleIDForModuleName:(NSString *)moduleName | ||
{ | ||
return ((NSNumber *)_moduleIDsByName[moduleName]).unsignedIntegerValue; | ||
} | ||
|
||
- (NSString *)moduleNameForModuleID:(NSUInteger)moduleID | ||
{ | ||
return _moduleNamesByID[moduleID]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.