From 5365e1bb139ba8879227157fdea68b9f49fc6033 Mon Sep 17 00:00:00 2001 From: blagoev Date: Mon, 6 Nov 2017 13:04:56 +0200 Subject: [PATCH] initial CFRunLoop impl --- react-native/ios/RealmReact/RealmReact.mm | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/react-native/ios/RealmReact/RealmReact.mm b/react-native/ios/RealmReact/RealmReact.mm index b39ebdd3..ee6382f9 100644 --- a/react-native/ios/RealmReact/RealmReact.mm +++ b/react-native/ios/RealmReact/RealmReact.mm @@ -88,6 +88,7 @@ extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executo #if DEBUG GCDWebServer *_webServer; std::unique_ptr _rpcServer; + std::thread _workerThread; #endif } @@ -210,6 +211,15 @@ RCT_REMAP_METHOD(emit, emitEvent:(NSString *)eventName withObject:(id)object) { return [ipAddresses copy]; } +static void +TimerCallback(CFRunLoopTimerRef timer, void *info) +{ + RPCServer* rpcServer = (RPCServer*)info; + rpcServer->try_run_task(); +done: + return; +} + - (void)startRPC { [GCDWebServer setLogLevel:3]; _webServer = [[GCDWebServer alloc] init]; @@ -250,6 +260,27 @@ RCT_REMAP_METHOD(emit, emitEvent:(NSString *)eventName withObject:(id)object) { [response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"]; return response; }]; + + _workerThread = std::thread([&]() { + + auto timerInterval = 0.5; + CFStringRef timerMode = CFSTR("TimerMode"); + CFRunLoopTimerContext timerContext = { 0, NULL, NULL, NULL, NULL }; + timerContext.info = &_rpcServer; + CFRunLoopTimerRef timerRef = CFRunLoopTimerCreate(kCFAllocatorDefault, + CFAbsoluteTimeGetCurrent() + timerInterval, + timerInterval, + 0, + 0, + TimerCallback, + &timerContext); + + CFRunLoopAddTimer(CFRunLoopGetCurrent(), timerRef, timerMode); + CFRunLoopRunInMode(timerMode, 4, false); + + + CFRunLoopRun(); + }); [_webServer startWithPort:WEB_SERVER_PORT bonjourName:nil]; return;