From efd386eba2a5cba68ad750c40443abae21d1847b Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Fri, 12 Jun 2015 17:01:27 -0700 Subject: [PATCH] [ReactNative] Fix timers for debugger executors Summary: @public `-[RCTJavaScriptExecutor executeBlockOnJavaScriptQueue:]` would always `dispatch_async` for the WebView and WebSocket executors, what caused for any frame aligned dispatch. Test Plan: Test the `Timers, TimerMixin` example on UIExplorer, `requestAnimationFrame` was taking ~33.3ms when debugging, now takes ~16.6ms as expected. --- Libraries/WebSocket/RCTWebSocketExecutor.m | 9 ++++++++ React/Executors/RCTWebViewExecutor.m | 24 ++++++++-------------- React/Modules/RCTTiming.m | 5 +++++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Libraries/WebSocket/RCTWebSocketExecutor.m b/Libraries/WebSocket/RCTWebSocketExecutor.m index a8cf9ae5e..c5c216fc7 100644 --- a/Libraries/WebSocket/RCTWebSocketExecutor.m +++ b/Libraries/WebSocket/RCTWebSocketExecutor.m @@ -185,6 +185,15 @@ RCT_EXPORT_MODULE() } - (void)executeBlockOnJavaScriptQueue:(dispatch_block_t)block +{ + if ([NSThread isMainThread]) { + block(); + } else { + dispatch_async(dispatch_get_main_queue(), block); + } +} + +- (void)executeAsyncBlockOnJavaScriptQueue:(dispatch_block_t)block { dispatch_async(dispatch_get_main_queue(), block); } diff --git a/React/Executors/RCTWebViewExecutor.m b/React/Executors/RCTWebViewExecutor.m index fb3c6f031..57c8d25db 100644 --- a/React/Executors/RCTWebViewExecutor.m +++ b/React/Executors/RCTWebViewExecutor.m @@ -182,25 +182,19 @@ RCT_EXPORT_MODULE() [_webView loadHTMLString:runScript baseURL:url]; } -/** - * In order to avoid `UIWebView` thread locks, all JS executions should be - * performed outside of the event loop that notifies the `UIWebViewDelegate` - * that the page has loaded. This is only an issue with the remote debug mode of - * `UIWebView`. For a production `UIWebView` deployment, this delay is - * unnecessary and possibly harmful (or helpful?) - * - * The delay might not be needed as soon as the following change lands into - * iOS7. (Review the patch linked here and search for "crash" - * https://bugs.webkit.org/show_bug.cgi?id=125746). - */ - (void)executeBlockOnJavaScriptQueue:(dispatch_block_t)block { - dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_MSEC); - dispatch_after(when, dispatch_get_main_queue(), ^{ - RCTAssertMainThread(); + if ([NSThread isMainThread]) { block(); - }); + } else { + dispatch_async(dispatch_get_main_queue(), block); + } +} + +- (void)executeAsyncBlockOnJavaScriptQueue:(dispatch_block_t)block +{ + dispatch_async(dispatch_get_main_queue(), block); } /** diff --git a/React/Modules/RCTTiming.m b/React/Modules/RCTTiming.m index 4f92cfb05..2f3f6d40b 100644 --- a/React/Modules/RCTTiming.m +++ b/React/Modules/RCTTiming.m @@ -182,6 +182,11 @@ RCT_EXPORT_METHOD(createTimer:(NSNumber *)callbackID NSTimeInterval jsSchedulingOverhead = -jsSchedulingTime.timeIntervalSinceNow; if (jsSchedulingOverhead < 0) { RCTLogWarn(@"jsSchedulingOverhead (%ims) should be positive", (int)(jsSchedulingOverhead * 1000)); + + /** + * Probably debugging on device, set to 0 so we don't ignore the interval + */ + jsSchedulingOverhead = 0; } NSTimeInterval targetTime = jsDuration - jsSchedulingOverhead;