-
-
Notifications
You must be signed in to change notification settings - Fork 358
Add a first-class expoRouterIntegration() with auto-registration (#6156) #6189
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
96c80d9
Add a first-class expoRouterIntegration() with auto-registration (#6156)
alwx 25475dc
Updated CHANGELOG.md
alwx 4d9f7e3
Fixes
alwx ec3c3fe
Merge branch 'main' into alwx/feature/expo-router-integration
alwx 3d6823f
Fixes
alwx f741102
Merge branch 'main' into alwx/feature/expo-router-integration
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import type { Client, Integration } from '@sentry/core'; | ||
|
|
||
| import { debug } from '@sentry/core'; | ||
|
|
||
| import { getReactNavigationIntegration, reactNavigationIntegration } from './reactnavigation'; | ||
|
|
||
| export const INTEGRATION_NAME = 'ExpoRouter'; | ||
|
|
||
| const POLL_INTERVAL_MS = 50; | ||
| const POLL_MAX_DURATION_MS = 5_000; | ||
|
|
||
| interface ExpoRouterNavigationRef { | ||
| current: unknown | null; | ||
| } | ||
|
|
||
| interface ExpoRouterStore { | ||
| navigationRef?: ExpoRouterNavigationRef; | ||
| } | ||
|
|
||
| type ExpoRouterIntegrationOptions = Parameters<typeof reactNavigationIntegration>[0]; | ||
|
|
||
| /** | ||
| * Integration that connects Expo Router with `reactNavigationIntegration` without | ||
| * requiring the user to manually pass a `useNavigationContainerRef()` ref. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * Sentry.init({ | ||
| * integrations: [Sentry.expoRouterIntegration()], | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export const expoRouterIntegration = (options: ExpoRouterIntegrationOptions = {}): Integration => { | ||
| let pollTimer: ReturnType<typeof setTimeout> | undefined; | ||
|
|
||
| const afterAllSetup = (client: Client): void => { | ||
| const store = tryGetExpoRouterStore(); | ||
| if (!store) { | ||
| // expo-router not installed | ||
| return; | ||
| } | ||
| if (!store.navigationRef) { | ||
| debug.warn( | ||
| `${INTEGRATION_NAME} Found expo-router router-store but it does not expose a \`navigationRef\`. ` + | ||
| `This likely means the installed expo-router version is incompatible with this integration.`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // reuse the user's reactNavigationIntegration if they registered one manually. | ||
| // Otherwise, create and add one. | ||
| let reactNavigation = getReactNavigationIntegration(client); | ||
| if (!reactNavigation) { | ||
| reactNavigation = reactNavigationIntegration(options); | ||
| client.addIntegration(reactNavigation); | ||
| } | ||
|
|
||
| const navigationRef = store.navigationRef; | ||
|
|
||
| if (navigationRef.current) { | ||
| reactNavigation.registerNavigationContainer(navigationRef); | ||
| return; | ||
| } | ||
|
|
||
| // Otherwise, poll until the Root Layout mounts and Expo Router sets `.current`. | ||
| const startedAt = Date.now(); | ||
| const poll = (): void => { | ||
| if (!navigationRef.current) { | ||
| if (Date.now() - startedAt >= POLL_MAX_DURATION_MS) { | ||
| debug.warn(`${INTEGRATION_NAME} Timed out waiting for Expo Router navigation container.`); | ||
| pollTimer = undefined; | ||
| return; | ||
| } | ||
| pollTimer = setTimeout(poll, POLL_INTERVAL_MS); | ||
| return; | ||
| } | ||
|
|
||
| reactNavigation?.registerNavigationContainer(navigationRef); | ||
| pollTimer = undefined; | ||
| }; | ||
|
|
||
| pollTimer = setTimeout(poll, POLL_INTERVAL_MS); | ||
|
|
||
| client.on('close', () => { | ||
| if (pollTimer !== undefined) { | ||
| clearTimeout(pollTimer); | ||
| pollTimer = undefined; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| return { | ||
| name: INTEGRATION_NAME, | ||
| afterAllSetup, | ||
| }; | ||
| }; | ||
|
|
||
| function tryGetExpoRouterStore(): ExpoRouterStore | null { | ||
| try { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const mod = require('expo-router/build/global-state/router-store') as { | ||
| store?: ExpoRouterStore; | ||
| }; | ||
| return mod?.store ?? null; | ||
|
sentry-warden[bot] marked this conversation as resolved.
|
||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import type { Integration } from '@sentry/core'; | ||
|
|
||
| import type { ReactNativeClientOptions } from '../../src/js/options'; | ||
|
|
||
| import { getDefaultIntegrations } from '../../src/js/integrations/default'; | ||
| import { notWeb } from '../../src/js/utils/environment'; | ||
|
|
||
| jest.mock('../../src/js/utils/environment', () => { | ||
| const actual = jest.requireActual('../../src/js/utils/environment'); | ||
| return { | ||
| ...actual, | ||
| notWeb: jest.fn(() => true), | ||
| }; | ||
| }); | ||
|
|
||
| const EXPO_ROUTER_INTEGRATION_NAME = 'ExpoRouter'; | ||
|
|
||
| describe('getDefaultIntegrations - expo-router integration', () => { | ||
| beforeEach(() => { | ||
| (notWeb as jest.Mock).mockReturnValue(true); | ||
| }); | ||
|
|
||
| const createOptions = (overrides: Partial<ReactNativeClientOptions>): ReactNativeClientOptions => { | ||
| return { | ||
| dsn: 'https://example.com/1', | ||
| enableNative: true, | ||
| ...overrides, | ||
| } as ReactNativeClientOptions; | ||
| }; | ||
|
|
||
| const getNames = (options: ReactNativeClientOptions): string[] => | ||
| getDefaultIntegrations(options).map((i: Integration) => i.name); | ||
|
|
||
| it('adds expoRouterIntegration when tracing and auto performance tracing are enabled', () => { | ||
| const names = getNames( | ||
| createOptions({ | ||
| tracesSampleRate: 1.0, | ||
| enableAutoPerformanceTracing: true, | ||
| }), | ||
| ); | ||
| expect(names).toContain(EXPO_ROUTER_INTEGRATION_NAME); | ||
| }); | ||
|
|
||
| it('does not add expoRouterIntegration when tracing is disabled', () => { | ||
| const names = getNames( | ||
| createOptions({ | ||
| enableAutoPerformanceTracing: true, | ||
| }), | ||
| ); | ||
| expect(names).not.toContain(EXPO_ROUTER_INTEGRATION_NAME); | ||
| }); | ||
|
|
||
| it('does not add expoRouterIntegration when auto performance tracing is disabled', () => { | ||
| const names = getNames( | ||
| createOptions({ | ||
| tracesSampleRate: 1.0, | ||
| enableAutoPerformanceTracing: false, | ||
| }), | ||
| ); | ||
| expect(names).not.toContain(EXPO_ROUTER_INTEGRATION_NAME); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.