clean up ctx initiliazation/remove code duplication

This commit is contained in:
Ari Lazier 2015-10-19 10:24:51 -07:00
parent 7074d66c5f
commit 832e0c5e13
1 changed files with 19 additions and 23 deletions

View File

@ -31,7 +31,22 @@
JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor) {
Ivar contextIvar = class_getInstanceVariable([executor class], "_context");
if (!contextIvar) {
return NULL;
}
id rctJSContext = contextIvar ? object_getIvar(executor, contextIvar) : nil;
if (!rctJSContext) {
Class RCTJavaScriptContext = NSClassFromString(@"RCTJavaScriptContext");
if (RCTJavaScriptContext) {
JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:ctx];
object_setIvar(executor, contextIvar, rctJSContext);
}
else {
NSLog(@"Failed to load RCTJavaScriptContext class");
}
}
return [rctJSContext ctx];
}
@ -61,10 +76,6 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor) {
- (void)setBridge:(RCTBridge *)bridge {
_bridge = bridge;
Ivar executorIvar = class_getInstanceVariable([bridge class], "_javaScriptExecutor");
id executor = object_getIvar(bridge, executorIvar);
Ivar contextIvar = class_getInstanceVariable([executor class], "_context");
static GCDWebServer *s_webServer;
if (s_webServer) {
[s_webServer stop];
@ -73,7 +84,9 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor) {
}
// The executor could be a RCTWebSocketExecutor, in which case it won't have a JS context.
if (!contextIvar) {
Ivar executorIvar = class_getInstanceVariable([bridge class], "_javaScriptExecutor");
id executor = object_getIvar(bridge, executorIvar);
if ([executor isMemberOfClass:NSClassFromString(@"RCTWebSocketExecutor")]) {
[GCDWebServer setLogLevel:3];
GCDWebServer *webServer = [[GCDWebServer alloc] init];
RJSRPCServer *rpcServer = [[RJSRPCServer alloc] init];
@ -106,24 +119,7 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor) {
}
[executor executeBlockOnJavaScriptQueue:^{
id rctJSContext = object_getIvar(executor, contextIvar);
JSGlobalContextRef ctx;
if (rctJSContext) {
ctx = [rctJSContext ctx];
}
else {
Class RCTJavaScriptContext = NSClassFromString(@"RCTJavaScriptContext");
if (RCTJavaScriptContext) {
ctx = JSGlobalContextCreate(NULL);
object_setIvar(executor, contextIvar, [[RCTJavaScriptContext alloc] initWithJSContext:ctx]);
}
else {
NSLog(@"Failed to load RCTJavaScriptContext class");
}
}
JSGlobalContextRef ctx = RealmReactGetJSGlobalContextForExecutor(executor);
[RealmJS initializeContext:ctx];
}];
}