bugfix and move gcd out of rpc

This commit is contained in:
Ari Lazier 2015-10-22 18:06:11 -07:00
parent 8b5a8a731c
commit 28e21041bc
2 changed files with 21 additions and 21 deletions

View File

@ -106,9 +106,14 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool cre
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) { processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
GCDWebServerResponse *response; GCDWebServerResponse *response;
try { try {
// perform all realm ops on the main thread
__block NSData *responseData;
dispatch_sync(dispatch_get_main_queue(), ^{
realm_js::json args = realm_js::json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]); realm_js::json args = realm_js::json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]);
std::string response_text = rpcServer->perform_request(request.path.UTF8String, args).dump(); std::string responseText = rpcServer->perform_request(request.path.UTF8String, args).dump();
response = [[GCDWebServerDataResponse alloc] initWithData:[NSData dataWithBytes:response_text.c_str() length:response_text.length()] contentType:@"application/json"]; responseData = [NSData dataWithBytes:responseText.c_str() length:responseText.length()];
});
response = [[GCDWebServerDataResponse alloc] initWithData:responseData contentType:@"application/json"];
} }
catch(std::exception &ex) { catch(std::exception &ex) {
NSLog(@"Invalid RPC request - %@", [(GCDWebServerDataRequest *)request text]); NSLog(@"Invalid RPC request - %@", [(GCDWebServerDataRequest *)request text]);

View File

@ -146,8 +146,9 @@ RPCServer::RPCServer() {
JSObjectSetPropertyAtIndex(m_context, m_objects[oid], name.get<unsigned int>(), value, &exception); JSObjectSetPropertyAtIndex(m_context, m_objects[oid], name.get<unsigned int>(), value, &exception);
} }
else { else {
static JSStringRef prop_string = RJSStringForString(name.get<std::string>()); JSStringRef prop_string = RJSStringForString(name.get<std::string>());
JSObjectSetProperty(m_context, m_objects[oid], prop_string, value, 0, &exception); JSObjectSetProperty(m_context, m_objects[oid], prop_string, value, 0, &exception);
JSStringRelease(prop_string);
} }
if (exception) { if (exception) {
@ -186,25 +187,19 @@ RPCServer::~RPCServer() {
} }
json RPCServer::perform_request(std::string name, json &args) { json RPCServer::perform_request(std::string name, json &args) {
// perform all realm ops on the main thread
__block json response;
dispatch_sync(dispatch_get_main_queue(), ^{
try { try {
RPCRequest action = m_requests[name]; RPCRequest action = m_requests[name];
assert(action); assert(action);
if (name == "/create_session" || m_session_id == args["sessionId"].get<RPCObjectID>()) { if (name == "/create_session" || m_session_id == args["sessionId"].get<RPCObjectID>()) {
response = action(args); return action(args);
assert(response.is_object());
} }
else { else {
response = {{"error", "Invalid session ID"}}; return {{"error", "Invalid session ID"}};
} }
} catch (std::exception &exception) { } catch (std::exception &exception) {
response = {{"error", (std::string)"exception thrown: " + exception.what()}}; return {{"error", (std::string)"exception thrown: " + exception.what()}};
} }
});
return response;
} }
RPCObjectID RPCServer::store_object(JSObjectRef object) { RPCObjectID RPCServer::store_object(JSObjectRef object) {