Prevent sleep timer from running on native platforms#1786
Conversation
thienlnam
left a comment
There was a problem hiding this comment.
Code looks good and it works, just had some questions about some of the code
| shouldReportActivity | ||
| && (appState === CONST.APP_STATE.INACTIVE || appState === CONST.APP_STATE.BACKGROUND) | ||
| && state === CONST.APP_STATE.ACTIVE |
There was a problem hiding this comment.
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.
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.
| AppState.addEventListener('change', appStateChangeCallback); | ||
| return () => { | ||
| AppState.removeEventListener('change', appStateChangeCallback); | ||
| }; |
There was a problem hiding this comment.
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.
Sure! So, addBecameActiveListener is both setting up an event listener and then returning a function that we can call later to remove the listener. The appStateChangeCallback exists in the closure created by calling the outermost function. It's just sort of a convenient pattern for cleaning up the listener we created and I borrowed it from how NetInfo works.
|
Sweet, thanks for the explanations, makes sense! |
|
@marcaaron Any way we could test the callbacks on our side? Or should we just test that the app is updated after reopening it from background? |
That should work! It's the only way to test this without having to look at the logs to verify that they are running correctly. |
Details
This sleep timer thing is wreaking certain havoc on native iOS and Android. Not sure why, but probably it should be disabled for now to restore performance on native apps. My current theory is that other work on the JS thread can cause the timer to get out of whack and cause reconnection callbacks to fire.
Fixed Issues
Fixes https://github.com/Expensify/Expensify/issues/157445
Tests
iOS/Android
Web/Desktop
Tested On
Screenshots
Web
Mobile Web
Desktop
iOS
Android