Add in the optimization to check the first/least timer in the queue before iterating (#27841)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/27841

Follow up from https://github.com/facebook/react-native/pull/27539 - adding back in the optimization that Detox has in TimersIdlingResource to avoid iterating over the entire timers queue if the next timer is already within the specified range.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D19522346

fbshipit-source-id: 32609f434d1ca575a5a49ad630e288c43fa90864
This commit is contained in:
Emily Janzer
2020-01-29 11:43:22 -08:00
committed by Facebook Github Bot
parent 6579d8f5bc
commit 8f5779cd70
@@ -384,12 +384,26 @@ public class JavaTimerManager {
*/
/* package */ boolean hasActiveTimersInRange(long rangeMs) {
synchronized (mTimerGuard) {
Timer nextTimer = mTimers.peek();
if (nextTimer == null) {
// Timers queue is empty
return false;
}
if (isTimerInRange(nextTimer, rangeMs)) {
// First check the next timer, so we can avoid iterating over the entire queue if it's
// already within range.
return true;
}
for (Timer timer : mTimers) {
if (!timer.mRepeat && timer.mInterval < rangeMs) {
if (isTimerInRange(timer, rangeMs)) {
return true;
}
}
}
return false;
}
private static boolean isTimerInRange(Timer timer, long rangeMs) {
return !timer.mRepeat && timer.mInterval < rangeMs;
}
}