Skip to content

Commit

Permalink
[Init] Add the executor class to RCTBridge's init method
Browse files Browse the repository at this point in the history
If you construct an RCTBridge you may want to configure the executor. However the constructor synchronously calls `setUp` and sets up the executor. Instead, let the code that constructs the bridge specify the executor class up front.

Now that RCTRootView takes a bridge in one of its initializers, it is possible to create a bridge with a custom executor and then use that to set up a root view.

Fixes #288
  • Loading branch information
ide committed Apr 7, 2015
1 parent 1dba7e0 commit 888ce41
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
8 changes: 7 additions & 1 deletion React/Base/RCTBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ extern NSString *const RCTReloadBridge;
*/
- (instancetype)initWithBundlePath:(NSString *)bundlepath
moduleProvider:(RCTBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions NS_DESIGNATED_INITIALIZER;
launchOptions:(NSDictionary *)launchOptions
executorClass:(Class)executorClass
debugExecutorClass:(Class)debugExecutorClass NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithBundlePath:(NSString *)bundlepath
moduleProvider:(RCTBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions;

/**
* This method is used to call functions in the JavaScript application context.
Expand Down
33 changes: 25 additions & 8 deletions React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ @implementation RCTModuleMethod
NSString *_methodName;
}

static Class _globalExecutorClass;

- (instancetype)initWithMethodName:(NSString *)methodName
JSMethodName:(NSString *)JSMethodName
{
Expand Down Expand Up @@ -507,6 +505,8 @@ @implementation RCTBridge
NSDictionary *_modulesByName;
id<RCTJavaScriptExecutor> _javaScriptExecutor;
Class _executorClass;
Class _debugExecutorClass;
BOOL _usingDebugExecutor;
NSString *_bundlePath;
NSDictionary *_launchOptions;
RCTBridgeModuleProviderBlock _moduleProvider;
Expand All @@ -518,23 +518,43 @@ @implementation RCTBridge
- (instancetype)initWithBundlePath:(NSString *)bundlePath
moduleProvider:(RCTBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions
executorClass:(Class)executorClass
debugExecutorClass:(Class)debugExecutorClass
{
if ((self = [super init])) {
_bundlePath = bundlePath;
_moduleProvider = block;
_launchOptions = launchOptions;
_executorClass = executorClass;
_debugExecutorClass = debugExecutorClass;
[self setUp];
[self bindKeys];
}

return self;
}

- (instancetype)initWithBundlePath:(NSString *)bundlePath
moduleProvider:(RCTBridgeModuleProviderBlock)block
launchOptions:(NSDictionary *)launchOptions
{
return [self initWithBundlePath:bundlePath
moduleProvider:block
launchOptions:launchOptions
executorClass:[RCTContextExecutor class]
debugExecutorClass:NSClassFromString(@"RCTWebSocketExecutor")];
}

- (void)setUp
{
Class executorClass = _executorClass ?: _globalExecutorClass ?: [RCTContextExecutor class];
Class executorClass = _usingDebugExecutor ? _debugExecutorClass : _executorClass;
if ([NSStringFromClass(executorClass) isEqualToString:@"RCTWebViewExecutor"]) {
_javaScriptExecutor = [[RCTWebViewExecutor alloc] initWithWebView:[[UIWebView alloc] init]];
} else {
if (!executorClass) {
RCTLogError(@"WebSocket debugger is not available. Did you forget to include RCTWebSocketExecutor?");
executorClass = _executorClass;
}
_javaScriptExecutor = [[executorClass alloc] init];
}
_latestJSExecutor = _javaScriptExecutor;
Expand Down Expand Up @@ -643,17 +663,14 @@ - (void)bindKeys
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"n"
modifierFlags:UIKeyModifierCommand
action:^(UIKeyCommand *command) {
_executorClass = Nil;
_usingDebugExecutor = NO;
[self reload];
}];

[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"d"
modifierFlags:UIKeyModifierCommand
action:^(UIKeyCommand *command) {
_executorClass = NSClassFromString(@"RCTWebSocketExecutor");
if (!_executorClass) {
RCTLogError(@"WebSocket debugger is not available. Did you forget to include RCTWebSocketExecutor?");
}
_usingDebugExecutor = YES;
[self reload];
}];
#endif
Expand Down

0 comments on commit 888ce41

Please sign in to comment.