Skip to content

Commit 3637bce

Browse files
astreetfacebook-github-bot
authored andcommitted
Warn when timers longer than 1 min are set pending AlarmManager support
Summary: These are bad since the app might not be foregrounded anymore and it also keeps the timing module awake. We could fix the latter but the former would require someone adding AlarmManager support. Open to better/more helpful ideas for the warning message but there's not a ton we can tell them to do right now. Reviewed By: achen1 Differential Revision: D4716273 fbshipit-source-id: c5d3a3ce8af53253b240477f2bde38094a138a02
1 parent 9d32920 commit 3637bce

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Libraries/Core/Timers/JSTimers.js

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// in dependencies. NativeModules > BatchedBridge > MessageQueue > JSTimersExecution
1616
const RCTTiming = require('NativeModules').Timing;
1717
const JSTimersExecution = require('JSTimersExecution');
18+
const Platform = require('Platform');
1819

1920
const parseErrorStack = require('parseErrorStack');
2021

@@ -64,6 +65,14 @@ function _freeCallback(timerID: number) {
6465
}
6566
}
6667

68+
const MAX_TIMER_DURATION_MS = 60 * 1000;
69+
const IS_ANDROID = Platform.OS === 'android';
70+
const ANDROID_LONG_TIMER_MESSAGE =
71+
'Setting a timer for a long period of time, i.e. multiple minutes, is a ' +
72+
'performance and correctness issue on Android as it keeps the timer ' +
73+
'module awake, and timers can only be called when the app is in the foreground. ' +
74+
'See https://github.com/facebook/react-native/issues/12981 for more info.';
75+
6776
/**
6877
* JS implementation of timer functions. Must be completely driven by an
6978
* external clock signal, all that's stored here is timerID, timer type, and
@@ -75,6 +84,11 @@ const JSTimers = {
7584
* @param {number} duration Number of milliseconds.
7685
*/
7786
setTimeout: function(func: Function, duration: number, ...args?: any): number {
87+
if (IS_ANDROID && duration > MAX_TIMER_DURATION_MS) {
88+
console.warn(
89+
ANDROID_LONG_TIMER_MESSAGE + '\n' + '(Saw setTimeout with duration ' +
90+
duration + 'ms)');
91+
}
7892
const id = _allocateCallback(() => func.apply(undefined, args), 'setTimeout');
7993
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ false);
8094
return id;
@@ -85,6 +99,11 @@ const JSTimers = {
8599
* @param {number} duration Number of milliseconds.
86100
*/
87101
setInterval: function(func: Function, duration: number, ...args?: any): number {
102+
if (IS_ANDROID && duration > MAX_TIMER_DURATION_MS) {
103+
console.warn(
104+
ANDROID_LONG_TIMER_MESSAGE + '\n' + '(Saw setInterval with duration ' +
105+
duration + 'ms)');
106+
}
88107
const id = _allocateCallback(() => func.apply(undefined, args), 'setInterval');
89108
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ true);
90109
return id;

0 commit comments

Comments
 (0)