Skip to content
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

[No QA] fix: null check for app state subscription added #6936

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libs/AppStateMonitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function addBecameActiveListener(callback) {
}
const appStateChangeSubscription = AppState.addEventListener('change', appStateChangeCallback);
return () => {
if (!appStateChangeSubscription) {
return;
}
appStateChangeSubscription.remove();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code change still makes sense regardless right? It might be redundant with the RN upgrade if that makes it so appStateChangeSubscription is never null, but it wouldn't hurt to have this check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dope then I think it's safe to merge!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I think including the null check is a good idea even in RN 0.65+, because, while unlikely, I think we could potentially have a race condition like this if a component mounts and unmounts almost instantly:

  1. Constructor initializes appStateChangeSubscription to null.
  2. Component mounts, componentDidMount runs and starts creating the subscription.
  3. Component unmounts and componentWillUnmount runs before componentDidMount finishes creating the subscription. So the subscription is still null 💥

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Thanks for the explanation :)

};
}
Expand Down