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:
Nick Lockwood
2015-11-25 03:09:00 -08:00
committed by facebook-github-bot-7
parent bba71f146d
commit 060664fd3d
54 changed files with 761 additions and 651 deletions

View File

@@ -45,17 +45,17 @@
RCT_EXPORT_MODULE()
- (void)setBridge:(RCTBridge *)bridge
- (void)setUp
{
// Get image loaders and decoders
NSMutableArray<id<RCTImageURLLoader>> *loaders = [NSMutableArray array];
NSMutableArray<id<RCTImageDataDecoder>> *decoders = [NSMutableArray array];
for (id<RCTBridgeModule> module in bridge.modules.allValues) {
if ([module conformsToProtocol:@protocol(RCTImageURLLoader)]) {
[loaders addObject:(id<RCTImageURLLoader>)module];
for (Class moduleClass in _bridge.moduleClasses) {
if ([moduleClass conformsToProtocol:@protocol(RCTImageURLLoader)]) {
[loaders addObject:[_bridge moduleForClass:moduleClass]];
}
if ([module conformsToProtocol:@protocol(RCTImageDataDecoder)]) {
[decoders addObject:(id<RCTImageDataDecoder>)module];
if ([moduleClass conformsToProtocol:@protocol(RCTImageDataDecoder)]) {
[decoders addObject:[_bridge moduleForClass:moduleClass]];
}
}
@@ -85,17 +85,16 @@ RCT_EXPORT_MODULE()
}
}];
_bridge = bridge;
_loaders = loaders;
_decoders = decoders;
_URLCacheQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLCacheQueue", DISPATCH_QUEUE_SERIAL);
_URLCache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024 // 5MB
diskCapacity:200 * 1024 * 1024 // 200MB
diskPath:@"React/RCTImageDownloader"];
}
- (id<RCTImageURLLoader>)imageURLLoaderForURL:(NSURL *)URL
{
if (!_loaders) {
[self setUp];
}
if (RCT_DEBUG) {
// Check for handler conflicts
float previousPriority = 0;
@@ -133,6 +132,10 @@ RCT_EXPORT_MODULE()
- (id<RCTImageDataDecoder>)imageDataDecoderForData:(NSData *)data
{
if (!_decoders) {
[self setUp];
}
if (RCT_DEBUG) {
// Check for handler conflicts
float previousPriority = 0;
@@ -212,7 +215,17 @@ RCT_EXPORT_MODULE()
}
// All access to URL cache must be serialized
if (!_URLCacheQueue) {
_URLCacheQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLCacheQueue", DISPATCH_QUEUE_SERIAL);
}
dispatch_async(_URLCacheQueue, ^{
if (!_URLCache) {
_URLCache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024 // 5MB
diskCapacity:200 * 1024 * 1024 // 200MB
diskPath:@"React/RCTImageDownloader"];
}
RCTImageLoader *strongSelf = weakSelf;
if (cancelled || !strongSelf) {
return;
@@ -385,14 +398,7 @@ RCT_EXPORT_MODULE()
- (BOOL)canHandleRequest:(NSURLRequest *)request
{
NSURL *requestURL = request.URL;
for (id<RCTBridgeModule> module in _bridge.modules.allValues) {
if ([module conformsToProtocol:@protocol(RCTImageURLLoader)] &&
[(id<RCTImageURLLoader>)module canLoadImageURL:requestURL]) {
return YES;
}
}
return NO;
return [self imageURLLoaderForURL:request.URL] != nil;
}
- (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
@@ -440,7 +446,7 @@ RCT_EXPORT_MODULE()
- (RCTImageLoader *)imageLoader
{
return self.modules[RCTBridgeModuleNameForClass([RCTImageLoader class])];
return [self moduleForClass:[RCTImageLoader class]];
}
@end

View File

@@ -30,15 +30,6 @@ static NSString *const RCTImageStoreURLScheme = @"rct-image-store";
RCT_EXPORT_MODULE()
- (instancetype)init
{
if ((self = [super init])) {
_store = [NSMutableDictionary new];
_id = 0;
}
return self;
}
- (void)removeImageForTag:(NSString *)imageTag withBlock:(void (^)())block
{
dispatch_async(_methodQueue, ^{
@@ -52,6 +43,12 @@ RCT_EXPORT_MODULE()
- (NSString *)_storeImageData:(NSData *)imageData
{
RCTAssertThread(_methodQueue, @"Must be called on RCTImageStoreManager thread");
if (!_store) {
_store = [NSMutableDictionary new];
_id = 0;
}
NSString *imageTag = [NSString stringWithFormat:@"%@://%tu", RCTImageStoreURLScheme, _id++];
_store[imageTag] = imageData;
return imageTag;
@@ -225,7 +222,7 @@ RCT_EXPORT_METHOD(addImageFromBase64:(NSString *)base64String
- (RCTImageStoreManager *)imageStoreManager
{
return self.modules[RCTBridgeModuleNameForClass([RCTImageStoreManager class])];
return [self moduleForClass:[RCTImageStoreManager class]];
}
@end