Add block-based initializer for RCTModuleData

Reviewed By: shergin

Differential Revision: D4821772

fbshipit-source-id: 2cd35ec6726b6fe1494a92966ade5aa0b86e769b
This commit is contained in:
Pieter De Baets 2017-04-07 11:11:03 -07:00 committed by Facebook Github Bot
parent 597c8c7700
commit 76b93e1253
9 changed files with 52 additions and 27 deletions

View File

@ -9,6 +9,8 @@
#import <Foundation/Foundation.h>
#import <React/RCTBridge.h>
#ifndef FB_REFERENCE_IMAGE_DIR
#define FB_REFERENCE_IMAGE_DIR ""
#endif
@ -49,7 +51,7 @@
*/
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
moduleProvider:(NSArray<id<RCTBridgeModule>> *(^)(void))block NS_DESIGNATED_INITIALIZER;
moduleProvider:(RCTBridgeModuleListProvider)block NS_DESIGNATED_INITIALIZER;
/**
* Simplest runTest function simply mounts the specified JS module with no

View File

@ -23,12 +23,12 @@ static const NSTimeInterval kTestTimeoutSeconds = 120;
@implementation RCTTestRunner
{
FBSnapshotTestController *_testController;
RCTBridgeModuleProviderBlock _moduleProvider;
RCTBridgeModuleListProvider _moduleProvider;
}
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
moduleProvider:(RCTBridgeModuleProviderBlock)block
moduleProvider:(RCTBridgeModuleListProvider)block
{
RCTAssertParam(app);
RCTAssertParam(referenceDirectory);

View File

@ -89,10 +89,14 @@ typedef NS_ENUM(NSUInteger, RCTBridgeFields) {
return self;
}
RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate
bundleURL:(NSURL *)bundleURL
moduleProvider:(RCTBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions)
RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(__unused id<RCTBridgeDelegate>)delegate
bundleURL:(__unused NSURL *)bundleURL
moduleProvider:(__unused RCTBridgeModuleListProvider)block
launchOptions:(__unused NSDictionary *)launchOptions)
RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleURL
moduleProvider:(__unused RCTBridgeModuleListProvider)block
launchOptions:(__unused NSDictionary *)launchOptions)
- (void)start
{
@ -588,10 +592,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)dele
RCTFatal(error);
}
RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleURL
moduleProvider:(__unused RCTBridgeModuleProviderBlock)block
launchOptions:(__unused NSDictionary *)launchOptions)
/**
* Prevent super from calling setUp (that'd create another batchedBridge)
*/

View File

