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

feat: make reactHost accessible #48338

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

WoLewicki
Copy link
Contributor

@WoLewicki WoLewicki commented Dec 19, 2024

Summary:

On bridgeless mode, reactHost is kept in memory even after destroying the DefaultReactNativeHost in brownfield scenario. Since it keeps references to the modules, they are not deallocated, and their initialize methods are not fired again when creating new instance of react-native later. It breaks the behavior of e.g. react-native-screens, which wants to listen for mutations and should get new FabricUIManager: https://github.com/software-mansion/react-native-screens/blob/20b7e83782cd5f79ddd0d61dadc13eeb4db4b258/android/src/main/java/com/swmansion/rnscreens/ScreensModule.kt#L45.

By adding invalidate method, which cleans up the reactHost instance, we can achieve it.

Changelog:

[ANDROID] [FIXED] - add invalidate method for destroying reactHost instance.

Test Plan:

In brownfield scenario, destroy the instance of RN and see that modules are also destroyed.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Dec 19, 2024
@cortinico
Copy link
Contributor

/rebase

@cortinico
Copy link
Contributor

Thanks for sending this over @WoLewicki This sounds like a reasonable approach, but I'd like to have @mdvacca 's opinion on this as well

@arushikesarwani94
Copy link
Contributor

IIRC for internal apps use-case we were able to make use of getReactHost().destroy() to address this scenario without needing an explicit invalidate() in DefaultReactHost

cc: @shwanton

Comment on lines 223 to 224
/** Cleanup function for brownfield scenarios. */
public abstract void invalidate();
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that ReactNativeHost already has a clear() method.
Could we instead do the invalidation in the DefaultReactHost.clear() method instead of having to create a brand new invalidate() abstract method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok so clear is a public API for cleaning all of those instances? If so, I think it will be the correct place to put it there then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it but I am not sure if the INSTANCE is the proper way of getting the instance of kotlin object.

Comment on lines +183 to +185
public fun invalidate() {
reactHost = null
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can you instead override fun clear(), call super.clear() and set the reactHost to null here?

@@ -78,6 +78,7 @@ public synchronized void clear() {
mReactInstanceManager.invalidate();
mReactInstanceManager = null;
}
DefaultReactHost.INSTANCE.invalidate();
Copy link
Contributor

Choose a reason for hiding this comment

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

this is probably not needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants