-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent sleep timer from running on native platforms #1786
Changes from all commits
0a79a6f
0d909c3
f80a12c
5776297
807b857
20eebc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {AppState} from 'react-native'; | ||
import CONST from '../../CONST'; | ||
import shouldReportActivity from './shouldReportActivity'; | ||
|
||
let appState = CONST.APP_STATE.ACTIVE; | ||
|
||
/** | ||
* Listener that will only fire the callback when the user has become active. | ||
* | ||
* @param {Function} callback | ||
* @returns {Function} to unsubscribe | ||
*/ | ||
function addBecameActiveListener(callback) { | ||
/** | ||
* @param {String} state | ||
*/ | ||
function appStateChangeCallback(state) { | ||
if ( | ||
shouldReportActivity | ||
&& (appState === CONST.APP_STATE.INACTIVE || appState === CONST.APP_STATE.BACKGROUND) | ||
&& state === CONST.APP_STATE.ACTIVE | ||
) { | ||
callback(); | ||
} | ||
appState = state; | ||
} | ||
AppState.addEventListener('change', appStateChangeCallback); | ||
return () => { | ||
AppState.removeEventListener('change', appStateChangeCallback); | ||
}; | ||
Comment on lines
+27
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain what is happening here? I'm not totally sure what this is doing, is it initializing with an event listener and then whenever it is returned it gets removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! So, |
||
} | ||
|
||
export default { | ||
addBecameActiveListener, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// We only need to report when the app becomes active on native since web maintains most of it's network functions while | ||
// in the "background" and the concept is not quite the same on mobile. We avoid setting this to true for web since | ||
// the event would fire much more frequently than it does on native causing performance issues. | ||
export default false; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default true; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* The Timer module is used on web/desktop to detect when a computer has gone to sleep. We don't use this on native | ||
* mobile since it does not work reliably and fires at inappropriate times. | ||
*/ | ||
let sleepTimer; | ||
let lastTime; | ||
|
||
/** | ||
* Adds a listener for detecting when laptop screens have closed or desktop computers put to sleep. Not reliable on | ||
* native platforms. | ||
* | ||
* @param {Function} onClockSkewCallback function to call when the | ||
* @returns {Fuction} that when called clears the timer | ||
*/ | ||
function addClockSkewListener(onClockSkewCallback) { | ||
clearInterval(sleepTimer); | ||
sleepTimer = setInterval(() => { | ||
const currentTime = (new Date()).getTime(); | ||
const isSkewed = currentTime > (lastTime + 8000); | ||
lastTime = currentTime; | ||
|
||
if (!isSkewed) { | ||
return; | ||
} | ||
|
||
onClockSkewCallback(); | ||
}, 2000); | ||
|
||
return () => { | ||
clearInterval(sleepTimer); | ||
sleepTimer = null; | ||
}; | ||
} | ||
|
||
export default { | ||
addClockSkewListener, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* We don't want the clock skew listener to run on native as it only helps us on desktop/web when a laptop is closed | ||
* and reopened. This method of detecting timing variance to see if we are inactive doesn't work well on native mobile | ||
* platforms. These platforms should use AppState instead to determine whether they must catch up on missing data. | ||
*/ | ||
export default { | ||
addClockSkewListener: () => () => {}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I understand this but basically this is only change the app state if the user has become active and the stored state is not active right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. We set up the state (assume to be active when the app inits) then listen for changes and save it locally each time so we can detect a change from one state to another.