Warn about slow main thread React methods

Summary:
This is revert of revert of https://github.com/facebook/react-native/pull/15542
WITHOUT default RCT_MAIN_THREAD_WATCH_DOG_THRESHOLD value. So, it makes it completly opt-in feature.

When code blocks the UI thread for too long, it's a bad sign because this can prevent the app from remaining responsive. This change helps detect such responsiveness issues by warning when a React method executes on the UI thread longer than some threshold.

Reviewed By: mmmulani

Differential Revision: D5772433

fbshipit-source-id: 24fe4fc0deffe9c091a4bfc4cbd76cb4f34c4091
This commit is contained in:
Valentin Shergin 2017-09-06 16:32:49 -07:00 committed by Facebook Github Bot
parent 34e9468b8f
commit c3038d7210
1 changed files with 19 additions and 0 deletions

View File

@ -516,7 +516,26 @@ RCT_EXTERN_C_END
}
// Invoke method
#ifdef RCT_MAIN_THREAD_WATCH_DOG_THRESHOLD
if (RCTIsMainQueue()) {
CFTimeInterval start = CACurrentMediaTime();
[_invocation invokeWithTarget:module];
CFTimeInterval duration = CACurrentMediaTime() - start;
if (duration > RCT_MAIN_THREAD_WATCH_DOG_THRESHOLD) {
RCTLogWarn(
@"Main Thread Watchdog: Invocation of %@ blocked the main thread for %dms. "
"Consider using background-threaded modules and asynchronous calls "
"to spend less time on the main thread and keep the app's UI responsive.",
[self methodName],
(int)(duration * 1000)
);
}
} else {
[_invocation invokeWithTarget:module];
}
#else
[_invocation invokeWithTarget:module];
#endif
index = 2;
[_retainedObjects removeAllObjects];