@@ -90,11 +90,11 @@ public boolean isIdleNow() {
90
90
return true ;
91
91
}
92
92
93
- Object timingModule = Reflect .on (reactContext ).call (METHOD_GET_NATIVE_MODULE , timingClass ).get ();
94
- Object timerLock = Reflect .on (timingModule ).field (LOCK_TIMER ).get ();
93
+ final Object timingModule = Reflect .on (reactContext ).call (METHOD_GET_NATIVE_MODULE , timingClass ).get ();
94
+ final Object timerLock = Reflect .on (timingModule ).field (LOCK_TIMER ).get ();
95
95
synchronized (timerLock ) {
96
96
final PriorityQueue <?> timers = Reflect .on (timingModule ).field (FIELD_TIMERS ).get ();
97
- final Object nextTimer = timers . peek ( );
97
+ final Object nextTimer = findNextTimer ( timers );
98
98
if (nextTimer == null ) {
99
99
if (callback != null ) {
100
100
callback .onTransitionToIdle ();
@@ -147,22 +147,38 @@ public void resume() {
147
147
paused .set (false );
148
148
}
149
149
150
+ private Object findNextTimer (PriorityQueue <?> timers ) {
151
+ Object nextTimer = timers .peek ();
152
+ if (nextTimer == null ) {
153
+ return null ;
154
+ }
155
+
156
+ final boolean isRepetitive = Reflect .on (nextTimer ).field (TIMER_FIELD_REPETITIVE ).get ();
157
+ if (!isRepetitive ) {
158
+ return nextTimer ;
159
+ }
160
+
161
+ Object timer = null ;
162
+ long targetTime = Long .MAX_VALUE ;
163
+ for (Object aTimer : timers ) {
164
+ final boolean timerIsRepetitive = Reflect .on (aTimer ).field (TIMER_FIELD_REPETITIVE ).get ();
165
+ final long timerTargetTime = Reflect .on (aTimer ).field (TIMER_FIELD_TARGET_TIME ).get ();
166
+ if (!timerIsRepetitive && timerTargetTime < targetTime ) {
167
+ targetTime = timerTargetTime ;
168
+ timer = aTimer ;
169
+ }
170
+ }
171
+ return timer ;
172
+ }
173
+
150
174
private boolean isTimerOutsideBusyWindow (Object nextTimer ) {
151
175
final long currentTimeMS = System .nanoTime () / 1000000L ;
152
176
final Reflect nextTimerReflected = Reflect .on (nextTimer );
153
177
final long targetTimeMS = nextTimerReflected .field (TIMER_FIELD_TARGET_TIME ).get ();
154
178
final int intervalMS = nextTimerReflected .field (TIMER_FIELD_INTERVAL ).get ();
155
- final boolean isRepetitive = nextTimerReflected .field (TIMER_FIELD_REPETITIVE ).get ();
156
179
157
180
// Log.i(LOG_TAG, "Next timer has duration of: " + intervalMS
158
- // + "; due time is: " + targetTimeMS + ", current is: " + currentTimeMS
159
- // + "; is " + (isRepetitive ? "repeating" : "a one-shot"));
160
-
161
- // Before making any concrete checks, be sure to ignore repeating timers or we'd loop forever.
162
- // TODO: Should we iterate to the first, non-repeating timer?
163
- if (isRepetitive ) {
164
- return true ;
165
- }
181
+ // + "; due time is: " + targetTimeMS + ", current is: " + currentTimeMS);
166
182
167
183
// Core condition is for the timer interval (duration) to be set beyond our window.
168
184
// Note: we check the interval in an 'absolute' way rather than comparing to the 'current time'
0 commit comments