Skip to content

Commit f99ca3c

Browse files
berickson1facebook-github-bot
authored andcommitted
Fix a race condition in AppState that prevents listeners from being notified
Summary: If someone has setup a subscription on AppState and we correct AppState via getCurrentAppState call, we need to notify all the subscribers of AppState. 1 ) Initial AppState.currentState = 'active' 2-start) Subscribe to AppState Changes 3-start) Fetch Current AppState 4 ) App Code subscribes to AppState module 5 ) App becomes backgrounded 2-finish) AppState listeners are setup (missing background event) 3-finish) AppState.currentState updated to background At this point the subscription setup in 4) will never be called with the change. AppState should always call subscribers on change This is very difficult to formally test since it's due to a race condition. We've seen this condition via bug reports but have had no local repro. [GENERAL][BUGFIX][AppState] - Fix a race condition that could prevent AppState subscription change listener from firing on initial launch Closes #18236 Differential Revision: D7823370 Pulled By: hramos fbshipit-source-id: 99b174df70262ceaf9da141d005131facd624594
1 parent 0f6762b commit f99ca3c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Libraries/AppState/AppState.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ class AppState extends NativeEventEmitter {
6060
// prop and expose `getCurrentAppState` method directly.
6161
RCTAppState.getCurrentAppState(
6262
(appStateData) => {
63-
if (!eventUpdated) {
63+
// It's possible that the state will have changed here & listeners need to be notified
64+
if (!eventUpdated && this.currentState !== appStateData.app_state) {
6465
this.currentState = appStateData.app_state;
66+
this.emit('appStateDidChange', appStateData);
6567
}
6668
},
6769
logError

0 commit comments

Comments
 (0)