Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Goal
createReactNativeClientallows upstream libraries such as Expo to create and export their own customized client instance, however there are some situations where the client may still be imported from@bugsnag/react-native-performancedirectly, for example in the navigation plugins or if a user inadvertently imports directly@bugsnag/react-native-performance. When that happens, we can end up with a separate, duplicate client instanceThis PR refactors the React Native client from a direct export to a singleton proxy pattern in order to ensure there is only ever a single client instance per app regardless of how it's imported.
Design
Upstream libraries (like Expo) can create and register a client via
registerClient()upfront before any usage - otherwise the client is instantiated lazily on first property access. This ensures there is only ever a single client instance per app regardless of how it's imported.This also has the slight added advantage of only initializing the client when it's actually used (e.g. when
startis called), rather than being a side effect of importing.Once the client instance is initialized, all of it's properties are added to the proxy target and the get trap is removed, effectively bypassing the proxy for all subsequent calls. This optimization means there is no significant performance hit once the client is initialized.
Testing
Added new unit tests to verify the new singleton behavior and also tested manually with Expo