@ -27,7 +27,7 @@ RCT_EXTERN void RCTVerifyAllModulesExported(NSArray *extraModules);
// Private designated initializer
- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate
bundleURL:(NSURL *)bundleURL
moduleProvider:(RCTBridgeModuleProviderBlock)block
moduleProvider:(RCTBridgeModuleListProvider)block
launchOptions:(NSDictionary *)launchOptions NS_DESIGNATED_INITIALIZER;
// Used for the profiler flow events between JS and native
@ -61,7 +61,7 @@ RCT_EXTERN void RCTVerifyAllModulesExported(NSArray *extraModules);
* The block that creates the modules' instances to be added to the bridge.
* Exposed for the RCTBatchedBridge
*/
@property (nonatomic, copy, readonly) RCTBridgeModuleProviderBlock moduleProvider;
@property (nonatomic, copy, readonly) RCTBridgeModuleListProvider moduleProvider;
/**
* Used by RCTDevMenu to override the `hot` param of the current bundleURL.

View File

@ -53,7 +53,7 @@ RCT_EXTERN NSString *const RCTDidInitializeModuleNotification;
* For this reason, the block should always return new module instances, and
* module instances should not be shared between bridges.
*/
typedef NSArray<id<RCTBridgeModule>> *(^RCTBridgeModuleProviderBlock)(void);
typedef NSArray<id<RCTBridgeModule>> *(^RCTBridgeModuleListProvider)(void);
/**
* This function returns the module name for a given class.
@ -88,7 +88,7 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
* parameters or configuration.
*/
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
moduleProvider:(RCTBridgeModuleProviderBlock)block
moduleProvider:(RCTBridgeModuleListProvider)block
launchOptions:(NSDictionary *)launchOptions;
/**

View File

@ -169,7 +169,7 @@ static RCTBridge *RCTCurrentBridgeInstance = nil;
}
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
moduleProvider:(RCTBridgeModuleProviderBlock)block
moduleProvider:(RCTBridgeModuleListProvider)block
launchOptions:(NSDictionary *)launchOptions
{
return [self initWithDelegate:nil
@ -180,7 +180,7 @@ static RCTBridge *RCTCurrentBridgeInstance = nil;
- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate
bundleURL:(NSURL *)bundleURL
moduleProvider:(RCTBridgeModuleProviderBlock)block
moduleProvider:(RCTBridgeModuleListProvider)block
launchOptions:(NSDictionary *)launchOptions
{
if (self = [super init]) {

View File

@ -15,9 +15,15 @@
@protocol RCTBridgeModule;
@class RCTBridge;
typedef id<RCTBridgeModule>(^RCTBridgeModuleProvider)(void);
@interface RCTModuleData : NSObject <RCTInvalidating>
- (instancetype)initWithModuleClass:(Class)moduleClass
bridge:(RCTBridge *)bridge;
- (instancetype)initWithModuleClass:(Class)moduleClass
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
bridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance

View File

@ -10,13 +10,12 @@
#import "RCTModuleData.h"
#import <objc/runtime.h>
#include <mutex>
#import "RCTBridge.h"
#import "RCTBridge+Private.h"
#import "RCTModuleMethod.h"
#import "RCTBridge.h"
#import "RCTLog.h"
#import "RCTModuleMethod.h"
#import "RCTProfile.h"
#import "RCTUtils.h"
@ -25,6 +24,7 @@
NSDictionary<NSString *, id> *_constantsToExport;
NSString *_queueName;
__weak RCTBridge *_bridge;
RCTBridgeModuleProvider _moduleProvider;
std::mutex _instanceLock;
BOOL _setupComplete;
}
@ -57,9 +57,19 @@
- (instancetype)initWithModuleClass:(Class)moduleClass
bridge:(RCTBridge *)bridge
{
if ((self = [super init])) {
return [self initWithModuleClass:moduleClass
moduleProvider:^id<RCTBridgeModule>{ return [moduleClass new]; }
bridge:bridge];
}
- (instancetype)initWithModuleClass:(Class)moduleClass
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
bridge:(RCTBridge *)bridge
{
if (self = [super init]) {
_bridge = bridge;
_moduleClass = moduleClass;
_moduleProvider = [moduleProvider copy];
[self setUp];
}
return self;
@ -68,7 +78,7 @@
- (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance
bridge:(RCTBridge *)bridge
{
if ((self = [super init])) {
if (self = [super init]) {
_bridge = bridge;
_instance = instance;
_moduleClass = [instance class];
@ -83,7 +93,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
- (void)setUpInstanceAndBridge
{
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpInstanceAndBridge] [_instanceLock lock]", @{ @"moduleClass": NSStringFromClass(_moduleClass) });
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpInstanceAndBridge]", @{
@"moduleClass": NSStringFromClass(_moduleClass)
});
{
std::unique_lock<std::mutex> lock(_instanceLock);
@ -92,8 +104,8 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
if (RCT_DEBUG && _requiresMainQueueSetup) {
RCTAssertMainQueue();
}
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpInstanceAndBridge] [_moduleClass new]", @{ @"moduleClass": NSStringFromClass(_moduleClass) });
_instance = [_moduleClass new];
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpInstanceAndBridge] Create module", nil);
_instance = _moduleProvider ? _moduleProvider() : nil;
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
if (!_instance) {
// Module init returned nil, probably because automatic instantatiation

View File

@ -758,9 +758,14 @@ struct RCTInstanceCallback : public InstanceCallback {
});
}
RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(__unused id<RCTBridgeDelegate>)delegate
bundleURL:(__unused NSURL *)bundleURL
moduleProvider:(__unused RCTBridgeModuleListProvider)block
launchOptions:(__unused NSDictionary *)launchOptions)
RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleURL
moduleProvider:(__unused RCTBridgeModuleProviderBlock)block
launchOptions:(__unused NSDictionary *)launchOptions)
moduleProvider:(__unused RCTBridgeModuleListProvider)block
launchOptions:(__unused NSDictionary *)launchOptions)
/**
* Prevent super from calling setUp (that'd create another batchedBridge)