2015-10-27 20:59:15 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-10-14 01:36:15 +00:00
|
|
|
#import "RealmReact.h"
|
2015-11-30 20:14:50 +00:00
|
|
|
#import "RCTBridge.h"
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
#import "js_init.h"
|
|
|
|
#import "shared_realm.hpp"
|
|
|
|
|
2015-10-22 22:31:26 +00:00
|
|
|
#import <objc/runtime.h>
|
|
|
|
#import <dlfcn.h>
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
#if DEBUG
|
|
|
|
#import <GCDWebServer/Core/GCDWebServer.h>
|
|
|
|
#import <GCDWebServer/Requests/GCDWebServerDataRequest.h>
|
|
|
|
#import <GCDWebServer/Responses/GCDWebServerDataResponse.h>
|
|
|
|
#import <GCDWebServer/Responses/GCDWebServerErrorResponse.h>
|
|
|
|
#import "rpc.hpp"
|
|
|
|
#endif
|
|
|
|
|
2015-11-13 18:13:33 +00:00
|
|
|
@interface NSObject ()
|
2015-11-13 19:15:44 +00:00
|
|
|
- (instancetype)initWithJSContext:(void *)context;
|
2015-10-23 00:59:05 +00:00
|
|
|
- (JSGlobalContextRef)ctx;
|
2015-08-13 16:12:48 +00:00
|
|
|
@end
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool create) {
|
2015-10-15 23:18:58 +00:00
|
|
|
Ivar contextIvar = class_getInstanceVariable([executor class], "_context");
|
2015-10-19 17:24:51 +00:00
|
|
|
if (!contextIvar) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:15:44 +00:00
|
|
|
id rctJSContext = object_getIvar(executor, contextIvar);
|
2015-10-19 18:28:50 +00:00
|
|
|
if (!rctJSContext && create) {
|
2015-10-19 17:24:51 +00:00
|
|
|
Class RCTJavaScriptContext = NSClassFromString(@"RCTJavaScriptContext");
|
2015-11-13 19:15:44 +00:00
|
|
|
NSMethodSignature *signature = [RCTJavaScriptContext instanceMethodSignatureForSelector:@selector(initWithJSContext:)];
|
|
|
|
assert(signature);
|
|
|
|
|
2015-11-13 19:21:55 +00:00
|
|
|
// React Native 0.14+ expects a JSContext here, but we also support 0.13.x, which takes a JSGlobalContextRef.
|
2015-11-13 19:15:44 +00:00
|
|
|
if (!strcmp([signature getArgumentTypeAtIndex:2], "@")) {
|
2015-11-13 18:13:33 +00:00
|
|
|
JSContext *context = [[JSContext alloc] init];
|
2015-11-13 19:15:44 +00:00
|
|
|
rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:(void *)context];
|
2015-10-19 17:24:51 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-11-13 19:15:44 +00:00
|
|
|
JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
|
|
|
|
rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:ctx];
|
2015-10-19 17:24:51 +00:00
|
|
|
}
|
2015-11-13 19:15:44 +00:00
|
|
|
|
|
|
|
object_setIvar(executor, contextIvar, rctJSContext);
|
2015-10-19 17:24:51 +00:00
|
|
|
}
|
2015-11-13 19:15:44 +00:00
|
|
|
|
2015-10-15 23:18:58 +00:00
|
|
|
return [rctJSContext ctx];
|
|
|
|
}
|
2015-10-22 22:31:26 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
@interface RealmReact () <RCTBridgeModule>
|
|
|
|
@end
|
2015-11-17 20:46:22 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
@implementation RealmReact {
|
|
|
|
NSMutableDictionary *_eventHandlers;
|
|
|
|
__weak NSThread *_currentJSThread;
|
|
|
|
__weak NSRunLoop *_currentJSRunLoop;
|
2015-11-16 19:26:36 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
#if DEBUG
|
2015-11-13 01:16:24 +00:00
|
|
|
GCDWebServer *_webServer;
|
|
|
|
std::unique_ptr<realm_js::RPCServer> _rpcServer;
|
|
|
|
#endif
|
2015-11-17 01:57:06 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
2015-10-14 01:36:15 +00:00
|
|
|
+ (void)load {
|
2015-10-22 22:31:26 +00:00
|
|
|
void (*RCTRegisterModule)(Class) = (void (*)(Class))dlsym(RTLD_DEFAULT, "RCTRegisterModule");
|
2015-10-14 01:36:15 +00:00
|
|
|
|
|
|
|
if (RCTRegisterModule) {
|
|
|
|
RCTRegisterModule(self);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NSLog(@"Failed to load RCTRegisterModule symbol - %s", dlerror());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)moduleName {
|
|
|
|
return @"Realm";
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_eventHandlers = [[NSMutableDictionary alloc] init];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (dispatch_queue_t)methodQueue {
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addListenerForEvent:(NSString *)eventName handler:(RealmReactEventHandler)handler {
|
2016-01-13 01:07:48 +00:00
|
|
|
NSMutableOrderedSet *handlers = _eventHandlers[eventName];
|
|
|
|
if (!handlers) {
|
|
|
|
handlers = _eventHandlers[eventName] = [[NSMutableOrderedSet alloc] init];
|
|
|
|
}
|
2016-01-06 23:24:00 +00:00
|
|
|
[handlers addObject:handler];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeListenerForEvent:(NSString *)eventName handler:(RealmReactEventHandler)handler {
|
|
|
|
NSMutableOrderedSet *handlers = _eventHandlers[eventName];
|
|
|
|
[handlers removeObject:handler];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_REMAP_METHOD(emit, emitEvent:(NSString *)eventName withObject:(id)object) {
|
|
|
|
for (RealmReactEventHandler handler in [_eventHandlers[eventName] copy]) {
|
|
|
|
handler(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 01:16:24 +00:00
|
|
|
#if DEBUG
|
|
|
|
- (void)startRPC {
|
|
|
|
[GCDWebServer setLogLevel:3];
|
|
|
|
_webServer = [[GCDWebServer alloc] init];
|
|
|
|
_rpcServer = std::make_unique<realm_js::RPCServer>();
|
|
|
|
__weak __typeof__(self) weakSelf = self;
|
|
|
|
|
|
|
|
// Add a handler to respond to POST requests on any URL
|
|
|
|
[_webServer addDefaultHandlerForMethod:@"POST"
|
|
|
|
requestClass:[GCDWebServerDataRequest class]
|
|
|
|
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
|
|
|
|
GCDWebServerResponse *response;
|
|
|
|
try {
|
|
|
|
// perform all realm ops on the main thread
|
|
|
|
__block NSData *responseData;
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
RealmReact *self = weakSelf;
|
|
|
|
if (self) {
|
|
|
|
if (_rpcServer) {
|
|
|
|
realm_js::json args = realm_js::json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]);
|
|
|
|
std::string responseText = _rpcServer->perform_request(request.path.UTF8String, args).dump();
|
|
|
|
responseData = [NSData dataWithBytes:responseText.c_str() length:responseText.length()];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we have been deallocated
|
|
|
|
responseData = [NSData data];
|
|
|
|
});
|
|
|
|
response = [[GCDWebServerDataResponse alloc] initWithData:responseData contentType:@"application/json"];
|
|
|
|
}
|
|
|
|
catch(std::exception &ex) {
|
|
|
|
NSLog(@"Invalid RPC request - %@", [(GCDWebServerDataRequest *)request text]);
|
|
|
|
response = [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_UnprocessableEntity
|
|
|
|
underlyingError:nil
|
|
|
|
message:@"Invalid RPC request"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"];
|
|
|
|
return response;
|
|
|
|
}];
|
|
|
|
|
|
|
|
[_webServer startWithPort:8082 bonjourName:nil];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)shutdownRPC {
|
|
|
|
[_webServer stop];
|
|
|
|
[_webServer removeAllHandlers];
|
|
|
|
_webServer = nil;
|
|
|
|
_rpcServer.reset();
|
|
|
|
}
|
2015-11-16 19:16:09 +00:00
|
|
|
#endif
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
- (void)invalidate {
|
2015-11-16 19:26:36 +00:00
|
|
|
#if DEBUG
|
|
|
|
// shutdown rpc if in chrome debug mode
|
|
|
|
[self shutdownRPC];
|
|
|
|
#endif
|
|
|
|
|
2015-11-16 19:16:09 +00:00
|
|
|
// block until JS thread exits
|
|
|
|
NSRunLoop *runLoop = _currentJSRunLoop;
|
|
|
|
if (runLoop) {
|
2016-01-06 23:24:00 +00:00
|
|
|
CFRunLoopStop([runLoop getCFRunLoop]);
|
2015-11-16 19:16:09 +00:00
|
|
|
while (_currentJSThread && !_currentJSThread.finished) {
|
2015-11-17 01:36:28 +00:00
|
|
|
[NSThread sleepForTimeInterval:0.01];
|
2015-11-16 19:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 19:26:36 +00:00
|
|
|
realm::Realm::s_global_cache.clear();
|
2015-11-16 19:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2016-01-06 23:24:00 +00:00
|
|
|
[self performSelectorOnMainThread:@selector(invalidate) withObject:nil waitUntilDone:YES];
|
2015-11-16 19:16:09 +00:00
|
|
|
}
|
2015-11-13 01:16:24 +00:00
|
|
|
|
2015-10-14 01:36:15 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge {
|
2015-08-13 16:12:48 +00:00
|
|
|
_bridge = bridge;
|
2015-11-16 19:16:09 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
static __weak RealmReact *s_currentModule = nil;
|
|
|
|
[s_currentModule invalidate];
|
|
|
|
s_currentModule = self;
|
|
|
|
|
|
|
|
id<RCTJavaScriptExecutor> executor = [bridge valueForKey:@"javaScriptExecutor"];
|
2015-11-16 19:16:09 +00:00
|
|
|
|
2015-11-16 19:26:36 +00:00
|
|
|
if ([executor isKindOfClass:NSClassFromString(@"RCTWebSocketExecutor")]) {
|
2015-11-11 23:37:03 +00:00
|
|
|
#if DEBUG
|
2015-11-13 01:16:24 +00:00
|
|
|
[self startRPC];
|
2015-11-16 19:26:36 +00:00
|
|
|
#else
|
2015-11-12 19:32:16 +00:00
|
|
|
@throw [NSException exceptionWithName:@"Invalid Executor" reason:@"Chrome debug mode not supported in Release builds" userInfo:nil];
|
2015-11-16 19:26:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
2015-11-17 01:36:28 +00:00
|
|
|
__weak __typeof__(self) weakSelf = self;
|
2016-01-06 23:24:00 +00:00
|
|
|
|
2015-11-16 19:26:36 +00:00
|
|
|
[executor executeBlockOnJavaScriptQueue:^{
|
2016-01-06 23:24:00 +00:00
|
|
|
__typeof__(self) self = weakSelf;
|
2015-11-17 01:57:06 +00:00
|
|
|
if (!self) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-06 23:24:00 +00:00
|
|
|
|
2015-11-17 01:57:06 +00:00
|
|
|
self->_currentJSThread = [NSThread currentThread];
|
|
|
|
self->_currentJSRunLoop = [NSRunLoop currentRunLoop];
|
2016-01-06 23:24:00 +00:00
|
|
|
|
2015-11-16 19:26:36 +00:00
|
|
|
JSGlobalContextRef ctx = RealmReactGetJSGlobalContextForExecutor(executor, true);
|
|
|
|
RJSInitializeInContext(ctx);
|
|
|
|
}];
|
2015-11-12 19:32:16 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|